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.

Let’s build a proper (yet simple) model for our regression task. It should look like

this:

Notebook Cell 1.9 - Building our "Manual" model, creating parameter by parameter!

1 class ManualLinearRegression(nn.Module):

2 def __init__(self):

3 super().__init__()

4 # To make "b" and "w" real parameters of the model,

5 # we need to wrap them with nn.Parameter

6 self.b = nn.Parameter(torch.randn(1,

7 requires_grad=True,

8 dtype=torch.float))

9 self.w = nn.Parameter(torch.randn(1,

10 requires_grad=True,

11 dtype=torch.float))

12

13 def forward(self, x):

14 # Computes the outputs / predictions

15 return self.b + self.w * x

Parameters

In the __init__() method, we define our two parameters, b and w, using the

Parameter class, to tell PyTorch that these tensors, which are attributes of the

ManualLinearRegression class, should be considered parameters of the model the

class represents.

Why should we care about that? By doing so, we can use our model’s parameters()

method to retrieve an iterator over the model’s parameters, including parameters

of nested models. Then we can use it to feed our optimizer (instead of building a list

of parameters ourselves!).

torch.manual_seed(42)

# Creates a "dummy" instance of our ManualLinearRegression model

dummy = ManualLinearRegression()

list(dummy.parameters())

Model | 105

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

Saved successfully!

Ooh no, something went wrong!