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.

Notice that the model is set to training mode after loading the checkpoint.

What about making predictions? To make it easier for the user to make predictions

for any new data points, we will be handling all the Numpy to PyTorch back and

forth conversion inside the function.

Making Predictions

def predict(self, x):

# Set it to evaluation mode for predictions

self.model.eval()

# Take a Numpy input and make it a float tensor

x_tensor = torch.as_tensor(x).float()

# Send input to device and use model for prediction

y_hat_tensor = self.model(x_tensor.to(self.device))

# Set it back to train mode

self.model.train()

# Detach it, bring it to CPU and back to Numpy

return y_hat_tensor.detach().cpu().numpy()

setattr(StepByStep, 'predict', predict)

First, we set the model to evaluation mode, as it is required in order to make

predictions. Then, we convert the x argument (assumed to be a Numpy array) to a

float PyTorch tensor, send it to the configured device, and use the model to make a

prediction.

Next, we set the model back to training mode. The last step includes detaching the

tensor containing the predictions and making it a Numpy array to be returned to

the user.

We have already covered most of what was developed in the previous chapters,

except for a couple of visualization functions. Let’s tackle them now.

Visualization Methods

Since we have kept track of both training and validation losses as attributes, let’s

build a simple plot for them:

192 | Chapter 2.1: Going Classy

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

Saved successfully!

Ooh no, something went wrong!