Hi @Rosa,
I think you can do it in your training function. This snippet should work:
confusion_matrix = torch.zeros(nb_labels, nb_labels)
for inputs, labels in dataloaders[phase]:
       inputs = inputs.to(device)
       labels = labels.to(device)
       optimizer.zero_grad()

       with torch.set_grad_enabled(phase == 'train'):
              outputs = model(inputs)
              _, preds = torch.max(outputs, 1)
              loss = criterion(outputs, labels)
              ....

              if phase == 'valid':
                      for l, p in zip(labels.view(-1), preds.view(-1)):
                      confusion_matrix[l.long(), p.long()] += 1
print(confusion_matrix)