Loss
Learn about how loss can be computed using loss functions and how the loss tensor can be converted to a Numpy array.
We'll cover the following...
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 functionloss_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, it returns another function, which we called loss_fn
. That is the actual loss function. So, we can pass a prediction and a label to it, and get the corresponding loss value:
Press + to interact
import torchimport torch.nn as nn# Defines a MSE loss functionloss_fn = nn.MSELoss(reduction='mean')# This is a random example to illustrate the loss functionpredictions = torch.tensor([0.5, 1.0])labels = torch.tensor([2.0, 1.3])print(loss_fn(predictions, labels))
...
Moreover, you ...