Transformer: The Final Linear and Softmax Layers
A Transformer's final linear layer acts as a classifier, converting the decoder's output vector into raw scores (logits) for every possible word. The softmax function then turns these scores into probabilities, allowing the model to pick the most likely next…
WHY IT EXISTS: After the decoder has processed all the contextual information, the model holds a vector representing the meaning of the next word to generate. However, this vector is abstract. The model needs a concrete mechanism to select one word from a vocabulary of thousands. The final linear layer and softmax function exist to solve this final translation step.
THE MENTAL MODEL: Imagine you have a complex idea in your head (the decoder's output vector) and you need to pick the single best word from a dictionary to express it. The linear layer is like a massive comparison engine that scores every word in the dictionary against your idea. The softmax function then takes all those scores and turns them into a ranked list of probabilities, making it easy to see which word is the best fit.
HOW IT WORKS: The process has two steps. First, the decoder produces a final vector for the current timestep. This vector is fed into a single, fully connected neural network layer, often called the 'Linear' layer or 'Logits Head'. This layer has a weight matrix with dimensions [decoder_output_size, vocabulary_size]. The multiplication projects the vector into a much larger vector called 'logits', with a raw, un-normalized score for every word the model knows. Second, the softmax function is applied to this logits vector. It exponentiates each score (making them all positive) and then divides each by the sum of all exponentiated scores. The result is a probability distribution where all values sum to 1. The word with the highest probability is the model's prediction.
WHEN TO USE IT: This linear-softmax combination is the standard final step in any generative Transformer model that needs to produce text, from machine translation (like the original Transformer) to large language models like GPT. It's the bridge from the model's internal representation to a human-readable output.
WHEN NOT TO USE IT: While standard for generation, this exact setup isn't used for tasks where the model output is not a word from a vocabulary, such as classification tasks that only need a 'yes/no' output or regression tasks that predict a continuous number.
ONE CANONICAL EXAMPLE: A model is translating "I am a student" to French. After generating "Je suis un", the decoder produces a vector. The linear layer projects this into a logits vector of size 50,000 (the vocabulary size). The softmax function calculates probabilities, and the word "étudiant" receives the highest score (e.g., 0.92), so the model selects it as the next token.
Read the original → jalammar.github.io
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.