Select and MSELoss for two torch tensors
I have two torch tensors:
predictions = tensor([[33, 34,  7,  5,  5, 23, 22,  1,  3,  5, 23,  1],
        [14,  1, 22,  7,  5, 11,  7, 33,  3, 12, 25, 22],
        [33,  1, 14, 12, 23, 22, 12,  2,  3, 12, 23, 14],
        [23, 34, 34,  3,  5, 25, 12, 11,  2, 23, 23, 13]])

labels = tensor([[-100, -100, -100, -100, -100,   11, -100, -100, -100, -100, -100, -100],
        [-100, -100, -100, -100, -100, -100, -100, -100,   40, -100, -100, -100],
        [-100,   42, -100,   43, -100, -100, -100, -100, -100, -100, -100, -100],
        [-100, -100, -100, -100, -100, -100, -100, -100, -100, -100,   32, -100]])

I'd like to calculate MSELoss(predictions, labels) only for values not equal to -100 - but don't know how to select corresponding values. For this example the result should be calculated for following values:
[23] and [11]
[3]  and [40]
[1,12] and [42, 43]
[23]  and [32]

I trying like this:
nn.MSELoss(preds.squeeze(), labels.squeeze())

but receive error.

In Short
# convert to float
predictions = predictions.to(torch.float)
labels = labels.to(torch.float)

# pick the right entries
reduced_predictions = predictions[labels != -100]
reduced_labels = labels[labels != -100]

# initialaize loss_fn
torch.nn.MSELoss()(reduced_predictions, reduced_labels)
# Note          ^     calling the nn.MSELoss() and passing tensors in additional ()

In Detail
First you need to filter the undesired entries, you can do so as follows:
import torch

predictions = torch.tensor([[33, 34,  7,  5,  5, 23, 22,  1,  3,  5, 23,  1],
        [14,  1, 22,  7,  5, 11,  7, 33,  3, 12, 25, 22],
        [33,  1, 14, 12, 23, 22, 12,  2,  3, 12, 23, 14],
        [23, 34, 34,  3,  5, 25, 12, 11,  2, 23, 23, 13]])

labels = torch.tensor([[-100, -100, -100, -100, -100,   11, -100, -100, -100, -100, -100, -100],
        [-100, -100, -100, -100, -100, -100, -100, -100,   40, -100, -100, -100],
        [-100,   42, -100,   43, -100, -100, -100, -100, -100, -100, -100, -100],
        [-100, -100, -100, -100, -100, -100, -100, -100, -100, -100,   32, -100]])

# find the entries different than -100
indices = labels != -100

# pick the corresponding values from predictions and labels
reduced_predictions = predictions[indices]
reduced_labels = labels[indices].to(torch.float)

The nn.MSELoss() receives floats so you must convert your tensors to a suitable dtype.
Then, you have to instantiate the loss function (create an instance of MSELoss by calling it's initiator):
loss_fn = torch.nn.MSELoss()

And only then call it using your tensors:
loss_fn(reduced_predictions, reduced_labels)

The result I received: tensor(847.2000)
