Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

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

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!