Charles Yan
Charles Yan

Reputation: 85

nn.MSELoss inputs dimension mismatch problem

I recently started learning how to use torch but when I was dealing with the sequential data. For example, the training x (input) is a tensor with shape ([1, 60, 278]), while the training y (target) has the shape ([1, 278]). The batch size is simply 1. Can I directly use the code below to calculate MSE?

criterion = nn.MSELoss()
mse = criterion(input, target)

I have no idea why sometimes it returned no error but sometimes it returned error: Please ensure they have the same size. return F.mse_loss(input, target, reduction=self.reduction)

Do I need to duplicate target so that the dimension of target can match input? I also found there are a few parameters in the function, say 'reduction', 'size_average', 'reduce'. Would they be helpful in dealing with this problem?

Upvotes: 0

Views: 721

Answers (1)

Valentin Goldité
Valentin Goldité

Reputation: 1229

The function MSELoss is defined by MSELoss = lambda x, y: torch.mean((x - y)**2) (with the reduction mean that is the default behavior). Here you are trying to apply the minus operator - on two tensors of different shapes. A priori it is prohibited BUT pytorch is convenient and will tried to change the tensors to have compatible shapes. Here he sees that the 1rst and 3rd dimension axis of input are the same as the axis of target but the second axis doesn't exist in the later tensor. In this case, pytorch will repeat 60 times the rows of the targets to match the good shape shape(1, 60, 278) before applying the function. But it may not be what you need!

In your case you probably need to compute a single-vector representation of your sequence using a model in order to have a predictions of shape shape(1, 278) then you can compute your criterion: criterion(model(inputs), target) and avoid duplication of rows.

Upvotes: 1

Related Questions