Size mismatch, m1: [2048 x 1], m2: [2048 x 1]

i am a new hand in this item, met a problem which is hard for me to solve…i need someone to help me
code below.:

class CNN(nn.Module):
def init(self,size):

    super(CNN, self).__init__()
    resnet = models.resnet152(pretrained=False)
    modules = list(resnet.children())[:-1]      # delete the last fc layer.
    self.resnet = nn.Sequential(*modules)
    self.linear = nn.Linear(resnet.fc.in_features, size)
   
            
def forward(self, images):
    """Extract feature vectors from input images."""
    
    with torch.no_grad():
        features = self.resnet(images)

    features = self.linear(features)
    
  
    return features

class NN(nn.Module):
def init(self, in_dim, n_hidden1, n_hidden2, out_dim):
super(NN, self).init()
self.layer1 = nn.Linear(in_dim, n_hidden1)
self.layer2 = nn.Linear(n_hidden1, n_hidden2)
self.layer3 = nn.Linear(n_hidden2, out_dim)

def forward(self, x):
    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)

device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)

size = 1
in_dim, n_hidden1, n_hidden2, out_dim = 20, 300, 100, 2
learning_rate = 0.001

encoder = CNN(size).cuda()
decoder = NN(in_dim, n_hidden1, n_hidden2, out_dim).cuda()

criterion = nn.MSELoss()
params = list(decoder.parameters()) + list(encoder.parameters())
optimizer = torch.optim.Adam(params, lr=learning_rate)
for epoch in range (10):
for img, rst, character in train_dataloader:

    img=img.to(device)
    rst =torch.Tensor(rst)
    rst = rst.to(device)
    character = torch.Tensor(character)
    character = character.to(device)
    
    
    features = encoder(img)
    
    character = torch.cat((features, character),1)
    outputs = decoder(character)
    loss = criterion(outputs, rst)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

could you post the error message?

Traceback (most recent call last):

File “”, line 1, in
runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 668, in runfile
execfile(filename, namespace)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 108, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)

File “/home/ywr/.config/spyder-py3/云溪/train.py”, line 32, in
features = encoder(img)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 489, in call
result = self.forward(*input, **kwargs)

File “/home/ywr/.config/spyder-py3/云溪/model.py”, line 27, in forward
features = self.linear(features)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 489, in call
result = self.forward(*input, **kwargs)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/linear.py”, line 67, in forward
return F.linear(input, self.weight, self.bias)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py”, line 1354, in linear
output = input.matmul(weight.t())

RuntimeError: size mismatch, m1: [2048 x 1], m2: [2048 x 1] at /opt/conda/conda-bld/pytorch_1549633347309/work/aten/src/THC/generic/THCTensorMathBlas.cu:266

I think you need to use .view(1, 2048) on the output of the resnet. So it would look like this:

with torch.no_grad():
    features = self.resnet(images)
    features = features.view(1, 2048)

Before the fully connected layer, torch.flatten changes the dimensions to [2048, 1].

thanks a lot but occur another problem like this:
Traceback (most recent call last):

File “”, line 1, in
runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 668, in runfile
execfile(filename, namespace)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 108, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)

File “/home/ywr/.config/spyder-py3/云溪/train.py”, line 32, in
features = encoder(img)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 489, in call
result = self.forward(*input, **kwargs)

File “/home/ywr/.config/spyder-py3/云溪/model.py”, line 27, in forward
features = self.linear(features)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 489, in call
result = self.forward(*input, **kwargs)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/linear.py”, line 67, in forward
return F.linear(input, self.weight, self.bias)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py”, line 1354, in linear
output = input.matmul(weight.t())

RuntimeError: size mismatch, m1: [2048 x 1], m2: [2048 x 1] at /opt/conda/conda-bld/pytorch_1549633347309/work/aten/src/THC/generic/THCTensorMathBlas.cu:266

runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)
Reloaded modules: model, data_loader
Traceback (most recent call last):

File “”, line 1, in
runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 668, in runfile
execfile(filename, namespace)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 108, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)

File “/home/ywr/.config/spyder-py3/云溪/train.py”, line 34, in
character = torch.cat((features, character),1)

RuntimeError: invalid argument 0: Tensors must have same number of dimensions: got 1 and 2 at /opt/conda/conda-bld/pytorch_1549633347309/work/aten/src/THC/generic/THCTensorMath.cu:74

And,i met another wired problem .when i change the batch size , the bug is different .how could batch size influence the net,idk.here is my dataloader:
root= “./dataset/train_data/”
datatxt=“train_data1.txt”
class MyDataset(torch.utils.data.Dataset):
def init(self,root, datatxt, transform=None, target_transform=None):
super(MyDataset,self).init()
fh = open(root + datatxt, ‘r’)

    imgs = []
    rst  = []   
    character = []                   
    for line in fh:              
        line = line.rstrip() 
       
        words = line.split() 
        
        imgs.append(  (words[0]) )
        
        rst.append( (float(words[1]), float(words[2]) ))
        
        character.append((float(words[3]),float(words[4]),float(words[5]),float(words[6]),float(words[7]),float(words[8]),float(words[9]),float(words[10]),float(words[11]),float(words[12])))
                           
    self.imgs = imgs
    self.rst = rst
    self.character = character
    self.transform = transform
    self.target_transform = target_transform

def __getitem__(self, index):    
    fn = self.imgs[index] 
    rst = self.rst[index]
    character = self.character[index]
    img = Image.open(root+fn).convert('RGB') 

    if self.transform is not None:
        img = self.transform(img)
    return img, rst, character

def __len__(self): 
    return len(self.imgs)

train_data=MyDataset(root = root, datatxt=datatxt, transform=transforms.ToTensor())
train_dataloader = torch.utils.data.DataLoader(dataset=train_data, batch_size=4, shuffle=False)

transform ={
transforms.Compose([
transforms.CenterCrop((224,224)),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225))
])
}

thanks a lot but occur another problem like this:
Traceback (most recent call last):

File “”, line 1, in
runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 668, in runfile
execfile(filename, namespace)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 108, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)

File “/home/ywr/.config/spyder-py3/云溪/train.py”, line 32, in
features = encoder(img)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 489, in call
result = self.forward(*input, **kwargs)

File “/home/ywr/.config/spyder-py3/云溪/model.py”, line 27, in forward
features = self.linear(features)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 489, in call
result = self.forward(*input, **kwargs)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/linear.py”, line 67, in forward
return F.linear(input, self.weight, self.bias)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py”, line 1354, in linear
output = input.matmul(weight.t())

RuntimeError: size mismatch, m1: [2048 x 1], m2: [2048 x 1] at /opt/conda/conda-bld/pytorch_1549633347309/work/aten/src/THC/generic/THCTensorMathBlas.cu:266

runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)
Reloaded modules: model, data_loader
Traceback (most recent call last):

File “”, line 1, in
runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 668, in runfile
execfile(filename, namespace)

File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 108, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)

File “/home/ywr/.config/spyder-py3/云溪/train.py”, line 34, in
character = torch.cat((features, character),1)

RuntimeError: invalid argument 0: Tensors must have same number of dimensions: got 1 and 2 at /opt/conda/conda-bld/pytorch_1549633347309/work/aten/src/THC/generic/THCTensorMath.cu:74

can you print the features tensor’s dimensions right after self.resnet(images)?

i have changed my net structure like this:
class CNN(nn.Module):
def init(self,size):

    super(CNN, self).__init__()
    resnet = models.resnet152(pretrained=False)
    modules = list(resnet.children())[:-1]      # delete the last fc layer.
    self.resnet = nn.Sequential(*modules)
    self.linear = nn.Linear(resnet.fc.in_features, size)
    self.bn = nn.BatchNorm1d(size, momentum=0.01)
   
            
def forward(self, images):
    """Extract feature vectors from input images."""
    
    with torch.no_grad():
        features = self.resnet(images)
        
    features = features.reshape(features.size(0), -1)
    
    features = self.bn(self.linear(features))

features = self.linear(features)

    return features

the output shape like this:
BatchNorm2d-494 [-1, 512, 7, 7] 1,024
ReLU-495 [-1, 512, 7, 7] 0
Conv2d-496 [-1, 512, 7, 7] 2,359,296
BatchNorm2d-497 [-1, 512, 7, 7] 1,024
ReLU-498 [-1, 512, 7, 7] 0
Conv2d-499 [-1, 2048, 7, 7] 1,048,576
BatchNorm2d-500 [-1, 2048, 7, 7] 4,096
ReLU-501 [-1, 2048, 7, 7] 0
Bottleneck-502 [-1, 2048, 7, 7] 0
Conv2d-503 [-1, 512, 7, 7] 1,048,576
BatchNorm2d-504 [-1, 512, 7, 7] 1,024
ReLU-505 [-1, 512, 7, 7] 0
Conv2d-506 [-1, 512, 7, 7] 2,359,296
BatchNorm2d-507 [-1, 512, 7, 7] 1,024
ReLU-508 [-1, 512, 7, 7] 0
Conv2d-509 [-1, 2048, 7, 7] 1,048,576
BatchNorm2d-510 [-1, 2048, 7, 7] 4,096
ReLU-511 [-1, 2048, 7, 7] 0
Bottleneck-512 [-1, 2048, 7, 7] 0
AdaptiveAvgPool2d-513 [-1, 2048, 1, 1] 0
Linear-514 [-1, 10] 20,490
BatchNorm1d-515 [-1, 10] 20

i have changed my net structure like this:
class CNN(nn.Module):
def init (self,size):

    super(CNN, self).__init__()
    resnet = models.resnet152(pretrained=False)
    modules = list(resnet.children())[:-1]      # delete the last fc layer.
    self.resnet = nn.Sequential(*modules)
    self.linear = nn.Linear(resnet.fc.in_features, size)
    self.bn = nn.BatchNorm1d(size, momentum=0.01)
   
            
def forward(self, images):
    """Extract feature vectors from input images."""
    
    with torch.no_grad():
        features = self.resnet(images)
        
    features = features.reshape(features.size(0), -1)
    
    features = self.bn(self.linear(features))

features = self.linear(features)

    return features

the output shape like this:

BatchNorm2d-494 [-1, 512, 7, 7] 1,024
ReLU-495 [-1, 512, 7, 7] 0
Conv2d-496 [-1, 512, 7, 7] 2,359,296
BatchNorm2d-497 [-1, 512, 7, 7] 1,024
ReLU-498 [-1, 512, 7, 7] 0
Conv2d-499 [-1, 2048, 7, 7] 1,048,576
BatchNorm2d-500 [-1, 2048, 7, 7] 4,096
ReLU-501 [-1, 2048, 7, 7] 0
Bottleneck-502 [-1, 2048, 7, 7] 0
Conv2d-503 [-1, 512, 7, 7] 1,048,576
BatchNorm2d-504 [-1, 512, 7, 7] 1,024
ReLU-505 [-1, 512, 7, 7] 0
Conv2d-506 [-1, 512, 7, 7] 2,359,296
BatchNorm2d-507 [-1, 512, 7, 7] 1,024
ReLU-508 [-1, 512, 7, 7] 0
Conv2d-509 [-1, 2048, 7, 7] 1,048,576
BatchNorm2d-510 [-1, 2048, 7, 7] 4,096
ReLU-511 [-1, 2048, 7, 7] 0
Bottleneck-512 [-1, 2048, 7, 7] 0
AdaptiveAvgPool2d-513 [-1, 2048, 1, 1] 0
Linear-514 [-1, 10] 20,490
BatchNorm1d-515 [-1, 10] 20