Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
np.concatenate([dummy_points[:5].numpy(),dummy_sbs.predict(dummy_points)[:5]], axis=1)Outputarray([[-0.9012059 , -0.9012059 ],[ 0.56559485, 0.56559485],[-0.48822638, -0.48822638],[ 0.75069577, 0.75069577],[ 0.58925384, 0.58925384]], dtype=float32)It looks like the model actually learned the identity function … or did it? Let’scheck its parameters:dummy_model.state_dict()OutputOrderedDict([('linear.weight', tensor([[0.1488]], device='cuda:0')),('linear.bias', tensor([-0.3326], device='cuda:0'))])For an input value equal to zero, the output of the linear layer will be -0.3326,which, in turn, will be chopped off by the ReLU activation. Now I have a questionfor you:"Which input values produce outputs greater than zero?"The answer: Input values above 2.2352 (=0.3326/0.1488) will produce positiveoutputs, which, in turn, will pass through the ReLU activation. But I have anotherquestion for you:"Guess what is the highest input value in our dataset?"Residual Connections | 549
Close enough! I am assuming you answered 2.2352, but it is just a little bit less thanthat:dummy_points.max()Outputtensor(2.2347)"So what? Does it actually mean anything?"It means the model learned to stay out of the way of the inputs! Now that themodel has the ability to use the raw inputs directly, its linear layer learned toproduce only negative values, so its nonlinearity (ReLU) produces only zeros. Cool,right?The Power of ShortcutsThe residual connection works as a shortcut, enabling the modelto skip the nonlinearities when it pays off to do so (if it yields alower loss). For this reason, residual connections are also knownas skip connections."I’m still not convinced … what’s the big deal about this?"The big deal is, these shortcuts make the loss surface smoother, so gradientdescent has an easier job finding a minimum. Don’t take my word for it—go andcheck the beautiful loss landscape visualizations produced by Li et al. in their paper"Visualizing the Loss Landscape of Neural Nets." [129]Awesome, right? These are projections of a multi-dimensional loss surface for theResNet model, with and without skip connections. Guess which one is easier totrain? :-)If you’re curious to see more landscapes like these, make sure tocheck their website: "Visualizing the Loss Landscape of NeuralNets." [130]550 | Chapter 7: Transfer Learning
- Page 524 and 525: ImportsFor the sake of organization
- Page 526 and 527: ILSVRC-2012The 2012 edition [111] o
- Page 528 and 529: remained unchanged.ResNet (MSRA Tea
- Page 530 and 531: Transfer Learning in PracticeIn Cha
- Page 532 and 533: dropout. You’re already familiar
- Page 534 and 535: OutputDownloading: "https://downloa
- Page 536 and 537: Replacing the "Top" of the Model1 a
- Page 538 and 539: Model Size Classifier Layer(s) Repl
- Page 540 and 541: Model TrainingWe have everything se
- Page 542 and 543: "Removing" the Top Layer1 alex.clas
- Page 544 and 545: torch.save(train_preproc.tensors, '
- Page 546 and 547: Outputtensor([[109, 124],[124, 124]
- Page 548 and 549: Model Configuration1 optimizer_mode
- Page 550 and 551: Figure 7.4 - 1x1 convolutionThe inp
- Page 552 and 553: The weights used by PIL are 0.299 f
- Page 554 and 555: • reduce the number of output cha
- Page 556 and 557: The constructor method defines the
- Page 558 and 559: Does it sound familiar? That’s wh
- Page 560 and 561: and w to represent these parameters
- Page 562 and 563: A mini-batch of size 64 is small en
- Page 564 and 565: normed1 = batch_normalizer(batch1[0
- Page 566 and 567: OutputOrderedDict([('running_mean',
- Page 568 and 569: OutputOrderedDict([('running_mean',
- Page 570 and 571: batch_normalizer = nn.BatchNorm2d(n
- Page 572 and 573: torch.manual_seed(23)dummy_points =
- Page 576 and 577: Another advantage of these shortcut
- Page 578 and 579: It should be pretty clear, except f
- Page 580 and 581: Data Preparation1 # ImageNet statis
- Page 582 and 583: Data Preparation — Preprocessing1
- Page 584 and 585: • freezing the layers of the mode
- Page 586 and 587: Extra ChapterVanishing and Explodin
- Page 588 and 589: discussing it, let me illustrate it
- Page 590 and 591: Model Configuration (2)1 loss_fn =
- Page 592 and 593: weights. If done properly, the init
- Page 594 and 595: just did), or, if you are training
- Page 596 and 597: Figure E.3 - The effect of batch no
- Page 598 and 599: Model Configuration1 torch.manual_s
- Page 600 and 601: torch.manual_seed(42)parm = nn.Para
- Page 602 and 603: (and only if) the norm exceeds the
- Page 604 and 605: if callable(self.clipping): 1self.c
- Page 606 and 607: Moreover, let’s use a ten times h
- Page 608 and 609: Clipping with HooksFirst, we reset
- Page 610 and 611: • visualizing the difference betw
- Page 612 and 613: Chapter 8SequencesSpoilersIn this c
- Page 614 and 615: Before shuffling, the pixels were o
- Page 616 and 617: And then let’s visualize the firs
- Page 618 and 619: sequence so far, and a data point f
- Page 620 and 621: Considering this, the not "unrolled
- Page 622 and 623: linear_input = nn.Linear(n_features
np.concatenate([dummy_points[:5].numpy(),
dummy_sbs.predict(dummy_points)[:5]], axis=1)
Output
array([[-0.9012059 , -0.9012059 ],
[ 0.56559485, 0.56559485],
[-0.48822638, -0.48822638],
[ 0.75069577, 0.75069577],
[ 0.58925384, 0.58925384]], dtype=float32)
It looks like the model actually learned the identity function … or did it? Let’s
check its parameters:
dummy_model.state_dict()
Output
OrderedDict([('linear.weight', tensor([[0.1488]], device='cuda:0')),
('linear.bias', tensor([-0.3326], device='cuda:0'))])
For an input value equal to zero, the output of the linear layer will be -0.3326,
which, in turn, will be chopped off by the ReLU activation. Now I have a question
for you:
"Which input values produce outputs greater than zero?"
The answer: Input values above 2.2352 (=0.3326/0.1488) will produce positive
outputs, which, in turn, will pass through the ReLU activation. But I have another
question for you:
"Guess what is the highest input value in our dataset?"
Residual Connections | 549