liveBook · Manning

Output layer

As explained in Build a Large Language Model (From Scratch)

The final layer of a model that produces prediction scores or class outputs from internal representations.

An output layer converts a model’s final hidden representations into task-specific prediction scores. In GPT classification fine-tuning, replace the vocabulary-sized layer with a linear layer whose outputs match the class count. section 6.5

A classification output layer follows this data flow:

  • The GPT model produces 768-dimensional representations through transformer blocks and final normalization. section 6.5
  • A linear layer maps each representation from 768 units to two class scores for spam and not spam; a three-class task uses three outputs. section 6.5

Replacing the GPT output layer

Set num_classes = 2 and assign torch.nn.Linear(in_features=BASE_CONFIG["emb_dim"], out_features=num_classes) to model.out_head. The resulting final-token scores are tensor([[-3.5983, 3.9902]].

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Explains replacement, class-dependent sizing, tensor shapes, and final-token selection, while deferring loss-function details.
Examples
Uses spam classification, two output scores, a four-token input, and a PyTorch Linear replacement.
What it assumes
Assumes familiarity with pretrained GPT architecture, hidden representations, transformer blocks, and tensor dimensions.
What you can do after
Apply it via PyTorch by replacing model.out_head, choosing outputs equal to the class count, and selecting final-token scores.
How it connects
Connects output layers with GPT models, transformer blocks, LayerNorm, classification heads, and causal attention.

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.