Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the MSE of a tensor across a specific dimension?

I have 2 tensors with .size of torch.Size([2272, 161]). I want to get mean-squared-error between them. However, I want it along each of the 161 channels, so that my error tensor has a .size of torch.Size([161]). How can I accomplish this?

It seems that torch.nn.MSELoss doesn't let me specify a dimension.

like image 614
Shamoon Avatar asked Apr 01 '20 17:04

Shamoon


People also ask

How is PyTorch MSE calculated?

Mean squared error is computed as the mean of the squared differences between the input and target (predicted and actual) values. To compute the mean squared error in PyTorch, we apply the MSELoss() function provided by the torch. nn module. It creates a criterion that measures the mean squared error.

What is MSE loss in PyTorch?

Mse stands for mean square error which is the most commonly used loss function for regression. The loss is the mean supervised data square difference between true and predicted values. PyTorch MSELoss is a process that measures the average of the square difference between actual value and predicted value.

What is MSELoss?

Mean squared error (MSE) is the most commonly used loss function for regression. The loss is the mean overseen data of the squared differences between true and predicted values, or writing it as a formula.


1 Answers

For the nn.MSELoss you can specify the option reduction='none'. This then gives you back the squared error for each entry position of both of your tensors. Then you can apply torch.sum/torch.mean.

a = torch.randn(2272,161)
b = torch.randn(2272,161)
loss = nn.MSELoss(reduction='none')
loss_result = torch.sum(loss(a,b),dim=0) 

I don't think there is a direct way to specify at the initialisation of the loss to which dimension to apply mean/sum. Hope that helps!

like image 154
Niklas Höpner Avatar answered Oct 14 '22 01:10

Niklas Höpner