Plot probability histogram for each class Params: - y_list: a tensor has dims (num_samples,) - logits_list: a tensor has dims (num_samples, num_classes)
(y_list, logits_list, ignore_label_lst=[-100,0])
| 427 | plt.show() |
| 428 | |
| 429 | def plot_prob_hist_each_class(y_list, logits_list, ignore_label_lst=[-100,0]): |
| 430 | ''' |
| 431 | Plot probability histogram for each class |
| 432 | |
| 433 | Params: |
| 434 | - y_list: a tensor has dims (num_samples,) |
| 435 | - logits_list: a tensor has dims (num_samples, num_classes) |
| 436 | ''' |
| 437 | |
| 438 | pad_mask = torch.not_equal(y_list, -100) |
| 439 | y_list, logits_list = y_list[pad_mask], logits_list[pad_mask] |
| 440 | |
| 441 | pred_list = torch.argmax(logits_list, dim=-1) |
| 442 | prob_list = torch.softmax(logits_list, dim=-1) |
| 443 | |
| 444 | for label_id in list(set(np.array(y_list))): |
| 445 | if label_id in ignore_label_lst: |
| 446 | continue |
| 447 | # print("label_id=%d:"%label_id) |
| 448 | y_mask = torch.eq(pred_list, label_id) |
| 449 | y_mask_correct = torch.logical_and(\ |
| 450 | torch.eq(y_list, label_id), |
| 451 | y_mask) |
| 452 | y_mask_wrong = torch.logical_and(\ |
| 453 | torch.not_equal(y_list, label_id), |
| 454 | y_mask) |
| 455 | y_logits_correct = np.array(prob_list[y_mask_correct][:,label_id]) |
| 456 | y_logits_wrong = np.array(prob_list[y_mask_wrong][:,label_id]) |
| 457 | print(len(y_logits_correct)) |
| 458 | print(len(y_logits_wrong)) |
| 459 | plt.hist([y_logits_correct,y_logits_wrong], |
| 460 | bins=list(np.arange(0,0.9,0.1))\ |
| 461 | +[0.9, 0.99, 0.999, 0.9999, 0.99999, 1], |
| 462 | color=['green','red'], |
| 463 | alpha=0.75) |
| 464 | plt.legend(['Correct','Wrong']) |
| 465 | plt.title('Prob distribution for class idx %d'%label_id) |
| 466 | plt.show() |
| 467 | |
| 468 | def decode_sentence(sentence, auto_tokenizer): |
| 469 | ''' |