Approaches to biomedical knowledge

Session #11: Transformer Architecture: BERT

Peter N Robinson

Free University Berlin

2026-04-26

Overview

Game plan

This lecture provides an introduction to the transformer architecture with a focus on the encoder.

BERT

** Bidirectional Encoder Representations from Transformers(BERT)**: - Developed in 2018 by researchers at Google AI Language - A swiss army knife solution to many common language tasks, such as sentiment analysis and named entity recognition.

  • BERT was trained on Wikipedia (~2.5B words) and Google’s BooksCorpus (~800M words).
  • Trained at Google on 64 TPUs (Tensor Processing Units - roughly speaking, Google’s version of the GPU) over 4 days
  • Many other versions of BERT have been trained for various purposes

Causal vs masked language modeling

  • Predecessors modelled language unidirectionally – train on predicting the next word
  • BERT is designed to extract bidirectional representations: predict the masked word given both left and right context

BERT: unidirectional vs bidirectional

  • The authors argue that the unidirectionality of models such as GPT limit the choice of architectures that can be used during pre-training because every token can only attend to previous tokens in the self-attention layers of the Transformer.
  • Such restrictions are sub-optimal for sentence-level tasks, and could be very harmful when applying finetuning based approaches to token-level tasks such as question answering, where it is crucial to incorporate context from both directions

BERT training

  • BERT is an encoder-only transformer and the training is similar to what was described in previous lectures.
Transformer Layers  Hidden Size Attention Heads Parameters Processing Length of Training
BERTbase 12 768 12 110M 4 TPUs 4 days
BERTlarge 24 1024 16 340M 16 TPUs 4 days

BERT Architecture

  • The dimension of the FFN is 3072

  • There are 12 attention heads in the multihead attention block (BERTbase – 16 for BERTlarge).

  • The transformer block is repeated Nx times (12 for BERTbase and 24 for BERTlarge), that is, the output of one block is used as input for the next

  • The output of the final block is used as input for the linear layer

  • Differences to the original transformer:

    • The embedding vector is 768 (BERTbase) or 1024 (BERTlarge) dimensional rather than 512
    • Positional embeddings are absolute and learnt during training (limited to 512 positions)1
    • The linear layer head changes according to the application
    • Uses WordPiece tokenizer

BERT vs. GPT

Major differences:

  • GPT handles specific tasks using specific prompts. BERT needs to be fine tuned.
  • GPT is trained using unidirectional context, BERT bidirectional
  • BERT is not designed to generate text (this is a decoder task)
  • We will provide some examples below

Masked Language Modeling (MLM)

Core Objective

MLM: Predict the correct word given both the left and right bidirectional context.

  • BERT is forced to utilize words on either side of the token simultaneously to predict the missing mask.
  • Comparing bidirectional reasoning capabilities across different sentence structures:

Left Context Task

“An elephant’s trunk can be used to grasp yellow [?].”

Full Context MLM Task

“An elephant’s [?] can be used to grasp yellow bananas.”

Left and right context in BERT

Recall the definition of the attention output

\[ \underbrace{\mathrm{Attention}(\mathbf{Q},\mathbf{K},\mathbf{V})}_{N\times N} = \mathrm{softmax}\left( \frac{ \underbrace{\mathbf{Q}}_{N\times d_{hidden}} \times \underbrace{\mathbf{K}^T}_{d_{hidden}\times N}} {\sqrt{d_{k}}} \right) \]

  • Recall that for the masked attention in the decoder, we mask words to “zero out” interactions of tokens with other subsequent tokens

Left and right context in BERT

  • In contrast, with BERT there is no mask
  • Each token therefore attends to other tokens to the left and the right
  • The pretraining procedure instead selects 15% of the tokens from the sentence to be masked
    • 80% of the time, the token is replaced by [MASK]: Berlin is the [MASK] of Germany
    • 10% of the time, the token is replaced by a random word: Berlin is the hedgehog of Germany
    • 10% of the time, the token is not replaced: Berlin is the capital of Germany

The motivation for not always replacing the token with [MASK] is that this would created a mismatch between pre-training and fine-tuning, since the [MASK] token does not appear during fine-tuning. Using the random/unchanged tokens mitigates this mismatch.

Loss: BERT

  • Like any encoder, the BERT model produces embeddings as output.
  • We focus on the output for the masked position to calculate the loss
  • Backpropation and gradient descent are as usual

Loss: BERT

  • In detail,
  • The LM head takes the output of the final transformation layer \(L\), multiplies it by the unemedding layer and uses softmax to transform the output into probabilities

\[ \begin{align} \mathbf{u}_i &= \mathbf{h}_i^L \mathbf{E}^T \\ \mathbf{y}_i &= \mathrm{softmax}(\mathbf{u}_i) \end{align} \]

  • In our example, the \(x_i\) that corresponds to “capital”, the loss is the probability of the correct word “capital” given the output \[ L_{MLM}(x_i) = -\log P(x_i\mid \mathbf{h}_i^L) \]
  • Gradients are obtained by taking the average loss over a batch

\[ -\frac{1}{M}\sum_{m=1}^M \log P(x_i\mid \mathbf{h}_i^L) \]

Next-sentence prediction (NSP)

NSP:

  • Many downstream applications involve learning relationships between sentences rather than single words (tokens)
  • BERT is therefore also trained on the next-sentence prediction task
  • 50% of the time, correct pairs of sentence are chosen

Why did the chicken cross the road? To get to the other side

  • 50% of the time, incorrect pairs of sentence are chosen

Why did the chicken cross the road? The climate crisis is driven by burning fossil fuels.

Next-sentence prediction (NSP)

NSP:

  • There are two problems:
    • How do we encode these two sentences as input for BERT?
    • How should BERT reply that a sentence does (IsNext) or does not (NotNext) follow another sentence?

Next-sentence prediction

  • BERT introduces two special tokens:
    • [CLS]
    • [SEP]
  • We encode the input as follows \[ \mathrm{[CLS]} \bullet \text{first sentence }\bullet \mathrm{[SEP]}\bullet \text{second sentence }\bullet \mathrm{[SEP]} \]

Next-sentence prediction

  • As discussed previously, if we only provide the tokens for \(\mathrm{[CLS]} \bullet \text{first sentence }\bullet \mathrm{[SEP]}\bullet \text{second sentence }\bullet \mathrm{[SEP]}\) as input to BERT
    • BERT would not be able to know that dog is from the first and play is from the second sentence
    • The segment embeddings \(\mathbf{E}_A\) and \(\mathbf{E}_B\) encode whether a token belongs to sentence A or B.

NSP

  • Pass output for first token (corresponding to [CLS]) to linear layer – with only two output features, next and notNext, and pass through softmax.
  • calculate the cross-entropy loss, perform backprop, update weights

NSP

  • The CLS token interacts with every other token (no mask) – it “captures” information about all other tokens as needed for the classification task
  • e.g., cell (1,1) of the attn output is the dot product of the first row of the attention matrix (for CLS) with the first column of \(\mathbf{V}\)
  • cell (1,2) of the attn output is the dot product of the first row of the attention matrix (for CLS) with the second column of \(\mathbf{V}\)
  • \(\ldots\)
  • cell (1,768) of the attn output is the dot product of the first row of the attention matrix (for CLS) with the \(768^{th}\) column of \(\mathbf{V}\)
  • Thus, the CLS has aggregated information from the entire \(\mathbf{V}\) matrix for the purposes of classification
  • For classification, we are only interested in the first row of the attention output

The [SEP] Token and Sentence Pairs

  • Some tasks require two input sentences at once (e.g., “do these two sentences mean the same thing?”).
  • BERT handles this with a [SEP] token separating the two sentences, plus a learned segment embedding added to each token indicating which sentence it belongs to:

\[ \text{[CLS]} \ \text{Sentence A} \ \text{[SEP]} \ \text{Sentence B} \ \text{[SEP]} \]

  • Segment embeddings are added elementwise to the token + positional embeddings, the same way positional encodings were added in Part 1 — just one more term in the sum.
  • This sentence-pair format is what BERT’s own pretraining uses, and it’s directly relevant to Section 3, where we’ll see why comparing two full sentences this way becomes a computational bottleneck.

MLM and NSP

  • For every training example, BERT builds one input (a sentence pair with some tokens replaced by [MASK]) and runs it through a single forward pass.

  • That one pass produces both:

    • MLM predictions: output vectors at each masked position, fed into a softmax over the vocabulary to predict the original token.
    • NSP prediction: the output vector at [CLS], fed into the binary classifier to predict IsNext/NotNext.
  • The two losses are then just added together \[ \mathcal{L} = \mathcal{L}_{MLM} + \mathcal{L}_{NSP} \]

  • The combined loss is backpropagated through the shared BERT weights in one update step.

FFN Expansion-Contraction

  • In BERT-Base, each encoder block’s FFN is two linear transformations with a non-linear activation (GELU) in between — applied independently, position-wise, to every token
  • An expansion–contraction (bottleneck-in-reverse) architecture
  • Input \(x\): \(1 \times 768\)
  • \(W_1\): \(768 \times 3072\) — the expansion weights
  • \(b_1\): \(1 \times 3072\) — first bias
  • \(W_2\): \(3072 \times 768\) — the projection weights
  • \(b_2\): \(1 \times 768\) — second bias

BERT’s FFN Block

  • Coming back to the FFN: BERT takes the 768-dimensional vector out of the Add & Norm layer, expands it into a 3072-dimensional space, applies GELU, then projects it back down to 768 dimensions for the next block

\[ \underbrace{1 \times 768}_{\text{Input}} \xrightarrow{W_1, b_1} \underbrace{1 \times 3072}_{\text{Linear Expansion}} \xrightarrow{\text{GELU}} \underbrace{1 \times 3072}_{\text{Non-linear Activation}} \xrightarrow{W_2, b_2} \underbrace{1 \times 768}_{\text{Output}} \]

BERT’s FFN Block

  • Why expand 4×? The attention sub-layer only mixes information that’s already there — it computes weighted averages of existing token vectors, which is a linear-ish operation with no added representational capacity. The FFN is where the model gets nonlinear capacity: projecting up to a much higher-dimensional space before the GELU gives that nonlinearity more “room” to carve out complex feature combinations, and the 4× ratio (\(3072 = 4 \times 768\)) is the ratio Vaswani et al. (2017) fixed in the original Transformer — BERT simply inherits it.
  • The FFN is applied identically and independently at every position (same \(W_1, b_1, W_2, b_2\) for every token) — it’s often described as two \(1\times1\) convolutions, and it’s the main place per-token nonlinear processing happens, complementing attention’s role of moving information between positions.
  • In parameter terms, the FFN dominates the model: for BERT-Base, \(2 \times 768 \times 3072 \approx 4.7M\) parameters per block just for \(W_1\) and \(W_2\) combined — roughly two-thirds of each encoder block’s parameters live in the FFN, not the attention heads.

Causal LM vs. Masked LM (recap)

Causal LM (GPT-style) Masked LM (BERT-style)
Direction Left-to-right only Bidirectional
Attention Causal mask (Section 2) No mask — full self-attention
Predicts Next token, given all past tokens Randomly masked tokens, given surrounding context (past and future)
Natural use Text generation Understanding/representation tasks (classification, extraction)
Reference Radford et al. (2018)1 Devlin et al. (2019)2

Fine-tuning BERT

  • Fine-tuning: Adapting the pretrained weights through additional training for a particular task.
  • The pretraining is self-supervised but the fine-tuning is supervised, i.e., we need to have human-curated labels
  • Pretraining BERT typically might involve billions of examples. Fine-tuning might involve a few thousand labeled examples (six orders of magnitude difference!).
  • democratization: No individual or university research group has the resources to train a model such as BERT, but are able to develop sophisticated ML models by fine-tuning
  • The same pre-trained BERT model can be used for various fine tuning tasks

Fine-tuning BERT for text classification

  • Next-sentence prediction is an example of binary text classification (isNext, notNext)
  • The following example corresponds to the binary classification explanation above.
  • In general, we can classify text using more categories.

Text-classification (Python)

  • We will demonstrate a Python script to classify text as PHISHING or SAFE
  • Setup:
python -m pip install datasets, transformers, evaluate
python -m pip install "accelerate>=1.1.0"

Import libraries (datasets, evaluate, and transformers are from huggingface)

from datasets import DatasetDict, Dataset
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer
import evaluate
import numpy as np
from transformers import DataCollatorWithPadding
  • Get datasets (3000 examples with training: 70%; 15%: testing; 15%: validation )
from datasets import load_dataset
# available on huggingface dataset hub
dataset_dict = load_dataset("shawhin/phishing-site-classification") 

adapted from Shaw Talebi Fine-Tuning BERT for Text Classification YouTube

google-bert/bert-base-uncased

  • This is the Hugging Face Hub identifier for the original BERT-base model Google released alongside the 2018 paper.
  • The “base” (smaller) model with 12 transformer encoder blocks, hidden size 768, 12 attention heads, FFN inner dimension 3072
  • uncased: text is lowercased and accent-stripped before tokenization
model_path = "google-bert/bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_path)

id2label = { 0: "Safe", 1: "Phishing"}
label2id = { "Safe": 0, "Phishing": 1}
model = AutoModelForSequenceClassification.from_pretrained(model_path,
                                                           num_labels=2,
                                                           id2label=id2label,
                                                           label2id=label2id,
                                                           attn_implementation="eager")
  • The AutoModelForSequenceClassification class adds a binary classification head to the BERT base model.
  • attn_implementation="eager" is needed to avoid the fused SDPA kernel, which is not available from PyTorch’s MPS (Apple GPU) backend1.

Parameters

  • By default, all of the model’s parameters (ca. 110,000,000 in this case) are trainable.
  • Fine-tuning all of them is computationally expensive and, with a small dataset, risks overfitting.
  • To reduce cost and (likely) improve generalization, we freeze the entire pretrained encoder (all 12 transformer blocks) and train only:
    • the pooler — a single dense layer + tanh applied to the [CLS] token, still part of base_model, which we explicitly unfreeze, and
    • the classification head added by AutoModelForSequenceClassification — untouched by the loop below since it lives outside base_model, and trainable simply because it’s newly initialized.
  • Net effect: only 4 parameter tensors update during training (pooler weight/bias, classifier weight/bias)
for name, param in model.base_model.named_parameters():
    if "pooler" in name:
        param.requires_grad = True
    else:
        param.requires_grad = False

Preprocess input data

We set up a tokenizer and tokenize the input texts.

def preprocess_function(examples):
    return tokenizer(examples["text"], truncation=True)

tokenized_data = dataset_dict.map(preprocess_function, batched=True)
  • The Data collator ensures all the examples in a batch have the same length
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)

Evaluation

accuracy = evaluate.load("accuracy")
auc_score = evaluate.load("roc_auc")

def compute_metrics(eval_pred):
    # logits and ground truth labels
    predictions, labels = eval_pred
    # softmax
    probabilities = np.exp(predictions)/np.exp(predictions).sum(-1, keepdims=True)
    positive_class_probs = probabilities[:,1]
    auc = np.round(auc_score.compute(prediction_scores=positive_class_probs, references=labels)['roc_auc'], 3)
    predicted_classes = np.argmax(predictions, axis=1)
    acc = np.round(accuracy.compute(predictions=predicted_classes, references=labels)['accuracy'], 3)
    return {"Accuracy": acc, "AUC": auc}
  • standard way to evaluate accuracy

Training parameters

lr = 2e-4
batch_size = 8
num_epochs = 10

training_args = TrainingArguments(
    output_dir="bert_phishing-classifier_teacher",
    learning_rate=lr,
    per_device_eval_batch_size=batch_size,
    per_device_train_batch_size=batch_size,
    num_train_epochs=num_epochs,
    logging_strategy="epoch",
    eval_strategy="epoch",
    save_strategy="epoch",
    load_best_model_at_end=True
)

Fine-tune model

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_data["train"],
    eval_dataset=tokenized_data["test"],
    data_collator=data_collator,
    compute_metrics=compute_metrics
)

trainer.train()

Results

  • The results on the independent validation set were: Accuracy: 88.7%, AUC94.6%

Single-sentence classification, multiple categories

  • HOMEWORK You will implement a multiple-category classifier
  • Sentences are given one of multiple labels
from datasets import load_dataset
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer
import evaluate
import numpy as np

dataset_name = "ag_news"
dataset = load_dataset(dataset_name)
# Similar code
# To get multiple classes
n_classes = dataset["train].features["label"].num_classes
model = AutoModelForSequenceClassification.from_pretrained(model_path,
                                                           num_labels=n_classes,
                                                           attn_implementation="eager")
  • Four classes: 1-World, 2-Sports, 3-Business, 4-Sci/Tech

See: https://www.kaggle.com/datasets/amananandrai/ag-news-classification-dataset

Named entity recognition

  • Named-entity recognition (NER) seeks to locate and classify named entities mentioned in unstructured text into pre-defined categories such as
  • person names (B-PER)
  • organizations (B-ORG),
  • locations (B-LOC)
  • ontology codes, e.g., B-HPO
  • etc.
  • O: None

NER (Token Classification)

  • every token position’s final hidden state \(\mathbf{h}_i \in \mathbb{R}^d\) gets its own prediction, using the same shared linear layer applied independently at each position:

\[ P(\text{label}_i) = \text{softmax}(\mathbf{h}_i W_{\text{tok}} + b_{\text{tok}}), \qquad \text{for each token } i \]

  • \(W_{\text{tok}} \in \mathbb{R}^{d\times L}\), where \(L\) is the number of entity label types.
  • Unlike sentence classification, [CLS] and [SEP] are typically excluded from the loss — they’re structural tokens, not real words with entity labels.
  • Part of speech tagging is analogous
  • Chunking: Find “chunks” of tokens that belong to the same entity (e.g., “Renal insufficiency”)

NER in Python

  • For the most part, the Python code for NER is very similar to that for single-sentence classification

We need to tell BERT about the following classification

|   ID | Tag    | Description                |
|-----:|:-------|:---------------------------|
|    0 | O      | Outside of a named entity  |
|    1 | B-PER  | Beginning of a PER entity  |
|    2 | I-PER  | Inside of a PER entity     |
|    3 | B-ORG  | Beginning of a ORG entity  |
|    4 | I-ORG  | Inside of a ORG entity     |
|    5 | B-LOC  | Beginning of a LOC entity  |
|    6 | I-LOC  | Inside of a LOC entity     |
|    7 | B-MISC | Beginning of a MISC entity |
|    8 | I-MISC | Inside of a MISC entity    |
  • We define the model to perform token classification rather than sentence classification
model = AutoModelForTokenClassification.from_pretrained(model_path, num_labels=9)
  • Most of the rest of the code is the same except for book-keeping details that we will not review here

NER: Results

  • After training the model for about 10 minutes on my laptop, it is already pretty good
from transformers import pipeline

nlp = pipeline("ner", model=model_fine_tuned, tokenizer=tokenizer)
example ="Konrad Adenauer was the first Chancellor of Germany"
ner_results = nlp(example)
print(ner_results)
  • NER predictions:
Token Entity  Probability
konrad B-PER  0.9857714
 aden  I-PER 0.9818236
##auer I-PER 0.979223
germany  B-LOC  0.9961558

Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks

  • To detemrine the similarity between sentences, BERT requires that both sentences are fed into the network
  • Finding the most similar pair in a collection of 10,000 sentences requires about 50 million inference computations (~65 hours) with BERT.
  • The construction of BERT makes it unsuitable for semantic similarity search as well as for unsupervised tasks like clustering

Sentence-BERT

  • With standard BERT, two sentences are passed to the transformer network together, and a single target value (e.g. similarity score) is predicted from that joint pass
  • This setup doesn’t scale to tasks that need many pairwise comparisons — e.g. clustering a collection of sentences, which requires a similarity score between every pair
  • For \(n = 10{,}000\) sentences, a full pairwise similarity matrix requires \[ n(n-1)/2 = 49{,}995{,}000 \] separate inference passes through BERT — each one a full forward pass of the transformer, since BERT has no way to compute a similarity score without processing both sentences jointly
  • According to the authors, the time required for this is reduced from 65 hours with BERT to less than 10 seconds with SBERT
    • computation of 10,000 sentence embeddings (~5 seconds with SBERT)
    • computing cosine similarity (~0.01 seconds).

SBERT sentence embeddings

  • Creation of sentence vector: The linear classification layer of BERT is not used
  • The attention outputs are pooled to derive a fixed sentence embedding

SBERT sentence embeddings

  • SBERT assesses the similarity of two sentences by the cosine similarity of their vectors

\[ \mathrm{sim}(\mathbf{x},\mathbf{y}) = \frac{\mathbf{x}\cdot \mathbf{y}}{\Vert \mathbf{x} \Vert \Vert \mathbf{y} \Vert} = \frac{\sum_i A_i\cdot B_i}{\sqrt{\sum_i A_i^2}\cdot \sqrt{\sum_i B_i^2}} \]

  • But nobody told BERT that the sentence embeddings it produces should be compatible with cosine similarity
  • How do we teach BERT to do this?

Classification: natural language inference (NLI)

  • SBERT is first trained on natural language inference (NLI) data: SNLI (570,000 pairs) + MultiNLI, each pair labeled entailment, contradiction, or neutral
  • Each pair has a premise and a hypothesis, encoded independently by the siamese BERT, then mean-pooled to give sentence vectors \(u\) and \(v\)
Premise Hypothesis Label
The sun is shining It is not raining entailment
The girl is playing with a ball The girl is sleeping contradiction
An older and younger man smiling Two men are smiling and laughing at cats playing on the floor neutral
  • Entailment: a human reading the premise would infer the hypothesis is most likely true — not strict logical entailment (sun shining doesn’t logically rule out rain — this is a plausibility judgment, not a proof)
  • Contradiction: premise and hypothesis can’t both be true
  • Neutral: hypothesis may or may not be true given the premise — it adds information the premise doesn’t confirm or deny
  • Entailment is directional (premise → hypothesis); contradiction and neutral aren’t

Classification: natural language inference (NLI)

  • The sentences (\(u\)=premise, \(v\)=hypothesis) are combined into a single feature vector:

\[ (u, v, |u-v|) \]

  • \(|u - v|\) is the element-wise absolute difference — it makes the relationship between the two embeddings explicit, rather than making the classifier re-derive it from \(u\) and \(v\) alone
  • This concatenated vector (size \(3n\), e.g. \(3\times768\)) is passed through a trainable linear layer \(W_t\) and softmax:

\[ o = \mathrm{softmax}\big(W_t\,(u, v, |u-v|)\big) \]

Classification: natural language inference (NLI)

  • Trained with standard cross-entropy loss against the gold label (entailment / contradiction / neutral)
  • This forces the model to learn fine-grained semantic distinctions — not just “same topic,” but “does this logically follow?”
  • \(W_t\) is a training-time-only scaffold: at inference, it’s discarded — only the sentence encoder + pooling is kept

Regression Objective Function

  • Now we want to train the model to recognize “similar” sentences.
  • The authors use a Semantic Textual Similarity (STS) Benchmark
Score Meaning Example 1 Example 2
5 The two sentences are completely equivalent, as they mean the same thing. The bird is bathing in the sink. Birdie is washing itself in the water basin.
4 The two sentences are mostly equivalent, but some unimportant details differ. Two boys on a couch are playing video games. Two boys are playing a video game.
3 The two sentences are roughly equivalent, but some important information differs/missing. John said he is considered a witness but not a suspect. “He is not a suspect anymore.” John said.
2 The two sentences are not equivalent, but share some details. They flew out of the nest in groups. They flew into the nest together.
1 The two sentences are not equivalent, but are on the same topic. The woman is playing the violin. The young lady enjoys listening to the guitar.
0 The two sentences are completely dissimilar. The black dog is running through the snow. A race car driver is driving his car through the mud.

Regression Objective Function

  • Mean squared-error (MSE) loss for the cosine similarity is used as the objective function.

  • The STS Benchmark labels are on a \(y\in 0–5\) scale, so before computing the loss, the gold score \(y\) is rescaled as \(y^{\prime} = \frac{y}{2.5} - 1\). For instance

    • if \(y=5\), \(y^{\prime} = \frac{5}{2.5} - 1 = 1\).
    • if \(y=0\), \(y^{\prime} = \frac{0}{2.5} - 1 = -1\).

Regression Objective Function

  • directly using the output of BERT leads to rather poor performances.
  • Averaging the BERT embeddings achieves an average correlation of only 54.81, and using the CLStoken output only achieves an average correlation of 29.19.
  • The SBERT/SRoBERTa approaches generally showed superior performance
Model STS12 STS13 STS14 STS15 STS16 STSb SICK-R Avg.
Avg. GloVe embeddings 55.14 70.66 59.73 68.25 63.66 58.02 53.76 61.32
Avg. BERT embeddings 38.78 57.98 57.98 63.15 61.06 46.35 58.40 54.81
BERT CLS-vector 20.16 30.01 20.09 36.88 38.08 16.50 42.63 29.19
InferSent - Glove 52.86 66.75 62.15 72.77 66.87 68.03 65.65 65.01
Universal Sentence Encoder 64.49 67.80 64.61 76.83 73.18 74.92 76.69 71.22
SBERT-NLI-base 70.97 76.53 73.19 79.09 74.30 77.03 72.91 74.89
SBERT-NLI-large 72.27 78.46 74.90 80.99 76.25 79.23 73.75 76.55
SRoBERTa-NLI-base 71.54 72.49 70.80 78.74 73.69 77.77 74.46 74.21
SRoBERTa-NLI-large 74.53 77.00 73.18 81.85 76.82 79.10 74.29 76.68

Triplet Loss

  • Requires triplets: an anchor \(a\), a positive \(p\) (similar to \(a\)), and a negative \(n\) (dissimilar to \(a\)) — no graded similarity score needed, just relative ordering

Given an anchor sentence \(a\), a positive sentence \(p\), and a negative sentence \(n\):

  • triplet loss tunes the network such that the distance between \(a\) and \(p\) is smaller than the distance between \(a\) and \(n\)
  • We assume that a sentence is thematically closer to sentences within its section than to sentences from other sections

  • \(a\): McDonnell resigned from Martin in 1938 and founded McDonnell Aircraft Corporation in 1939

  • \(p\): In 1967, McDonnell Aircraft merged with the Douglas Aircraft Company to create McDonnell Douglas

  • \(n\): Born in Denver, Colorado, McDonnell was raised in Little Rock, Arkansas, and graduated from Little Rock High School in 1917

The authors assumed that sentences from the same Wikipedia section are more similar than those from different sections - Dor, YM, et al. (2018) Learning Thematic Similarity Metric from Article Sections Using Triplet Networks

Triplet Loss

  • Compute two Euclidean distances: \[ d_{ap} = \|s_a - s_p\|, \qquad d_{an} = \|s_a - s_n\| \]

  • Loss (with margin \(\epsilon\)): \[ \mathcal{L} = \max\big(d_{ap} - d_{an} + \epsilon,\ 0\big) \]

  • The margin \(\epsilon\) ensures that \(s_p\) is at least \(\epsilon\) closer to \(s_a\) than \(s_n\).

  • As metric Euclidean distance was used.

  • \(\epsilon=1\)

Triplet Loss — role of the margin \(\epsilon\)

  • If \(p\) is already closer to \(a\) than \(n\) is, by at least \(\epsilon\) → loss is 0, no gradient, nothing to learn
  • Otherwise → loss is positive, and gradients pull \(a\) and \(p\) together / push \(a\) and \(n\) apart until the margin is satisfied
\(d_{ap}\) \(d_{an}\) \(d_{ap}-d_{an}+1\) Loss Interpretation
0.3 2.0 \(-0.7\) 0 well-separated — no update
0.3 0.9 \(0.4\) 0.4 \(p\) closer, but less than \(\epsilon\)
1.2 0.8 \(1.4\) 1.4 \(n\) is closer — larger loss
  • Without the margin, the loss would be 0 even if \(d_{ap}\) is only slightly smaller than \(d_{an}\) — a weak signal that stops training too early
  • The margin forces a gap, producing embeddings with clearer separation between similar and dissimilar sentences

SBERT: three objectives

Classification (NLI) Regression (STS) Triplet
Data needed labeled pairs (entailment/contradiction/neutral) labeled pairs (0–5 similarity score) (anchor, positive, negative) triplets — no score
Extra trainable layer? yes — \(W_t\), size \(3n \times k\) no no
Similarity metric — (cross-entropy on softmax) cosine similarity Euclidean distance
Loss cross-entropy MSE hinge (\(\max(\cdot,0)\)) with margin

SBERT - Summary

  • SBERT maps sentences to a vector space that better reflects sentence emantic similarity than BERT’s sentence vectors
  • SBERT is computationally efficient. SBERT can be used for tasks which are computationally not feasible to be modeled with BERT (e.g., clustering of large corpora of sentences).
  • We will see in a later lecture that SBERT is often used to implement Retrieval Augmented Generation (RAG)

Sources