Tensor by Tensor

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

  1. A module is a reusable object. Its forward method contains the calculation.
  2. For four inputs and two outputs, the weight matrix has shape (2,4). That gives 8 weights.
  3. Each output has one bias. The layer therefore has 8+2=10 parameters.
  4. For the shown illustrative weights, input [1,0,2,1] returns [3,1].
  5. Common mistake: parameters inside a plain unregistered list may be missing from the optimizer.
Open the detailed notes ↗