DataLoader with NLLLoss: input / target format

I’m working on a sentence sentiment classification network that averages pre-trained word embeddings. My input consists of embedding indices and a binary label. According to NLLLoss documentation, there should be a single target tensor for a mini-batch N of training examples:

Input: (N,C) where C = number of classes
Target: (N) where each value is 0 <= targets[i] <= C-1

In other words, for a mini-batch size of N=3, a target tensor should look like:

target = autograd.Variable(torch.LongTensor([1, 0, 1]))

How do I load data into DataLoader to satisfy this requirement? I’m currently providing (tensor_idx, target_label) for each example in the batch as a list of dictionaries:

x = get_tensor_idx(tokens, word_to_idx, MAX_SENT_LEN)
sample = {'x': x, 'y': torch.from_numpy(np.array([label], dtype=np.int64))}
train_data_loader =, ...)

However, this approach results in a run-time error:

RuntimeError: multi-target not supported at /pytorch/torch/lib/THNN/generic/ClassNLLCriterion.c:22

So how do I correctly feed my (tensor_idx, target_label) into the DataLoader?

I found the following worked. I had to reshape the target tensor to the required dimension (N):

x = Variable(batch['x'])
y_temp = batch['y'].numpy().ravel()
y_batch = torch.from_numpy(y_temp)
y = Variable(y_batch)