RuntimeError: The size of tensor a (10) must match the size of tensor b (64) at non-singleton dimension 1

Hi, I’m very new to NNs and Pytorch and I’m getting the following error for a multiclass text classification task using LSTM.
I have 10 classes in my dataset. This code has originally been for binary classification and I’m changing it. When I changed the criterion to nn.CrossEntropyLoss() this issue happened. preds and targets shape is printed in the code.


Traceback (most recent call last):
  File "/Users/nimashahbazi/PycharmProjects/reddit/RNN/", line 59, in <module>
    train_loss, train_acc = train(model, train_iterator, optimizer, criterion)
  File "/Users/nimashahbazi/PycharmProjects/reddit/RNN/", line 26, in train
    acc = accuracy(predictions, batch.label)
  File "/Users/nimashahbazi/PycharmProjects/reddit/RNN/", line 8, in accuracy
    correct = (rounded_preds == y).float()
  File "/Users/nimashahbazi/PycharmProjects/reddit/env/lib/python3.7/site-packages/torch/", line 28, in wrapped
    return f(*args, **kwargs)
RuntimeError: The size of tensor a (10) must match the size of tensor b (64) at non-singleton dimension 1

Process finished with exit code 1
TEXT = data.Field(tokenize='spacy', batch_first=True, include_lengths=True)
LABEL = data.LabelField(dtype=torch.long, batch_first=True)
training_data = data.TabularDataset(path='/Users/nimashahbazi/PycharmProjects/reddit/data/scraper/train/train.csv', format='csv', skip_header=True,
                                    fields=[('label', LABEL), ('text', TEXT)])
train_data, valid_data = training_data.split(split_ratio=0.9, random_state=random.seed(SEED))
TEXT.build_vocab(train_data, min_freq=3, vectors="glove.840B.300d")
train_iterator, valid_iterator = data.BucketIterator.splits(
    (train_data, valid_data),
    sort_key=lambda x: len(x.text),

size_of_vocab = len(TEXT.vocab)
embedding_dim = 300
num_hidden_nodes = 128
num_output_nodes = 10
num_layers = 2
bidirection = True
dropout = 0.2
def train(model, iterator, optimizer, criterion):
    # initialize every epoch
    epoch_loss = 0
    epoch_acc = 0

    # set the model in training phase

    for batch in iterator:
        # resets the gradients after every batch

        # retrieve text and no. of words
        text, text_lengths = batch.text

        # convert to 1D tensor
        predictions = model(text, text_lengths).squeeze()

        # compute the loss
        loss = criterion(predictions, batch.label)

        # compute the binary accuracy
        acc = accuracy(predictions, batch.label)

        # backpropage the loss and compute the gradients

        # update the weights

        # loss and accuracy
        epoch_loss += loss.item()
        epoch_acc += acc.item()

    return epoch_loss / len(iterator), epoch_acc / len(iterator)
def accuracy(preds, y):
    rounded_preds = torch.round(preds)
    print(rounded_preds.shape)   // torch.Size([64, 10])
    print(y.shape)                          //torch.Size([64])
    correct = (rounded_preds == y).float()
    acc = correct.sum() / len(correct)
    return acc

Can anyone help me with the problem??

rounded_preds is of shape: [64,10]
y is of shape: [64]
You would have to convert rounded_preds into [64] before you could carry out the operation. You would have to select the index which has the maximum probability value as the predicted class.

correct = (rounded_preds == y).float()

_,pred_label = torch.max(rounded_pred, dim = 1)
correct = (pred_label == y).float()

Hi, I also got this error. How to fix this?

File “”, line 58, in
main(args, CORE)
File “”, line 48, in main
File “/home/ali/project3_1/pytorch_version/”, line 88, in train
output = model(x)
File “/home/ali/anaconda3/envs/torch/lib/python3.6/site-packages/torch/nn/modules/”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “/home/ali/project3_1/pytorch_version/”, line 116, in forward
d3 = self.decoder3(d4) + e2
RuntimeError: The size of tensor a (63) must match the size of tensor b (64) at non-singleton dimension 3