Skip to Content
Deep Learning with PyTorch
book

Deep Learning with PyTorch

by Vishnu Subramanian
February 2018
Intermediate to advanced
262 pages
6h 59m
English
Packt Publishing
Content preview from Deep Learning with PyTorch

Training the model

In the previous sections, we have created DataLoader instances and algorithms. Now, let's train the model. To do this we need a loss function and an optimizer:

# Loss and Optimizerlearning_rate = 0.001criterion = nn.CrossEntropyLoss()optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9)exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7,    gamma=0.1)

In the preceding code, we created our loss function based on CrossEntropyLoss and the optimizer based on SGD. The StepLR function helps in dynamically changing the learning rate. We will discuss different strategies available to tune the learning rate in Chapter 4, Fundamentals of Machine Learning.

The following train_model function takes in a ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Deep Learning with PyTorch

Deep Learning with PyTorch

Eli Stevens, Thomas Viehmann, Luca Pietro Giovanni Antiga
Grokking Deep Learning

Grokking Deep Learning

Andrew W. Trask

Publisher Resources

ISBN: 9781788624336Supplemental Content