If I may post on this thread, I’m having a similar issue as the original one so I thought I’d post it here rather than creating a new thread.
My training code looks as follows:
model.train()
train_loss = []
train_accu = []
i = 0
for epoch in range(30):
    for data, target in test_loader:
        print(target.shape)
        print(data.view(batch_size,1,64,64).shape)
        data, target = Variable(data), Variable(target)
        optimizer.zero_grad()
        output = model(data.view(batch_size,1,64,64))
        print(output.shape)
        loss = F.nll_loss(output, target.view(batch_size)) # Negative log likelihood (goes with softmax). 
        loss.backward()    # calc gradients
        train_loss.append(loss.data[0]) # Calculating the loss
        optimizer.step()   # update gradients
        prediction = output.data.max(1)[1]   # first column has actual prob.
        accuracy = (prediction.eq(target.data).sum()/batch_size)*100
        train_accu.append(accuracy)
        if i % 10 == 0:
            print('Epoch:',str(epoch),'Train Step: {}\tLoss: {:.3f}\tAccuracy: {:.3f}'.format(i, loss.data[0], accuracy))
        i += 1

giving:

torch.Size([3])
torch.Size([3, 1, 64, 64])
torch.Size([12, 12])
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-334-ce8b8adb782b> in <module>()
     11         output = model(data.view(batch_size,1,64,64))
     12         print(output.shape)
---> 13         loss = F.nll_loss(output, target.view(batch_size)) # Negative log likelihood (goes with softmax).
     14         loss.backward()    # calc gradients
     15         train_loss.append(loss.data[0]) # Calculating the loss

~/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce)
   1047         weight = Variable(weight)
   1048     if dim == 2:
-> 1049         return torch._C._nn.nll_loss(input, target, weight, size_average, ignore_index, reduce)
   1050     elif dim == 4:
   1051         return torch._C._nn.nll_loss2d(input, target, weight, size_average, ignore_index, reduce)

RuntimeError: Assertion `THIndexTensor_(size)(target, 0) == batch_size' failed.  at /opt/conda/conda-bld/pytorch-cpu_1515613813020/work/torch/lib/THNN/generic/ClassNLLCriterion.c:79

The last layer of my CNN outputs 12 numbers:
class Net(nn.Module):
    
    def __init__(self):
        super(Net,self).__init__()
        self.conv1  = nn.Conv2d(1,32,5,padding=2) # 1 input, 32 out, filter size = 5x5, 2 block outer padding
        self.conv2  = nn.Conv2d(32,64,5,padding=2) # 32 input, 64 out,  filter size = 5x5, 2 block padding
        self.fc1    = nn.Linear(64*8*8,1024) # Fully connected layer 
        self.fc2    = nn.Linear(1024,12) #Fully connected layer 2 out.
        
    def forward(self,x):
        x = F.max_pool2d(F.relu(self.conv1(x)), 2) # Max pool over convolution with 2x2 pooling 
        x = F.max_pool2d(F.relu(self.conv2(x)), 2) # Max pool over convolution with 2x2 pooling 
        x = x.view(-1,64*8*8) # tensor.view() reshapes the tensor
        x = F.relu(self.fc1(x)) # Activation function after passing through fully connected layer
#        x = F.dropout(x, training = self.training) #Dropout regularisation
        x = self.fc2(x) # Pass through final fully connected layer
        output= F.log_softmax(x,dim=1) # Give results using softmax
        return output
  
model = Net()
model.apply(weight_init)
model = model.double()
print(model)

But for some reason in my mind (given the above thread) my target should have size (3,12) if it is going to match the batch size issue.
Does anyone have any ideas as  to how to fix this problem?