-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutil.py
183 lines (154 loc) · 6.18 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import itertools
import torch
import copy
import random
import csv
import sys
import torch.nn.functional as F
from torch import nn
from tqdm import tqdm_notebook, trange, tqdm
from pytorch_pretrained_bert.optimization import BertAdam
from pytorch_pretrained_bert.modeling import WEIGHTS_NAME, CONFIG_NAME, BertPreTrainedModel,BertModel
from pytorch_pretrained_bert.tokenization import BertTokenizer
from torch.utils.data import (DataLoader, RandomSampler, SequentialSampler, TensorDataset)
from datetime import datetime
from sklearn.metrics import confusion_matrix, f1_score, accuracy_score
def debug(data, manager_p, manager, args):
print('-----------------Data--------------------')
data_attrs = ["data_dir","n_known_cls","num_labels","all_label_list","known_label_list"]
for attr in data_attrs:
attr_name = attr
attr_value = data.__getattribute__(attr)
print(attr_name,':',attr_value)
print('-----------------Args--------------------')
for k in list(vars(args).keys()):
print(k,':',vars(args)[k])
print('-----------------Manager_pretrain--------------------')
manager_p_attrs = ["device","num_train_optimization_steps","best_eval_score"]
for attr in manager_p_attrs:
attr_name = attr
attr_value = manager_p.__getattribute__(attr)
print(attr_name,':',attr_value)
print('-----------------Manager--------------------')
manager_attrs = ["device","best_eval_score","test_results"]
for attr in manager_attrs:
attr_name = attr
attr_value = manager.__getattribute__(attr)
print(attr_name,':',attr_value)
if manager.predictions is not None:
print('-----------------Predictions--------------------')
show_num = 100
for i,example in enumerate(data.test_examples):
if i >= show_num:
break
sentence = example.text_a
true_label = manager.true_labels[i]
predict_label = manager.predictions[i]
print(i,':',sentence)
print('Pred:{}; True:{}'.format(predict_label,true_label))
def F_measure(cm):
idx = 0
rs, ps, fs = [], [], []
n_class = cm.shape[0]
for idx in range(n_class):
TP = cm[idx][idx]
r = TP / cm[idx].sum() if cm[idx].sum() != 0 else 0
p = TP / cm[:, idx].sum() if cm[:, idx].sum() != 0 else 0
f = 2 * r * p / (r + p) if (r + p) != 0 else 0
rs.append(r * 100)
ps.append(p * 100)
fs.append(f * 100)
f = np.mean(fs).round(4)
f_seen = np.mean(fs[:-1]).round(4)
f_unseen = round(fs[-1], 4)
result = {}
result['Known'] = f_seen
result['Open'] = f_unseen
result['F1-score'] = f
return result
def plot_confusion_matrix(cm, classes, save_name, normalize=False, title='Confusion matrix', figsize=(12, 10),
cmap=plt.cm.Blues, save=False):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
plt.switch_backend('agg')
np.set_printoptions(precision=2)
plt.figure(figsize=figsize)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 1.2
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
if save:
plt.savefig(save_name)
def draw(x, y):
from matplotlib.colors import ListedColormap
from MulticoreTSNE import MulticoreTSNE as TSNE
print("TSNE: fitting start...")
tsne = TSNE(2, n_jobs=4, perplexity=30)
Y = tsne.fit_transform(x)
# matplotlib_axes_logger.setLevel('ERROR')
labels = ['c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','open']
id_to_label = {i: label for i, label in enumerate(labels) }
y_true = pd.Series(y)
plt.style.use('ggplot')
n_class = y_true.unique().shape[0]
colors = ( 'gray','lightgreen', 'plum','DarkMagenta','SkyBlue','PaleTurquoise','DeepPink','Gold','Orange','Brown','DarkKhaki')
fig, ax = plt.subplots(figsize=(9, 6), )
la = [i for i in range(n_class)]
la = sorted(la,reverse=True)
cmap = ListedColormap(colors)
for idx, label in enumerate(la):
ix = y_true[y_true==label].index
x = Y[:, 0][ix]
y = Y[:, 1][ix]
ax.scatter(x, y, c=cmap(idx), label=id_to_label[label], alpha=0.5)
# Shrink current axis by 20%
ax.set_title('proto_loss')
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
def plot_curve(points):
plt.rcParams['pdf.fonttype'] = 42
plt.rcParams['ps.fonttype'] = 42
centers = [[] for x in range(len(points[0]))]
print('centers',centers)
for clusters in points:
clusters = clusters.cpu().detach().numpy()
for i,c in enumerate(clusters):
centers[i].append(c)
print('centers',centers)
plt.figure()
plt.grid(alpha=0.4)
markers = ['o', '*', 's', '^', 'x', 'd', 'D', 'H', 'v', '>', 'h', 'H', 'v', '>', 'v', '<', '>', '1', '2', '3', '4', 'p']
labels = ['c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','unknown']
x = np.arange(-0.02, len(centers[0]) + 0.01).astype(dtype=np.str)
for i,y in enumerate(centers):
plt.plot(x,y,label=labels[i], marker=markers[i])
plt.xlim(0, 20, 1)
plt.xlabel('Epoch', fontsize=12)
plt.ylabel('Decision Boundary $\Delta$', fontsize=12)
plt.legend()
plt.title('50% Known Classes on StackOverflow')
plt.show()
plt.savefig('curve.pdf')