Module 15 — Output Projection
The Output Projection is the final stage of a Transformer model. After the encoder or decoder generates hidden representations, these vectors are projected into the vocabulary space to predict the next token.
The output layer converts hidden features into logits, applies the Softmax function to obtain probabilities, and selects the most likely token.
This process is used in almost every Transformer-based language model, including GPT, BERT, T5, LLaMA, Gemma, Qwen, and Mistral.
Topics
- Vocabulary Projection
- Logits
- Softmax
- Token Prediction
1. Hidden Representation
The final hidden representation from the Transformer is
Formula
where
- = Sequence Length
- = Hidden Dimension
2. Vocabulary Projection
The hidden vectors are projected into the vocabulary dimension using a linear layer.
Formula
where
Here,
- = Vocabulary Size
The output shape is
3. Logits
Logits are the raw prediction scores before applying Softmax.
Formula
Each value in the logits vector corresponds to one vocabulary token.
4. Softmax
Softmax converts logits into a probability distribution.
Formula
or equivalently,
Properties
and
5. Token Prediction
The predicted token is the vocabulary token with the highest probability.
Formula
where
6. Complete Output Projection
The complete prediction pipeline is
7. Language Modeling Probability
The probability of predicting the next token is
where
- represents all previously generated tokens.
8. Matrix Dimensions
Suppose
- Sequence Length =
- Hidden Dimension =
- Vocabulary Size =
Hidden Representation
Vocabulary Matrix
Logits
Probability Distribution
Predicted Tokens
9. Weight Tying (Optional)
Many modern language models share the input embedding matrix with the output projection matrix.
Formula
This technique reduces the number of parameters and often improves performance.
10. Complete Output Layer Pipeline
The final prediction process in a Transformer model is
or mathematically,
11. Applications
Output Projection is used in
- GPT
- GPT-2
- GPT-3
- GPT-4
- BERT (Masked Language Modeling Head)
- RoBERTa
- T5
- BART
- LLaMA
- Gemma
- Qwen
- DeepSeek
- Mistral
- Phi
- Vision-Language Models
- Modern Large Language Models (LLMs)
Summary
| Concept | Formula |
|---|---|
| Hidden Representation | |
| Vocabulary Projection |