Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
Next, we’ll standardize the features using Scikit-Learn’s StandardScaler:Feature Standardization1 sc = StandardScaler()2 sc.fit(X_train)34 X_train = sc.transform(X_train)5 X_val = sc.transform(X_val)Remember, you should use only the training set to fit theStandardScaler, and then use its transform() method to applythe pre-processing step to all datasets: training, validation, andtest. Otherwise, you’ll be leaking information from the validationand / or test sets to your model!Figure 3.1 - Moons datasetData PreparationHopefully, this step feels familiar to you already! As usual, the data preparationstep converts Numpy arrays into PyTorch tensors, builds TensorDatasets for them,and creates the corresponding data loaders.Data Preparation | 209
Data Preparation1 torch.manual_seed(13)23 # Builds tensors from Numpy arrays4 x_train_tensor = torch.as_tensor(X_train).float()5 y_train_tensor = torch.as_tensor(y_train.reshape(-1, 1)).float()67 x_val_tensor = torch.as_tensor(X_val).float()8 y_val_tensor = torch.as_tensor(y_val.reshape(-1, 1)).float()910 # Builds dataset containing ALL data points11 train_dataset = TensorDataset(x_train_tensor, y_train_tensor)12 val_dataset = TensorDataset(x_val_tensor, y_val_tensor)1314 # Builds a loader of each set15 train_loader = DataLoader(16 dataset=train_dataset,17 batch_size=16,18 shuffle=True19 )20 val_loader = DataLoader(dataset=val_dataset, batch_size=16)There are 80 data points (N = 80) in our training set. We have two features, x 1 andx 2 , and the labels (y) are either zero (red) or one (blue). We have a dataset; now weneed a…ModelGiven a classification problem, one of the more straightforward models is thelogistic regression. But, instead of simply presenting it and using it right away, I amgoing to build up to it. The rationale behind this approach is twofold: First, it willmake clear why this algorithm is called logistic regression if it is used forclassification; second, you’ll get a clear understanding of what a logit is.Well, since it is called logistic regression, I would say that linear regression is agood starting point. What would a linear regression model with two features looklike?210 | Chapter 3: A Simple Classification Problem
- 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 232 and 233: import numpy as npimport torchimpor
- 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
- Page 282 and 283: actual data, it is as bad as it can
Next, we’ll standardize the features using Scikit-Learn’s StandardScaler:
Feature Standardization
1 sc = StandardScaler()
2 sc.fit(X_train)
3
4 X_train = sc.transform(X_train)
5 X_val = sc.transform(X_val)
Remember, you should use only the training set to fit the
StandardScaler, and then use its transform() method to apply
the pre-processing step to all datasets: training, validation, and
test. Otherwise, you’ll be leaking information from the validation
and / or test sets to your model!
Figure 3.1 - Moons dataset
Data Preparation
Hopefully, this step feels familiar to you already! As usual, the data preparation
step converts Numpy arrays into PyTorch tensors, builds TensorDatasets for them,
and creates the corresponding data loaders.
Data Preparation | 209