Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
torch.manual_seed(44)dropping_model.train()output_train = dropping_model(spaced_points)output_trainOutputtensor([0.0000, 0.4000, 0.0000, 0.8000, 0.0000, 1.2000, 1.4000,1.6000, 1.8000, 0.0000, 2.2000])There are many things to notice here:• The model is in train mode (very important, hold on to this!).• Since this model does not have any weights, it becomes clear that dropoutdrops inputs, not weights.• It dropped four elements only!• The remaining elements have different values now!"What’s going on here?"First, dropping is probabilistic, so each input had a 50% chance of being dropped.In our tiny example, by chance, only four out of ten were actually dropped (hold onto this thought too!).Figure 6.6 - Applying dropoutSecond, the remaining elements need to be proportionally adjusted by a factor of1/p. In our example, that’s a factor of two.Dropout | 433
output_train / spaced_pointsOutputtensor([0., 2., 0., 2., 0., 2., 2., 2., 2., 0., 2.])"Why?"This adjustment has the purpose of preserving (or at least trying to) the overalllevel of the outputs in the particular layer that’s "suffering" the dropout. So, let’simagine that these inputs (after dropping) will feed a linear layer and, foreducational purposes, that all their weights are equal to one (and bias equals zero).As you already know, a linear layer will multiply these weights by the (dropped)inputs and sum them up:F.linear(output_train, weight=torch.ones(11), bias=torch.tensor(0))Outputtensor(9.4000)The sum is 9.4. It would have been half of this (4.7) without the adjusting factor."OK, so what? Why do I need to preserve the level of the outputsanyway?"Because there is no dropping in evaluation mode! We’ve talked about it briefly inthe past—the dropout is random in nature, so it would produce slightly (or maybenot so slightly) different predictions for the same inputs. You don’t want that,that’s bad business. So, let’s set our model to eval mode (and that’s why I chose tomake it a model instead of using functional dropout) and see what happens there:dropping_model.eval()output_eval = dropping_model(spaced_points)output_eval434 | Chapter 6: Rock, Paper, Scissors
- Page 408 and 409: IMPORTANT: I can’t stress this en
- Page 410 and 411: figures at the beginning of this ch
- Page 412 and 413: The three units in the output layer
- Page 414 and 415: StepByStep Method@staticmethoddef _
- Page 416 and 417: The meow() method is totally indepe
- Page 418 and 419: StepByStep Methoddef visualize_filt
- Page 420 and 421: dummy_model = nn.Linear(1, 1)dummy_
- Page 422 and 423: dummy_listOutput[(Linear(in_feature
- Page 424 and 425: Output{Conv2d(1, 1, kernel_size=(3,
- Page 426 and 427: will be the externally defined vari
- Page 428 and 429: Removing Hookssbs_cnn1.remove_hooks
- Page 430 and 431: return figsetattr(StepByStep, 'visu
- Page 432 and 433: Figure 5.22 - Feature maps (classif
- Page 434 and 435: classification: The predicted class
- Page 436 and 437: convolutional layers to our model a
- Page 438 and 439: Capturing Outputsfeaturizer_layers
- Page 440 and 441: the filters learned by the model pr
- Page 442 and 443: given chapter are imported at its v
- Page 444 and 445: Data PreparationThe data preparatio
- Page 446 and 447: model anyway. We’ll use it to com
- Page 448 and 449: StepByStep Method@staticmethoddef m
- Page 450 and 451: "What’s wrong with the colors?"Th
- Page 452 and 453: three_channel_filter = np.array([[[
- Page 454 and 455: Fancier Model (Constructor)class CN
- Page 456 and 457: Fancier Model (Classifier)def class
- Page 460 and 461: Outputtensor([0.1000, 0.2000, 0.300
- Page 462 and 463: Figure 6.8 - Output distribution fo
- Page 464 and 465: Adaptive moment estimation (Adam) u
- Page 466 and 467: torch.manual_seed(13)# Model Config
- Page 468 and 469: Outputtorch.Size([5, 3, 3, 3])Its s
- Page 470 and 471: Choosing a learning rate that works
- Page 472 and 473: Higher-Order Learning Rate Function
- Page 474 and 475: Perfect! Now let’s build the actu
- Page 476 and 477: ax.set_xlabel('Learning Rate')ax.se
- Page 478 and 479: LRFinderThe function we’ve implem
- Page 480 and 481: value in our moving average has an
- Page 482 and 483: Figure 6.15 - Distribution of weigh
- Page 484 and 485: In code, the implementation of the
- Page 486 and 487: As expected, the EWMA without corre
- Page 488 and 489: optimizer = optim.Adam(model.parame
- Page 490 and 491: IMPORTANT: The logging function mus
- Page 492 and 493: Output{'state': {140601337662512: {
- Page 494 and 495: different optimizer, set them to ca
- Page 496 and 497: • dampening: dampening factor for
- Page 498 and 499: Figure 6.20 - Paths taken by SGD (w
- Page 500 and 501: Equation 6.16 - Looking aheadOnce N
- Page 502 and 503: Figure 6.22 - Path taken by each SG
- Page 504 and 505: for epoch in range(4):# training lo
- Page 506 and 507: course) up to a given number of epo
torch.manual_seed(44)
dropping_model.train()
output_train = dropping_model(spaced_points)
output_train
Output
tensor([0.0000, 0.4000, 0.0000, 0.8000, 0.0000, 1.2000, 1.4000,
1.6000, 1.8000, 0.0000, 2.2000])
There are many things to notice here:
• The model is in train mode (very important, hold on to this!).
• Since this model does not have any weights, it becomes clear that dropout
drops inputs, not weights.
• It dropped four elements only!
• The remaining elements have different values now!
"What’s going on here?"
First, dropping is probabilistic, so each input had a 50% chance of being dropped.
In our tiny example, by chance, only four out of ten were actually dropped (hold on
to this thought too!).
Figure 6.6 - Applying dropout
Second, the remaining elements need to be proportionally adjusted by a factor of
1/p. In our example, that’s a factor of two.
Dropout | 433