Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
The three units in the output layer produce three logits, one for each class (C 0 , C 1 ,and C 2 ). We could have added an nn.LogSoftmax layer to the model, and it wouldhave converted the three logits to log probabilities.Since our model produces logits, we must use the nn.CrossEntropyLoss()function:Model Configuration — Loss and Optimizer1 lr = 0.12 multi_loss_fn = nn.CrossEntropyLoss(reduction='mean')3 optimizer_cnn1 = optim.SGD(model_cnn1.parameters(), lr=lr)And then we create an optimizer (SGD) with a given learning rate (0.1), as usual.Boring, right? No worries; we’ll finally change the optimizer in the Rock PaperScissors classification problem in the next chapter.Model TrainingThis part is completely straightforward. First, we instantiate our class and set theloaders:Model Training1 sbs_cnn1 = StepByStep(model_cnn1, multi_loss_fn, optimizer_cnn1)2 sbs_cnn1.set_loaders(train_loader, val_loader)Then, we train it for 20 epochs and visualize the losses:Model Training1 sbs_cnn1.train(20)fig = sbs_cnn1.plot_losses()A Multiclass Classification Problem | 387
Figure 5.18 - LossesOK, it seems to have reached a minimum at the fifth epoch.Visualizing Filters and More!In Chapter 4, we briefly discussed visualizing weights as pixels. We’re going to divedeeper into the visualization of filters (weights), as well as the transformed imagesproduced by each of our model’s layers.First, let’s add another method to our tool belt!388 | Chapter 5: Convolutions
- Page 362 and 363: Figure B.5 - In the beginning…But
- Page 364 and 365: OK, now we can clearly see a differ
- Page 366 and 367: In the model above, the sigmoid fun
- Page 368 and 369: the more dimensions, the more separ
- Page 370 and 371: import randomimport numpy as npfrom
- Page 372 and 373: identity = np.array([[[[0, 0, 0],[0
- Page 374 and 375: Figure 5.4 - Striding the image, on
- Page 376 and 377: Output-----------------------------
- Page 378 and 379: Outputtensor([[[[9., 5., 0., 7.],[0
- Page 380 and 381: OutputParameter containing:tensor([
- Page 382 and 383: Moreover, notice that if we were to
- Page 384 and 385: In code, as usual, PyTorch gives us
- Page 386 and 387: Outputtensor([[[[5., 5., 0., 8., 7.
- Page 388 and 389: edge = np.array([[[[0, 1, 0],[1, -4
- Page 390 and 391: A pooling kernel of two-by-two resu
- Page 392 and 393: Outputtensor([[22., 23., 11., 24.,
- Page 394 and 395: Figure 5.15 - LeNet-5 architectureS
- Page 396 and 397: • second block: produces 16-chann
- Page 398 and 399: Transformed Dataset1 class Transfor
- Page 400 and 401: LossNew problem, new loss. Since we
- Page 402 and 403: Outputtensor([4.0000, 1.0000, 0.500
- Page 404 and 405: The loss only considers the predict
- Page 406 and 407: Outputtensor([[-1.5229, -0.3146, -2
- Page 408 and 409: IMPORTANT: I can’t stress this en
- Page 410 and 411: figures at the beginning of this ch
- 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 460 and 461: Outputtensor([0.1000, 0.2000, 0.300
Figure 5.18 - Losses
OK, it seems to have reached a minimum at the fifth epoch.
Visualizing Filters and More!
In Chapter 4, we briefly discussed visualizing weights as pixels. We’re going to dive
deeper into the visualization of filters (weights), as well as the transformed images
produced by each of our model’s layers.
First, let’s add another method to our tool belt!
388 | Chapter 5: Convolutions