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

neemashahbazi: <p>Hi, I’m very new to NNs and Pytorch and I’m getting the following error for a multiclass text classification task using LSTM.<br>
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.</p>
<p>ERROR:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "/Users/nimashahbazi/PycharmProjects/reddit/RNN/embedding_layer.py", line 59, in &lt;module&gt;
    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
</code></pre>
<pre><code class="lang-auto">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
</code></pre>
<pre><code class="lang-auto">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)
</code></pre>
<pre><code class="lang-auto">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
</code></pre>
<p>Can anyone help me with the problem??</p>

charan_Vjy: <p><code>rounded_preds</code> is of shape: [64,10]<br>
<code>y</code> is of shape: [64]<br>
You would have to convert <code>rounded_preds</code> 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.</p>
<p><strong>Replace</strong><br>
<code>correct = (rounded_preds == y).float()</code><br>
<strong>with</strong></p>
<pre><code class="lang-auto">_,pred_label = torch.max(rounded_pred, dim = 1)
correct = (pred_label == y).float()
</code></pre>

Jhone: <p>Hi, I also got this error. How to fix this?</p>
<p>File “main.py”, line 58, in<br>
main(args, CORE)<br>
File “main.py”, line 48, in main<br>
CORE.train(args)<br>
File “/home/ali/project3_1/pytorch_version/utils.py”, line 88, in train<br>
output = model(x)<br>
File “/home/ali/anaconda3/envs/torch/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 889, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “/home/ali/project3_1/pytorch_version/model.py”, line 116, in forward<br>
d3 = self.decoder3(d4) + e2<br>
RuntimeError: The size of tensor a (63) must match the size of tensor b (64) at non-singleton dimension 3</p>