22.02.2024 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sequential Models

Our model was simple enough. You may be thinking: "Why even bother to build a

class for it?!" Well, you have a point…

For straightforward models that use a series of built-in PyTorch models (like

Linear), where the output of one is sequentially fed as an input to the next, we can

use a, er … Sequential model :-)

In our case, we would build a sequential model with a single argument; that is, the

Linear model we used to train our linear regression. The model would look like

this:

Notebook Cell 1.12 - Building a model using PyTorch’s Sequential model

1 torch.manual_seed(42)

2 # Alternatively, you can use a Sequential model

3 model = nn.Sequential(nn.Linear(1, 1)).to(device)

4

5 model.state_dict()

Output

OrderedDict([('0.weight', tensor([[0.7645]], device='cuda:0')),

('0.bias', tensor([0.8300], device='cuda:0'))])

Simple enough, right?

We’ve been talking about models inside other models. This may get confusing real

quick, so let’s follow convention and call any internal model a layer.

112 | Chapter 1: A Simple Regression Problem

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

Saved successfully!

Ooh no, something went wrong!