Mean Square Error Loss Function

The Mean Squared Error is a loss function typically used in regression problems to adjust regression parameters by providing the average squared difference between the dependent variable and the predicted variable. Like many other loss functions, it provides the how far off the predicted values are from the dependent values given the choice of estimated parameters.

Mathematically, it is represented by: $$l(y, \hat{y} ) = \frac {1}{n} \sum_{i=1}^{n} (y - \hat{y})^2 $$

$y: target \ output$
$\hat{y}: predicted \ output$

In the example below, we use the torch library to compute the MSE between some generated dependent and predicted values

import torch 

# initializing mean square error 
mean_square_error = torch.nn.MSELoss() 

target_output = torch.randn(1, 10)
predicted_output = torch.randn(1, 10, requires_grad=True)

loss = mean_square_error(predicted_output, target_output)

The output is:

tensor(2.5247, grad_fn=<MseLossBackward>)