# PyTorch MultiLayer Perceptron Classification Size of Features vs Labels Wrong

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.

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

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.