Group-Based Policy Optimization › A Worked Example by Hand
Language AI HandbookAll chapters Before writing code, work through a small group by hand. Suppose a prompt receives five sampled responses with rewards . The group mean is and the population standard deviation is . The normalization gives:
The unrounded advantages sum to zero. The displayed values sum to because of rounding. There are fewer incorrect answers in this group, so each has a larger absolute advantage than each correct one. A zero sum of advantage coefficients does not imply a zero update.
Return to the opening group with seven rewards of 1 and one reward of 0. Its mean is and its population standard deviation is . Each correct completion gives its tokens advantage . The incorrect completion gives its tokens advantage . At the start of an update with the learner equal to the behavior policy, every ratio is one. The clipped surrogate for each token then equals that token’s advantage before the KL term and the two averaging steps apply.
The group mean plays a role similar to the reference point in grading on a curve. In the five-response group, the correct answers sit 0.4 above it and the incorrect answers sit 0.6 below it before scaling. If all answers receive the same score, every score equals the group mean and the guarded advantages are zero.
PythonIn [13]Expand
import torch
rewards_example = torch.tensor([1.0, 0.0, 1.0, 1.0, 0.0])
mean_r = rewards_example.mean()
std_r = rewards_example.std(unbiased=False)
adv_example = (rewards_example - mean_r) / (std_r + 1e-4)
print(f"rewards: {rewards_example.tolist()}")
print(f"group mean: {mean_r.item():.4f}")
print(f"group std: {std_r.item():.4f}")
print(f"advantages: {[round(x, 4) for x in adv_example.tolist()]}")
print(f"adv mean: {adv_example.mean().item():.6f}")ConsoleOut [13]Expand
rewards: [1.0, 0.0, 1.0, 1.0, 0.0] group mean: 0.6000 group std: 0.4899 advantages: [0.8163, -1.2245, 0.8163, 0.8163, -1.2245] adv mean: -0.000000
The computed advantages match the hand calculation. The mean of the advantages is approximately zero, which is what the within-prompt normalization is designed to enforce. Note that the epsilon in the denominator has essentially no effect here, because the standard deviation is well above it; the epsilon exists for the case where the standard deviation collapses to zero.
The figure below visualizes the mapping from rewards to advantages for this group.
VisualizationOut [14]Expand

Two properties are worth noting. First, the correct responses are visually shorter than the incorrect ones: versus . That asymmetry follows directly from the mean being , closer to 1 than to 0. Second, subtracting one shared mean makes the advantages sum to zero in exact arithmetic. Dividing by one shared positive number, including the standard deviation plus epsilon, preserves that arithmetic identity. The inclusive group mean still depends on each action, and the weighted token gradient can be nonzero.
If all five rewards were one, the group mean would be one and the standard deviation would be zero. The unguarded equation would be undefined. With the positive epsilon used in the example, every advantage is zero and this group contributes no reward-surrogate gradient.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!