TorchLean API

NN.Runtime.RL.Eval

RL Evaluation Helpers (Executable Runtime) #

This module factors out small non-differentiable evaluation helpers used in executable RL workflows:

These helpers are intentionally written against the unified checked-session interface Runtime.RL.Session.CheckedSession, so the same evaluation code can be reused with:

Semantics and references #

Greedy action selection #

def Runtime.RL.Eval.greedy {α : Type} [TorchLean.Storage α] [LT α] [DecidableRel fun (x1 x2 : α) => x1 > x2] {nActions : ℕ} [NeZero nActions] (logits : TorchLean.Tensor α [nActions]) :
Fin nActions

Pick a greedy discrete action from a logits vector by argmax.

If nActions = 0 then Fin nActions is uninhabited, so we require [NeZero nActions]. When nActions > 0, Metrics.argmax? never returns none; the none branch returns 0 only as an unreachable totality fallback.

Instances For

    Episode evaluation #

    def Runtime.RL.Eval.totalReward {obsShape : Spec.Shape} {nActions : ℕ} [NeZero nActions] (sess : Session.CheckedSession obsShape nActions) (policyLogits : TorchLean.Tensor Float obsShape → TorchLean.Tensor Float [nActions]) (maxSteps : ℕ := 1000) :

    Run one episode (up to maxSteps) using a greedy policy derived from policyLogits, and return the total (undiscounted) reward.

    The episode terminates early when the checked transition reports terminated || truncated.

    Instances For
      def Runtime.RL.Eval.path {obsShape : Spec.Shape} {nActions : ℕ} [NeZero nActions] (sess : Session.CheckedSession obsShape nActions) (policyLogits : TorchLean.Tensor Float obsShape → TorchLean.Tensor Float [nActions]) (maxSteps : ℕ := 1000) :
      IO (Array sess.State)

      Run one greedy-policy episode (up to maxSteps) and record the visited session states.

      The returned path includes the initial session state from sess.start, and then each state after every checked step.

      Note: for Session.CheckedSession.ofEnv, State is the Lean-native environment's latent state. For Gymnasium-backed sessions, State is the session record (it stores the last observation, etc.), not the underlying Python environment's internal state.

      Instances For
        def Runtime.RL.Eval.meanReward {obsShape : Spec.Shape} {nActions : ℕ} [NeZero nActions] (mkSession : ℕ → Session.CheckedSession obsShape nActions) (policyLogits : TorchLean.Tensor Float obsShape → TorchLean.Tensor Float [nActions]) (baseSeed episodes : ℕ) (maxSteps : ℕ := 1000) :

        Average totalReward across multiple episodes, seeding each episode by baseSeed + k.

        The mkSession callback is responsible for interpreting the seed:

        • for Gymnasium-backed sessions it typically passes the seed to reset, and
        • for Lean-native environments it can ignore the seed.

        This is the mean of episode totals, not the mean reward per step. When episodes = 0, the function returns 0 without creating or starting a session.

        Instances For