Autoencoder (spec model) #
This file defines a small fully-connected autoencoder:
- encoder:
h = act(W_enc x + b_enc) - decoder:
x̂ = W_dec h + b_dec
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:
encoderWeight : (hiddenDim × inputDim)decoderWeight : (inputDim × hiddenDim)encoderBias : (hiddenDim)decoderBias : (inputDim)
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 #
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
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
Full autoencoder forward pass: decode(encode(x)).
Instances For
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:
- If
y = W x + b, thendW = dY ⊗ x,db = dY, anddX = Wᵀ dY. - If
h = act(z), thendZ = dH ⊙ act'(z).
Gradient w.r.t. encoder weights: dW_enc = dZ ⊗ x.
Instances For
Gradient w.r.t. encoder bias: db_enc = dZ.
Instances For
Gradient w.r.t. decoder weights: dW_dec = dOut ⊗ h.
Instances For
Gradient w.r.t. decoder bias: db_dec = dOut.
Instances For
Gradient w.r.t. input: dX = W_encᵀ dZ.
Instances For
Gradients for a linear autoencoder: one bundle per half, plus the input gradient.
- encoder : LinearParameterGradients α inputDim hiddenDim
Gradients for the encoder
inputDim -> hiddenDim. - decoder : LinearParameterGradients α hiddenDim inputDim
Gradients for the decoder
hiddenDim -> inputDim. - inputGradient : TorchLean.Tensor α [inputDim]
Gradient with respect to the reconstructed input.
Instances For
Complete backward pass for an autoencoder.
Instances For
Mean-squared reconstruction error (single example).
PyTorch analogy: F.mse_loss(x_hat, x, reduction="mean").