PPO clipped surrogate loss
Lesson: Proximal Policy Optimization (PPO)Cheat sheetTrack: RL basics
Problem
PPO trains a policy for several epochs on one batch of samples. The importance ratio compares the new policy with the policy that collected the samples. PPO clips this ratio, so that one update cannot move the policy too far. The same clipped loss is the core of many RLHF pipelines.
Task
Write three functions. Each takes the new log probabilities, the old log probabilities and the advantages of N samples, and the clip size eps.
ppo_clip_loss(logp_new, logp_old, advantages, eps=0.2)returns the loss to minimize.clip_fraction(logp_new, logp_old, advantages, eps=0.2)returns the fraction of clipped samples.ppo_clip_grad(logp_new, logp_old, advantages, eps=0.2)returns the gradient of the loss with respect tologp_new.
The ratio of sample t is r_t = \exp(\log\pi_{new,t} - \log\pi_{old,t}). The loss is:
Sample t is clipped when A_t > 0 and r_t > 1 + \epsilon, or when A_t < 0 and r_t < 1 - \epsilon. Both comparisons are strict. A sample with A_t = 0 is not clipped.
The gradient of a clipped sample is 0. The gradient of any other sample is -r_t A_t / N.
Input and output
| Name | Type | Shape | Meaning |
|---|---|---|---|
logp_new | float array | (N,) | the log probability of each action under the new policy |
logp_old | float array | (N,) | the log probability of each action under the old policy |
advantages | float array | (N,) | the advantage of each sample, used as given |
eps | float | the clip size, from 0 to 1 |
ppo_clip_lossreturns a float.clip_fractionreturns a float from 0 to 1.ppo_clip_gradreturns a float array of shape(N,).
Constraints
1 <= N <= 4096.- Log probabilities are from -1000 to 0.
- Do not normalize the advantages.
- Do not change the input arrays.
- Use numpy only.
Examples
pythonlogp_old = np.log(np.array([0.5, 0.5, 0.5, 0.5]))
logp_new = np.log(np.array([0.7, 0.55, 0.3, 0.45]))
advantages = np.array([1.0, 2.0, -1.0, -0.5])
ppo_clip_loss(logp_new, logp_old, advantages)
clip_fraction(logp_new, logp_old, advantages)
ppo_clip_grad(logp_new, logp_old, advantages)text-0.5375000000000001
0.5
[ 0. -0.55 0. 0.1125]The ratios are [1.4, 1.1, 0.6, 0.9]. Sample 0 has A > 0 and r > 1.2. Sample 2 has A < 0 and r < 0.8. These two samples are clipped.
Hints
4 hintsSolution and notes
LockedPass every check with Submit to open the reference solution and the notes.
Run it on your computer
Clone the repository, install numpy and run the same checks in a terminal:
python3 scripts/practice/run.py ppo-clip-loss my_answer.py
python3 scripts/practice/run.py ppo-clip-loss my_answer.py --examplesStart my_answer.py from starter.py.
Your code is saved in this browser as you type. On a phone you can read and edit the code, but the checks are easier to use on a desktop.
Keyboard shortcuts
- Ctrl Enter
- Run the examples
- Ctrl Shift Enter
- Submit
- Tab / Shift Tab
- Indent or outdent the lines
- Esc then Tab
- Move the focus out of the editor
- Alt H
- Show the next hint
On a Mac, use Cmd in place of Ctrl.
Select Run to check the examples, or Submit to run every check.