I’m a bit confused by your code, previously I thought len(name) was your batch size, is this correct? That error message suggests the batch size of your target and output are different, which does seem plausible if the batch dimension of your input tensor changes according to the name it’s given (without the target tensor also changing).
If it helps here is a self-contained example:
from __future__ import print_function
import torch
import torch.nn as nn
from torch.autograd import Variable

batch_size = 10
input_size = 2
output_size = 2

net = nn.Linear(input_size,output_size)
x = Variable(torch.FloatTensor(batch_size,input_size).normal_(0,1))
criterion = nn.NLLLoss()
target = Variable(torch.LongTensor(batch_size).fill_(1))

output = nn.LogSoftmax()(net(x))

loss = criterion(output, target)

print(loss)

print(x)
print(target)
print(output)

Also you should move the lines
loss_func = nn.NLLLoss()
optimizer = optim.SGD(NN.parameters(), lr=0.0001, momentum=0.9)

outside of your train() function, as these only need to be set once.