Size mismatch error

Hi, all.

I’m trying to built a Linear model with this parameters:

CONST_DE_NORMALIZACAO = 255.0

descritores = torch.Tensor([[247, 248, 244], [246, 245, 243],[194, 189, 186], [167, 159, 153],[142, 131, 123],[126, 114, 106], [102, 88, 79], [96, 82, 74],[89, 75, 66], [88, 74, 65]])

labels = [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]] 

# Normalização
treino_x = descritores / CONST_DE_NORMALIZACAO
treino_y = Variable(torch.Tensor(labels))

class Model(torch.nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear = torch.nn.Linear(10, 1)

    def forward(self, x):
        y_predict = self.linear(x)
        return y_predict

but, when I call this part of my code:

y_predito = modelo(treino_x)

I’m receiving this error message:

return torch.addmm(bias, input, weight.t())
RuntimeError: size mismatch, m1: [10 x 3], m2: [10 x 1] at …

Hi,

You can use “```” to format your code properly here, that makes it easier to read.
Also, you should use the latest version of pytorch and so Variables are not needed anymore: they have been merged with Tensors.

Your tensor treino_x is of size 10x3 so represent a batch size of 10 and feature size of 3.
Your Linear(10, 1) layer will convert a feature size of 10 to a feature size of 1.
So there is a size mismatch where a linear layer expects a tensor with feature size 10 and you give it one of size 3.

Hi, alanD.

Thank you for your support. I changed my code and now it’s ok.