Introduction
Welcome back! In our last lesson, we established a high-level map of the Transformer world by comparing the three main architectural families: encoder-only (like BERT), decoder-only (like GPT), and encoder-decoder (like T5). You learned that these architectures are blueprints optimized for different kinds of tasks: understanding, generating, and transforming text, respectively.
Today, we drill down into the "training philosophy" that gives these architectures their distinct capabilities. If the architecture is the what, the pre-training objective is the how—it's the fundamental task the model is trained on to learn the statistical patterns of language. Our learning outcome for this lesson is to contrast the causal language modeling objective (GPT-style) with the masked language modeling objective (BERT-style).
Understanding these two objectives is crucial for you as an applied AI engineer. It explains why GPT-style models are generative powerhouses and why BERT-style models are masters of semantic analysis. This knowledge directly informs model selection for any given business problem.
From Architecture to Objective
Let's start by visually connecting this lesson to the last one. Recall the architectural differences between GPT (decoder-only) and BERT (encoder-only).

These different attention mechanisms—one that can only look backward (causal) and one that can look in both directions (bidirectional)—enable two very different ways of learning from text. Let's define them.
How 🤗 Transformers solve tasks - Hugging Face LLM Course
The Hugging Face LLM Course provides a concise introduction to the two primary training approaches. This will give us our core definitions.
Read the section titled 'How language models work'. Focus on the definitions of Masked Language Modeling (MLM) and Causal Language Modeling (CLM) and which type of model they are associated with.
Now, let's dissect each of these objectives in detail.
1. Causal Language Modeling (CLM): The Generative Engine
Causal Language Modeling is conceptually straightforward: predict the next token in a sequence given all the preceding tokens. It's an autoregressive process, just like how you might write a sentence one word at a time.
- Objective: Maximize the probability of the next token, , given the context of previous tokens .
- Context: Unidirectional (left-to-right). The model is "causal" because it cannot be influenced by future (unavailable) information.
- Architecture: This objective is the natural fit for decoder-only models like GPT. The masked self-attention mechanism within the decoder is what enforces this unidirectional constraint, preventing the model from "cheating" by looking ahead.
To see how this works mechanically, let's watch a detailed explanation.
BERT explained: Training, Inference, BERT vs GPT/LLamA, Fine tuning, [CLS] token
This video by Umar Jamil clearly illustrates how causal masking is implemented in an attention matrix to prevent the model from seeing future tokens. This is the core mechanism that enables Causal Language Modeling.
Watch the segment from 22:50 to 27:30. Pay close attention to how the attention score matrix is modified by the 'causal mask' (setting future positions to -infinity) and how this affects the output calculation, ensuring a token's representation only depends on previous tokens.
Because CLM's training task is literally to generate the next piece of text, models trained this way (like the GPT family, Llama, and Mistral) excel at text generation. Their entire pre-training process is practice for this exact task.
2. Masked Language Modeling (MLM): The Comprehension Engine
Masked Language Modeling takes a different approach. Instead of predicting what comes next, its goal is to predict a missing token from within a sequence. This is often compared to a "fill-in-the-blank" or "cloze" task.
- Objective: Reconstruct original tokens that have been randomly "masked" in the input sequence.
- Context: Bidirectional. To make an accurate prediction for a masked word, the model is encouraged to use context from both the left (preceding words) and the right (following words).
- Architecture: This objective is designed for encoder-only models like BERT. The standard (unmasked) self-attention in an encoder allows every token to attend to every other token, providing the full bidirectional context needed for this task.
This bidirectional nature is MLM's key advantage for understanding tasks. Consider the sentence: "I went to the bank to deposit money." A CLM model predicting "bank" only sees "I went to the". An MLM model, however, can also see "to deposit money," which disambiguates the meaning of "bank" (financial institution vs. river bank).
Let's explore the motivation and mechanics behind this.
Stanford CS224N: NLP with Deep Learning | Winter 2020 | BERT and Other Pre-trained Language Models
This clip from a Stanford lecture (by Jacob Devlin, a lead author of the BERT paper) explains the rationale behind BERT's design. He discusses the limitations of unidirectional models for language understanding and introduces MLM as the solution.
Watch the segment from 09:17 to 14:21. Focus on the core problem with unidirectional models for understanding tasks and how the 'mask and predict' strategy enables true bidirectional learning.
The MLM Training Recipe
The BERT paper introduced a specific strategy to make MLM work effectively:
- Masking Rate: Randomly select 15% of the tokens in a sequence for prediction.
- The 80-10-10 Rule: Of the 15% of tokens selected:
- 80% are replaced with a special
[MASK]token. - 10% are replaced with a random token from the vocabulary.
- 10% are left unchanged.
- 80% are replaced with a special
This 80-10-10 rule is a clever trick. The [MASK] token signals a blank to fill, but the random and unchanged tokens force the model to learn a rich representation of every token, as it can't be sure if a given word is the original one or not. This helps close the gap between pre-training (where [MASK] tokens are seen) and fine-tuning (where they are not).
Masked Language Modeling: Bidirectional Understanding in BERT
This article provides an excellent deep dive into the MLM objective. We'll focus on the sections that formalize the objective and explain the masking strategy.
Read the sections titled 'The MLM Objective', 'The 15% Masking Rate', and 'The 80-10-10 Masking Strategy'. Since you have a strong CS background, pay attention to the loss function and how it differs from CLM (summing loss only over masked positions).
This bidirectional pre-training makes MLM-based models like BERT and RoBERTa exceptionally good at Natural Language Understanding (NLU) tasks such as text classification, named entity recognition, and extractive question answering.
Synthesis: CLM vs. MLM at a Glance
We've established two different philosophies for language model pre-training. Let's bring it all together.

The fundamental trade-off is between generation and understanding.
Masked Language Modeling: Bidirectional Understanding in BERT
The article on MLM provides a final, clear comparison table that summarizes the key differences. This is a great reference to internalize.
Read the section 'Understanding vs. Generation', paying close attention to the comparison table. This table is the core takeaway of the lesson.
| Aspect | Causal LM (GPT-style) | Masked LM (BERT-style) |
|---|---|---|
| Context | Unidirectional (left-only) | Bidirectional (left & right) |
| Primary Use | Generation (writing, chatting) | Understanding (classifying, extracting) |
| Loss Positions | All tokens in the sequence | Masked tokens only (~15%) |
| Architecture | Decoder-only | Encoder-only |
| Inference | Autoregressive (token-by-token) | Single pass (process whole input at once) |
| Example Models | GPT-3, Llama 3, Claude 3 | BERT, RoBERTa, ALBERT |
Test your understanding!
You are tasked with building a system to scan legal documents and identify all clauses related to "liability". The output should be a list of the identified clauses, extracted verbatim from the text.
Which pre-training objective, CLM or MLM, is better suited for the base model you would choose for this task? Why?
Show answer
Masked Language Modeling (MLM) is the better choice.
The task is one of understanding and extraction, not generation. The model needs to build a deep, contextual understanding of the entire document to accurately identify specific clauses. The bidirectional context learned through MLM is critical for this. For example, understanding whether a sentence limits or expands liability might depend on phrases that appear both before and after the word "liability." An MLM-based model like BERT is designed for exactly this kind of full-text analysis.
Conclusion
You have now completed the final lesson of Module 3, connecting the architectural patterns of Transformers to their underlying training philosophies. This is a cornerstone concept that separates a novice from an expert practitioner.
Key Takeaways:
- Causal Language Modeling (CLM) powers decoder-only models like GPT. It's an autoregressive, "next token prediction" task that uses unidirectional context, making these models experts at generation.
- Masked Language Modeling (MLM) powers encoder-only models like BERT. It's a "fill-in-the-blank" task that uses bidirectional context, making these models masters of understanding and analysis.
- The choice between a CLM-based or MLM-based model is a primary decision in system design, dictated entirely by whether your core task is to generate new text or analyze existing text.
What's Next?
With this foundational knowledge of Transformer architectures and training objectives secure, you are ready to move on to the cutting edge. In our next lesson, we will begin Module 4: Modern LLM Architectures and Optimizations.
We will start by exploring the Mixture-of-Experts (MoE) architecture. You'll learn how models like Mixtral 8x7B and reportedly GPT-4 use this technique to massively scale up their parameter counts—and thus their capabilities—while keeping inference computation relatively constant. It's a key innovation that builds directly on the decoder-only, CLM-based models we discussed today.