22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The most fundamental methods a model class needs to implement are:

• __init__(self): It defines the parts that make up the model—in our case, two

parameters, b and w.

You are not limited to defining parameters, though. Models can

contain other models as their attributes as well, so you can easily

nest them. We’ll see an example of this shortly as well.

Besides, do not forget to include super().__init__() to execute

the __init__() method of the parent class (nn.Module) before

your own.

• forward(self, x): It performs the actual computation; that is, it outputs a

prediction, given the input x.

It may seem weird but, whenever using your model to make

predictions, you should NOT call the forward(x) method!

You should call the whole model instead, as in model(x), to

perform a forward pass and output predictions.

The reason is, the call to the whole model involves extra steps,

namely, handling forward and backward hooks. If you don’t use

hooks (and we don’t use any right now), both calls are equivalent.

Hooks are a very useful mechanism that allows retrieving

intermediate values in deeper models. We’ll get to them

eventually.

104 | Chapter 1: A Simple Regression Problem

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!