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.

print(model.state_dict())

Output

OrderedDict([('0.weight', tensor([[1.9690]], device='cuda:0')),

('0.bias', tensor([1.0235], device='cuda:0'))])

So, I guess we could say all these lines of code (7-23) perform a training step. For a

given combination of model, loss, and optimizer, it takes the features and

corresponding labels as arguments. Right?

How about writing a function that takes a model, a loss, and an optimizer and

returns another function that performs a training step? The latter would then

take the features and corresponding labels as arguments and return the

corresponding loss.

"Wait; what?! A function that returns another function?"

Sounds complicated, right? It is not as bad as it sounds, though; that’s called a

higher-order function, and it is very useful for reducing boilerplate.

If you’re familiar with the concept of higher-order functions, feel free to skip the

aside.

126 | Chapter 2: Rethinking the Training Loop

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

Saved successfully!

Ooh no, something went wrong!