TorchLean API

NN.Spec.Module.Linear

Linear module wrappers #

NN/Spec/Layers/Linear.lean defines the math-level linear layer spec (parameters + gradients).

This file provides NNModuleSpec wrappers so linear layers can be:

These wrappers are thin on purpose: the meaning is still the underlying linear_spec; the extra fields are just metadata.

Most readers can map these directly to PyTorch: LinearModuleSpec is nn.Linear, and the sequence helpers are the common pattern "apply a linear layer to each timestep, then maybe select the last step for classification".

def Spec.LinearModuleSpec {α : Type} [Add α] [Mul α] [Zero α] {inDim outDim : } (m : LinearSpec α inDim outDim) :

Wrap LinearSpec forward as an NNModuleSpec.

Instances For
    def Spec.LinearSeqModuleSpec {α : Type} [Context α] {seqLen hiddenSize outputSize : } (m : LinearSpec α hiddenSize outputSize) :

    Apply a linear layer independently at each timestep (sequence lift).

    Instances For
      def Spec.getLastTimestepSpec {α : Type} [Context α] {seqLen hiddenSize : } (seq : Tensor α (Shape.dim seqLen (Shape.dim hiddenSize Shape.scalar))) (h : seqLen 0) :
      Tensor α (Shape.dim hiddenSize Shape.scalar)

      Extract the last timestep of a sequence (requires seqLen ≠ 0).

      In PyTorch terms: seq[-1] when seq has shape (seqLen, hiddenSize).

      Instances For
        def Spec.LinearClassifierModuleSpec {α : Type} [Context α] {seqLen hiddenSize numClasses : } (m : LinearSpec α hiddenSize numClasses) (h : seqLen 0) :

        Sequence classifier: take last timestep, then apply a linear projection to numClasses.

        Instances For
          def Spec.twoLayerLinear {α : Type} [Add α] [Mul α] [Zero α] {inDim hidDim outDim : } (l1 : LinearSpec α inDim hidDim) (l2 : LinearSpec α hidDim outDim) :

          Example: Linear → Linear composition via SpecChain.

          Instances For