Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
import numpy as npimport torchimport torch.optim as optimimport torch.nn as nnimport torch.functional as Ffrom torch.utils.data import DataLoader, TensorDatasetfrom sklearn.datasets import make_moonsfrom sklearn.preprocessing import StandardScalerfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import confusion_matrix, roc_curve, \precision_recall_curve, aucfrom stepbystep.v0 import StepByStepA Simple Classification ProblemIt is time to handle a different class of problems: classification problems (punintended). In a classification problem, we’re trying to predict which class a datapoint belongs to.Let’s say we have two classes of points: They are either red or blue. These are thelabels (y) of the points. Sure enough, we need to assign numeric values to them. Wecould assign zero to red and one to blue. The class associated with zero is thenegative class, while one corresponds to the positive class.In a nutshell, for binary classification, we have:Color Value ClassRed 0 NegativeBlue 1 PositiveIMPORTANT: In a classification model, the output is thepredicted probability of the positive class. In our case, the modelwill predict the probability of a point being blue.A Simple Classification Problem | 207
The choice of which class is positive and which class is negative doesnot affect model performance. If we reverse the mapping, makingred the positive class, the only difference would be that the modelwould predict the probability of a point being red. But, since bothprobabilities have to add up to one, we could easily convertbetween them, so the models are equivalent.Instead of defining a model first and then generating synthetic data for it, we’ll do itthe other way around.Data GenerationLet’s make the data a bit more interesting by using two features (x 1 and x 2 ) thistime. We’ll use Scikit-Learn’s make_moons() to generate a toy dataset with 100data points. We will also add some Gaussian noise and set a random seed to ensurereproducibility.Data Generation1 X, y = make_moons(n_samples=100, noise=0.3, random_state=0)Then, we’ll perform the train-validation split using Scikit-Learn’strain_test_split() for convenience (we’ll get back to splitting indices later):Train-validation Split1 X_train, X_val, y_train, y_val = train_test_split(2 X,3 y,4 test_size=.2,5 random_state=136 )Remember, the split should always be the first thing you do—nopre-processing, no transformations, nothing happens before thesplit.208 | Chapter 3: A Simple Classification Problem
- 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
- 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 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
- Page 244 and 245: A picture is worth a thousand words
- Page 246 and 247: OutputOrderedDict([('linear.weight'
- Page 248 and 249: The first summation adds up the err
- Page 250 and 251: IMPORTANT: Make sure to pass the pr
- Page 252 and 253: To make it clear: In this chapter,
- Page 254 and 255: argument of nn.BCEWithLogitsLoss().
- Page 256 and 257: It is not that hard, to be honest.
- Page 258 and 259: Figure 3.6 - Training and validatio
- Page 260 and 261: Outputarray([[0.5504593 ],[0.949995
- Page 262 and 263: decision boundary.Look at the expre
- Page 264 and 265: Are my data points separable?That
- Page 266 and 267: model = nn.Sequential()model.add_mo
- Page 268 and 269: It looks like this:Figure 3.10 - Sp
- Page 270 and 271: True and False Positives and Negati
- Page 272 and 273: tpr_fpr(cm_thresh50)Output(0.909090
- Page 274 and 275: The trade-off between precision and
- Page 276 and 277: Figure 3.13 - Using a low threshold
- Page 278 and 279: Figure 3.16 - Trade-offs for two di
- Page 280 and 281: thresholds do not necessarily inclu
import numpy as np
import torch
import torch.optim as optim
import torch.nn as nn
import torch.functional as F
from torch.utils.data import DataLoader, TensorDataset
from sklearn.datasets import make_moons
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, roc_curve, \
precision_recall_curve, auc
from stepbystep.v0 import StepByStep
A Simple Classification Problem
It is time to handle a different class of problems: classification problems (pun
intended). In a classification problem, we’re trying to predict which class a data
point belongs to.
Let’s say we have two classes of points: They are either red or blue. These are the
labels (y) of the points. Sure enough, we need to assign numeric values to them. We
could assign zero to red and one to blue. The class associated with zero is the
negative class, while one corresponds to the positive class.
In a nutshell, for binary classification, we have:
Color Value Class
Red 0 Negative
Blue 1 Positive
IMPORTANT: In a classification model, the output is the
predicted probability of the positive class. In our case, the model
will predict the probability of a point being blue.
A Simple Classification Problem | 207