'RuntimeError: mat1 and mat2 shapes cannot be multiplied', how do I solve it?
I'm trying to implement a ResNet1D, that should take as input a window of ECG signal ,containing a single heart beat, in my case with size 950 samples, and I want to predict the length of the QRS interval.
Here's the code for the network implementation:
class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, in_planes, planes, stride=1):
        super(Bottleneck, self).__init__()

        self.conv1 = nn.Conv1d(in_planes, planes, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm1d(planes)

        self.conv2 = nn.Conv1d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm1d(planes)

        self.conv3 = nn.Conv1d(planes,    self.expansion*planes, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm1d(self.expansion*planes)

        self.shortcut = nn.Sequential()
        if stride != 1 or in_planes != self.expansion*planes:
            self.shortcut = nn.Sequential(
                nn.Conv1d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm1d(self.expansion*planes)
                )


    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = F.relu(self.bn2(self.conv2(out)))
        out = self.bn3(self.conv3(out))
        out += self.shortcut(x)
        out = F.relu(out)
        return out

class ResNet(nn.Module):
    def __init__(self, block, num_blocks, num_classes=3):
        super(ResNet, self).__init__()
        self.in_planes = 64
    
        self.avg1 = nn.AvgPool1d(1024, stride=2)
        self.conv1 = nn.Conv1d(1, 128, kernel_size=3, stride=1, padding=1, bias=False)
        self.bn1 = nn.BatchNorm1d(128)

        self.layer1 = self._make_layer(block, 128, num_blocks[0], stride=1)
        self.layer2 = self._make_layer(block, 256, num_blocks[1], stride=2)
        self.layer3 = self._make_layer(block, 512, num_blocks[2], stride=2)
        self.layer4 = self._make_layer(block, 1024, num_blocks[3], stride=2)
        self.linear1 = nn.Linear(19968*block.expansion, 1024)
        self.linear2 = nn.Linear(1024, num_classes)


    def _make_layer(self, block, planes, num_blocks, stride):
        strides = [stride] + [1]*(num_blocks-1)
        layers = []
        for stride in strides:
            layers.append(block(self.in_planes, planes, stride))
            self.in_planes = planes * block.expansion

        return nn.Sequential(*layers)


    def forward(self, x):
        out = self.avg1(x)
        out = F.rel(self.bn1(self.conv1(out)))

        out = self.layer1(out)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.layer4(out)

        out = F.avg_pool1d(out, 16)
        out = out.view(out.size(0), -1)
        out = self.linear1(out)
        out = self.linear2(out)

        return out

    def ResNet50():
        return ResNet(Bottleneck, [3, 4, 6, 3], num_classes=1)

The input that I'm feeding to the network is a dataloader with batch size = 32, number of channels = 1 and sample length = 950.
When training the network I get this error:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x3584 and 19968x1024)

I get that the error is in the Linear layer, but I don't understand how I should change the dimensions in order to make it work. Can you please explain this to me?

Matrices are multiplicable if the number of columns of the first matrix matches the number of rows of the second one. For instance, MxN * NxK. So here you have wrong shapes. You can always calculate the sizes of the outputs of each layer by yourself to make sure if your shapes are correct.
So here I think it should work if you change this

self.linear1 = nn.Linear(19968*block.expansion, 1024)

to this

self.linear1 = nn.Linear(3584*block.expansion, 1024)

