02.04 · UNIT 01 · Regression and tensor mechanics · Lesson
Python functions to reusable neural layers
A PyTorch module keeps a calculation and its learned tensors together.
PLAIN-LANGUAGE INTRODUCTION
What is this?
A PyTorch module keeps a calculation and its learned tensors together.
One simple example
A 4→2 linear layer has 8 weights and 2 biases. It owns 10 parameters.
What goes in?
A batch whose last dimension has length 4.
What comes out?
A batch whose last dimension has length 2.
Why does it matter?
Registration lets PyTorch find every parameter for training and saving.
What is it not?
Creating a fresh layer inside every call does not preserve learned values.
WORK THROUGH THE IDEA
See the idea in more detail
- A module is a reusable object. Its
forwardmethod contains the calculation. - For four inputs and two outputs, the weight matrix has shape
(2,4). That gives8weights. - Each output has one bias. The layer therefore has
8+2=10parameters. - For the shown illustrative weights, input
[1,0,2,1]returns[3,1]. - Common mistake: parameters inside a plain unregistered list may be missing from the optimizer.