Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
RecapFirst of all, congratulations are in order: You have successfully implemented a fullyfunctioning model and training loop in PyTorch!We have covered a lot of ground in this first chapter:• implementing a linear regression in Numpy using gradient descent• creating tensors in PyTorch, sending them to a device, and making parametersout of them• understanding PyTorch’s main feature, autograd, to perform automaticdifferentiation using its associated properties and methods, like backward(),grad, zero_(), and no_grad()• visualizing the dynamic computation graph associated with a sequence ofoperations• creating an optimizer to simultaneously update multiple parameters, using itsstep() and zero_grad() methods• creating a loss function using PyTorch’s corresponding higher-order function(more on that topic in the next chapter)• understanding PyTorch’s Module class and creating your own models,implementing __init__() and forward() methods, and making use of its builtinparameters() and state_dict() methods• transforming the original Numpy implementation into a PyTorch one using theelements above• realizing the importance of including model.train() inside the training loop(never forget that!)• implementing nested and sequential models using PyTorch’s layers• putting it all together into neatly organized code divided into three distinctparts: data preparation, model configuration, and model trainingYou are now ready for the next chapter. We’ll see more of PyTorch’s capabilities,and we’ll further develop our training loop so it can be used for different problemsand models. You’ll be building your own, small draft of a library for training deeplearning models.[39] https://github.com/dvgodoy/PyTorchStepByStep/blob/master/Chapter01.ipynb[40] https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter01.ipynbRecap | 121
[41] https://en.wikipedia.org/wiki/Gaussian_noise[42] https://bit.ly/2XZXjnk[43] https://bit.ly/3fjCSHR[44] https://bit.ly/2Y0lhPn[45] https://bit.ly/2UDXDWM[46] https://twitter.com/alecrad[47] http://cs231n.stanford.edu/[48] https://realpython.com/python3-object-oriented-programming/[49] https://realpython.com/python-super/[50] https://ipython.readthedocs.io/en/stable/interactive/magics.html[51] https://bit.ly/30GH0vO[52] https://bit.ly/3g1eQCm122 | Chapter 1: A Simple Regression Problem
- Page 96 and 97: # Sanity Check: do we get the same
- Page 98 and 99: Outputtensor(3.1416)tensor([1, 2, 3
- Page 100 and 101: Outputtensor([[1., 2., 1.],[1., 1.,
- Page 102 and 103: dummy_array = np.array([1, 2, 3])du
- Page 104 and 105: n_cudas = torch.cuda.device_count()
- Page 106 and 107: back_to_numpy = x_train_tensor.nump
- Page 108 and 109: I am assuming you’d like to use y
- Page 110 and 111: Outputtensor([0.1940], device='cuda
- Page 112 and 113: print(error.requires_grad, yhat.req
- Page 114 and 115: Output(tensor([0.], device='cuda:0'
- Page 116 and 117: 56 # need to tell it to let it go..
- Page 118 and 119: computation.If you chose "Local Ins
- Page 120 and 121: Figure 1.6 - Now parameter "b" does
- 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 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 194 and 195: After recovering our model’s stat
Recap
First of all, congratulations are in order: You have successfully implemented a fully
functioning model and training loop in PyTorch!
We have covered a lot of ground in this first chapter:
• implementing a linear regression in Numpy using gradient descent
• creating tensors in PyTorch, sending them to a device, and making parameters
out of them
• understanding PyTorch’s main feature, autograd, to perform automatic
differentiation using its associated properties and methods, like backward(),
grad, zero_(), and no_grad()
• visualizing the dynamic computation graph associated with a sequence of
operations
• creating an optimizer to simultaneously update multiple parameters, using its
step() and zero_grad() methods
• creating a loss function using PyTorch’s corresponding higher-order function
(more on that topic in the next chapter)
• understanding PyTorch’s Module class and creating your own models,
implementing __init__() and forward() methods, and making use of its builtin
parameters() and state_dict() methods
• transforming the original Numpy implementation into a PyTorch one using the
elements above
• realizing the importance of including model.train() inside the training loop
(never forget that!)
• implementing nested and sequential models using PyTorch’s layers
• putting it all together into neatly organized code divided into three distinct
parts: data preparation, model configuration, and model training
You are now ready for the next chapter. We’ll see more of PyTorch’s capabilities,
and we’ll further develop our training loop so it can be used for different problems
and models. You’ll be building your own, small draft of a library for training deep
learning models.
[39] https://github.com/dvgodoy/PyTorchStepByStep/blob/master/Chapter01.ipynb
[40] https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter01.ipynb
Recap | 121