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.

ERROR:

Traceback (most recent call last):
  File "/Users/nimashahbazi/PycharmProjects/reddit/RNN/embedding_layer.py", line 59, in <module>
    train_loss, train_acc = train(model, train_iterator, optimizer, criterion)
  File "/Users/nimashahbazi/PycharmProjects/reddit/RNN/train.py", line 26, in train
    acc = accuracy(predictions, batch.label)
  File "/Users/nimashahbazi/PycharmProjects/reddit/RNN/accuracy.py", line 8, in accuracy
    correct = (rounded_preds == y).float()
  File "/Users/nimashahbazi/PycharmProjects/reddit/env/lib/python3.7/site-packages/torch/tensor.py", 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")
LABEL.build_vocab(train_data)
BATCH_SIZE = 64
train_iterator, valid_iterator = data.BucketIterator.splits(
    (train_data, valid_data),
    batch_size=BATCH_SIZE,
    sort_key=lambda x: len(x.text),
    sort_within_batch=True,
    device='cpu')

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
    model.train()

    for batch in iterator:
        # resets the gradients after every batch
        optimizer.zero_grad()

        # 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
        loss.backward()

        # update the weights
        optimizer.step()

        # 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.

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

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

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

File “main.py”, line 58, in
main(args, CORE)
File “main.py”, line 48, in main
CORE.train(args)
File “/home/ali/project3_1/pytorch_version/utils.py”, line 88, in train
output = model(x)
File “/home/ali/anaconda3/envs/torch/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “/home/ali/project3_1/pytorch_version/model.py”, 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