Expected 3-dimensional input for 3-dimensional weight [64, 3, 7], but got 2-dimensional input of size [317829, 3] instead

Hi I am trying to apply a Resnet work model to my data.
But i have the run time error Expected 3-dimensional input for 3-dimensional weight [64, 3, 7], but got 2-dimensional input of size [317829, 3] instead.
What should i do now?

Your code is quite hard to read as it’s not formatted, so please wrap it into three backticks ``` which would make debugging easier. :wink:

Based on the error message the shape mismatch is most likely raised in the first nn.Conv1d layer, which expects an input in the shape [batch_size, channels, seq_len] while you are passing a 2D input to it.

class BasicBlock7x7(nn.Module):
expansion = 1

def __init__(self, inplanes7, planes, stride=1, downsample=None):
    super(BasicBlock7x7, self).__init__()
    self.conv1 = conv7x7(inplanes7, planes, stride)
    self.bn1 = nn.BatchNorm1d(planes)
    self.relu = nn.ReLU(inplace=True)
    self.conv2 = conv7x7(planes, planes)
    self.bn2 = nn.BatchNorm1d(planes)
    self.downsample = downsample
    self.stride = stride

def forward(self, x):
    residual = x

    out = self.conv1(x)
    out = self.bn1(out)
    out = self.relu(out)

    out = self.conv2(out)
    out = self.bn2(out)

    if self.downsample is not None:
        residual = self.downsample(x)

    d = residual.shape[2] - out.shape[2]
    out1 = residual[:, :, 0:-d] + out
    out1 = self.relu(out1)
    # out += residual

    return out1

class BasicBlock9x9(nn.Module):
expansion = 1

def __init__(self, inplanes9, planes, stride=1, downsample=None):
    super(BasicBlock9x9, self).__init__()
    self.conv1 = conv9x9(inplanes9, planes, stride)
    self.bn1 = nn.BatchNorm1d(planes)
    self.relu = nn.ReLU(inplace=True)
    self.conv2 = conv9x9(planes, planes)
    self.bn2 = nn.BatchNorm1d(planes)
    self.downsample = downsample
    self.stride = stride

def forward(self, x):
    residual = x

    out = self.conv1(x)
    out = self.bn1(out)
    out = self.relu(out)

    out = self.conv2(out)
    out = self.bn2(out)

    if self.downsample is not None:
        residual = self.downsample(x)

    d = residual.shape[2] - out.shape[2]
    out1 = residual[:, :, 0:-d] + out
    out1 = self.relu(out1)
    # out += residual

    return out1

class MSResNet(nn.Module):
def init(self, input_channel, layers=[1, 1, 1, 1], num_classes=10):
self.inplanes3 = 64
self.inplanes5 = 64
self.inplanes7 = 64
self.inplanes9 = 64

    super(MSResNet, self).__init__()

    self.conv1 = nn.Conv1d(input_channel, 64, kernel_size=7, stride=2, padding=3,
                           bias=False)
    self.bn1 = nn.BatchNorm1d(64)
    self.relu = nn.ReLU(inplace=True)
    self.maxpool = nn.MaxPool1d(kernel_size=3, stride=2, padding=1)

    self.layer3x3_1 = self._make_layer3(BasicBlock3x3, 64, layers[0], stride=2)
    self.layer3x3_2 = self._make_layer3(BasicBlock3x3, 128, layers[1], stride=2)
    self.layer3x3_3 = self._make_layer3(BasicBlock3x3, 256, layers[2], stride=2)
    self.layer3x3_4 = self._make_layer3(BasicBlock3x3, 512, layers[3], stride=2)
    self.layer3x3_5 = self._make_layer3(BasicBlock3x3, 1024, layers[3], stride=2)

    # maxplooing kernel size: 16, 11, 6
    self.maxpool3 = nn.AvgPool1d(kernel_size=1, stride=1, padding=0)


    self.layer5x5_1 = self._make_layer5(BasicBlock5x5, 64, layers[0], stride=2)
    self.layer5x5_2 = self._make_layer5(BasicBlock5x5, 128, layers[1], stride=2)
    self.layer5x5_3 = self._make_layer5(BasicBlock5x5, 256, layers[2], stride=2)
    self.layer5x5_4 = self._make_layer5(BasicBlock5x5, 512, layers[3], stride=2)
    self.maxpool5 = nn.AvgPool1d(kernel_size=3, stride=1, padding=0)


    self.layer7x7_1 = self._make_layer7(BasicBlock7x7, 64, layers[0], stride=2)
    self.layer7x7_2 = self._make_layer7(BasicBlock7x7, 128, layers[1], stride=2)
    self.layer7x7_3 = self._make_layer7(BasicBlock7x7, 256, layers[2], stride=2)
    self.layer7x7_4 = self._make_layer7(BasicBlock7x7, 512, layers[3], stride=2)
    self.maxpool7 = nn.AvgPool1d(kernel_size=3, stride=1, padding=0)

    self.layer9x9_1 = self._make_layer9(BasicBlock9x9, 64, layers[0], stride=2)
    self.layer9x9_2 = self._make_layer9(BasicBlock9x9, 128, layers[1], stride=2)
    self.layer9x9_3 = self._make_layer9(BasicBlock9x9, 256, layers[2], stride=2)
    self.layer9x9_4 = self._make_layer9(BasicBlock9x9, 512, layers[3], stride=2)
    self.maxpool9 = nn.AvgPool1d(kernel_size=3, stride=1, padding=0)

    # self.drop = nn.Dropout(p=0.2)
    
    self.fc = nn.Linear(4*256+1024, num_classes)
    
    # todo: modify the initialization
    # for m in self.modules():
    #     if isinstance(m, nn.Conv1d):
    #         n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
    #         m.weight.data.normal_(0, math.sqrt(2. / n))
    #     elif isinstance(m, nn.BatchNorm1d):
    #         m.weight.data.fill_(1)
    #         m.bias.data.zero_()

def _make_layer3(self, block, planes, blocks, stride=2):
    downsample = None
    if stride != 1 or self.inplanes3 != planes * block.expansion:
        downsample = nn.Sequential(
            nn.Conv1d(self.inplanes3, planes * block.expansion,
                      kernel_size=1, stride=stride, bias=False),
            nn.BatchNorm1d(planes * block.expansion),
        )

    layers = []
    layers.append(block(self.inplanes3, planes, stride, downsample))
    self.inplanes3 = planes * block.expansion
    for i in range(1, blocks):
        layers.append(block(self.inplanes3, planes))

    return nn.Sequential(*layers)

def _make_layer5(self, block, planes, blocks, stride=2):
    downsample = None
    if stride != 1 or self.inplanes5 != planes * block.expansion:
        downsample = nn.Sequential(
            nn.Conv1d(self.inplanes5, planes * block.expansion,
                      kernel_size=1, stride=stride, bias=False),
            nn.BatchNorm1d(planes * block.expansion),
        )

    layers = []
    layers.append(block(self.inplanes5, planes, stride, downsample))
    self.inplanes5 = planes * block.expansion
    for i in range(1, blocks):
        layers.append(block(self.inplanes5, planes))

    return nn.Sequential(*layers)


def _make_layer7(self, block, planes, blocks, stride=2):
    downsample = None
    if stride != 1 or self.inplanes7 != planes * block.expansion:
        downsample = nn.Sequential(
            nn.Conv1d(self.inplanes7, planes * block.expansion,
                      kernel_size=1, stride=stride, bias=False),
            nn.BatchNorm1d(planes * block.expansion),
        )

    layers = []
    layers.append(block(self.inplanes7, planes, stride, downsample))
    self.inplanes7 = planes * block.expansion
    for i in range(1, blocks):
        layers.append(block(self.inplanes7, planes))

    return nn.Sequential(*layers)

def _make_layer9(self, block, planes, blocks, stride=2):
    downsample = None
    if stride != 1 or self.inplanes9 != planes * block.expansion:
        downsample = nn.Sequential(
            nn.Conv1d(self.inplanes9, planes * block.expansion,
                      kernel_size=1, stride=stride, bias=False),
            nn.BatchNorm1d(planes * block.expansion),
        )

    layers = []
    layers.append(block(self.inplanes9, planes, stride, downsample))
    self.inplanes9 = planes * block.expansion
    for i in range(1, blocks):
        layers.append(block(self.inplanes9, planes))

    return nn.Sequential(*layers)
def forward(self, x0):
    x0 = self.conv1(x0)
    x0 = self.bn1(x0)
    x0 = self.relu(x0)
    x0 = self.maxpool(x0)

    x = self.layer3x3_1(x0)
    x = self.layer3x3_2(x)
    x = self.layer3x3_3(x)
    x = self.layer3x3_4(x)
    x = self.layer3x3_5(x)           
    x = self.maxpool3(x) 

    y = self.layer5x5_1(x0)
    y = self.layer5x5_2(y)
    y = self.layer5x5_3(y)
    y = self.layer5x5_4(y)
    y = self.maxpool5(y)

    z = self.layer7x7_1(x0)
    z = self.layer7x7_2(z)
    z = self.layer7x7_3(z)
    z = self.maxpool7(z)

    xx = self.layer9x9_1(x0)
    xx = self.layer9x9_2(xx)
    xx = self.layer9x9_3(xx)
    xx = self.maxpool9(xx)

    out = torch.cat([x, y, z, xx], dim=1)

    out = out.squeeze()
    # out = self.drop(out)
    out1 = self.fc(out)
    
    return out1, out

i have reformatted the code, if it possible, please help me debug the code.
Thanks

Unfortunately, you haven’t properly formatted the code and even after copying and fixing the code locally, the code is still not executable since BasicBlock3x3 is undefined.
As previously described, check the input shape and make sure it’s a 3D tensor with the expected shapes.

it is just right here, first convolution
seems like you’re passing single tensor shaped [317829, 3] but you need to pass 3 dimensional tensor where dim 0 contains batch size of elements. If you want to pass single element, unsqueeze(0) tensor you’re passing to model to add dim 0. Also, if your tensor is correct, you’ll need to transpose it before unsqueeze.

Thanks for your time. I have solved the probelm.