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.

Deploying / Making Predictions

Again, if we’re starting fresh (as if we had just turned on the computer and started

Jupyter), we have to set the stage before actually loading the model. But, this time,

we only need to configure the model:

Notebook Cell 2.8

%run -i model_configuration/v3.py

Once again, we have an untrained model at this point. The loading procedure is

simpler, though:

• load the dictionary back using torch.load()

• load model state dictionary back using its method load_state_dict()

Since the model is fully trained, we don’t need to load the optimizer or anything

else.

Notebook Cell 2.9 - Loading a fully trained model to make predictions

checkpoint = torch.load('model_checkpoint.pth')

model.load_state_dict(checkpoint['model_state_dict'])

print(model.state_dict())

Output

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

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

168 | Chapter 2: Rethinking the Training Loop

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

Saved successfully!

Ooh no, something went wrong!