DL0067 BERT VS GPT Pretraining

How does BERT’s pre-training objective differ from GPT’s?

Answer

BERT is trained primarily with masked language modeling: selected input tokens are corrupted, and a bidirectional encoder predicts the original tokens from visible context on both sides. GPT uses causal language modeling: a decoder predicts each next token using only earlier tokens, enforced by a triangular attention mask. MLM supplies loss only at selected positions and creates a corruption mismatch between pre-training and ordinary inputs, whereas causal modeling supplies a target at nearly every position and matches left-to-right generation. Original BERT also used next sentence prediction, while standard GPT pre-training relies on the autoregressive token objective rather than NSP.

Side-by-side BERT masked-language-modeling and GPT causal-language-modeling pre-training objectives.

Figure 1: BERT reconstructs selected corrupted tokens from two-sided context, while GPT predicts the next token at every step from a left-only prefix.

(1) Context Visibility: BERT’s visible tokens attend bidirectionally; GPT position t cannot attend to positions later than t.
(2) Prediction Targets: BERT reconstructs a selected masked subset, while GPT shifts the sequence and predicts the next token at each usable position.
(3) Downstream Bias: BERT naturally supports full-context understanding, whereas GPT’s objective directly trains open-ended autoregressive generation; either family can be adapted beyond that bias.

Mathematical Formulation:
\mathcal{L}_{\mathrm{BERT}}=-\sum_{i\in\mathcal{M}}\log p(x_i\mid x_{\setminus\mathcal{M}})
\mathcal{L}_{\mathrm{GPT}}=-\sum_{t=1}^{T}\log p(x_t\mid x_{1:t-1})

Where:

  • \mathcal{L}_{\mathrm{BERT}} and \mathcal{L}_{\mathrm{GPT}} are the masked and causal language-modeling losses.
  • \mathcal{M} is BERT’s selected target set and i\in\mathcal{M} indexes one target token x_i.
  • x_{\setminus\mathcal{M}} denotes BERT’s visible corrupted context outside the selected targets, and p(x_i\mid x_{\setminus\mathcal{M}}) is the reconstruction probability.
  • t\in\{1,\ldots,T\} indexes a GPT target position, T is sequence length, and x_{1:t-1} is the preceding token prefix.
  • p(x_t\mid x_{1:t-1}) is the next-token probability and \log converts each probability into a log-likelihood term.
BERT bidirectional and GPT causal attention matrices with their respective training target positions.

Figure 2: Attention visibility explains the objective difference: BERT uses a full visible-context matrix, while GPT uses a lower-triangular causal matrix.


Login to view more content

Did you solve the problem?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *