Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
Run - Model Training V4%run -i model_training/v4.pyAfter updating all parts, in sequence, our current state ofdevelopment is:• Data Preparation V2• Model Configuration V2• Model Training V4Let’s inspect the model’s state:# Checks model's parametersprint(model.state_dict())OutputOrderedDict([('0.weight', tensor([[1.9419]], device='cuda:0')),('0.bias', tensor([1.0244], device='cuda:0'))])Plotting LossesLet’s take a look at both losses—training and validation.Figure 2.1 - Training and validation losses during trainingEvaluation | 151
Does your plot look different? Try running the whole pipelineagain:Full Pipeline%run -i data_preparation/v2.py%run -i model_configuration/v2.py%run -i model_training/v4.pyAnd then plot the resulting losses one more time.Cool, right? But, remember in the training step function, when I mentioned thatadding losses to a list was not very cutting-edge? Time to fix that! To bettervisualize the training process, we can make use of…TensorBoardYes, TensorBoard is that good! So good that we’ll be using a tool from thecompeting framework, TensorFlow :-) Jokes aside, TensorBoard is a very usefultool, and PyTorch provides classes and methods so that we can integrate it withour model.Running It Inside a NotebookThis section applies to both Google Colab and local installation.If you are using a local installation, you can either runTensorBoard inside a notebook or separately (check the nextsection for instructions).If you chose to follow this book using Google Colab, you’ll need to run TensorBoardinside a notebook. Luckily, this is easily accomplished using some Jupyter magics.If you are using Binder, this Jupyter magic will not work, forreasons that are beyond the scope of this section. More details onhow to use TensorBoard with Binder can be found in thecorresponding section below.First, we need to load TensorBoard’s extension for Jupyter:152 | Chapter 2: Rethinking the Training Loop
- 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 172 and 173: EvaluationHow can we evaluate the m
- Page 174 and 175: And then, we update our model confi
- 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
- Page 222 and 223: Model TrainingWe start by instantia
- Page 224 and 225: Making PredictionsLet’s make up s
Run - Model Training V4
%run -i model_training/v4.py
After updating all parts, in sequence, our current state of
development is:
• Data Preparation V2
• Model Configuration V2
• Model Training V4
Let’s inspect the model’s state:
# Checks model's parameters
print(model.state_dict())
Output
OrderedDict([('0.weight', tensor([[1.9419]], device='cuda:0')),
('0.bias', tensor([1.0244], device='cuda:0'))])
Plotting Losses
Let’s take a look at both losses—training and validation.
Figure 2.1 - Training and validation losses during training
Evaluation | 151