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.

In the __init__() method, we create an attribute that contains our nested Linear

model.

In the forward() method, we call the nested model itself to perform the forward

pass (notice, we are not calling self.linear.forward(x)!).

Now, if we call the parameters() method of this model, PyTorch will figure out the

parameters of its attributes recursively.

torch.manual_seed(42)

dummy = MyLinearRegression().to(device)

list(dummy.parameters())

Output

[Parameter containing:

tensor([[0.7645]], device='cuda:0', requires_grad=True),

Parameter containing:

tensor([0.8300], device='cuda:0', requires_grad=True)]

You can also add extra Linear attributes, and, even if you don’t use them at all in

the forward pass, they will still be listed under parameters().

If you prefer, you can also use state_dict() to get the parameter values, together

with their names:

dummy.state_dict()

Output

OrderedDict([('linear.weight',

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

('linear.bias',

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

Notice that both bias and weight have a prefix with the attribute name: linear, from

the self.linear in the __init__() method.

Model | 111

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

Saved successfully!

Ooh no, something went wrong!