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.

After updating two out of three fundamental parts, our current

state of development is:

• Data Preparation V0

• Model Configuration V1

• Model Training V1

How do we check if our changes introduced any bugs? We can inspect our model’s

state_dict():

# Checks model's parameters

print(model.state_dict())

Output

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

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

Let’s give our training loop a rest and focus on our data for a while. So far, we’ve

simply used our Numpy arrays turned into PyTorch tensors. But we can do better;

we can build a…

Dataset

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

the Dataset class. You can think of it as a list of tuples, each tuple corresponding to

one point (features, label).

The most fundamental methods it needs to implement are:

• __init__(self): This takes whatever arguments are needed to build a list of

tuples—it may be the name of a CSV file that will be loaded and processed; it

may be two tensors, one for features, another one for labels; or anything else,

depending on the task at hand.

134 | Chapter 2: Rethinking the Training Loop

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

Saved successfully!

Ooh no, something went wrong!