22.02.2024 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Model Configuration

We can easily translate the model depicted above to code:

Model Configuration

1 # Sets learning rate - this is "eta" ~ the "n"-like Greek letter

2 lr = 0.1

3

4 torch.manual_seed(17)

5 # Now we can create a model

6 model_nn = nn.Sequential()

7 model_nn.add_module('flatten', nn.Flatten())

8 model_nn.add_module('hidden0', nn.Linear(25, 5, bias=False))

9 model_nn.add_module('hidden1', nn.Linear(5, 3, bias=False))

10 model_nn.add_module('output', nn.Linear(3, 1, bias=False))

11 model_nn.add_module('sigmoid', nn.Sigmoid())

12

13 # Defines an SGD optimizer to update the parameters

14 optimizer_nn = optim.SGD(model_nn.parameters(), lr=lr)

15

16 # Defines a binary cross-entropy loss function

17 binary_loss_fn = nn.BCELoss()

I’ve kept the names of the modules consistent with the captions in the figure so it is

easier to follow. The rest of the code should already be familiar to you.

Model Training

Let’s train our new deep-ish model for 100 epochs using the StepByStep class and

visualize the losses:

Model Training

1 n_epochs = 100

2

3 sbs_nn = StepByStep(model_nn, binary_loss_fn, optimizer_nn)

4 sbs_nn.set_loaders(train_loader, val_loader)

5 sbs_nn.train(n_epochs)

304 | Chapter 4: Classifying Images

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

Saved successfully!

Ooh no, something went wrong!