liveBook · Manning

Last token

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

The final token position in an input sequence, used here as the summary position for classification.

The last token is the final position in a sequence. In GPT-style classification, its output serves as the sequence summary because causal attention lets it incorporate every preceding token. section 6.5

For a four-token input, the data flow is:

  • The model produces one classification vector per input position, with shape [batch, sequence length, classes]. section 6.5
  • Select the final position with outputs[:, -1, :]. section 6.5
  • Use the resulting vector as the input for spam-versus-not-spam prediction. section 6.5

Four-token input

For “Do you have time,” the output has shape [1, 4, 2]. Selecting the last position yields [[-3.5983, 3.9902]], the two-class logits for the sequence. section 6.5

Figure 6.11 The GPT model with a four-token example input and output. The output tensor consists of two columns due to the modified output layer. We are only interested in the last row corresponding to the last token when fine-tuning the model for spam classification.
figure

Shows the four-token output tensor and highlights the final row used for classification. section 6.5

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

How deep it goes
Explains causal-attention rationale, tensor shapes, indexing, and logits, but stops before converting logits into labels.
Examples
Uses “Do you have time,” a four-position output tensor, and spam-versus-not-spam logits.
What it assumes
Assumes familiarity with token sequences and transformer outputs while teaching causal attention’s information-flow consequence.
What you can do after
Apply it via PyTorch tensor indexing to extract classification logits from the final sequence position.
How it connects
Connects causal attention, attention masks, transformer blocks, and classification heads.

Related concepts

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.