Mat1 and mat2 shapes cannot be multiplied, torch on audio!

I have the following code,

import torch.nn as nn

class MaqamCNN(nn.Module):
    def __init__(self):
        super(MaqamCNN, self).__init__()
        self.conv1 = nn.Conv2d(1, 16, kernel_size=(5, 1), stride=(1, 1), padding=(2, 0))
        self.bn1 = nn.BatchNorm2d(16)
        self.relu1 = nn.ReLU()
        self.pool1 = nn.MaxPool2d(kernel_size=(4, 1), stride=(4, 1))
        self.conv2 = nn.Conv2d(16, 32, kernel_size=(5, 1), stride=(1, 1), padding=(2, 0))
        self.bn2 = nn.BatchNorm2d(32)
        self.relu2 = nn.ReLU()
        self.pool2 = nn.MaxPool2d(kernel_size=(4, 1), stride=(4, 1))
        self.fc1 = nn.Linear(32 * 5 * 20, 128)
        self.bn3 = nn.BatchNorm1d(128)
        self.relu3 = nn.ReLU()
        self.dropout = nn.Dropout(p=0.5)
        self.fc2 = nn.Linear(128, 8)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu1(x)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self.bn2(x)
        x = self.relu2(x)
        x = self.pool2(x)
        print(x.shape)
        x = x.view(-1, 16*90000*1)
        x = self.fc1(x)
        x = self.bn3(x)
        x = self.relu3(x)
        x = self.dropout(x)
        x = self.fc2(x)
        return x

And the error I get is:

C:\Users\omari\anaconda3\lib\site-packages\torch\nn\functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject 
to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  ..\c10/core/TensorImpl.h:1156.)
  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
torch.Size([16, 32, 90000, 1])
Traceback (most recent call last):
  File "C:\Users\omari\Documents\GitHub\Classifying_Maqams\model3\train.py", line 29, in <module>
    outputs = model(inputs)
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\omari\Documents\GitHub\Classifying_Maqams\model3\model.py", line 31, in forward
    x = self.fc1(x)
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\nn\modules\linear.py", line 96, in forward
    return F.linear(input, self.weight, self.bias)
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\nn\functional.py", line 1847, in linear
    return torch._C._nn.linear(input, weight, bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x1440000 and 3200x128)

previously I edited the x.view size to be according to the shape of x, but now I don’t know how to solve this error<<
Any help?

the issue is with your fc layer
the shape of your fc layer’s weight is 3200 x 128, while the shape of your x which you passed onto it is 32x1440000
I suppose 32 is your batch size.
then, you need to have 1440000 x128 as the size of your fc layer

Thx! but now I get another error, the batch_size is actually 16 and not 32, the error currently is:

C:\Users\omari\anaconda3\lib\site-packages\torch\nn\functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject 
to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  ..\c10/core/TensorImpl.h:1156.)
  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
torch.Size([16, 32, 90000, 1])
Traceback (most recent call last):
  File "C:\Users\omari\Documents\GitHub\Classifying_Maqams\model3\train.py", line 30, in <module>
    loss = criterion(outputs, labels)
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\nn\modules\loss.py", line 1120, in forward
    return F.cross_entropy(input, target, weight=self.weight,
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\nn\functional.py", line 2824, in cross_entropy
    return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
ValueError: Expected input batch_size (32) to match target batch_size (16).

and even if I change the batch_size to 32 I get the same error but with different values:

C:\Users\omari\anaconda3\lib\site-packages\torch\nn\functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject 
to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  ..\c10/core/TensorImpl.h:1156.)
  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
torch.Size([32, 32, 90000, 1])
Traceback (most recent call last):
  File "C:\Users\omari\Documents\GitHub\Classifying_Maqams\model3\train.py", line 30, in <module>
    loss = criterion(outputs, labels)
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\nn\modules\loss.py", line 1120, in forward
    return F.cross_entropy(input, target, weight=self.weight,
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\nn\functional.py", line 2824, in cross_entropy
    return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
ValueError: Expected input batch_size (64) to match target batch_size (32).

oops. havent seen your print statement (batch size is16)
your next error says that loss function cant compare your outputs because your model predicts 32 batches while in your target you gave it 16 items
change this part
x = x.view(-1, 16900001)
to
x = x.view(x.size(0), -1)
also, you need to change your fc layer as needed
when you put -1 in the first dimension of view, it means that next shapes should be correctly specified. otherwise it may break the batch dimension

That works! Thx, another question that I have, when I ran it, I printed different statements in the loop in the training Python file as you can see

import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
import dataset
import model

# Define hyperparameters
batch_size = 4
learning_rate = 0.001
num_epochs = 10

# Load the dataset
train_dataset = dataset.MaqamDataset(mode='train')
train_loader = data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)

# Define the model
model = model.MaqamCNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# Train the model
for epoch in range(num_epochs):
    print("________________+++++++++++++++++++++++++++++++++++",epoch,"++++++++++++++++++++++______________________")
    running_loss = 0.0
    for i, data in enumerate(train_loader, 0):
        print("HERE1 ", i)
        inputs, labels = data
        print(inputs.shape)
        print("HERE2 ", i)
        inputs = inputs.unsqueeze(1).unsqueeze(3)
        print("HERE3 ", i)
        optimizer.zero_grad()
        print("HERE4 ", i)
        outputs = model(inputs)
        print("Outputs shaepe = ",outputs.shape)
        print("HERE5 ", i)
        loss = criterion(outputs, labels)
        print("HERE6 ", i)
        loss.backward()
        print("HERE7 ", i)
        optimizer.step()
        print("HERE8 ", i)
        running_loss += loss.item()
        print("HERE9 ", i)
    print('Epoch %d, loss: %.3f' % (epoch + 1, running_loss / len(train_loader)))

# Save the model
torch.save(model.state_dict(), 'maqam_cnn.pth')

The thing that I ran the code twice, every time in another level into the loop, it gives me the following error:

Traceback (most recent call last):
  File "C:\Users\omari\Documents\GitHub\Classifying_Maqams\model3\train.py", line 25, in <module>
    for i, data in enumerate(train_loader, 0):
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 521, in __next__
    data = self._next_data()
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 561, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\utils\data\_utils\fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\utils\data\_utils\collate.py", line 84, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\utils\data\_utils\collate.py", line 84, in <listcomp>
    return [default_collate(samples) for samples in transposed]
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\utils\data\_utils\collate.py", line 56, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: stack expects each tensor to be equal size, but got [1440000] at entry 0 and [1439994] at entry 3

can you help me with this?

this issue is when your batch has different shapes.
for example, you have x and y in a batch now, x[0] has 1440000 numbers, x[1] is the same, x[2] as well, but x[3] has another shape than others.
if your dataset is of different length, you need to pad/trim it, or, if you want to load the items dynamically, your tensors should all be in equal length in a batch.

I’ve tried to pad the dataset using collate_fn as described in the following link
But, every time I try to run the train.py, at a different level I get the following error, I’ve also tried another option of padding but still have the same error.
The Error:

Traceback (most recent call last):
  File "C:\Users\omari\Documents\GitHub\Classifying_Maqams\model3\train.py", line 38, in <module>
    for i, data in enumerate(train_loader, 0):
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 521, in __next__
    data = self._next_data()
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 561, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\utils\data\_utils\fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\utils\data\_utils\collate.py", line 84, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\utils\data\_utils\collate.py", line 84, in <listcomp>
    return [default_collate(samples) for samples in transposed]
  File "C:\Users\omari\anaconda3\lib\site-packages\torch\utils\data\_utils\collate.py", line 56, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: stack expects each tensor to be equal size, but got [1439994] at entry 0 and [1440000] at entry 1

can you help me with this?
Here is the code of the model.py in case it helps:

import torch.nn as nn

class MaqamCNN(nn.Module):
    def __init__(self):
        super(MaqamCNN, self).__init__()
        self.conv1 = nn.Conv2d(1, 16, kernel_size=(5, 1), stride=(1, 1), padding=(2, 0))
        self.bn1 = nn.BatchNorm2d(16)
        self.relu1 = nn.ReLU()
        self.pool1 = nn.MaxPool2d(kernel_size=(4, 1), stride=(4, 1))
        self.conv2 = nn.Conv2d(16, 32, kernel_size=(5, 1), stride=(1, 1), padding=(2, 0))
        self.bn2 = nn.BatchNorm2d(32)
        self.relu2 = nn.ReLU()
        self.pool2 = nn.MaxPool2d(kernel_size=(4, 1), stride=(4, 1))
        self.fc1 = nn.Linear(32*90000, 128)
        self.bn3 = nn.BatchNorm1d(128)
        self.relu3 = nn.ReLU()
        self.dropout = nn.Dropout(p=0.5)
        self.fc2 = nn.Linear(128, 8)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu1(x)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self.bn2(x)
        x = self.relu2(x)
        x = self.pool2(x)
        print(x.shape)
        batch_size = x.size(0)
        x = x.view(batch_size, -1)
        x = self.fc1(x)
        x = self.bn3(x)
        x = self.relu3(x)
        x = self.dropout(x)
        x = self.fc2(x)
        print("x shape = ", x.shape)
        return x