title: RuntimeError: The size of tensor a (64) must match the size of tensor b (128) at non-singleton dimension 3
body: <p>I want to train the model. In the model vgg16 with first ten (10) layers has been used as a frontend. Now I want to change its network from 10 to 13 layers of vgg16. the following error has occurred.
  Runtime Error: The size of tensor a (64) must match the size of tensor b (128) at non-singleton dimension 3</p>
  <p>Here is the error stacktrace :</p>
  <pre><code>G:\Saadain\Anaconda\envs\CSRNet\lib\site-packages\torch\nn\modules\loss.py:528: UserWarning: Using a target size (torch.Size([1, 1, 85, 128])) that is different to the input size (torch.Size([1, 1, 42, 64])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
    return F.mse_loss(input, target, reduction=self.reduction)
  Traceback (most recent call last):
    File &quot;train.py&quot;, line 230, in &lt;module&gt;
      main()
    File &quot;train.py&quot;, line 92, in main
      train(train_list, model, criterion, optimizer, epoch)
    File &quot;train.py&quot;, line 145, in train
      loss = criterion(output, target)
    File &quot;G:\Saadain\Anaconda\envs\CSRNet\lib\site-packages\torch\nn\modules\module.py&quot;, line 889, in _call_impl
      result = self.forward(*input, **kwargs)
    File &quot;G:\Saadain\Anaconda\envs\CSRNet\lib\site-packages\torch\nn\modules\loss.py&quot;, line 528, in forward
      return F.mse_loss(input, target, reduction=self.reduction)
    File &quot;G:\Saadain\Anaconda\envs\CSRNet\lib\site-packages\torch\nn\functional.py&quot;, line 2928, in mse_loss
      expanded_input, expanded_target = torch.broadcast_tensors(input, target)
    File &quot;G:\Saadain\Anaconda\envs\CSRNet\lib\site-packages\torch\functional.py&quot;, line 74, in broadcast_tensors
      return _VF.broadcast_tensors(tensors)  # type: ignore
  RuntimeError: The size of tensor a (64) must match the size of tensor b (128) at non-singleton dimension 3
  </code></pre>
  <p>The model defintion :</p>
  <pre><code>import torch.nn as nn
  import torch
  from torchvision import models
  from utils import save_net,load_net
  class CSRNet(nn.Module):
      def __init__(self, load_weights=False):
          super(CSRNet, self).__init__()
          self.seen = 0
          self.frontend_feat = [64,64,'M',128,128,'M',256,256,256,'M',512,512,512,'M',512,512,512,]
          self.backend_feat  = [512, 512, 512,256,128,64]
          self.frontend = make_layers(self.frontend_feat)
          self.backend = make_layers(self.backend_feat,in_channels = 512,dilation = True)
          self.output_layer = nn.Conv2d(64, 1, kernel_size=1)
          if not load_weights:
              mod = models.vgg16(pretrained = True)
              self._initialize_weights()
              for i in range(len(self.frontend.state_dict().items())):
                  list(self.frontend.state_dict().items())[i][1].data[:] = list(mod.state_dict().items())[i][1].data[:]
      def forward(self,x):
          x = self.frontend(x)
          x = self.backend(x)
          x = self.output_layer(x)
          return x
      def _initialize_weights(self):
          for m in self.modules():
              if isinstance(m, nn.Conv2d):
                  nn.init.normal_(m.weight, std=0.01)
                  if m.bias is not None:
                      nn.init.constant_(m.bias, 0)
              elif isinstance(m, nn.BatchNorm2d):
                  nn.init.constant_(m.weight, 1)
                  nn.init.constant_(m.bias, 0) 
  def make_layers(cfg, in_channels = 3,batch_norm=False,dilation = False):
      if dilation:
          d_rate = 2
      else:
          d_rate = 1
      layers = []
      for v in cfg:
          if v == 'M':
              layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
          else:
              conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=d_rate,dilation = d_rate)
              if batch_norm:
                  layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
              else:
                  layers += [conv2d, nn.ReLU(inplace=True)]
              in_channels = v
      return nn.Sequential(*layers)
  </code></pre>
  <p>And the train function :</p>
  <pre><code>import sys
  import os
  
  import warnings
  
  from model import CSRNet
  
  from utils import save_checkpoint
  
  import torch
  import torch.nn as nn
  from torch.autograd import Variable
  from torchvision import datasets, transforms
  
  import numpy as np
  import argparse
  import json
  import cv2
  import dataset
  import time
  
  parser = argparse.ArgumentParser(description='PyTorch CSRNet')
  
  parser.add_argument('train_json', metavar='TRAIN',
                      help='path to train json')
  parser.add_argument('test_json', metavar='TEST',
                      help='path to test json')
  
  parser.add_argument('--pre', '-p', metavar='PRETRAINED', default=None,type=str,
                      help='path to the pretrained model')
  
  parser.add_argument('gpu',metavar='GPU', type=str,
                      help='GPU id to use.')
  
  parser.add_argument('task',metavar='TASK', type=str,
                      help='task id to use.')
  
  def main():
      
      global args,best_prec1
      
      best_prec1 = 1e6
      
      args = parser.parse_args()
      args.original_lr = 1e-7
      args.lr = 1e-7
      args.batch_size    = 1
      args.momentum      = 0.95
      args.decay         = 5*1e-4
      args.start_epoch   = 0
      args.epochs = 400
      args.steps         = [-1,1,100,150]
      args.scales        = [1,1,1,1]
      args.workers = 4
      args.seed = time.time()
      args.print_freq = 30
      with open(args.train_json, 'r') as outfile:        
          train_list = json.load(outfile)
      with open(args.test_json, 'r') as outfile:       
          val_list = json.load(outfile)
      
      os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
      torch.cuda.manual_seed(args.seed)
      
      model = CSRNet()
      
      model = model.cuda()
      
      criterion = nn.MSELoss(size_average=False).cuda()
      
      optimizer = torch.optim.SGD(model.parameters(), args.lr,
                                  momentum=args.momentum,
                                  weight_decay=args.decay)
  
      if args.pre:
          if os.path.isfile(args.pre):
              print(&quot;=&gt; loading checkpoint '{}'&quot;.format(args.pre))
              checkpoint = torch.load(args.pre)
              args.start_epoch = checkpoint['epoch']
              best_prec1 = checkpoint['best_prec1']
              model.load_state_dict(checkpoint['state_dict'])
              optimizer.load_state_dict(checkpoint['optimizer'])
              print(&quot;=&gt; loaded checkpoint '{}' (epoch {})&quot;
                    .format(args.pre, checkpoint['epoch']))
          else:
              print(&quot;=&gt; no checkpoint found at '{}'&quot;.format(args.pre))
              
      for epoch in range(args.start_epoch, args.epochs):
          
          adjust_learning_rate(optimizer, epoch)
          
          train(train_list, model, criterion, optimizer, epoch)
          prec1 = validate(val_list, model, criterion)
          
          is_best = prec1 &lt; best_prec1
          best_prec1 = min(prec1, best_prec1)
          print(' * best MAE {mae:.3f} '
                .format(mae=best_prec1))
          save_checkpoint({
              'epoch': epoch + 1,
              'arch': args.pre,
              'state_dict': model.state_dict(),
              'best_prec1': best_prec1,
              'optimizer' : optimizer.state_dict(),
          }, is_best,args.task)
  
  def train(train_list, model, criterion, optimizer, epoch):
      
      losses = AverageMeter()
      batch_time = AverageMeter()
      data_time = AverageMeter()
      
      
      train_loader = torch.utils.data.DataLoader(
          dataset.listDataset(train_list,
                         shuffle=True,
                         transform=transforms.Compose([
                         transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                       std=[0.229, 0.224, 0.225]),
                     ]), 
                         train=True, 
                         seen=model.seen,
                         batch_size=args.batch_size,
                         num_workers=args.workers),
          batch_size=args.batch_size)
      print('epoch %d, processed %d samples, lr %.10f' % (epoch, epoch * len(train_loader.dataset), args.lr))
      
      model.train()
      end = time.time()
      
      for i,(img, target)in enumerate(train_loader):
          data_time.update(time.time() - end)
          
          img = img.cuda()
          img = Variable(img)
          output = model(img)
          
          
          
          
          target = target.type(torch.FloatTensor).unsqueeze(0).cuda()
          target = Variable(target)
          
          
          loss = criterion(output, target)
          
          losses.update(loss.item(), img.size(0))
          optimizer.zero_grad()
          loss.backward()
          optimizer.step()    
          
          batch_time.update(time.time() - end)
          end = time.time()
          
          if i % args.print_freq == 0:
              print('Epoch: [{0}][{1}/{2}]\t'
                    'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                    'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
                    'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                    .format(
                     epoch, i, len(train_loader), batch_time=batch_time,
                     data_time=data_time, loss=losses))
      
  def validate(val_list, model, criterion):
      print ('begin test')
      test_loader = torch.utils.data.DataLoader(
      dataset.listDataset(val_list,
                     shuffle=False,
                     transform=transforms.Compose([
                         transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                       std=[0.229, 0.224, 0.225]),
                     ]),  train=False),
      batch_size=args.batch_size)    
      
      model.eval()
      
      mae = 0
      
      for i,(img, target) in enumerate(test_loader):
          img = img.cuda()
          img = Variable(img)
          output = model(img)
          
          mae += abs(output.data.sum()-target.sum().type(torch.FloatTensor).cuda())
          
      mae = mae/len(test_loader)    
      print(' * MAE {mae:.3f} '
                .format(mae=mae))
  
      return mae    
          
  def adjust_learning_rate(optimizer, epoch):
      &quot;&quot;&quot;Sets the learning rate to the initial LR decayed by 10 every 30 epochs&quot;&quot;&quot;
      
      
      args.lr = args.original_lr
      
      for i in range(len(args.steps)):
          
          scale = args.scales[i] if i &lt; len(args.scales) else 1
          
          
          if epoch &gt;= args.steps[i]:
              args.lr = args.lr * scale
              if epoch == args.steps[i]:
                  break
          else:
              break
      for param_group in optimizer.param_groups:
          param_group['lr'] = args.lr
          
  class AverageMeter(object):
      &quot;&quot;&quot;Computes and stores the average and current value&quot;&quot;&quot;
      def __init__(self):
          self.reset()
  
      def reset(self):
          self.val = 0
          self.avg = 0
          self.sum = 0
          self.count = 0
  
      def update(self, val, n=1):
          self.val = val
          self.sum += val * n
          self.count += n
          self.avg = self.sum / self.count    
      
  if __name__ == '__main__':
      main()
  </code></pre>