/
active_learning.py
307 lines (248 loc) · 12.6 KB
/
active_learning.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
from model.config import Config
import numpy as np
import math
import random
import collections
import sys
from sklearn.metrics.pairwise import cosine_similarity as cos_sim
from sklearn.cluster import SpectralClustering
import tensorflow as tf
config = Config()
#--------------------------------------------------------------------------
# follow instructions from : https://github.com/ryankiros/skip-thoughts
#--------------------------------------------------------------------------
#sys.path.append('../Skip Thoughts')
#import skipthoughts
class Siamese_Model():
def __init__(self, session):
self.sess = session
self.saver1 = tf.train.import_meta_graph(config.dir_model_similarity
+ '/siamese_model.meta')
self.siamese_graph = tf.get_default_graph()
self.saver1.restore(self.sess, config.dir_model_similarity
+ '/siamese_model')
self.output1 = self.siamese_graph.get_tensor_by_name('output/out1:0')
self.output2 = self.siamese_graph.get_tensor_by_name('output/out2:0')
def run(self, x1_batch, x2_batch, seq_len1, seq_len2, max_len, batch_size, len2):
feed_dict = {
'input_x1:0': x1_batch,
'input_x2:0': x2_batch,
'seq_len1:0': seq_len1,
'seq_len2:0': seq_len2,
'max_seq_len:0': max_len,
'dropout_keep_prob:0': 1,
'batch_size_dynamic:0': batch_size,
}
out1, out2 = self.sess.run([self.output1, self.output2],
feed_dict=feed_dict)
return out1, out2[:len2]
#----------------------------------------------------------------------
# Active Learning : Layer 1 (retains the most confused examples)
# : Layer 2 (retains the most representative examples)
# : Layer 3 (outlier detection)
#----------------------------------------------------------------------
class Active_Learning():
def __init__(self, strategy):
self.active_algo = strategy
def active_strategy(self, score, transition_params, tag_to_idx):
"""
Args: output of CRF
score: A [seq_len, num_tags] matrix of unary potentials.
transition_params: A [num_tags, num_tags] matrix of binary potentials.
"""
if self.active_algo == "cluster":
# print('score: ',score)
return score
trellis = np.zeros_like(score)
backpointers = np.zeros_like(score, dtype=np.int32)
trellis[0] = score[0]
for t in range(1, score.shape[0]):
v = np.expand_dims(trellis[t - 1], 1) + transition_params
trellis[t] = score[t] + np.max(v, 0)
backpointers[t] = np.argmax(v, 0)
viterbi = [np.argmax(trellis[-1])]
for bp in reversed(backpointers[1:]):
viterbi.append(bp[viterbi[-1]])
viterbi.reverse()
score_final = np.max(trellis[-1]) # Score of sequences (higher = better)
if (self.active_algo == 'margin'):
top_scores = trellis[-1][np.argsort(trellis[-1])[-2:]]
margin = abs(top_scores[0] - top_scores[1])
score_final = margin
# print('score_final: ', score_final)
elif (self.active_algo == 'ne'):
ne = ['NE.AMBIG', 'NE.DE', 'NE.LANG3', 'NE.MIXED', 'NE.OTHER', 'NE.TR']
ne_idx = []
for i in tag_to_idx:
if i in ne:
ne_idx.append(tag_to_idx[i])
# Get the highest score of NE
max_ne = []
# for i in ne_idx:
# max_ne.append(np.max(score[:,i]))
score_final = 0
for i in viterbi:
if i in ne_idx:
score_final += 1 # give higher score to sequences that have more named entities
# score_final = np.max(max_ne)
elif (self.active_algo == 'nemg'): # ne margin
ne_idx = tag_to_idx['NE.DE']
ne_de = tag_to_idx['DE']
margin = np.add(score[:, ne_idx], score[:, ne_de])
margin2 = abs(np.multiply(score[:, ne_idx], score[:, ne_de]))
margin = np.divide(margin, margin2)
sum_margin = np.sum(margin)
score_final = sum_margin
if (self.active_algo == 'entropy'):
# Find the highest prob for each token
ntoken = len(score)
ntags = len(score[0])
l = [] # max prob of each token
for i in range(0, ntoken):
l.append(np.max(score[i]))
ne_idx = tag_to_idx
# Compute entropy
score_final = 0.0
for i in range(0, ntoken):
score_final += l[i] * np.log(l[i])
score_final = score_final / ntoken #length normalized entropy
return score_final
def feedback(self, newSamples, dummy_train, words_conf, tags_conf, prob, enc, seq_len):
'''
feeds back data from validation set to retrain
set in a self-training (active learning) setup
function performs :
1) selecting most confused samples
2) retaining most representative samples from Step 1
2) outlier removal
newSamples : writing the most representative samples
to a different file for re-trainings.
'''
if config.similarity == 'None':
index = np.arange(len(prob))
else:
print('\nClustering to find similarity \n')
clusters= self.cluster_sentences(enc, seq_len, words_conf)
most_representative_index = []
for cluster in range(config.nclusters):
clust = clusters[cluster]
# ----------------------------------------------------------------
# Select the top two lowest confidence sentences from each cluster.
# The clusters which have lesser than a fixed number of samples are
# considered as outliers and dropped.
#-----------------------------------------------------------------
most_representative_index += \
[x for _,x in sorted(zip(list(map(lambda x:prob[x],clust)),clust))][:2]
index = sorted(most_representative_index)
with open(newSamples, 'a') as handle1, \
open(dummy_train, 'a') as handle2:
for words, tags in zip(np.array(words_conf)[index], np.array(tags_conf)[index]):
[handle1.write(word + ' ' + tag + '\n')
for word, tag in zip(words, tags) if word not in ['$UNK$', '$NUM$']]
handle1.write('\n\n')
[handle2.write(word + ' ' + tag + '\n')
for word, tag in zip(words, tags) if word not in ['$UNK$', '$NUM$']]
handle2.write('\n\n')
print('sentences fedback : ', len(index))
return len(index)
def random_sampling(self, train_file, newSamples, retrain_file, num_fedback):
'''
mixed samples from train data and the feedback samples
function performs : mixed sampling for incremental training
retrain_file : all mixed samples are written to the retrain file
'''
split_size = math.ceil(14986 / config.num_splits) # train_size = 14986
#--------------------------------------------------------------------------------------
# keeping a fixed proportion of samples from both train file and low confidence samples
# num_fedback : total samples fedback from all active learning rounds
#--------------------------------------------------------------------------------------
train_samples = random.sample(range(split_size), config.sample_train)
confused_samples = list(random.sample(range(int(num_fedback)),
min(int(num_fedback * 0.5), config.sample_train)))
def write(samples, filename, text, type):
j = 0
curr_line = None
file = open(filename, 'r')
with open(retrain_file, type) as sp:
print('\n Writing {} {} to retrain file...\n'.format(len(samples), text))
for line in file.readlines():
line = line.strip()
if line == '' and curr_line == '':
j += 1
curr_line = line
[sp.write(line + '\n') for i in samples if j == i]
#--------------------------------------------------
# writing feedback samples to corresponding files
#--------------------------------------------------
write(confused_samples, newSamples, 'low confidence sentences', 'w')
write(train_samples, train_file, 'new samples from train', 'a')
def spectral_clustering(self, X, nclusters):
#--------------------------------------------------------------
# performs spectral clustering on predefined distance matrix X
#-------------------------------------------------------------
print('======Clustering======')
clustering = SpectralClustering(n_clusters=nclusters, random_state=0, affinity='precomputed').fit(X)
clusters = collections.defaultdict(list)
for idx, label in enumerate(clustering.labels_):
clusters[label].append(idx)
return clusters
def cluster_sentences(self, enc, seq_len, words_conf):
# ------------------------------------------------------------
# dynamic clustering depending on num low confidence samples
# ------------------------------------------------------------
n_clusters = int(len(seq_len) / config.num_clusters)
print('\nSimilarity metric is {}\n'.format(config.similarity))
if config.similarity == 'siamese':
print("\nReloading the sentence similarity model...\n")
graph = tf.Graph()
with graph.as_default():
sess = tf.Session()
siamese = Siamese_Model(sess)
#---------------------------------------------------
# take all possible pairwise combinations of confused
# samples to obtain the similarity scores pairwise.
# The similarity matrix is symmetric.
#---------------------------------------------------
split1, split2 = np.array_split(np.arange(len(seq_len)),2)
max_len = max(seq_len)
seq_len1, seq_len2 = \
[seq_len[i] for i in split1], [seq_len[i] for i in split2]
if not config.model_aware:
sent1, sent2 = [np.array(enc[i][0][0]).tolist() for i in split1], [np.array(enc[i][0][0]).tolist() for i in split2]
else:
sent1, sent2 = [enc[i] for i in split1], [enc[i] for i in split2]
if config.model.split()[1] == 'LSTM' or not config.model_aware:
dim = config.hidden_size_lstm
else:
dim = 2 * config.hidden_size_lstm
for i, row in enumerate(sent1):
if len(row) <= max_len:
sent1[i] += [np.zeros(dim).tolist()] * (max_len - len(row))
try:
sent2[i] += [np.zeros(dim).tolist()] * (max_len - len(sent2[i]))
except IndexError:
sent2 += [[np.zeros(dim).tolist()] * len(sent1[i])]
seq_len2 += [1]
siamese_enc = np.concatenate(siamese.run(sent1, sent2, seq_len1, seq_len2,
max_len, len(split1), len(split2)))
def similarity_scores(enc):
shape = np.array(enc).shape
out = np.reshape(np.repeat(enc, [shape[0]], axis=0), (-1,shape[0],shape[1]))
X = np.exp(-1 * np.sqrt(np.sum(
np.square(out - np.transpose(out, (1,0,2))), 2, keepdims=False)))
return X
X = similarity_scores(siamese_enc)
clustering = self.spectral_clustering(X, n_clusters)
elif config.similarity == 'cosine':
enc1 = [emb[-1] for emb in enc]
X = np.exp(cos_sim(enc1, enc1))
clustering = self.spectral_clustering(X, n_clusters)
elif config.similarity == 'skipthoughts':
model = skipthoughts.load_model()
encoder = skipthoughts.Encoder(model)
vectors = encoder.encode([' '.join(list) for list in words_conf])
X = np.exp(cos_sim(vectors, vectors))
#vectors = vectors / np.linalg.norm(vectors)
#X = np.cos(np.dot(vectors, np.transpose(vectors)))
clustering = self.spectral_clustering(X, n_clusters)
return clustering