Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
To make it clear: In this chapter, we’re dealing with a single-labelbinary classification (we have only one label per data point), andthe label is binary (there are only two possible values for it, zeroor one). If the label is zero, we say it belongs to the negative class.If the label is one, it belongs to the positive class.Please do not confuse the positive and negative classes of oursingle label with c, the so-called class number in thedocumentation. That c corresponds to the number of differentlabels associated with a data point. In our example, c = 1.You can use this argument to handle imbalanced datasets, butthere’s more to it than meets the eye. We’ll get back to it in thenext sub-section.Enough talking (or writing!): Let’s see how to use this loss in code. We start bycreating the loss function itself:loss_fn_logits = nn.BCEWithLogitsLoss(reduction='mean')loss_fn_logitsOutputBCEWithLogitsLoss()Next, we use logits and labels to compute the loss. Following the same principle asbefore, logits first, then labels. To keep the example consistent, let’s get the valuesof the logits corresponding to the probabilities we used before, 0.9 and 0.2, usingour log_odds_ratio() function:Loss | 227
logit1 = log_odds_ratio(.9)logit2 = log_odds_ratio(.2)dummy_labels = torch.tensor([1.0, 0.0])dummy_logits = torch.tensor([logit1, logit2])print(dummy_logits)Outputtensor([ 2.1972, -1.3863])We have logits, and we have labels. Time to compute the loss:loss = loss_fn_logits(dummy_logits, dummy_labels)lossOutputtensor(0.1643)OK, we got the same result, as expected.Imbalanced DatasetIn our dummy example with two data points, we had one of each class: positive andnegative. The dataset was perfectly balanced. Let’s create another dummy examplebut with an imbalance, adding two extra data points belonging to the negativeclass. For the sake of simplicity and to illustrate a quirk in the behavior ofnn.BCEWithLogitsLoss(), I will give those two extra points the same logits as theother data point in the negative class. It looks like this:dummy_imb_labels = torch.tensor([1.0, 0.0, 0.0, 0.0])dummy_imb_logits = torch.tensor([logit1, logit2, logit2, logit2])Clearly, this is an imbalanced dataset. There are three times more data points inthe negative class than in the positive one. Now, let’s turn to the pos_weight228 | Chapter 3: A Simple Classification Problem
- 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 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 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
- Page 284 and 285: If you want to learn more about bot
- Page 286 and 287: Model Training1 n_epochs = 10023 sb
- Page 288 and 289: step in your journey! What’s next
- Page 290 and 291: Chapter 4Classifying ImagesSpoilers
- Page 292 and 293: Data GenerationOur images are quite
- Page 294 and 295: Images and ChannelsIn case you’re
- Page 296 and 297: image_rgb = np.stack([image_r, imag
- Page 298 and 299: That’s fairly straightforward; we
- Page 300 and 301: • Transformations based on Tensor
logit1 = log_odds_ratio(.9)
logit2 = log_odds_ratio(.2)
dummy_labels = torch.tensor([1.0, 0.0])
dummy_logits = torch.tensor([logit1, logit2])
print(dummy_logits)
Output
tensor([ 2.1972, -1.3863])
We have logits, and we have labels. Time to compute the loss:
loss = loss_fn_logits(dummy_logits, dummy_labels)
loss
Output
tensor(0.1643)
OK, we got the same result, as expected.
Imbalanced Dataset
In our dummy example with two data points, we had one of each class: positive and
negative. The dataset was perfectly balanced. Let’s create another dummy example
but with an imbalance, adding two extra data points belonging to the negative
class. For the sake of simplicity and to illustrate a quirk in the behavior of
nn.BCEWithLogitsLoss(), I will give those two extra points the same logits as the
other data point in the negative class. It looks like this:
dummy_imb_labels = torch.tensor([1.0, 0.0, 0.0, 0.0])
dummy_imb_logits = torch.tensor([logit1, logit2, logit2, logit2])
Clearly, this is an imbalanced dataset. There are three times more data points in
the negative class than in the positive one. Now, let’s turn to the pos_weight
228 | Chapter 3: A Simple Classification Problem