TorchLean API

NN.Spec.RL.Core

Core Reinforcement-Learning Definitions #

This module collects the small mathematical definitions that sit underneath TorchLean's RL development.

These definitions are intentionally spec-level rather than runtime-level:

That keeps the actual RL mathematics in a proof-friendly namespace and avoids duplicating it inside runtime/trainer code.

Numerical Containers #

Trajectories use Tensor α [horizon], including when the horizon is chosen at runtime. Rewards, values, and termination markers share that index, so mismatched trajectories cannot be silently truncated. Scalar recurrences and their evaluation order are explicit below.

Primary references:

def Spec.RL.continueMask {α : Type} [Zero α] [One α] (done : Bool) :
α

Convert a terminal flag into a multiplicative continuation mask (1 for continue, 0 for stop).

Instances For
    def Spec.RL.discountedBackup {α : Type} [Zero α] [One α] [Add α] [Mul α] (reward gamma bootstrap : α) (done : Bool) :
    α

    Bellman-style one-step backup: $r+\gamma(1-\mathtt{done})\mathtt{bootstrap}$.

    Instances For
      def Spec.RL.tdTarget {α : Type} [Zero α] [One α] [Add α] [Mul α] (reward gamma nextValue : α) (done : Bool) :
      α

      One-step TD target for state-value or action-value updates.

      Instances For
        def Spec.RL.tdResidual {α : Type} [Zero α] [One α] [Add α] [Mul α] [Sub α] (value reward gamma nextValue : α) (done : Bool) :
        α

        TD residual / Bellman error: $r+\gamma(1-d)\mathtt{nextValue}-\mathtt{value}$.

        Instances For

          Shape-indexed trajectory calculations #

          def Spec.RL.discountedReturnsFrom {α : Type} [TorchLean.Storage α] [Zero α] [Add α] [Mul α] {n : } (gamma : α) (rewards : TorchLean.Tensor α [n]) (bootstrap : α := 0) :

          Discounted returns with a far-right bootstrap, evaluated from right to left.

          Instances For
            def Spec.RL.discountedReturns {α : Type} [TorchLean.Storage α] [Zero α] [Add α] [Mul α] {n : } (gamma : α) (rewards : TorchLean.Tensor α [n]) :

            Discounted returns for a terminal trajectory.

            Instances For
              def Spec.RL.discountedReturnsDone {α : Type} [TorchLean.Storage α] [Zero α] [One α] [Add α] [Mul α] {n : } (gamma : α) (rewards : TorchLean.Tensor α [n]) (dones : TorchLean.Tensor Bool [n]) (bootstrap : α := 0) :

              Discounted returns with one termination marker per reward; unequal lengths are unrepresentable.

              The multiplication order matches discountedBackup, including its floating-point behavior.

              Instances For
                def Spec.RL.generalizedAdvantageEstimation {α : Type} [TorchLean.Storage α] [Zero α] [One α] [Add α] [Mul α] [Sub α] {n : } (gamma lam : α) (rewards values nextValues : TorchLean.Tensor α [n]) (dones : TorchLean.Tensor Bool [n]) :

                Generalized Advantage Estimation, retaining the common horizon in all five tensor types.

                Instances For
                  def Spec.RL.returnsFromAdvantages {α : Type} [TorchLean.Storage α] [Add α] {n : } (advantages values : TorchLean.Tensor α [n]) :

                  Lambda-returns R_t = A_t + V_t, with equal lengths enforced by the tensor shape.

                  Instances For