Policy-Gradient Objectives #
This module exposes typed helpers for the main categorical-policy objectives that modern policy-gradient code tends to rely on:
- REINFORCE,
- advantage actor-critic,
- trust-region / KL-penalized policy-gradient helpers,
- entropy regularization,
- soft actor-critic policy terms,
- PPO's clipped surrogate.
The helpers operate on logits for a finite action space and stay purely functional so they can be used from either eager runtime code or proof-oriented spec code.
Primary references:
- Williams, "Simple Statistical Gradient-Following Algorithms for Connectionist Reinforcement Learning" (1992): https://doi.org/10.1023/A:1022672621406
- Mnih et al., "Asynchronous Methods for Deep Reinforcement Learning" (2016): https://arxiv.org/abs/1602.01783
- Schulman et al., "Trust Region Policy Optimization" (2015): https://arxiv.org/abs/1502.05477
- Schulman et al., "Proximal Policy Optimization Algorithms" (2017): https://arxiv.org/abs/1707.06347
- Schulman et al., "High-Dimensional Continuous Control Using Generalized Advantage Estimation" (2015): https://arxiv.org/abs/1506.02438
Softmax policy induced by a vector of logits.
Instances For
Probability of a selected action under a categorical policy.
Instances For
Log-probability of a selected action.
Instances For
Guarded entropy bonus -Σ q(a) log q(a), where p = softmax logits and
q = clamp p epsilon (1 - epsilon).
Clamped probabilities are used both as weights and as logarithm inputs, without
renormalization. For positive epsilon, this can differ from the Shannon entropy of p.
Instances For
REINFORCE loss for one sampled action:
-G_t * log π(a_t | s_t).
Instances For
Advantage actor-critic policy loss:
-A_t * log π(a_t | s_t).
Instances For
Value-regression loss used by actor-critic and PPO critics.
Instances For
Combined advantage actor-critic loss: policy term + value regression - entropy bonus.
Instances For
Advantage actor-critic loss with an explicit entropy bonus coefficient.
This is the A2C/A3C-shaped single-sample objective:
-A_t log π(a_t|s_t) + c_v value_loss - c_e H(π(.|s_t)).
Instances For
Importance ratio π_new(a|s) / π_old(a|s) computed from log-probabilities.
Instances For
Categorical KL divergence after clamping and normalizing both probability vectors.
For finite inputs and 0 < epsilon < 1/2, clamp each vector into [epsilon, 1-epsilon],
then divide by its sum before computing Σ_a old(a) * (log old(a) - log new(a)).
Normalization keeps the guarded inputs probability distributions. The result agrees with
KL(old || new) when clamping leaves normalized inputs unchanged, up to floating-point rounding.
Empty action vectors return zero.
Instances For
Categorical KL divergence from logits, using the clamped, normalized policies of
categoricalKL.
Instances For
TRPO-style surrogate objective from a precomputed importance ratio:
ratio * A.
TRPO maximizes this surrogate subject to a KL trust-region constraint. We expose the scalar surrogate separately from the constraint so callers can choose line search / penalty / diagnostics.
Instances For
KL-penalized policy-gradient loss:
-(ratio * A) + β * KL(old || new).
This is not the full constrained TRPO optimizer; it is the differentiable scalar objective commonly used as a practical surrogate or diagnostic when implementing trust-region updates.
Instances For
Finite-action SAC actor objective, minimized over actor logits:
∑ a, π(a|s) * (temperature * log π(a|s) - Q(s,a)).
Max-shifted exponentials supply normalized policy weights. When a negative logit and positive maximum could overflow their difference, weight each before subtracting. Otherwise keep the usual shifted expression. Temperature multiplies the weighted entropy term. These rearrangements retain differentiation through the weights and normalization without taking the log of a zero probability.
For an actor-only gradient, callers hold qValues and temperature constant with respect to actor
parameters. If two critics are used, pass their pointwise minimum as qValues. This function sums
over actions for one state without averaging across states.
Instances For
PPO clipped surrogate objective from a precomputed importance ratio:
min(ratio * A, clip(ratio, 1-ε, 1+ε) * A).
This helper is useful when you already have the ratio (e.g. from cached log-probabilities) and want to avoid recomputing it from logits.
Instances For
PPO clipped surrogate objective for one sampled action.
This is the objective to maximize:
min(r_t A_t, clip(r_t, 1-ε, 1+ε) A_t).
Instances For
PPO loss to minimize:
-L_clip + c_v * value_loss - c_e * entropy.
Instances For
Sample from a categorical distribution represented as a probability vector.
seed and counter form an explicit RNG stream identifier. The function returns the incremented
counter together with the sampled action index.
Implementation note: this uses the standard cumulative-sum / inverse-CDF sampler.
Instances For
Sample an action from logits by applying softmax then sampleCategorical.