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

Figure 4.16 - Deep model (for real)Let’s see how it performs now.Model ConfigurationFirst, we translate the model above to code:Model Configuration1 # Sets learning rate - this is "eta" ~ the "n"-like Greek letter2 lr = 0.134 torch.manual_seed(17)5 # Now we can create a model6 model_relu = nn.Sequential()7 model_relu.add_module('flatten', nn.Flatten())8 model_relu.add_module('hidden0', nn.Linear(25, 5, bias=False))9 model_relu.add_module('activation0', nn.ReLU())10 model_relu.add_module('hidden1', nn.Linear(5, 3, bias=False))11 model_relu.add_module('activation1', nn.ReLU())12 model_relu.add_module('output', nn.Linear(3, 1, bias=False))13 model_relu.add_module('sigmoid', nn.Sigmoid())1415 # Defines an SGD optimizer to update the parameters16 # (now retrieved directly from the model)17 optimizer_relu = optim.SGD(model_relu.parameters(), lr=lr)1819 # Defines a binary cross-entropy loss function20 binary_loss_fn = nn.BCELoss()Deep Model | 321

The chosen activation function is the rectified linear unit (ReLU), one of the mostcommonly used functions.We kept the bias out of the picture for the sake of comparing this model to theprevious one, which is completely identical except for the activation functionsintroduced after each hidden layer.In real problems, as a general rule, you should keep bias=True.Model TrainingLet’s train our new, deep, and activated model for 50 epochs using the StepByStepclass and visualize the losses:Model Training1 n_epochs = 5023 sbs_relu = StepByStep(model_relu, binary_loss_fn, optimizer_relu)4 sbs_relu.set_loaders(train_loader, val_loader)5 sbs_relu.train(n_epochs)fig = sbs_relu.plot_losses()Figure 4.17 - LossesThis is more like it! But, to really grasp the difference made by the activationfunctions, let’s plot all models on the same chart.322 | Chapter 4: Classifying Images

The chosen activation function is the rectified linear unit (ReLU), one of the most

commonly used functions.

We kept the bias out of the picture for the sake of comparing this model to the

previous one, which is completely identical except for the activation functions

introduced after each hidden layer.

In real problems, as a general rule, you should keep bias=True.

Model Training

Let’s train our new, deep, and activated model for 50 epochs using the StepByStep

class and visualize the losses:

Model Training

1 n_epochs = 50

2

3 sbs_relu = StepByStep(model_relu, binary_loss_fn, optimizer_relu)

4 sbs_relu.set_loaders(train_loader, val_loader)

5 sbs_relu.train(n_epochs)

fig = sbs_relu.plot_losses()

Figure 4.17 - Losses

This is more like it! But, to really grasp the difference made by the activation

functions, let’s plot all models on the same chart.

322 | Chapter 4: Classifying Images

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

Saved successfully!

Ooh no, something went wrong!