Output Layers in Neural Networks
Each node in the original output layer stands for one possible next token. Because every token in the vocabulary has a node, the model can, in principle, continue any piece of text.
A Model Built to Continue Text
A large language model is built to continue text, one token at a time. Its ordinary output is a choice among tens of thousands of possible next tokens. That is a sensible shape for writing a sentence, but it is the wrong shape for a simple decision such as whether a text message is spam or not spam. Classification fine-tuning is the process that adapts the model to this kind of task. In this article we follow the model's output side step by step, so you can see what state it is in before, during, and after the change.
What do you think happens?
A spam versus not spam task has two possible answers. How many output nodes should the new classification layer have?
Reveal answer
Two. The classification layer has one output node per class, and spam versus not spam has two classes.
The number of output nodes is set by the number of labels the model is allowed to output, not by the size of the original vocabulary.
Tracing the Original Output Layer
The output layer is the part of the model that turns its internal computation into a final result. In the original setup, this layer has one output node for every unique token in the vocabulary. The vocabulary size used here is 50,256, so the original layer has 50,256 nodes.
Each node in the original output layer stands for one possible next token. Because every token in the vocabulary has a node, the model can, in principle, continue any piece of text.
Notice what this means for the state of the model before any fine-tuning. Its output is not a verdict. It is a score over tens of thousands of candidate tokens, and whichever candidate scores highest becomes the next word the model produces.
What Gets Swapped and What Stays
Classification fine-tuning swaps the large original output layer for a new, much smaller one. The new layer is sized to the task rather than to the vocabulary. The rest of the model is the part being adapted for the task, while the output layer is the part that changes so that answers take the form of class labels.
Read the diagram as a before and after of one component. The body of the model is carried over, and the output layer is the piece that changes. In the before state the output layer has 50,256 nodes. In the after state, for a two-class task, it has 2 nodes. The number of nodes is the only size change this section describes; the source does not specify other changes to the layer.
Counting Output Nodes for a Task
The number of output nodes in the new classification layer equals the number of classes in the task. Each node corresponds to one label the model is allowed to output. The class count is the deciding factor, so the first step in designing the layer is to list the labels.
The diagram shows the spam case from the source. There are two labels, so there are two output nodes, and each node is tied to one label. Which node takes which label is a convention chosen when the model is set up; what the source fixes is that each node corresponds to exactly one class.
Sizing a classification layer for three sentiment labels
A team wants a model that reads a product review and outputs one of three labels: positive, neutral, or negative. How many output nodes does the classification layer need?
- 1List the classes
The labels are positive, neutral, and negative. That is three classes.
- 2Apply the rule
The number of output nodes equals the number of classes, so the layer needs one node per label.
- 3Check against the vocabulary
The original layer would have one node per vocabulary token, 50,256 in this setup. The task needs only the three label nodes, so the layer shrinks to match the task.
Three output nodes, one for positive, one for neutral, and one for negative.
Next-Token Prediction Versus Class Labels
The change in the output layer also changes what the model is trained to produce. During pretraining, the model predicts the next token in the text. Classification fine-tuning instead trains the model to output a correct class label, such as spam or not spam. The question the model answers moves from what word comes next to which category this input belongs to.
Following One Input to a Predicted Label
Trace a single text message through the fine-tuned model. The text enters the model body, where it is processed into an internal computation. That computation reaches the classification layer, whose nodes correspond to the labels. The label tied to the highest-scoring node is the model's predicted label. The output is a class, not a continuation of the text.
Mistakes Beginners Make with Output Layers
Common mistake Keeping the 50,256-node output layer for a spam task
What it looks likeAssuming a spam detector still needs one output node for every vocabulary token.
Why it failsThe classification layer is sized to the task, not to the vocabulary. A two-class task uses two output nodes.
Do this insteadCount the classes first, then give the classification layer one node per class.
Common mistake Confusing the number of nodes with the number of inputs
Why it failsThe source ties the node count to the number of classes. The size of the input text does not determine the output node count.
Do this insteadAsk only how many labels the model may output when deciding the node count.
Common mistake Believing the fine-tuned model still predicts the next token
What it looks likeExpecting a spam classifier to produce a continuation of the message.
Why it failsAfter fine-tuning, the model is trained to output a correct class label rather than predict the next token.
Do this insteadRead the output as a label choice, such as spam or not spam.
Common mistake Treating the node-to-label assignment as fixed by the source instead of a setup convention
Sizing a Layer for Your Own Labels
A support team wants a model that reads an incoming ticket and assigns one of four categories: billing, technical, account, or other. Before you answer, trace the state of the output layer: start from the original layer, then identify what replaces it. How many output nodes does the classification layer need, and what does each node correspond to?
Hints
- Start by listing the classes in the task.
- The original layer has one node per vocabulary token; the new layer has one node per class.
- Each node corresponds to exactly one label the model may output.
Explain in one sentence why a model fine-tuned for a four-category ticket task would not need the 50,256-node output layer, and say what question its output now answers.
Hints
- Refer to what the output layer is sized to.
- Refer to whether the output is a next token or a class label.
Key Takeaways
- Classification fine-tuning replaces the large output layer of a language model with a smaller classification layer.
- The original output layer has one node per unique vocabulary token, which is 50,256 in the setup described here.
- A classification layer has one output node per class, so a spam versus not spam task uses two nodes.
- After fine-tuning, the model is trained to output a correct class label instead of predicting the next token.