Linear layer (spec layer) #
This file defines a fully‑connected layer and its gradients:
- forward:
y = W x + b - backward: ∂L/∂W, ∂L/∂b, ∂L/∂x
Definitions are purely functional and shape‑indexed, suitable for both proofs and reuse by
autograd wrappers in NN/Spec/Autograd.
Linear layer specification (pure, shape-indexed).
This is the spec-level analogue of PyTorch torch.nn.Linear / torch.nn.functional.linear:
- weights : TorchLean.Tensor α [outDim, inDim]
Weight matrix with rows indexed by output features.
- bias : TorchLean.Tensor α [outDim]
Bias vector added to each output feature.
Instances For
Unbatched forward pass: y = W x + b.
PyTorch analogue: torch.nn.functional.linear.
Instances For
Gradient w.r.t. weights: ∂L/∂W = (∂L/∂y) ⊗ x (outer product).
This is the standard linear-layer backward formula for y = W x + b.
Instances For
Gradient w.r.t. bias: ∂L/∂b = ∂L/∂y.
Since y = W x + b, the Jacobian of y w.r.t. b is the identity.
Instances For
Gradient w.r.t. input: ∂L/∂x = Wᵀ (∂L/∂y).
This is the standard "matmul by the transpose" rule for y = W x + b.
Instances For
Gradients for the parameters of a linear layer y = W x + b.
LinearSpec stores weights and bias, so this bundle names its fields the same way with a
Gradient suffix. The LSTM output head in NN/Spec/Module/LstmModels.lean and the seq2seq decoder
projection in NN/Spec/Models/Seq2seq.lean each used to declare a private copy of exactly this
record, which meant a gradient produced by one model could not be read by code written against the
other. One shared record fixes that.
PyTorch analogue: (layer.weight.grad, layer.bias.grad) for torch.nn.Linear.
- weightGradient : TorchLean.Tensor α [outDim, inDim]
Gradient with respect to the weight matrix
W. - biasGradient : TorchLean.Tensor α [outDim]
Gradient with respect to the bias vector
b.
Instances For
Instances For
Everything a linear backward pass produces: the two parameter gradients, plus the gradient that keeps travelling backwards into the layer's input.
inputShape is a parameter because the same rule serves the unbatched case ([inDim]) and the
batched one (leading.appendDim inDim). The three fields are spelled out rather than inherited from
LinearParameterGradients: a structure that extends another prints its parent as a nested
toLinearParameterGradients := ..., and these records get #eval'd in the guide, where a flat line
is what a reader wants to compare against the formula. LinearGradients.parameters recovers the
parameter pair for model-level gradient records.
Convolution returns its gradients the same way, as Spec.ConvGradients.
- weightGradient : TorchLean.Tensor α [outDim, inDim]
Gradient with respect to the weight matrix
W. - biasGradient : TorchLean.Tensor α [outDim]
Gradient with respect to the bias vector
b. - inputGradient : TorchLean.Tensor α inputShape
Gradient with respect to the layer input.
Instances For
Instances For
Forget the input gradient and keep the two parameter gradients, which is what a model-level gradient record stores for a layer whose input gradient has already been consumed.
Instances For
Linear derivatives over any nonempty leading shape.
The leading axes are flattened only while accumulating the parameter gradients:
d_weights = (gradOutputᵀ) · input,d_bias = sum(gradOutput)over every leading coordinate,d_input = gradOutput · weights.
Instances For
Complete unbatched backward pass for a linear layer.
Returns ∂L/∂W, ∂L/∂b and ∂L/∂x given the layer params, input x, and output gradient ∂L/∂y.
Instances For
Backward pass for a linear layer applied at every position of a sequence.
The layer is shared across positions, so its parameter gradients accumulate over the sequence while
each position gets its own input gradient. This is the rule behind both the LSTM output head
(Spec.Lstm.Model.backward) and the seq2seq decoder projection
(Spec.Seq2SeqDecoderSpec.backwardTeacherForcing); those two files each carried a byte-identical
copy of it under different binder names until the record above gave them a common vocabulary.
PyTorch analogue: backprop through nn.Linear applied inside a loop over timesteps, where
weight.grad sums the per-step contributions.
Instances For
Accumulate two weight gradients by addition.
This is a small helper used by batching/training code.
Instances For
Scale a weight gradient by a scalar factor (e.g. learning-rate adjustment).