22.02.2024 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Model Configuration

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

2 lr = 0.1

3

4 torch.manual_seed(42)

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 optimizer_relu = optim.SGD(model_relu.parameters(), lr=lr)

17

18 # Defines a binary cross-entropy loss function

19 binary_loss_fn = nn.BCELoss()

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)

Putting It All Together | 329

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

Saved successfully!

Ooh no, something went wrong!