Compute Six Learning Signals for the Same Trajectory
A policy-gradient method estimates the gradient of the expected return as a sum over the steps of a sampled trajectory:
g^=t=0∑T−1Ψt∇θlogπθ(at∣st)
Ψt is a number that says how good the action at step t was. The paper that introduced generalized advantage estimation (Schulman et al., 2015) lists six common choices. Compute all six for one trajectory so they can be compared side by side.
The trajectory has T steps with rewards r0,…,rT−1 and discount γ. A critic supplies state values V(s0),…,V(sT) (the expected return from a state; T+1 numbers, the last one for the state after the final action), action values Q(st,at) (the expected return after taking at in st) and a baseline b(st) that depends only on the state.
psi_signals(rewards, values, q_values, baselines, gamma)
rewards, q_values, baselines: length T. values: length T+1. Otherwise raise ValueError.
- Return a dict with these keys, each a list of T floats rounded to 4 decimals:
"total_return": R=∑k=0T−1γkrk, the same number at every step.
"return_to_go": Gt=∑k=tT−1γk−trk, the return that follows the action.
"baselined": Gt−b(st).
"q": Q(st,at).
"advantage": Q(st,at)−V(st).
"td_residual": rt+γV(st+1)−V(st).
- With T=0 every list is empty.
policy_gradient(grad_log_probs, psi)
grad_log_probs: shape (T, D), row t is ∇θlogπθ(at∣st). psi: length T (one of the lists above).
- Return g^ as a list of D floats rounded to 4 decimals (
[] when T=0). Raise ValueError if the two lengths differ.
Hints
- One backward pass over the rewards gives every Gt; R is G0.
- The return-to-go restarts the discount at step t: the reward at step t itself is not discounted.
- The advantage subtracts the critic's value V(st), not the baseline argument.