Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
After recovering our model’s state, we can finally use it to make predictions fornew inputs:Notebook Cell 2.10new_inputs = torch.tensor([[.20], [.34], [.57]])model.eval() # always use EVAL for fully trained models! 1model(new_inputs.to(device))1 Never forget to set the mode!Outputtensor([[1.4185],[1.6908],[2.1381]], device='cuda:0', grad_fn=<AddmmBackward>)Since Model Configuration V3 created a model and sent it automatically to ourdevice, we need to do the same with our new inputs.After loading a fully trained model for deployment / to makepredictions, make sure you ALWAYS set it to evaluation mode:model.eval()Congratulations, you "deployed" your first model :-)Setting the Model’s ModeI know, I am probably a bit obsessive about this, but here we go one more time:After loading the model, DO NOT FORGET to SET THE MODE:• checkpointing: model.train()• deploying / making predictions: model.eval()Saving and Loading Models | 169
Putting It All TogetherWe have updated each of the three fundamental parts of our code at least twice. Itis time to put it all together to get an overall view of what we have achieved so far.Behold your pipeline: Data Preparation V2, Model Configuration V3, and ModelTraining V5!Run - Data Preparation V21 # %load data_preparation/v2.py23 torch.manual_seed(13)45 # Builds tensors from numpy arrays BEFORE split6 x_tensor = torch.as_tensor(x).float()7 y_tensor = torch.as_tensor(y).float()89 # Builds dataset containing ALL data points10 dataset = TensorDataset(x_tensor, y_tensor)1112 # Performs the split13 ratio = .814 n_total = len(dataset)15 n_train = int(n_total * ratio)16 n_val = n_total - n_train17 train_data, val_data = random_split(dataset, [n_train, n_val])18 # Builds a loader of each set19 train_loader = DataLoader(20 dataset=train_data,21 batch_size=16,22 shuffle=True,23 )24 val_loader = DataLoader(dataset=val_data, batch_size=16)170 | Chapter 2: Rethinking the Training Loop
- 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 172 and 173: EvaluationHow can we evaluate the m
- 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 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
- Page 222 and 223: Model TrainingWe start by instantia
- Page 224 and 225: Making PredictionsLet’s make up s
- Page 226 and 227: OutputOrderedDict([('0.weight', ten
- Page 228 and 229: Run - Data Preparation V21 # %load
- Page 230 and 231: • defining our StepByStep class
- Page 232 and 233: import numpy as npimport torchimpor
- Page 234 and 235: Next, we’ll standardize the featu
- Page 236 and 237: Equation 3.1 - A linear regression
- Page 238 and 239: The odds ratio is given by the rati
- Page 240 and 241: As expected, probabilities that add
- Page 242 and 243: Sigmoid Functiondef sigmoid(z):retu
After recovering our model’s state, we can finally use it to make predictions for
new inputs:
Notebook Cell 2.10
new_inputs = torch.tensor([[.20], [.34], [.57]])
model.eval() # always use EVAL for fully trained models! 1
model(new_inputs.to(device))
1 Never forget to set the mode!
Output
tensor([[1.4185],
[1.6908],
[2.1381]], device='cuda:0', grad_fn=<AddmmBackward>)
Since Model Configuration V3 created a model and sent it automatically to our
device, we need to do the same with our new inputs.
After loading a fully trained model for deployment / to make
predictions, make sure you ALWAYS set it to evaluation mode:
model.eval()
Congratulations, you "deployed" your first model :-)
Setting the Model’s Mode
I know, I am probably a bit obsessive about this, but here we go one more time:
After loading the model, DO NOT FORGET to SET THE MODE:
• checkpointing: model.train()
• deploying / making predictions: model.eval()
Saving and Loading Models | 169