Search notes:

torch.nn.MSELoss

torch.nn.MSELoss creates a criterion that calculates the mean squared error between a set of predictions and target (ground-truth) values.
The following example calculates the MSE without and with torch.nn.MSELoss:
prd = [1.2, 2.5, 3.8, 4.1] # Predicted values
tgt = [1.0, 2.0, 4.0, 4.5] # Ground-truth (i. e. target) values

# Calculate the squares of the pairwise differences between predicted and target values:
squared_diffs = [  (p-t)**2  for  p, t in zip(prd, tgt)]

# Sum the the squares and divide result by the number of squares:
mse = sum(squared_diffs) / len(squared_diffs)

print(f"Mean Squared Error (no library):    {mse:.4f}")

#
#    Same thing, but using PyTorch
#

import torch

criterion = torch.nn.MSELoss()

loss = criterion(
           torch.tensor(prd),
           torch.tensor(tgt)
        )

print(f"Mean squared Error (using PyTorch): {loss:.4f}")

Index