Search code examples
pythonmathmatrixpytorchmatrix-multiplication

PYTORCH - TENSORS issue - Mat1 and mat2 shapes cannot be multiplied (8x10 and 8x8)


[I know this question comes a lot, but I could not find any answer matching my use case]

[edit: please answer this question if you know about Pytorch, I know about matrix multiplication, this is not the issue here]

I feed N = 10 inputs made out of 8 floats to a 8 input layer

but I get that error

Mat1 and mat2 shapes cannot be multiplied (8x10 and 8x8) 

What am I missing ?

I generate random data in my dataset

class MyDataset(Dataset):
    def __init__(self):
        self.data = []
        self.input_size = 0
        for i in range(0,10):
            label = random.randint(0, 1)
            data = [random.uniform(0.0, 1.0) for _ in range(8)]
            self.data.append((data , label))
            self.input_size = len(data ) if self.input_size < len(encoded_text) else self.input_size
  
    def __len__(self):
        return len(self.data)
    
    def __getitem__(self, idx):
        text, label = self.data[idx]
        return text, label

This is my model

class MyModel(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(MyModel, self).__init__()
        self.input = nn.Linear(input_size,hidden_size)
        self.hidden = nn.Linear(hidden_size, output_size)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        # padding is there as original dataset does not have full 8 floats inputs
        x_padded = pad_sequence(x, batch_first=True, padding_value=0).float()  
        output = self.input(x_padded) >>>>>>>> ERROR
        return torch.sigmoid(output)

def train_model(model, train_loader, criterion, optimizer, num_epochs):
    for epoch in range(num_epochs):
        for inputs, labels in train_loader:
            outputs = model(inputs)

The initialisation part

if __name__ == "__main__":
    hidden_size = 8  # hidden size 
    output_size = 1  # binary classification 
    learning_rate = 0.001
    num_epochs = 10
    
    dataset = MyDataset()
    train_loader = DataLoader(dataset, batch_size=64, shuffle=True)
    
    input_size = dataset.input_size
    
    model = MyModel(input_size, hidden_size, output_size)
    criterion = nn.BCEWithLogitsLoss()
    optimizer = optim.Adam(model.parameters(), lr=learning_rate)
    train_model(model, train_loader, criterion, optimizer, num_epochs)`

This is a really simple beginner use case, nothing fancy

thanks for your help

[edit: the x input is made out of 8 tensors of 10 values but it should be 10 tensors of 8 values ]

enter image description here

enter image description here


Solution

  • problem was this line :

    x_flattened = x_padded.view(x_padded.size(0), -1) 
    

    had to use this :

    x_flattened = x_padded.view(x_padded.size(1), -1)