TorchLean API

NN.Runtime.RL.PolicyGradient.Autograd

Autograd Policy-Gradient Objectives #

This module provides differentiable policy-gradient / actor-critic helpers expressed in terms of TorchLean's backend-generic Ops interface, so they can run under:

This file lives with the RL runtime, not the TorchLean runtime internals, because these are RL objectives that happen to be differentiable through TorchLean. It is the autograd companion to the pure helpers in NN.Runtime.RL.Algorithms.PolicyGradient:

Action Encoding #

We assume categorical (finite-action) policies parameterized by logits, and we represent the chosen action as a one-hot tensor with the same shape as the logits. The selected log-probability remains differentiable with respect to the logits without introducing a separate integer index type into the Ops surface.

Primary References #

Log-probabilities and entropy (batched, one-hot actions) #

Per-sample log-probability for one-hot actions under a batched categorical policy.

Input shapes:

  • logits : (N × A)
  • actionOneHot : (N × A)

Output shape:

  • logProb : (N) where logProb[i] = log π(a_i | s_i).

Implementation note: this uses logSoftmax and a reduce-sum over the action axis.

Instances For

    Mean entropy of a batched categorical policy.

    Input shape:

    • logits : (N × A)

    Output shape:

    • scalar entropy mean: mean_i[ -Σ_a p_i(a) log p_i(a) ].
    Instances For

      PPO (batched) #

      def Runtime.RL.PolicyGradient.Autograd.ppoClippedObjectiveBatch {α : Type} [TorchLean.Storage α] [Context α] {m : TypeType} [Monad m] [Autograd.Torch.Ops m α] {batch nActions : } [NeZero batch] [NeZero nActions] (newLogits actionOneHot : Autograd.Model.RefTy m α (Spec.Shape.dim batch (Spec.Shape.dim nActions Spec.Shape.scalar))) (oldLogProb advantage : Autograd.Model.RefTy m α (Spec.Shape.dim batch Spec.Shape.scalar)) (clipEps : α := 1 / 5) :

      PPO clipped surrogate objective (the thing to maximize), computed per sample:

      L_clip_i = min(r_i * A_i, clip(r_i, 1-ε, 1+ε) * A_i)

      where r_i = exp(logπ_new(a_i|s_i) - logπ_old(a_i|s_i)).

      Instances For
        def Runtime.RL.PolicyGradient.Autograd.ppoLossBatch {α : Type} [TorchLean.Storage α] [Context α] {m : TypeType} [Monad m] [Autograd.Torch.Ops m α] {batch nActions : } [NeZero batch] [NeZero nActions] (newLogits actionOneHot : Autograd.Model.RefTy m α (Spec.Shape.dim batch (Spec.Shape.dim nActions Spec.Shape.scalar))) (oldLogProb advantage : Autograd.Model.RefTy m α (Spec.Shape.dim batch Spec.Shape.scalar)) (valuePred valueTarget : Autograd.Model.RefTy m α (Spec.Shape.dim batch (Spec.Shape.dim 1 Spec.Shape.scalar))) (clipEps : α := 1 / 5) (valueCoef : α := 1 / 2) (entropyCoef : α := 1 / 100) :

        PPO scalar loss to minimize (mean over batch):

        loss = -mean(L_clip) + c_v * MSE(v, v_target) - c_e * mean(entropy)

        This is the standard discrete-action PPO loss used in many reference implementations.

        Instances For

          PPO module wrapper (two-model actor/critic) #

          Bundle an actor and critic into an ObjectiveDef whose inputs are a PPO minibatch:

          • states : (N × stateDim)
          • actionsOneHot : (N × A)
          • oldLogProb : (N)
          • advantages : (N)
          • valueTarget : (N × 1)

          The model state is actor.state ++ critic.state, and one optimizer step updates both models.

          Instances For