0
$\begingroup$

I am getting the following error:

/usr/localnfs/Compiler/python/Anaconda3-2019.07/lib/python3.7/site-packages/torch/nn/modules/loss.py:431: 
UserWarning: Using a target size (torch.Size([30, 1])) that is different to the input size (torch.Size([30, 10])).

This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.

I think I know why this is happening but I don't know how to fix it. I am using the loss function torch.nn.MSELoss(), with no inputs. My model is the multilayer perceptron, and this is how I'm doing it:

class MLP(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(MLP, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.output_size = output_size

        self.fc1 = nn.Linear(self.input_size, self.hidden_size)
        self.fc2 = nn.Linear(self.hidden_size, self.output_size)

    def forward(self, features):
        out = self.fc1(features)
        out = self.fc2(out)
        return out

My input size is 100, my hidden size is 50, and my output size is 10, because my dataset has 10 columns with 30 rows (the size of my mini batch). The size of my labels is 30x1.

 predict_label = mlp_model(features.float())
 loss_batch = loss_func(input=predict_label, target=label.float())

I am somehow supposed to predict labels for my 30x10 dataset, and compare them to my 30x1 label vector, and I don't know how to do that. The error shows up when I execute the loss_batch line. Please help, I'm very new to this.

$\endgroup$
2
  • $\begingroup$ how are you initiating the MLP? It is a problem of the number of input size $\endgroup$ – Carlos Mougan Mar 18 '20 at 11:10
  • 1
    $\begingroup$ How can your output size be 10 if your labels are single dimensional? $\endgroup$ – noe Mar 18 '20 at 11:46
1
$\begingroup$

You have stated that:

  • Batch size: 30
  • Input dimensionality: 10
  • Label dimensionality: 1

With this in mind, the output dimensionality of the MLP must be the same dimensionality as the labels, which is 1. Therefore, the output size of the MLP cannot be 10, but 1.

Apart from that, please note that concatenating 2 linear transformations (without any non-linearity in between) is equivalent to a single linear layer. Your MLP should have a non-linearity (e.g. ReLU) applied after fc1 and before fc2.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.