Different classification results

1st code is just classification test code as usual.
2nd code is to get accuracy from class-wise accuracy.
Why two codes have different accuracy?

def test(dataloader, model):
    model.eval()
    correct = 0
    total = 0
    for batch_idx, (inputs, labels) in enumerate(dataloader):
        inputs, labels = inputs.cuda(), labels.cuda()
        with torch.no_grad():
            outputs = model(inputs)
        _, predicted = outputs.max(1)
        total += labels.size(0)
        correct += predicted.eq(labels).sum().item()
    model.train()
    return correct / total
confusion_matrix = torch.zeros(nb_classes, nb_classes)
model.eval()
for batch_idx, (inputs, labels) in enumerate(dataloader):
    inputs, labels = inputs.cuda(), labels.cuda()
    outputs = model(inputs)
    _, preds = torch.max(outputs, 1)
    for t, p in zip(labels.view(-1), preds.view(-1)):
        confusion_matrix[t.long(), p.long()] += 1
ele_wise_acc = confusion_matrix.diag() / confusion_matrix.sum(1)
total_acc = ele_wise_acc.mean()
model.train()
return total_acc

Try to disable shuffling in the DataLoader and compare the outputs of both code snippets.
Also make sure that the indexing in confusion_matrix is right and compare the number of class predictions to the correct value in the first code.