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

Model TrainingLet’s train our model for 100 epochs using the StepByStep class and visualize thelosses:Model Training1 n_epochs = 10023 sbs_logistic = StepByStep(4 model_logistic, binary_loss_fn, optimizer_logistic5 )6 sbs_logistic.set_loaders(train_loader, val_loader)7 sbs_logistic.train(n_epochs)fig = sbs_logistic.plot_losses()Figure 4.5 - Losses for the logistic regression modelAwful, right? It seems our model is barely learning anything! Maybe a deepermodel can do better.Deep-ish ModelThere we go, let’s add not one, but two hidden layers to our model and make itdeep-ish. We still start with a nn.Flatten layer, and the last part of our model stillis a Sigmoid, but there are two extra Linear layers before the already existingoutput layer.Deep-ish Model | 301

Let’s visualize it.Figure 4.6 - Deep-ish modelBy the way, in the figure above, the subscripts for both w and z represent the zerobasedindices for layer and unit: In the output layer, for instance, w 20 represents theweights corresponding to the first unit (#0) of the third layer (#2).What’s happening here? Let’s work out the forward pass; that is, the path frominputs (x) to output (y):1. An image is flattened to a tensor with 25 features, from x 0 to x 24 (not depictedin the figure above).2. The 25 features are forwarded to each of the five units in Hidden Layer #0.3. Each unit in Hidden Layer #0 use its weights, from w 00 to w 04 , and the featuresfrom the Input Layer to compute its corresponding outputs, from z 00 to z 04 .4. The outputs of Hidden Layer #0 are forwarded to each of the three units inHidden Layer #1 (in a way, the outputs of Hidden Layer #0 work as if they werefeatures to Hidden Layer #1).5. Each unit in Hidden Layer #1 uses its weights, from w 10 to w 12 , and the z 0 valuesfrom the preceding hidden layer to compute its corresponding outputs, from z 10to z 12 .6. The outputs of Hidden Layer #1 are forwarded to the single unit in the outputlayer (again, the outputs of Hidden Layer #1 work as if they were features to theOutput Layer).7. The unit in the Output Layer uses its weights (w 20 ) and the z 1 values from the302 | Chapter 4: Classifying Images

Model Training

Let’s train our model for 100 epochs using the StepByStep class and visualize the

losses:

Model Training

1 n_epochs = 100

2

3 sbs_logistic = StepByStep(

4 model_logistic, binary_loss_fn, optimizer_logistic

5 )

6 sbs_logistic.set_loaders(train_loader, val_loader)

7 sbs_logistic.train(n_epochs)

fig = sbs_logistic.plot_losses()

Figure 4.5 - Losses for the logistic regression model

Awful, right? It seems our model is barely learning anything! Maybe a deeper

model can do better.

Deep-ish Model

There we go, let’s add not one, but two hidden layers to our model and make it

deep-ish. We still start with a nn.Flatten layer, and the last part of our model still

is a Sigmoid, but there are two extra Linear layers before the already existing

output layer.

Deep-ish Model | 301

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

Saved successfully!

Ooh no, something went wrong!