Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

After recovering our model’s state, we can finally use it to make predictions fornew inputs:Notebook Cell 2.10new_inputs = torch.tensor([[.20], [.34], [.57]])model.eval() # always use EVAL for fully trained models! 1model(new_inputs.to(device))1 Never forget to set the mode!Outputtensor([[1.4185],[1.6908],[2.1381]], device='cuda:0', grad_fn=<AddmmBackward>)Since Model Configuration V3 created a model and sent it automatically to ourdevice, we need to do the same with our new inputs.After loading a fully trained model for deployment / to makepredictions, make sure you ALWAYS set it to evaluation mode:model.eval()Congratulations, you "deployed" your first model :-)Setting the Model’s ModeI know, I am probably a bit obsessive about this, but here we go one more time:After loading the model, DO NOT FORGET to SET THE MODE:• checkpointing: model.train()• deploying / making predictions: model.eval()Saving and Loading Models | 169

Putting It All TogetherWe have updated each of the three fundamental parts of our code at least twice. Itis time to put it all together to get an overall view of what we have achieved so far.Behold your pipeline: Data Preparation V2, Model Configuration V3, and ModelTraining V5!Run - Data Preparation V21 # %load data_preparation/v2.py23 torch.manual_seed(13)45 # Builds tensors from numpy arrays BEFORE split6 x_tensor = torch.as_tensor(x).float()7 y_tensor = torch.as_tensor(y).float()89 # Builds dataset containing ALL data points10 dataset = TensorDataset(x_tensor, y_tensor)1112 # Performs the split13 ratio = .814 n_total = len(dataset)15 n_train = int(n_total * ratio)16 n_val = n_total - n_train17 train_data, val_data = random_split(dataset, [n_train, n_val])18 # Builds a loader of each set19 train_loader = DataLoader(20 dataset=train_data,21 batch_size=16,22 shuffle=True,23 )24 val_loader = DataLoader(dataset=val_data, batch_size=16)170 | Chapter 2: Rethinking the Training Loop

After recovering our model’s state, we can finally use it to make predictions for

new inputs:

Notebook Cell 2.10

new_inputs = torch.tensor([[.20], [.34], [.57]])

model.eval() # always use EVAL for fully trained models! 1

model(new_inputs.to(device))

1 Never forget to set the mode!

Output

tensor([[1.4185],

[1.6908],

[2.1381]], device='cuda:0', grad_fn=<AddmmBackward>)

Since Model Configuration V3 created a model and sent it automatically to our

device, we need to do the same with our new inputs.

After loading a fully trained model for deployment / to make

predictions, make sure you ALWAYS set it to evaluation mode:

model.eval()

Congratulations, you "deployed" your first model :-)

Setting the Model’s Mode

I know, I am probably a bit obsessive about this, but here we go one more time:

After loading the model, DO NOT FORGET to SET THE MODE:

• checkpointing: model.train()

• deploying / making predictions: model.eval()

Saving and Loading Models | 169

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

Saved successfully!

Ooh no, something went wrong!