python - Confusion matrix and test accuracy for PyTorch ...
stackoverflow.com › questions › 53290306Nov 14, 2018 · Here is a slightly modified(direct) approach using sklearn's confusion_matrix:-from sklearn.metrics import confusion_matrix nb_classes = 9 # Initialize the prediction and label lists(tensors) predlist=torch.zeros(0,dtype=torch.long, device='cpu') lbllist=torch.zeros(0,dtype=torch.long, device='cpu') with torch.no_grad(): for i, (inputs, classes) in enumerate(dataloaders['val']): inputs = inputs.to(device) classes = classes.to(device) outputs = model_ft(inputs) _, preds = torch.max(outputs, 1 ...