Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
Outputtensor([0.1000, 0.2000, 0.3000, 0.4000, 0.5000, 0.6000, 0.7000,0.8000, 0.9000, 1.0000, 1.1000])Pretty boring, right? This isn’t doing anything!Finally, an actual difference in behavior between train and evalmodes! It was long overdue!The inputs are just passing through. What’s the implication of this? Well, thatlinear layer that receives these values is still multiplying them by the weights andsumming them up:F.linear(output_eval, weight=torch.ones(11), bias=torch.tensor(0))Outputtensor(6.6000)This is the sum of all inputs (because all the weights were set to one and no inputwas dropped). If there was no adjusting factor, the outputs in evaluation andtraining modes would be substantially different, simply because there would bemore terms to add up in evaluation mode."I am still not convinced … without adjusting the output would be4.7, which is closer to 6.6 than the adjusted 9.4 … what is up?"This happened because dropping is probabilistic, and only four out of ten elementswere actually dropped (that was the thought I asked you to hold on to). The factoradjusts for the average number of dropped elements. We set the probability to50% so, on average, five elements will be dropped. By the way, if you change theseed to 45 and re-run the code, it will actually drop half of the inputs, and theadjusted output will be 6.4 instead of 9.4.Instead of setting a different random seed and manually checking which value itproduces, let’s generate 1,000 scenarios and compute the sum of the adjusteddropped outputs to get their distribution:Dropout | 435
torch.manual_seed(17)p = 0.5distrib_outputs = torch.tensor([F.linear(F.dropout(spaced_points, p=p),weight=torch.ones(11), bias=torch.tensor(0))for _ in range(1000)])Figure 6.7 - Distribution of outputsThe figure above shows us that, for that set of inputs, the output of our simplelinear layer with dropout will not be exactly 6.6 anymore, but something between0 and 12. The mean value for all scenarios is quite close to 6.6, though.Dropout not only drops some inputs but, due to its probabilisticnature, produces a distribution of outputs.In other words, the model needs to learn how to handle adistribution of values that is centered at the value the outputwould have if there was no dropout.Moreover, the choice of the dropout probability determines how spread out theoutputs will be.436 | Chapter 6: Rock, Paper, Scissors
- 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 458 and 459: torch.manual_seed(44)dropping_model
- 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
- Page 508 and 509: Next, we create a protected method
torch.manual_seed(17)
p = 0.5
distrib_outputs = torch.tensor([
F.linear(F.dropout(spaced_points, p=p),
weight=torch.ones(11), bias=torch.tensor(0))
for _ in range(1000)
])
Figure 6.7 - Distribution of outputs
The figure above shows us that, for that set of inputs, the output of our simple
linear layer with dropout will not be exactly 6.6 anymore, but something between
0 and 12. The mean value for all scenarios is quite close to 6.6, though.
Dropout not only drops some inputs but, due to its probabilistic
nature, produces a distribution of outputs.
In other words, the model needs to learn how to handle a
distribution of values that is centered at the value the output
would have if there was no dropout.
Moreover, the choice of the dropout probability determines how spread out the
outputs will be.
436 | Chapter 6: Rock, Paper, Scissors