22.02.2024 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

And then, we update our model configuration code to include the creation of the

corresponding function for the validation step.

Define - Model Configuration V2

1 %%writefile model_configuration/v2.py

2

3 device = 'cuda' if torch.cuda.is_available() else 'cpu'

4

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

6 lr = 0.1

7

8 torch.manual_seed(42)

9 # Now we can create a model and send it at once to the device

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

11

12 # Defines an SGD optimizer to update the parameters

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

14

15 # Defines an MSE loss function

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

17

18 # Creates the train_step function for our model, loss function

19 # and optimizer

20 train_step_fn = make_train_step_fn(model, loss_fn, optimizer)

21

22 # Creates the val_step function for our model and loss function

23 val_step_fn = make_val_step_fn(model, loss_fn) 1

1 Creating a function that performs a validation step

Run - Model Configuration V2

%run -i model_configuration/v2.py

Finally, we need to change the training loop to include the evaluation of our model.

The first step is to include another inner loop to handle the mini-batches that come

from the validation loader, sending them to the same device as our model. Then,

inside that inner loop, we use the validation step function to compute the loss.

"Wait, this looks oddly familiar too…" Evaluation | 149

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

Saved successfully!

Ooh no, something went wrong!