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.

This is what the model configuration looks like for our classification problem:

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 model = nn.Sequential()

6 model.add_module('linear', nn.Linear(2, 1))

7

8 # Defines an SGD optimizer to update the parameters

9 optimizer = optim.SGD(model.parameters(), lr=lr)

10

11 # Defines a BCE with logits loss function

12 loss_fn = nn.BCEWithLogitsLoss()

Model Training

Time to train our model! We can leverage the StepByStep class we built in Chapter

2.1 and use pretty much the same code as before:

Model Training

1 n_epochs = 100

2

3 sbs = StepByStep(model, loss_fn, optimizer)

4 sbs.set_loaders(train_loader, val_loader)

5 sbs.train(n_epochs)

fig = sbs.plot_losses()

232 | Chapter 3: A Simple Classification Problem

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

Saved successfully!

Ooh no, something went wrong!