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

ywr461903285: <p>i am a new hand in this item, met a problem which is hard for me to solve…i need someone to help me<br>
code below.:</p>
<p>class CNN(nn.Module):<br>
def <strong>init</strong>(self,size):</p>
<pre><code>    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
</code></pre>
<p>class NN(nn.Module):<br>
def <strong>init</strong>(self, in_dim, n_hidden1, n_hidden2, out_dim):<br>
super(NN, self).<strong>init</strong>()<br>
self.layer1 = nn.Linear(in_dim, n_hidden1)<br>
self.layer2 = nn.Linear(n_hidden1, n_hidden2)<br>
self.layer3 = nn.Linear(n_hidden2, out_dim)</p>
<pre><code>def forward(self, x):
    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)
</code></pre>
<p>device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)</p>
<p>size = 1<br>
in_dim, n_hidden1, n_hidden2, out_dim = 20, 300, 100, 2<br>
learning_rate = 0.001</p>
<p>encoder = CNN(size).cuda()<br>
decoder = NN(in_dim, n_hidden1, n_hidden2, out_dim).cuda()</p>
<p>criterion = nn.MSELoss()<br>
params = list(decoder.parameters()) + list(encoder.parameters())<br>
optimizer = torch.optim.Adam(params, lr=learning_rate)<br>
for epoch in range (10):<br>
for img, rst, character in train_dataloader:</p>
<pre><code>    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()</code></pre>

urw7rs: <p>could you post the error message?</p>

ywr461903285: <p>Traceback (most recent call last):</p>
<p>File “”, line 1, in <br>
runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 668, in runfile<br>
execfile(filename, namespace)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 108, in execfile<br>
exec(compile(f.read(), filename, ‘exec’), namespace)</p>
<p>File “/home/ywr/.config/spyder-py3/云溪/train.py”, line 32, in <br>
features = encoder(img)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 489, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)</p>
<p>File “/home/ywr/.config/spyder-py3/云溪/model.py”, line 27, in forward<br>
features = self.linear(features)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 489, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/linear.py”, line 67, in forward<br>
return F.linear(input, self.weight, self.bias)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py”, line 1354, in linear<br>
output = input.matmul(weight.t())</p>
<p>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</p>

urw7rs: <p>I think you need to use .view(1, 2048) on the output of the resnet. So it would look like this:</p>
<pre><code class="lang-auto">with torch.no_grad():
    features = self.resnet(images)
    features = features.view(1, 2048)
</code></pre>
<p>Before the fully connected layer, torch.flatten changes the dimensions to [2048, 1].</p>

ywr461903285: <p>thanks a lot but occur another problem like this:<br>
Traceback (most recent call last):</p>
<p>File “”, line 1, in <br>
runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 668, in runfile<br>
execfile(filename, namespace)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 108, in execfile<br>
exec(compile(f.read(), filename, ‘exec’), namespace)</p>
<p>File “/home/ywr/.config/spyder-py3/云溪/train.py”, line 32, in <br>
features = encoder(img)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 489, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)</p>
<p>File “/home/ywr/.config/spyder-py3/云溪/model.py”, line 27, in forward<br>
features = self.linear(features)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 489, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/linear.py”, line 67, in forward<br>
return F.linear(input, self.weight, self.bias)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py”, line 1354, in linear<br>
output = input.matmul(weight.t())</p>
<p>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</p>
<p>runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)<br>
Reloaded modules: model, data_loader<br>
Traceback (most recent call last):</p>
<p>File “”, line 1, in <br>
runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 668, in runfile<br>
execfile(filename, namespace)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 108, in execfile<br>
exec(compile(f.read(), filename, ‘exec’), namespace)</p>
<p>File “/home/ywr/.config/spyder-py3/云溪/train.py”, line 34, in <br>
character = torch.cat((features, character),1)</p>
<p>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</p>

ywr461903285: <p>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:<br>
root= “./dataset/train_data/”<br>
datatxt=“train_data1.txt”<br>
class MyDataset(torch.utils.data.Dataset):<br>
def <strong>init</strong>(self,root, datatxt, transform=None, target_transform=None):<br>
super(MyDataset,self).<strong>init</strong>()<br>
fh = open(root + datatxt, ‘r’)</p>
<pre><code>    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)
</code></pre>
<p>train_data=MyDataset(root = root, datatxt=datatxt, transform=transforms.ToTensor())<br>
train_dataloader = torch.utils.data.DataLoader(dataset=train_data, batch_size=4, shuffle=False)</p>
<p>transform ={<br>
transforms.Compose([<br>
transforms.CenterCrop((224,224)),<br>
transforms.ToTensor(),<br>
transforms.Normalize((0.485, 0.456, 0.406),<br>
(0.229, 0.224, 0.225))<br>
])<br>
}</p>

ywr461903285: <p>thanks a lot but occur another problem like this:<br>
Traceback (most recent call last):</p>
<p>File “”, line 1, in<br>
runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 668, in runfile<br>
execfile(filename, namespace)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 108, in execfile<br>
exec(compile(f.read(), filename, ‘exec’), namespace)</p>
<p>File “/home/ywr/.config/spyder-py3/云溪/train.py”, line 32, in<br>
features = encoder(img)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 489, in  <strong>call</strong><br>
result = self.forward(*input, **kwargs)</p>
<p>File “/home/ywr/.config/spyder-py3/云溪/model.py”, line 27, in forward<br>
features = self.linear(features)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 489, in  <strong>call</strong><br>
result = self.forward(*input, **kwargs)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/modules/linear.py”, line 67, in forward<br>
return F.linear(input, self.weight, self.bias)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py”, line 1354, in linear<br>
output = input.matmul(weight.t())</p>
<p>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</p>
<p>runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)<br>
Reloaded modules: model, data_loader<br>
Traceback (most recent call last):</p>
<p>File “”, line 1, in<br>
runfile(’/home/ywr/.config/spyder-py3/云溪/train.py’, wdir=’/home/ywr/.config/spyder-py3/云溪’)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 668, in runfile<br>
execfile(filename, namespace)</p>
<p>File “/home/ywr/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”, line 108, in execfile<br>
exec(compile(f.read(), filename, ‘exec’), namespace)</p>
<p>File “/home/ywr/.config/spyder-py3/云溪/train.py”, line 34, in<br>
character = torch.cat((features, character),1)</p>
<p>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</p>

urw7rs: <p>can you print the features tensor’s dimensions right after self.resnet(images)?</p>

ywr461903285: <p>i have changed my net structure like this:<br>
class CNN(nn.Module):<br>
def <strong>init</strong>(self,size):</p>
<pre><code>    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))
</code></pre>
<h1>features = self.linear(features)</h1>
<pre><code>    return features
</code></pre>
<h1>the output shape like this:<br>
BatchNorm2d-494            [-1, 512, 7, 7]           1,024<br>
ReLU-495            [-1, 512, 7, 7]               0<br>
Conv2d-496            [-1, 512, 7, 7]       2,359,296<br>
BatchNorm2d-497            [-1, 512, 7, 7]           1,024<br>
ReLU-498            [-1, 512, 7, 7]               0<br>
Conv2d-499           [-1, 2048, 7, 7]       1,048,576<br>
BatchNorm2d-500           [-1, 2048, 7, 7]           4,096<br>
ReLU-501           [-1, 2048, 7, 7]               0<br>
Bottleneck-502           [-1, 2048, 7, 7]               0<br>
Conv2d-503            [-1, 512, 7, 7]       1,048,576<br>
BatchNorm2d-504            [-1, 512, 7, 7]           1,024<br>
ReLU-505            [-1, 512, 7, 7]               0<br>
Conv2d-506            [-1, 512, 7, 7]       2,359,296<br>
BatchNorm2d-507            [-1, 512, 7, 7]           1,024<br>
ReLU-508            [-1, 512, 7, 7]               0<br>
Conv2d-509           [-1, 2048, 7, 7]       1,048,576<br>
BatchNorm2d-510           [-1, 2048, 7, 7]           4,096<br>
ReLU-511           [-1, 2048, 7, 7]               0<br>
Bottleneck-512           [-1, 2048, 7, 7]               0<br>
AdaptiveAvgPool2d-513           [-1, 2048, 1, 1]               0<br>
Linear-514                   [-1, 10]          20,490<br>
BatchNorm1d-515                   [-1, 10]              20</h1>

ywr461903285: <p>i have changed my net structure like this:<br>
class CNN(nn.Module):<br>
def  <strong>init</strong> (self,size):</p>
<pre><code class="lang-auto">    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))
</code></pre>
<h1>features = self.linear(features)</h1>
<pre><code class="lang-auto">    return features
</code></pre>
<h1>the output shape like this:</h1>
<p>BatchNorm2d-494 [-1, 512, 7, 7] 1,024<br>
ReLU-495 [-1, 512, 7, 7] 0<br>
Conv2d-496 [-1, 512, 7, 7] 2,359,296<br>
BatchNorm2d-497 [-1, 512, 7, 7] 1,024<br>
ReLU-498 [-1, 512, 7, 7] 0<br>
Conv2d-499 [-1, 2048, 7, 7] 1,048,576<br>
BatchNorm2d-500 [-1, 2048, 7, 7] 4,096<br>
ReLU-501 [-1, 2048, 7, 7] 0<br>
Bottleneck-502 [-1, 2048, 7, 7] 0<br>
Conv2d-503 [-1, 512, 7, 7] 1,048,576<br>
BatchNorm2d-504 [-1, 512, 7, 7] 1,024<br>
ReLU-505 [-1, 512, 7, 7] 0<br>
Conv2d-506 [-1, 512, 7, 7] 2,359,296<br>
BatchNorm2d-507 [-1, 512, 7, 7] 1,024<br>
ReLU-508 [-1, 512, 7, 7] 0<br>
Conv2d-509 [-1, 2048, 7, 7] 1,048,576<br>
BatchNorm2d-510 [-1, 2048, 7, 7] 4,096<br>
ReLU-511 [-1, 2048, 7, 7] 0<br>
Bottleneck-512 [-1, 2048, 7, 7] 0<br>
AdaptiveAvgPool2d-513 [-1, 2048, 1, 1] 0<br>
Linear-514 [-1, 10] 20,490<br>
BatchNorm1d-515 [-1, 10] 20</p>