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.

Just like make_train_step_fn(), our new function, make_val_step_fn(), is a

higher-order function. Its code looks like this:

Helper Function #3

1 def make_val_step_fn(model, loss_fn):

2 # Builds function that performs a step

3 # in the validation loop

4 def perform_val_step_fn(x, y):

5 # Sets model to EVAL mode

6 model.eval() 1

7

8 # Step 1 - Computes our model's predicted output

9 # forward pass

10 yhat = model(x)

11 # Step 2 - Computes the loss

12 loss = loss_fn(yhat, y)

13 # There is no need to compute Steps 3 and 4,

14 # since we don't update parameters during evaluation

15 return loss.item()

16

17 return perform_val_step_fn

1 Setting model to evaluation mode

148 | Chapter 2: Rethinking the Training Loop

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

Saved successfully!

Ooh no, something went wrong!