title: Target size (torch.Size([12])) must be the same as input size (torch.Size([12, 1000]))
body: <p>I am using <code>models.vgg16(pretrained=True)</code> model for image classification, where number of classes = 3.</p>
  
  <p>Batch size is 12 <code>trainloader = torch.utils.data.DataLoader(train_data, batch_size=12, shuffle=True)</code> since error says <code>Target size (torch.Size([12])) must be the same as input size (torch.Size([12, 1000]))</code></p>
  
  <p>I have changed last fc layer parameters and got last FC layer as  <code>Linear(in_features=1000, out_features=3, bias=True)</code></p>
  
  <p>Loss function is <code>BCEWithLogitsLoss()</code></p>
  
  <pre class="lang-py prettyprint-override"><code>criterion = nn.BCEWithLogitsLoss()
  optimizer = optim.SGD(vgg16.parameters(), lr=0.001, momentum=0.9)
  </code></pre>
  
  <p>Training code is  </p>
  
  <pre class="lang-py prettyprint-override"><code>        # zero the parameter gradients
          optimizer.zero_grad()
          outputs = vgg16(inputs)               #----&gt; forward pass
          loss = criterion(outputs, labels)   #----&gt; compute loss #error occurs here
          loss.backward()                     #----&gt; backward pass
          optimizer.step()                    #----&gt; weights update
  </code></pre>
  
  <p>While computing loss, I get this error <code>Target size (torch.Size([12])) must be the same as input size (torch.Size([12, 1000]))</code></p>
  
  <p>code is available at: <a href="https://github.com/irfanumar1994/vgg/blob/master/code.ipynb" rel="nofollow noreferrer">code</a></p>