MLP (spec wiring example) #
This file defines a 2-layer MLP by composing SpecChains from module specs:
Linear → ReLU → Linear (optionally followed by a softmax head).
The file is organized around module wiring rather than re-implementing matrix multiplications
directly. Linear and ReLU come from the spec layer and are composed through NNModuleSpec /
SpecChain, matching the usual PyTorch workflow: 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
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.