LSTM runtime shape error in evaluation - training is fine

I’m able to successfully train a LSTM model but when I tried to evaluate it it throws a runtime shape error RuntimeError: shape '[3, 1, 5000] is invalid for input of size 5000 on the last batch. My test data is 4776 samples and each sample has 5000 features.

with torch.no_grad():
n_correct = 0
n_samples = 0
for x_batch, y_batch in test_loader:
    x_batch = x_batch.reshape(batch_size,  1, input_dim).to(device)
    y_batch = y_batch.to(device)
    outputs = model(x_batch)
    # max returns (value ,index)
    _, predicted = torch.max(outputs.data, 1)
    n_samples += y_batch.size(0)
    n_correct += (predicted == y_batch).sum().item()
    print(f'Testing accuracy: ' + str( 100.0 * n_correct / n_samples) + " samples tested: " + str(n_samples))

acc = 100.0 * n_correct / n_samples
print(f'Accuracy of the network on the 4776 test samples: {acc} %')

Below is the last output I get from my print statement:

What is the batch size: torch.Size([3, 1, 5000])
Testing accuracy: 96.5011523151058 samples tested: 4773

I’m confused as to how I could train the model to completion using the same dimensions but it fails on the last batch in the evaluation. According to the error is seems that only one sample is in that last batch.

I guess it is because the total number of samples is not divisible by the batch_size. So, when you are creating the DataLoader there is this argument drop_last. This is by default set to False. Try changing it to True.

Thanks for the help! It did run to completion without the error but the test dataset has 4776 samples and that is divisible by the batch size (3).