Transformer-based language models use different parts of the Transformer and different pre-training objectives. These choices affect whether a model is strongest at text understanding, text generation, or both.

Pre-training and fine-tuning

Pre-training learns broad language patterns from a large and diverse collection of unlabelled text.

Fine-tuning continues training on a smaller labelled dataset for a specific task such as sentiment classification, question answering, or named entity recognition.

large unlabelled corpus -> pre-trained model -> task dataset -> fine-tuned model

Main architecture and objective choices

FamilyTransformer partMain objectiveMain strength
Encoder-onlyEncoder stackReconstruct masked or corrupted textBidirectional understanding
Decoder-onlyDecoder stackPredict the next token from left to rightAutoregressive generation
Encoder-decoderEncoder and decoderEncode source or corrupted text, then generate target textText-to-text tasks

Autoregressive objective

An autoregressive model predicts later text from earlier text. The decoder uses a causal mask, so each position can attend only to the current and earlier positions.

This objective matches text generation because pre-training and generation both use left-to-right prediction.

Autoencoding objective

An autoencoding model receives corrupted text and reconstructs the original content. Masked language modelling is one example.

The encoder can use context from both sides of a masked position. This design supports language-understanding tasks.

Position and token representation

The original Transformer adds absolute sinusoidal positional encodings to token embeddings. Transformer-XL uses relative positions so attention can represent the distance between tokens.

Large language models commonly use subword tokenisation. Common methods include WordPiece and Byte Pair Encoding, or BPE. Subwords reduce the unknown-word problem while keeping the vocabulary manageable.

BERT family

BERT uses an encoder stack and two pre-training tasks.

Masked Language Modelling

BERT selects 15 percent of its input tokens:

  • 80 percent of the selected tokens become [MASK]
  • 10 percent become random tokens
  • 10 percent remain unchanged

The model predicts the original selected tokens from bidirectional context.

Next Sentence Prediction

BERT receives two sentences and predicts whether the second sentence follows the first. Half of the training pairs are consecutive sentences, and half use a random second sentence.

Fine-tuning uses

BERT fine-tuning supports four main task types:

  • Sentence-pair classification
  • Single-sentence classification
  • Question answering
  • Single-sentence tagging, such as named entity recognition

RoBERTa and distilled variants

RoBERTa changes the BERT training procedure:

  • Removes Next Sentence Prediction
  • Uses dynamic masking across epochs
  • Uses BPE instead of WordPiece
  • Trains with more data and longer sequences

DistilBERT trains a smaller student model to imitate a larger BERT teacher. It is 40 percent smaller, runs 60 percent faster, and retains 97 percent of BERT's language-understanding capability.

DistilRoBERTa applies the same teacher-student idea with RoBERTa as the teacher.

GPT family

GPT uses a decoder-only architecture and left-to-right autoregressive pre-training.

The GPT family developed through this progression:

  • GPT-1 relied on downstream fine-tuning
  • GPT-2 showed stronger in-context task behaviour
  • GPT-3 demonstrated zero-shot and few-shot in-context learning at a much larger scale
  • GPT-4 added multimodal input and can process text and images

In-context learning and fine-tuning

MethodTask informationWeight update
Zero-shotInstruction with no exampleNo
One-shotInstruction with one exampleNo
Few-shotInstruction with several examplesNo
Fine-tuningTask datasetYes

In-context learning changes the prompt and the model activations. Fine-tuning changes model parameters.

InstructGPT and human feedback

InstructGPT first learns from human-written demonstrations. It then uses reinforcement learning from human feedback, where human rankings guide the model towards preferred outputs.

Encoder-decoder models

T5

T5 converts every task into text input and text output. During pre-training, it replaces masked spans with sentinel tokens and learns to generate the missing spans.

BART

BART uses a bidirectional encoder and an autoregressive decoder. Its denoising objective masks 30 percent of tokens and permutes sentence order. The decoder reconstructs the original text.

Transformer-XL and XLNet

Transformer-XL caches hidden states from an earlier segment and reuses them when it processes the next segment. This segment-level recurrence supports dependencies that extend beyond one fixed segment. It also uses relative positional information.

XLNet uses permutation language modelling. It learns to predict tokens under different factorisation orders while keeping autoregressive prediction. It is based on Transformer-XL and uses segment recurrence and relative positions.

Main comparison

ModelArchitectureKey learning method
BERTEncoder-onlyMasked language modelling and Next Sentence Prediction
RoBERTaEncoder-onlyBERT-style masking without Next Sentence Prediction
DistilBERTEncoder-only studentDistillation from BERT
GPTDecoder-onlyLeft-to-right next-token prediction
T5Encoder-decoderSpan corruption and text-to-text generation
BARTEncoder-decoderDenoising and autoregressive reconstruction
XLNetTransformer-XL basedPermutation language modelling

Related notes: Transformers · Attention Masks · Causal Language Modelling · In-Context Learning · LLM Training Stages · Tokenization Schemes

Code

Hugging Face: fine-tune BERT for five-class text classification

import numpy as np
import torch
import evaluate
from datasets import load_dataset
from transformers import (
    AutoModelForSequenceClassification,
    AutoTokenizer,
    Trainer,
    TrainingArguments,
)
 
dataset = load_dataset("yelp_review_full")
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
 
def tokenize_function(examples):
    return tokenizer(
        examples["text"],
        padding="max_length",
        truncation=True,
    )
 
tokenized = dataset.map(tokenize_function, batched=True)
train_data = tokenized["train"].shuffle(seed=42).select(range(1000))
eval_data = tokenized["test"].shuffle(seed=42).select(range(1000))
 
model = AutoModelForSequenceClassification.from_pretrained(
    "bert-base-cased",
    num_labels=5,
)
 
accuracy = evaluate.load("accuracy")
 
def compute_metrics(eval_pred):
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=-1)
    return accuracy.compute(predictions=predictions, references=labels)
 
training_args = TrainingArguments(
    output_dir="test_trainer",
    evaluation_strategy="epoch",
)
 
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_data,
    eval_dataset=eval_data,
    compute_metrics=compute_metrics,
)
 
trainer.train()

The tokenizer returns token IDs and an attention mask. The attention mask marks real tokens and padding positions. AutoModelForSequenceClassification adds a classification head to BERT. With num_labels=5, the model returns five logits for each review.

PyTorch: run one prediction

model.eval()
 
example = tokenizer(
    "The food was excellent.",
    return_tensors="pt",
    padding=True,
    truncation=True,
)
 
with torch.no_grad():
    logits = model(**example).logits
 
predicted_class = logits.argmax(dim=-1)
print(tuple(logits.shape))        # (1, 5)
print(predicted_class.item())

model.eval() selects evaluation behaviour for dropout and similar layers. It does not freeze parameters. torch.no_grad() prevents gradient storage during this prediction. The output still contains raw logits, and argmax selects the largest class score.