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

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

EvaluationHow can we evaluate the model? We can compute the validation loss; that is, howwrong the model’s predictions are for unseen data.First, we need to use the model to compute predictions and then use the lossfunction to compute the loss, given our predictions and the true labels. Soundsfamiliar? These are pretty much the first two steps of the training step function webuilt as Helper Function #1.So, we can use that code as a starting point, getting rid of steps 3 and 4, and, mostimportant, we need to use the model’s eval() method. The only thing it does is setthe model to evaluation mode (just like its train() counterpart did), so the modelcan adjust its behavior accordingly when it has to perform some operations, likeDropout."Why is setting the mode so important?"As mentioned above, dropout (a regularization technique commonly used forreducing overfitting) is the main reason for it, since it requires the model to behavedifferently during training and evaluation. In a nutshell, dropout randomly setssome weights to zero during training.We’ll get back to dropout in the second volume of the series.What would happen if this behavior persisted outside of training time? You wouldend up with possibly different predictions for the same input since differentweights would be set to zero every time you made a prediction. It would ruinevaluation and, if deployed, would also ruin the confidence of the user.We don’t want that, so we use model.eval() to prevent it!Evaluation | 147

Just like make_train_step_fn(), our new function, make_val_step_fn(), is ahigher-order function. Its code looks like this:Helper Function #31 def make_val_step_fn(model, loss_fn):2 # Builds function that performs a step3 # in the validation loop4 def perform_val_step_fn(x, y):5 # Sets model to EVAL mode6 model.eval() 178 # Step 1 - Computes our model's predicted output9 # forward pass10 yhat = model(x)11 # Step 2 - Computes the loss12 loss = loss_fn(yhat, y)13 # There is no need to compute Steps 3 and 4,14 # since we don't update parameters during evaluation15 return loss.item()1617 return perform_val_step_fn1 Setting model to evaluation mode148 | Chapter 2: Rethinking the Training Loop

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!