liveBook · Manning

Output layer

As explained in Reinforcement Learning from Human Feedback

A scalar head is a model output component that maps hidden representations or logits to a single numerical prediction.

An output layer converts a model’s internal representation into the prediction consumed by the next stage. In a scalar reward model, a small linear head maps the final hidden representation of a sequence to one numerical score. Pairwise preference learning then compares the chosen and rejected scores; the loss depends on their difference, not their absolute offset. section 5.1 section 5.9

A typical scalar output pipeline proceeds as follows:

  • Run tokenized input through the base language model and request hidden states. section 5.2
  • Select one sequence representation, commonly the last non-padding or EOS token, using the attention mask. section 5.2
  • Apply a linear layer from the model hidden size to one output unit, producing one scalar per sequence. section 5.2
  • Compare scalar outputs for chosen and rejected completions with a sigmoid or log-sigmoid preference loss. section 5.1 section 5.2

The same architectural idea supports different prediction granularities. A standard preference reward model emits one logit for an entire sequence, whereas an outcome reward model applies a scalar head at every token and predicts correctness probabilities for completion tokens. Prompt tokens are masked during training and scoring. section 5.4 section 5.9

Minimal scalar head

Given a base language model with hidden size h, define a head as a linear map from h features to one value. Run chosen and rejected inputs separately, obtain rewards_chosen and rewards_rejected, and optimize -logsigmoid(rewards_chosen - rewards_rejected). section 5.2

import torch.nn as nn

rewards_chosen = model(**inputs_chosen) 1
rewards_rejected = model(**inputs_rejected)  2

loss = -nn.functional.logsigmoid(rewards_chosen - rewards_rejected).mean()

The listing connects the scalar output layer directly to pairwise reward-model training. section 5.2

How Reinforcement Learning from Human Feedback covers it

How deep it goes
Explains sequence pooling, attention-mask indexing, linear projection, pairwise loss, and per-token variants without covering distributed loader details.
Examples
Uses last-token pooling, chosen-versus-rejected completions, scalar reward comparisons, and token-aligned correctness labels.
What it assumes
Assumes familiarity with transformer hidden states, attention masks, logits, and preference or binary-cross-entropy losses.
What you can do after
Build it from scratch by attaching a linear projection to hidden states and training sequence or token scores with the appropriate objective.
How it connects
Connects hidden states, language models, reward models, Bradley–Terry models, preference learning, and outcome reward models.

Also explained in

Compare how 6 books explain output layer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Reinforcement Learning from Human Feedback. The section links point to the parts of the book it draws on.