Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
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
- Page 122 and 123: There are many optimizers: SGD is t
- Page 124 and 125: 41 optimizer.zero_grad() 34243 prin
- Page 126 and 127: Notebook Cell 1.8 - PyTorch’s los
- Page 128 and 129: Outputarray(0.00804466, dtype=float
- Page 130 and 131: Let’s build a proper (yet simple)
- Page 132 and 133: "What do we need this for?"It turns
- Page 134 and 135: 1 Instantiating a model2 What IS th
- Page 136 and 137: In the __init__() method, we create
- Page 138 and 139: LayersA Linear model can be seen as
- Page 140 and 141: There are MANY different layers tha
- Page 142 and 143: We use magic, just like that:%run -
- Page 144 and 145: • Step 1: compute model’s predi
- Page 146 and 147: RecapFirst of all, congratulations
- Page 148 and 149: Chapter 2Rethinking the Training Lo
- Page 150 and 151: Let’s take a look at the code onc
- Page 152 and 153: Higher-Order FunctionsAlthough this
- Page 154 and 155: def exponentiation_builder(exponent
- Page 156 and 157: Apart from returning the loss value
- Page 158 and 159: Our code should look like this; see
- Page 160 and 161: There is no need to load the whole
- Page 162 and 163: but if we want to get serious about
- Page 164 and 165: How does this change our code so fa
- Page 166 and 167: Run - Model Training V2%run -i mode
- Page 168 and 169: piece of code that’s going to be
- Page 170 and 171: for it. We could do the same for th
- Page 174 and 175: And then, we update our model confi
- Page 176 and 177: Run - Model Training V4%run -i mode
- Page 178 and 179: Loading Extension# Load the TensorB
- Page 180 and 181: browser, you’ll likely see someth
- Page 182 and 183: model’s graph (not quite the same
- Page 184 and 185: Figure 2.5 - Scalars on TensorBoard
- Page 186 and 187: Define - Model Training V51 %%write
- Page 188 and 189: If, by any chance, you ended up wit
- Page 190 and 191: The procedure is exactly the same,
- Page 192 and 193: soon, so please bear with me for no
- Page 194 and 195: After recovering our model’s stat
- Page 196 and 197: Run - Model Configuration V31 # %lo
- Page 198 and 199: This is the general structure you
- Page 200 and 201: Chapter 2.1Going ClassySpoilersIn t
- Page 202 and 203: # A completely empty (and useless)
- Page 204 and 205: # These attributes are defined here
- Page 206 and 207: # Creates the train_step function f
- Page 208 and 209: # Builds function that performs a s
- Page 210 and 211: setattrThe setattr function sets th
- Page 212 and 213: See? We effectively modified the un
- Page 214 and 215: the random seed as arguments.This s
- Page 216 and 217: The current state of development of
- Page 218 and 219: Lossesdef plot_losses(self):fig = p
- Page 220 and 221: Run - Data Preparation V21 # %load
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