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}$.

    Termination masks by multiplication, without short-circuiting the bootstrap. In floating-point arithmetic, a terminal transition can therefore still produce NaN from a NaN or infinite bootstrap.

    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.discountedReturns {α : Type} [TorchLean.Storage α] [Zero α] [Add α] [Mul α] {n : ℕ} (gamma : α) (rewards : TorchLean.Tensor α [n]) (bootstrap : α := 0) :

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

        The default bootstrap is 0, representing 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. Terminal branches do not multiply unused bootstrap values or future advantages by zero.

            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