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.

Output

array(0.00804466, dtype=float32)

This seems like a lot of work; there must be an easier way! And there is one,

indeed: We can use item(), for tensors with a single element, or tolist()

otherwise (it still returns a scalar if there is only one element, though).

print(loss.item(), loss.tolist())

Output

0.008044655434787273 0.008044655434787273

At this point, there’s only one piece of code left to change: the predictions. It is

then time to introduce PyTorch’s way of implementing a…

Model

In PyTorch, a model is represented by a regular Python class that inherits from the

Module class.

IMPORTANT: Are you comfortable with object-oriented

programming (OOP) concepts like classes, constructors, methods,

instances, and attributes?

If you’re unsure about any of these terms, I’d strongly

recommend you follow tutorials like Real Python’s "Object-

Oriented Programming (OOP) in Python 3" [48] and "Supercharge

Your Classes With Python super()" [49] before proceeding.

Having a good understanding of OOP is key to benefitting the

most from PyTorch’s capabilities.

So, assuming you’re already comfortable with OOP, let’s dive into developing a

model in PyTorch.

Model | 103

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

Saved successfully!

Ooh no, something went wrong!