Problem: Plotting confusion metrics result vary each time

Hi, I am trying to plot the confusion metric but each time when I run a cell I am getting a different result.
Here is code:
pred_list=torch.zeros(0,dtype=torch.long, device=‘cpu’)

label_list=torch.zeros(0,dtype=torch.long, device=‘cpu’)
with torch.no_grad():
for inputs, labels in train_loader:

  inputs = inputs.to(device)

  labels = labels.to(device)

  outputs = model(inputs)

  _, preds = torch.max(outputs, 1)

 

  # Append batch prediction results

  pred_list=torch.cat([pred_list,preds.view(-1).cpu()])

  label_list=torch.cat([label_list,labels.view(-1).cpu()])

Confusion matrix

conf_matrix=confusion_matrix(label_list.numpy(), pred_list.numpy())

print(conf_matrix)

Iteration 1: image

Iteration 2: image

Due to this my classification report is also varies. Any help in this regard is appriciated.

Hi. If I understand your problem correctly, you are getting a slightly different confusion matrix values each time you run the complete cell that makes the inference using your trained model.
If my understanding is correct, then one thing to try out in this case would be to set seed, something like this:

seed = 0
torch.manual_seed(seed)
if torch.cuda.is_available():
    torch.cuda.manual_seed_all(seed)

Setting a seed helps in result reproducibility. You can read more on this here: Reproducibility — PyTorch 1.7.0 documentation

Yes, your understanding is correct. In each iteration when i run a complete cell confusion matrix values slightly updated. Let me try this solution which you shared with me.