title: PyTorch AttributeError: 'UNet3D' object has no attribute 'size'
body: <p>I am making an image segmentation transfer learning project using Pytorch. I am using the weights of this pre-trained model and class UNet3D.
  <a href="https://github.com/MrGiovanni/ModelsGenesis" rel="nofollow noreferrer">https://github.com/MrGiovanni/ModelsGenesis</a></p>
  <p>When I run the following codes I get this error at the line which MSELoss is called: &quot;AttributeError: 'DataParallel' object has no attribute 'size' &quot;.</p>
  <p>When I delete the first line I get a similar error: &quot;AttributeError: 'UNet3D' object has no attribute 'size'</p>
  <p>&quot;</p>
  <p>How can I convert DataParallel or UNet3D class to an object which MSELoss can use? I do not need DataParallel for now. I need to run the UNet3D() class for transfer learning.</p>
  <pre><code>model = nn.DataParallel(model, device_ids = [i for i in range(torch.cuda.device_count())])
  criterion = nn.MSELoss()
  optimizer = torch.optim.SGD(model.parameters(), conf.lr, momentum=0.9, weight_decay=0.0, nesterov=False)
  scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
  initial_epoch=10
  for epoch in range(initial_epoch, conf.nb_epoch):
      scheduler.step(epoch)
      model.train()
      for batch_ndx, (x,y) in enumerate(train_loader):
          x, y = x.float().to(device), y.float().to(device)
          pred = model
          loss = criterion(pred, y)
          optimizer.zero_grad()
          loss.backward()
          optimizer.step()
  </code></pre>
  <pre><code>---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-46-20d1943b3498&gt; in &lt;module&gt;
       25         x, y = x.float().to(device), y.float().to(device)
       26         pred = model
  ---&gt; 27         loss = criterion(pred, y)
       28         optimizer.zero_grad()
       29         loss.backward()
  
  /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
      548             result = self._slow_forward(*input, **kwargs)
      549         else:
  --&gt; 550             result = self.forward(*input, **kwargs)
      551         for hook in self._forward_hooks.values():
      552             hook_result = hook(self, input, result)
  
  /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
      430 
      431     def forward(self, input, target):
  --&gt; 432         return F.mse_loss(input, target, reduction=self.reduction)
      433 
      434 
  
  /opt/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py in mse_loss(input, target, size_average, reduce, reduction)
     2528                 mse_loss, tens_ops, input, target, size_average=size_average, reduce=reduce,
     2529                 reduction=reduction)
  -&gt; 2530     if not (target.size() == input.size()):
     2531         warnings.warn(&quot;Using a target size ({}) that is different to the input size ({}). &quot;
     2532                       &quot;This will likely lead to incorrect results due to broadcasting. &quot;
  
  /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
      592                 return modules[name]
      593         raise AttributeError(&quot;'{}' object has no attribute '{}'&quot;.format(
  --&gt; 594             type(self).__name__, name))
      595 
      596     def __setattr__(self, name, value):
  
  AttributeError: 'UNet3D' object has no attribute 'size'
  
  </code></pre>