Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
There are MANY different layers that can be used in PyTorch:• Convolution Layers• Pooling Layers• Padding Layers• Non-linear Activations• Normalization Layers• Recurrent Layers• Transformer Layers• Linear Layers• Dropout Layers• Sparse Layers (embeddings)• Vision Layers• DataParallel Layers (multi-GPU)• Flatten LayerSo far, we have just used a Linear layer. In the chapters ahead, we’ll use manyothers, like convolution, pooling, padding, flatten, dropout, and non-linearactivations.Putting It All TogetherWe’ve covered a lot of ground so far, from coding a linear regression in Numpyusing gradient descent to transforming it into a PyTorch model, step-by-step.It is time to put it all together and organize our code into three fundamental parts,namely:• data preparation (not data generation!)• model configuration• model trainingLet’s tackle these three parts, in order.Putting It All Together | 115
Data PreparationThere hasn’t been much data preparation up to this point, to be honest. Aftergenerating our data points in Notebook Cell 1.1, the only preparation stepperformed so far has been transforming Numpy arrays into PyTorch tensors, as inNotebook Cell 1.3, which is reproduced below:Define - Data Preparation V01 %%writefile data_preparation/v0.py23 device = 'cuda' if torch.cuda.is_available() else 'cpu'45 # Our data was in Numpy arrays, but we need to transform them6 # into PyTorch's Tensors and then send them to the7 # chosen device8 x_train_tensor = torch.as_tensor(x_train).float().to(device)9 y_train_tensor = torch.as_tensor(y_train).float().to(device)Run - Data Preparation V0%run -i data_preparation/v0.pyThis part will get much more interesting in the next chapter when we get to useDataset and DataLoader classes :-)"What’s the purpose of saving cells to these files?"We know we have to run the full sequence to train a model: data preparation,model configuration, and model training. In Chapter 2, we’ll gradually improve eachof these parts, versioning them inside each corresponding folder. So, saving them tofiles allows us to run a full sequence using different versions without having toduplicate code.Let’s say we start improving model configuration (and we will do exactly that inChapter 2), but the other two parts are still the same; how do we run the fullsequence?116 | Chapter 1: A Simple Regression Problem
- Page 90 and 91: Step 2# Step 2 - Computing the loss
- Page 92 and 93: Output[0.49671415] [-0.1382643][0.8
- Page 94 and 95: Notebook Cell 1.2 - Implementing gr
- 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 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 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
There are MANY different layers that can be used in PyTorch:
• Convolution Layers
• Pooling Layers
• Padding Layers
• Non-linear Activations
• Normalization Layers
• Recurrent Layers
• Transformer Layers
• Linear Layers
• Dropout Layers
• Sparse Layers (embeddings)
• Vision Layers
• DataParallel Layers (multi-GPU)
• Flatten Layer
So far, we have just used a Linear layer. In the chapters ahead, we’ll use many
others, like convolution, pooling, padding, flatten, dropout, and non-linear
activations.
Putting It All Together
We’ve covered a lot of ground so far, from coding a linear regression in Numpy
using gradient descent to transforming it into a PyTorch model, step-by-step.
It is time to put it all together and organize our code into three fundamental parts,
namely:
• data preparation (not data generation!)
• model configuration
• model training
Let’s tackle these three parts, in order.
Putting It All Together | 115