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.

Run - Model Configuration V4

1 # %load 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 Training

1 n_epochs = 200

2

3 sbs = StepByStep(model, loss_fn, optimizer)

4 sbs.set_loaders(train_loader, val_loader)

5 sbs.set_tensorboard('classy')

6 sbs.train(n_epochs=n_epochs)

print(model.state_dict())

Output

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

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

Recap

In this chapter, we’ve revisited and reimplemented many methods. This is what

we’ve covered:

204 | Chapter 2.1: Going Classy

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

Saved successfully!

Ooh no, something went wrong!