Skip to content

Classical policy gradient

StableTested and in active use. Breaking changes will be called out.

Everything on the TD methods page learns a value and derives a policy from it. These learn the policy directly, which is what makes continuous actions and stochastic policies natural.

They use linear function approximation, so the algorithm is visible without a network in the way — and they are the direct ancestors of PPO, A2C and the continuous-control family.

from noeira.agents import (
REINFORCEAgent, REINFORCEWithEntropyAgent,
ActorCriticAgent, ActorCriticLambdaAgent,
A2CAgent, PPOAgent, PPOAgentWithMinibatch,
)

The original policy gradient. Run a full episode, then push up the probability of every action taken, weighted by the return that followed it.

AgentNotes
REINFORCEAgentMonte-Carlo policy gradient
REINFORCEWithEntropyAgentplus an entropy bonus

Unbiased and very high variance — the whole episode’s return is credited to every action in it, including the ones that had nothing to do with it. That variance is the problem every later method attacks.

The entropy variant adds a bonus for keeping the policy stochastic, which prevents premature collapse onto a mediocre deterministic policy. The same idea, formalised, is SAC’s maximum-entropy objective.

AgentNotes
ActorCriticAgentone-step actor-critic
ActorCriticLambdaAgentwith eligibility traces

Replace the Monte-Carlo return with a learned value estimate. The critic gives a baseline, so the actor is updated by the advantage — how much better the action was than the state’s average — rather than the raw return.

Much lower variance, at the cost of bias from the critic’s own error. That bias/variance dial is exactly what GAE’s λ exposes in PPO.

AgentNotes
A2CAgentadvantage actor-critic
PPOAgentclipped surrogate
PPOAgentWithMinibatchwith minibatch updates

compute_gae is shared with the deep PPO implementation, so the advantage estimation is literally the same code.

Terminal window
pixi run mojo run -I . examples/cartpole/cartpole_policy_gradient.mojo

DDPGAgent, TD3Agent and SACAgent also exist in linear form. They work on Pendulum and are a good way to see each algorithm’s structure — twin critics, target smoothing, entropy temperature — before networks are involved.

Terminal window
pixi run mojo run -I . examples/solve_pendulum.mojo

Reading these in order is the cheapest way to understand the deep algorithms:

LinearDeep counterpartWhat changed
REINFORCE—baseline added → actor-critic
Actor-criticA2Cneural critic, batched envs
Linear PPOPPOneural nets, GAE, minibatch epochs
Linear SACSACtwin critics, auto-tuned temperature

The algorithms are the same. What the deep versions add is a function approximator that scales — and every failure mode that comes with it.

  • PPO — the deep version
  • A2C — the simplest deep policy gradient
  • TD methods — the value-based side