TorchLean API

NN.Spec.Models.Autoencoder

Autoencoder (spec model) #

This file defines a small fully-connected autoencoder:

PyTorch analogue: nn.Sequential(nn.Linear(inputDim, hiddenDim), act, nn.Linear(hiddenDim, inputDim)) applied to a single vector (no batch dimension).

This is spec-level/reference code. It is written for auditability and differentiation, and it is intended to be instantiated over multiple scalar backends (Float, intervals, proof-level reals, ...).

The activation is represented by Activation.Kind, so a misspelled configuration cannot silently change the model into an identity activation.

Implementation status #

nn.models.Generative.autoencoder (NN/API/Models/Generative.lean) builds a different architecture, x -> hidden -> latent -> hidden -> reconstruction, and is not derived from this record. NN/Spec/Module/Autoencoder.lean wraps this file as a Spec.Module. No theorem relates either to the other.

Parameters #

We store the encoder and decoder weights explicitly.

Shapes:

structure Spec.AutoencoderSpec (α : Type) [TorchLean.Storage α] (inputDim hiddenDim : ℕ) :

Parameters for a 1-hidden-layer fully-connected autoencoder.

  • encoderWeight : TorchLean.Tensor α [hiddenDim, inputDim]

    Encoder weights with shape (hiddenDim × inputDim).

  • encoderBias : TorchLean.Tensor α [hiddenDim]

    Encoder bias with shape (hiddenDim).

  • decoderWeight : TorchLean.Tensor α [inputDim, hiddenDim]

    Decoder weights with shape (inputDim × hiddenDim).

  • decoderBias : TorchLean.Tensor α [inputDim]

    Decoder bias with shape (inputDim).

  • activation : Activation.Kind

    Pointwise activation between the encoder and decoder.

Instances For

    Forward #

    def Spec.autoencoderEncodeSpec {α : Type} [TorchLean.Storage α] [Context α] {inputDim hiddenDim : ℕ} (m : AutoencoderSpec α inputDim hiddenDim) (input : TorchLean.Tensor α [inputDim]) :
    TorchLean.Tensor α [hiddenDim]

    Encode a vector into a hidden representation:

    h = act(W_enc x + b_enc).

    PyTorch analogy: act(linear(x)) for a single nn.linear.

    Instances For
      def Spec.autoencoderDecodeSpec {α : Type} [TorchLean.Storage α] [Context α] {inputDim hiddenDim : ℕ} (m : AutoencoderSpec α inputDim hiddenDim) (hidden : TorchLean.Tensor α [hiddenDim]) :
      TorchLean.Tensor α [inputDim]

      Decode a hidden representation back to input space:

      x̂ = W_dec h + b_dec.

      PyTorch analogy: a second nn.Linear(hiddenDim, inputDim) without an activation.

      Instances For
        def Spec.autoencoderForwardSpec {α : Type} [TorchLean.Storage α] [Context α] {inputDim hiddenDim : ℕ} (m : AutoencoderSpec α inputDim hiddenDim) (input : TorchLean.Tensor α [inputDim]) :
        TorchLean.Tensor α [inputDim]

        Full autoencoder forward pass: decode(encode(x)).

        Instances For
          def Spec.autoencoderForwardLeadingSpec {α : Type} [TorchLean.Storage α] [Context α] (leading : Shape) {inputDim hiddenDim : ℕ} (m : AutoencoderSpec α inputDim hiddenDim) (input : TorchLean.Tensor α (leading.concat [inputDim])) :
          TorchLean.Tensor α (leading.concat [inputDim])

          Apply an autoencoder independently at every index of a leading shape.

          Instances For

            Backward (manual VJP) #

            This file includes a small, explicit backward pass for the autoencoder.

            PyTorch analogy: this is what autograd computes, but spelled out as pure functions. The key linear-algebra identities used are:

            structure Spec.AutoencoderGradients (α : Type) [TorchLean.Storage α] (inputDim hiddenDim : ℕ) :

            Autoencoder gradients: one linear-parameter bundle per half, plus the input gradient.

            Instances For
              def Spec.autoencoderBackwardSpec {α : Type} [TorchLean.Storage α] [Context α] {inputDim hiddenDim : ℕ} (m : AutoencoderSpec α inputDim hiddenDim) (input gradOutput : TorchLean.Tensor α [inputDim]) :
              AutoencoderGradients α inputDim hiddenDim

              Complete backward pass, sharing the hidden gradient across encoder and input derivatives.

              Instances For
                def Spec.autoencoderReconstructionErrorSpec {α : Type} [TorchLean.Storage α] [Context α] {inputDim hiddenDim : ℕ} (m : AutoencoderSpec α inputDim hiddenDim) (input : TorchLean.Tensor α [inputDim]) (h : inputDim ≠ 0) :
                α

                Mean-squared reconstruction error (single example).

                PyTorch analogy: F.mse_loss(x_hat, x, reduction="mean").

                Instances For