MLP (spec wiring example) #
This file defines a small 2-layer MLP by composing SpecChains from module specs:
Linear → ReLU → Linear (optionally followed by a softmax head).
This is intentionally written in a "wiring-first" style: instead of re-implementing matrix
multiplications directly, we reuse the spec-layer definitions for Linear and ReLU and compose
them through NNModuleSpec / SpecChain. This matches how most PyTorch users think about MLPs:
define a few modules, then run a forward pass.
A 2-layer MLP as a SpecChain:
Linear(inDim → hidDim) then ReLU then Linear(hidDim → outDim).
PyTorch analogy: nn.Sequential(nn.Linear(inDim, hidDim), nn.ReLU(), nn.Linear(hidDim, outDim)).
Instances For
MLP with a softmax head (Linear → ReLU → Linear → Softmax).
PyTorch analogy: nn.Sequential(..., nn.Softmax(dim=-1)).
Note: this is a shape-safe softmax spec (applied along the last dimension). In PyTorch you
choose dim at runtime; here the shape index already tells us what "the last dim" is.
Instances For
Run the MLP forward on a single input vector.
Instances For
Backward pass for the 2-layer MLP. Returns (∂L/∂W1, ∂L/∂b1, ∂L/∂W2, ∂L/∂b2, ∂L/∂x).
Instances For
Sanity lemma: the composed SpecChain forward equals the hand-written Linear → ReLU → Linear
computation.
OpSpec for the same 2-layer MLP.
This packaging is convenient for symbolic gradient checks: OpSpec pairs a forward definition with
an explicit reverse-mode definition, and it composes cleanly.
Instances For
Composed backward of the MLP using the OpSpec chain.