In each iteration of the PPO algorithm, actors each run the current policy for steps, advantage estimates are computed for every step of every segment, and the clipped objective is then optimised on the samples for epochs of minibatch gradient ascent. Implement one such iteration for a linear softmax policy, with the advantages from the paper's truncated generalized advantage estimator.
The policy. theta has shape (K_act, D). For a state vector of length the action probabilities are .
segment_advantages(rewards, values, gamma, lam)
rewards: shape (N, T), row is actor 's segment . values: shape (N, T + 1), the value predictions ; the last column is the value of the state after the segment. No episode ends inside a segment.(N, T) NumPy array of
ppo_iteration(theta, states, actions, rewards, values, gamma=0.99, lam=0.95, eps=0.2, lr=0.1, epochs=3, minibatch=4, seed=0)
states: shape (N, T, D); actions: (N, T) ints; rewards, values as above. theta is the policy that collected the data (); do not modify the caller's array.segment_advantages. Flatten states, actions and advantages to samples in actor-major order (actor 0's steps first). Store for every sample.np.random.RandomState seeded with seed, before the first epoch. For each of the epochs epochs, take perm = rng.permutation(N * T) and cut it into consecutive minibatches of minibatch indices (the last one may be shorter). For each minibatch , compute the gradient of
at the current and take a step of gradient ascent: .Gradient convention: a sample counts when and then contributes ; otherwise the minimum is a constant and it contributes 0.
Return a dict: "advantages" (the (N, T) advantages as a nested list), "n_steps" (number of gradient steps taken, int), "objective" and "clip_fraction" (one value per epoch), and "theta" (the final parameters as a nested list), all floats rounded to 4 decimals.