Skip to content

Instantly share code, notes, and snippets.

@henry16lin
Created February 15, 2020 15:12
Show Gist options
  • Save henry16lin/bba753622b0156fcc2f40a02398d0ea4 to your computer and use it in GitHub Desktop.
Save henry16lin/bba753622b0156fcc2f40a02398d0ea4 to your computer and use it in GitHub Desktop.
from sklearn.metrics import confusion_matrix
true=[]
predictions=[]
with torch.no_grad():
model.eval()
for data in testloader:
tokens_tensors, segments_tensors,masks_tensors, labels = [t.to(device) for t in data]
val_outputs = model(input_ids=tokens_tensors,
token_type_ids=segments_tensors,
attention_mask=masks_tensors,
labels=labels)
logits = val_outputs[1]
_, pred = torch.max(logits.data, 1)
true.extend(labels.cpu().tolist())
predictions.extend(pred.cpu().tolist())
c = confusion_matrix(true, predictions)
print(c)
accuracy_score(predictions,true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment