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.

Figure 4.16 - Deep model (for real)

Let’s see how it performs now.

Model Configuration

First, we translate the model 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_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())

14

15 # Defines an SGD optimizer to update the parameters

16 # (now retrieved directly from the model)

17 optimizer_relu = optim.SGD(model_relu.parameters(), lr=lr)

18

19 # Defines a binary cross-entropy loss function

20 binary_loss_fn = nn.BCELoss()

Deep Model | 321

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

Saved successfully!

Ooh no, something went wrong!