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.

Define - Model Configuration V4

1 %%writefile model_configuration/v4.py

2

3 # Sets learning rate - this is "eta" ~ the "n"-like Greek letter

4 lr = 0.1

5

6 torch.manual_seed(42)

7 # Now we can create a model

8 model = nn.Sequential(nn.Linear(1, 1))

9

10 # Defines an SGD optimizer to update the parameters

11 # (now retrieved directly from the model)

12 optimizer = optim.SGD(model.parameters(), lr=lr)

13

14 # Defines an MSE loss function

15 loss_fn = nn.MSELoss(reduction='mean')

Run - Model Configuration V4

%run -i model_configuration/v4.py

Let’s inspect the randomly initialized parameters of our model:

print(model.state_dict())

Output

OrderedDict([('0.weight', tensor([[0.7645]])),

('0.bias', tensor([0.8300]))])

These are CPU tensors, since our model wasn’t sent anywhere (yet).

And now the fun begins: Let’s put our StepByStep class to good use and train our

model.

196 | Chapter 2.1: Going Classy

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

Saved successfully!

Ooh no, something went wrong!