Loss

Learn about how loss can be computed using loss functions and how the loss tensor can be converted to a Numpy array.

Introduction to loss functions

We will now tackle the loss computation process. As expected, PyTorch got us covered once again. There are many loss functions to choose from depending on the task at hand. Since ours is a regression, we are using the Mean Squared Error (MSE) as our loss, and thus we need PyTorch’s nn.MSELoss:

Press + to interact
import torch.nn as nn
# Defines a MSE loss function
loss_fn = nn.MSELoss(reduction='mean')
print(loss_fn)

Notice that nn.MSELoss is not the loss function itself. We do not pass predictions and labels to it! Instead, as you can see, ...

Access this course and 1400+ top-rated courses and projects.