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.

Notebook Cell 2.1.3

sbs.save_checkpoint('model_checkpoint.pth')

Resuming Training

Remember, when we did this in Chapter 2 we had to set the stage, loading the data

and configuring the model, before actually loading the model. We still need to do

this, but we are now using the latest version of model configuration:

Run - Model Configuration V4

%run -i model_configuration/v4.py

Let’s double-check that we do have an untrained model:

print(model.state_dict())

Output

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

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

Good, same as before! Besides, the model configuration has created the three

elements we need to pass as arguments to instantiate our StepByStep class:

Notebook Cell 2.1.4

new_sbs = StepByStep(model, loss_fn, optimizer)

Next, let’s load the trained model back using the load_checkpoint() method and

then inspect the model’s weights:

Notebook Cell 2.1.5

new_sbs.load_checkpoint('model_checkpoint.pth')

print(model.state_dict())

200 | Chapter 2.1: Going Classy

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

Saved successfully!

Ooh no, something went wrong!