I use the following function to make predictions on the test dataset. However, I find that when I set shuffle=True in the data loader, the order of y_pred and y_true got messed up. I go over this function multiple times but I cannot see where I got wrong. Can someone please help me detect if there is any bug in the following code?
def test(model, device, test_loader, criterion):
model.eval()
y_pred = torch.empty(0).to(device)
y_true = torch.empty(0).to(device)
with torch.no_grad():
for img, meteo, target in test_loader:
img, meteo, target = img.to(device), meteo.to(device), torch.squeeze(target.to(device))
output = torch.squeeze(model(img, meteo))
y_pred = torch.cat((y_pred, output))
y_true = torch.cat((y_true, target))
test_loss = criterion(y_pred, y_true)
print('Test set Loss: {:.4f}'.format(test_loss))
return y_pred, y_true, test_loss