Learn · 3
Modules
nn.Module holds parameters and knows how to run forward. The names
are torch's; the one difference is that you call it with
model.call(x), because JavaScript cannot call an object.
A layer on its own
nn.Linear(in, out) stores weight as
(out, in) — the same layout as torch, so a checkpoint moves across
without transposing.
Stacking
nn.Sequential chains modules. parameters() collects
every tensor the optimizer should touch — if a parameter is missing from that
list, training runs and that layer simply never learns, with no error.
Writing your own
Extend nn.Module and implement forward. Parameters you
create yourself must be claimed, otherwise they do not appear in
parameters() — the failure is silent, so the base class makes you say it.
Losses
Losses live in the same namespace and are called the same way.
CrossEntropyLoss takes logits and int64 targets, exactly
as torch does — not one-hot vectors.
A layer is a matrix of weights
A Linear layer is a weight matrix (plus a bias) that it will learn. Here is a fresh 16×16 one at initialisation, drawn — random and structureless now; training is what gives it structure.
What to remember
- A module bundles parameters with a
forward();nn.Linearandnn.Sequentialare the basics. parameters()hands the learnables to an optimiser.- Write your own layer by subclassing and defining
forward(). - A loss (MSE, cross-entropy) turns outputs and targets into the one number to minimise.