PPO: learning without overcorrecting.
One section from Auto Park
I used Proximal Policy Optimization, or PPO, to train a model called a policy to choose throttle and steering in the parking experiments. I gave it the car’s state and a target directly from PyBullet, then used the drives it collected to update the model.
I scored what the car did through rewards from the environment and combined those over time into a return, giving later rewards less weight. Even when a maneuver earned a good return, making its actions much more likely after one batch could make the next drives worse. I used PPO-Clip to limit how much extra credit the objective gave that kind of increase.
What gets updated?
I sampled actions from the policy during training, then compared how likely the new and old policies made the same action in the same state. I used their ratio, r, which starts at 1 before the update, rises when the action becomes more likely, and falls when it becomes less likely. Because throttle and steering are continuous, I was comparing probability densities.
I also needed an advantage estimate to judge whether the action led to a better or worse outcome than expected from that state. I used its sign to decide whether the update should make that action more likely or less likely.
I collected drives with the old policy and updated the model using smaller groups of those samples, called minibatches, while keeping their stored log-likelihoods fixed. Subtracting those log-likelihoods and taking exp gave me the new likelihood divided by the old one.
# Old scores and advantages stay fixed.
for batch in minibatches(drives):
joint_logp = policy.joint_log_prob(
batch.states, batch.actions)
r = exp(joint_logp - batch.old_joint_logp)
A = batch.advantages
raw = r * A
limited_r = clip(r, 0.8, 1.2)
clipped = limited_r * A
loss = -mean(minimum(raw, clipped))
gradient_step(policy, loss)
I used minimum to take the smaller contribution for each sample, then averaged those contributions across the batch. I used the minus sign to turn the objective into a loss I could minimize. I’ve used an illustrative ε = 0.2 for the bounds here.
I’ve only shown the policy update in this snippet. In the full PPO implementation, I also needed advantage estimates and a trained value function, and could reuse a rollout across several optimization passes. I used the conceptual helper joint_log_prob for the log-likelihood of the whole throttle-and-steering pair, summing across the action dimensions when they’re modeled independently.
One control decision
To work through one update, I can take a sampled throttle-and-steering action with A = +1. If the new policy makes it 1.5 times as likely, the raw contribution becomes 1.5, while the clipped contribution stays at 1.2. I use the smaller value, 1.2, in the objective.
I checked the other direction with A = −1 and r = 0.5, which gives min(−0.5, −0.8) = −0.8. That stops me gaining more from this sample just by making the bad action even less likely, while still penalizing a change that makes it more likely.
What clipping doesn’t promise
I could still end up with ratios outside the clipping bounds, because clipping changed the objective’s incentive without enforcing a hard boundary on the policy. I handled the car’s steering limits separately in the environment.
I had to keep that distinction in mind when reading a parking result: making the training update more conservative didn’t prove the car could handle a new path. I still needed to evaluate it in situations it hadn’t practiced.
I used Stable-Baselines3’s PPO implementation for these experiments, and I’ve kept the pseudocode focused on its central objective so the update is easier to follow.