-
Notifications
You must be signed in to change notification settings - Fork 0
/
classification.py
433 lines (354 loc) · 14.6 KB
/
classification.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import classifier as cl
from par2vec import load_all_models
from gensim.models import Doc2Vec
from corpora import get_utterances_from_file
from sklearn import preprocessing
from sklearn import naive_bayes
from collections import Counter
from sklearn.feature_extraction.text import CountVectorizer
import hidden_markov_model as hmm
import numpy as np
import sys
from itertools import groupby
from operator import add
from operator import concat
from itertools import islice
from corpora import get_swda_utterances
from tagset import get_aggregated_tagset_mapping
from utils import Doc2VecKeyError
def load_data_hmm():
train_sequences = []
test_sequences = []
curr_sequence = []
first = True
for tag, tokens in get_utterances_from_file("data/swda_utterances.train"):
if first:
cur_id = tag.split('/')[1].split('_')[0]
first = False
elif cur_id != tag.split('/')[1].split('_')[0]:
cur_id = tag.split('/')[1].split('_')[0]
train_sequences.append(curr_sequence)
curr_sequence = []
curr_sequence.append((tag, " ".join(tokens)))
train_sequences.append(curr_sequence)
curr_sequence = []
first = True
for tag, tokens in get_utterances_from_file("data/swda_utterances.test"):
if first:
cur_id = tag.split('/')[1].split('_')[0]
first = False
elif cur_id != tag.split('/')[1].split('_')[0]:
cur_id = tag.split('/')[1].split('_')[0]
test_sequences.append(curr_sequence)
curr_sequence = []
curr_sequence.append((tag, " ".join(tokens)))
test_sequences.append(curr_sequence)
curr_sequence = []
return train_sequences, test_sequences
def load_data():
train_utt = []
train_Y = []
test_utt = []
test_Y = []
for tag, tokens in get_utterances_from_file("data/swda_utterances.train"):
train_utt.append(" ".join(tokens))
train_Y.append(tag)
for tag, tokens in get_utterances_from_file("data/swda_utterances.test"):
test_utt.append(" ".join(tokens))
test_Y.append(tag)
return train_utt, train_Y, test_utt, test_Y
def encode_tags(train_Y, test_Y, aggregated_tagset=False):
mapping = get_aggregated_tagset_mapping()
# get tags according to aggregated group
if not aggregated_tagset:
train_Y = [t.split("/")[0] for t in train_Y]
test_Y = [t.split("/")[0] for t in test_Y]
else:
train_Y = [mapping[t.split("/")[0]] for t in train_Y]
test_Y = [mapping[t.split("/")[0]] for t in test_Y]
assert len(set(train_Y).difference(set(mapping.values()))) == 0, 'Error in mapping train tagset'
assert len(set(test_Y).difference(set(mapping.values()))) == 0, 'Error in mapping test tagset'
# train the encoder
le = preprocessing.LabelEncoder()
le.fit(train_Y + test_Y)
# transform tags
train_Y = le.transform(train_Y)
test_Y = le.transform(test_Y)
return train_Y, test_Y
def represent_random(train_utt, test_utt):
# this doesn't actually do anything meaningfull
train_X = []
test_X = []
i = 0
for trans in test_utt:
for utt in trans:
test_X.append(np.random.random(10))
sys.stderr.write("\r");
sys.stderr.write("utterance %s" % i);
sys.stderr.flush();
i += 1
for trans in train_utt:
for utt in trans:
train_X.append(np.random.random(10))
sys.stderr.write("\r");
sys.stderr.write("utterance %s" % i);
sys.stderr.flush();
i += 1
sys.stderr.write("\n")
return train_X, test_X
def represent_simple(utterances, model):
# this represents every utterance as it's own embedding this should be extended to encorporate some context
X = []
i = 0
for utt in utterances:
X.append(model.infer_vector(utt.split()))
sys.stderr.write("\r");
sys.stderr.write("utterance %s" % i);
sys.stderr.flush();
i += 1
sys.stderr.write("\n")
return X
def represent_lookup(labels, model):
# this represents every utterance as it's own embedding this should be extended to encorporate some context
X = []
i = 0
for label in labels:
X.append(model.docvecs[label])
sys.stderr.write("\r");
sys.stderr.write("utterance %s" % i);
sys.stderr.flush();
i += 1
sys.stderr.write("\n")
return X
def e_add(utt1,utt2):
'''
returns a function that adds two lists elementwise.
:param utt1:
:param utt2:
:return:
'''
return map(add, utt1, utt2)
def represent_mix_simple(utterances, tags, model, op):
'''
incorporates previous utterance with operator param.
retrieves utterance representations via inference
:param labels:
:param model:
:return:
'''
# utterances: tag + utt.conversation_no + utt.caller + c
# this represents every utterance as it's own embedding this should be extended to encorporate some context
X = []
for j, utt in enumerate(utterances):
actual_utt = list(model.infer_vector(utt.split()))
if j == 0:
# if its the first utt in the list, we dont have a prev utterance. Repeat the actual utt.
mixed_rep = op(actual_utt,actual_utt)
else:
# if its the beginning of a convo, we dont have a prev utterance. Repeat the actual utt.
prev_conv_nr = tags[j-1].split('/')[1].split('_')[0]
actual_conv_nr = tags[j].split('/')[1].split('_')[0]
prev_utt = list(model.infer_vector(utterances[j-1])) if prev_conv_nr == actual_conv_nr else actual_utt
mixed_rep = op(prev_utt,actual_utt)
X.append(mixed_rep)
if (j+1) % 1000 == 0:
sys.stderr.write("\r");
sys.stderr.write("utterance %s" % (j+1));
sys.stderr.flush();
sys.stderr.write("\r");
sys.stderr.write("utterance %s" % (j+1));
sys.stderr.flush();
sys.stderr.write("\n")
return X
def get_lookedup_vector(model, key):
'''
doc2vec returns an ndim-3 array if it doesnt find the key,
:param model:
:param key:
:return:
'''
vec = model.docvecs[key]
# if the doc2vec model returns trash, return None
if not vec.ndim == 1:
raise Doc2VecKeyError
return vec
def represent_mix_lookup(labels, model, op):
'''
incorporates previous utterance with operator param.
retrieves utterance representations via lookup
:param labels:
:param model:
:return:
'''
i = 0 # nr of keys not found
X = []
for j, label in enumerate(labels):
try:
actual_vector = get_lookedup_vector(model, label)
except Doc2VecKeyError:
i += 1
continue
actual_utt = list(actual_vector)
if j == 0:
mixed_rep = op(actual_utt, actual_utt)
else:
prev_conv_utt = labels[j-1].split('/')[1].split('_')[0]
actual_conv_utt = label.split('/')[1].split('_')[0]
try:
prev_vector = get_lookedup_vector(model, labels[j-1])
except Doc2VecKeyError:
i += 1
continue
prev_utt = list(prev_vector) if prev_conv_utt == actual_conv_utt else actual_utt
mixed_rep = op(prev_utt, actual_utt)
X.append(mixed_rep)
if (j+1) % 1000 == 0:
sys.stderr.write("\r");
sys.stderr.write("utterance %s" % (j+1));
sys.stderr.flush();
sys.stderr.write("\r");
sys.stderr.write("utterance %s" % (j+1));
sys.stderr.flush();
sys.stderr.write("\n")
if i > 0:
sys.stderr.write('## WARNING: Some samples were discarded!\n')
return X
def bow_representation(train_utt, test_utt):
# how are we going to do some context representation here?
vectorizer = CountVectorizer(min_df=1)
# fit + transform on all training data
train_X = vectorizer.fit_transform(train_utt)
# only transform on all test data
test_X = vectorizer.transform(test_utt)
return train_X, test_X
def baseline_scores(train_utt, train_Y, test_utt, test_Y):
print "Calculating baseline BOW scores"
# Baseline scores, use BOW representation of utterances
train_X, test_X = bow_representation(train_utt, test_utt)
train_Y, test_Y = encode_tags(train_Y, test_Y)
print "BOW representation created"
print "KNN Accuracy: ", cl.KNN_classifier(train_X, train_Y, test_X, test_Y)
# print "SVM Accuracy: ", cl.SVM_classifier(train_X, train_Y, test_X, test_Y)
print "NB Accuracy: ", cl.NB_classifier(train_X, train_Y, test_X, test_Y)
def evaluate_model(embedding_model_location, with_context=False, f=e_add, aggregated_tagset=False):
# load training and test data
train_utt, train_Y, test_utt, test_Y = load_data()
# uncomment this to find basline scores
# baseline_scores(train_utt, train_Y, test_utt, test_Y)
print "Creating representations"
# Load utterance embedding models
embedding_model, _ = load_all_models(embedding_model_location)
# represent utterances in some way,
# train_X = represent_simple(train_utt, embedding_model)
# test_X = represent_simple(test_utt, embedding_model)
if with_context:
# ---------- lqrz: add or concatenate the previous utterance
# f = concat
# f = e_add
test_X = represent_mix_simple(test_utt, test_Y, embedding_model, f)
train_X = represent_mix_lookup(train_Y, embedding_model, f)
# ----------
else:
train_X = represent_lookup(train_Y, embedding_model)
test_X = represent_simple(test_utt, embedding_model)
# encode tags
train_Y, test_Y = encode_tags(train_Y, test_Y, aggregated_tagset=aggregated_tagset)
# print np.array(train_X).shape, np.array(test_X).shape, np.array(train_Y).shape, np.array(test_Y).shape
print "Training classifiers"
# Train classifiers, print scores
print "Model: ", embedding_model_location
print "KNN Accuracy: ", cl.KNN_classifier(train_X, train_Y, test_X, test_Y)
# print "SVM Accuracy: ", cl.SVM_classifier(train_X, train_Y, test_X, test_Y)
print "NB Accuracy: ", cl.NB_classifier(train_X, train_Y, test_X, test_Y)
print "MLP Accuracy: ", cl.MLP_classifier(train_X, train_Y, test_X, test_Y, n_iter=10)
def classify_context_dependent():
# load data as:
# training:
# sequences of DA tags with corresponding speaker sequences for discourse model
# utterances with corresponding DA tags for language model
# testing:
# sequences of utterances with corresponding speakers and DA tags (for evaluation)
embedding_model_location = 'data/swda_bnc_noint_50_300'
print "Loading Data"
train_dialogs, test_dialogs = load_data_hmm()
# train a label encoder
all_tags = []
i = 0
for dialog in train_dialogs+test_dialogs:
for tag, utterance in dialog:
all_tags.append(tag.split('/')[0])
i += 1
le = preprocessing.LabelEncoder()
transformed_tags = le.fit_transform(all_tags)
# This is not the way to do it
emmision_probabilites = np.array([len(list(group)) for key, group in groupby(sorted(transformed_tags))])/float(len(transformed_tags))
emmision_probability_matrix = np.vstack((emmision_probabilites,)*len(dialog)).T # for testing
print "Loading sentence model"
# train/load 'language model'
# embedding_model = Doc2Vec.load(embedding_model_location+'.model')
# load a doc2vec model to train the language model with
vectorizer = CountVectorizer(min_df=1)
# fit + transform on all training data
train_utt =[]
train_Y = []
# train_X = []
for dialog in train_dialogs:
for tag, utt in dialog:
train_utt.append(utt)
# train_X.append(embedding_model.infer_vector(utt))
train_Y.append(tag.split('/')[0])
train_X = vectorizer.fit_transform(train_utt)
# model = mlp.Classifier(layers=[mlp.Layer("Sigmoid", units=100), mlp.Layer("Softmax")], learning_rate=0.001, n_iter=25)
model = naive_bayes.BernoulliNB()
model.fit(train_X, train_Y)
# model.fit(np.array(train_X), np.array(train_Y))
# learn transition matrix
speaker_sequences = []
tag_sequences = []
for dialog in train_dialogs:
speaker_sequences.append([tag[0].split('/')[1].split('_')[1] for tag in dialog])
tag_sequences.append(le.transform([tag[0].split('/')[0] for tag in dialog]))
print "Learning a Transition Matrix"
start_prob, transition_matrix = hmm.learn_transition_matrix(tag_sequences, speaker_sequences, number_of_states = 43, order = 1)
print "Decoding"
# evauluation:
correct = 0
total = 0
probas = []
i = 0
for dialog in test_dialogs:
print i
i += 1
for tag, utt in dialog:
# rep = embedding_model.infer_vector([utt])
rep = vectorizer.transform([utt])
probas.append(model.predict_proba(rep)/ emmision_probabilites)
# this needs to be given the representation it needs in the language model
# then we can use this with the predict_proba to calculate the emmision probability matrix
# emmision_probability_matrix = np.vstack((emmision_probabilites,)*len(dialog)).T # for testing
# print emmision_probability_matrix
emmision_probability_matrix = np.vstack(probas).T
# print emmision_probability_matrix
true_tags = le.transform([tag[0].split('/')[0] for tag in dialog])
speakers = [tag[0].split('/')[1].split('_')[1] for tag in dialog]
predicted = hmm.viterbi_decoder(speakers,speakers, start_prob, transition_matrix, emmision_probability_matrix, order = 1)
score = hmm.evaluate_sequence(predicted, true_tags)
correct += score
total += len(dialog)
print float(correct)/total
# for every testing dialog, decode with viterbi
# calculate accuracy maybe confusion matrix
if __name__ == '__main__':
# classify_context_dependent()
train_utt, train_Y, test_utt, test_Y = load_data()
train_Y, test_Y = encode_tags(train_Y,test_Y)
c = Counter(test_Y)
print c.most_common(1)[0][1]/float(len(test_Y))
# baseline_scores(train_utt, train_Y, test_utt, test_Y)
# # train_utt, train_Y, test_utt, test_Y = load_data()
# # baseline_scores(train_utt, train_Y, test_utt, test_Y)
# # evaluate_model('data/models/swda_only_300')
# # train_utt, train_Y, test_utt, test_Y = load_data()
# # baseline_scores(train_utt, train_Y, test_utt, test_Y)
# # if with_context selected, then choose an agg function such as 'concat' or 'e_add'
# evaluate_model('data/models/swda_only_10_300', with_context=True, f=e_add, aggregated_tagset=True)