-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperform_dcec.py
195 lines (138 loc) · 6.27 KB
/
perform_dcec.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
import pickle
import numpy as np
import torch
import torch.nn as nn
import keras
from keras.layers import Input, Conv1D, MaxPool1D, Flatten, UpSampling1D, BatchNormalization, LSTM, RepeatVector
from keras.models import Model, load_model
import keras.backend as K
import h5py
from sklearn.cluster import KMeans
from sklearn import metrics
import collections
from model import DCEC
from config import opt
class PerformClustering:
def __init__(self, dic_path, embeddings_path, label_id=None):
'''
data:
1. embeddings: id: [(sent1,emb1), (sent2,emb2), (sent3,emb3), ...]
2. data: [emb1, emb2, emb3, ...]
dictinary:
1. dic: label: label_id
2. reverse_dic: label_id: label
3. emb2sent: emb: sentence
4. emb2id: emb: sentence_id
5. labels_ref: sentence_id: label_id
'''
self.dic_path = dic_path
self.embeddings_path = embeddings_path
with open(self.dic_path, 'rb') as f:
self.dic = pickle.load(f)
self.reverse_dic = {v: k for k,v in self.dic.items()}
self.embeddings = torch.load(self.embeddings_path)
self.emb2sent, self.emb2id, self.data, self.attdata, self.lengths, self.labels_ref = self.prepare_data()
def prepare_data(self):
num_data = sum([len(value) for value in self.embeddings.values()])
emb2sent = {}
emb2id = {}
labels_ref = {}
data = np.zeros((num_data, 768))
attdata = np.zeros((num_data, 20, 768))
lengths = np.zeros((num_data, 1))
number = 0
for key, value in self.embeddings.items():
for (sent, emb, word_emb) in value:
emb2sent[tuple(emb)] = sent
emb2id[tuple(emb)] = key
data[number] = emb
attdata[number] = word_emb
lengths[number] = 20-len(sent.split(" "))
labels_ref[number] = key
number += 1
return emb2sent, emb2id, data, attdata, lengths.astype(np.int32), labels_ref
def random_split(self, ratio):
indices = np.random.permutation(len(self.data))
train_size = int(ratio*len(self.data))
emb_train, emb_test = self.data[indices[:train_size],:], self.data[indices[train_size:],:]
att_train, att_test = self.attdata[indices[:train_size],:,:], self.attdata[indices[train_size:],:,:]
l_train, l_test = self.lengths[indices[:train_size]], self.lengths[indices[train_size:]]
return emb_train, emb_test, att_train, att_test, l_train, l_test
def train(**kwargs):
for k, v in kwargs.items():
setattr(opt, k, v)
cluster = PerformClustering(opt.dic_path, opt.embedding_path)
data = cluster.random_split(0.8)
emb_train, emb_test, att_train, att_test, l_train, l_test = data
print("1. Get data ready!")
model = DCEC(opt.input_shape, opt.filters, opt.kernel_size, opt.n_clusters, opt.weights, data, opt.alpha, pretrain=False)
model.compile(loss='kld', optimizer='adam')
print("3. Compile model!")
model.fit(data, opt)
emb_train, emb_test, att_train, att_test, l_train, l_test = data
# 1. Attention weights
test_func = K.function(model.model.input, model.model.get_layer(name='att_weights').output)
att_weights = test_func([att_test, l_test])
# 2. Cluster center
test_func = K.function(model.model.input, model.model.get_layer(name='cluster').weights)
cluster_centers = test_func([att_test, l_test])
q = model.model.predict([att_test, l_test])
cur_label = np.argmax(q, axis = 1)
with open(opt.cluster_label_path, 'wb') as f:
pickle.dump(cur_label, f)
with open(opt.cluster_data_path, 'wb') as f:
pickle.dump(data, f)
with open(opt.cluster_weight_path, 'wb') as f:
pickle.dump(att_weights, f)
true_label = np.array([cluster.emb2id[tuple(emb.tolist())] for emb in emb_test])
with open('clustering_labels/different_amount_data/se_true_0.pkl', 'wb') as f:
pickle.dump(true_label, f)
with open('clustering_labels/different_amount_data/se_pred_0.pkl', 'wb') as f:
pickle.dump(cur_label, f)
def test(**kwargs):
for k, v in kwargs.items():
setattr(opt, k, v)
cluster = PerformClustering(opt.dic_path, opt.embedding_path)
with open(opt.cluster_data_path, 'rb') as f:
data = pickle.load(f)
with open(opt.cluster_label_path, 'rb') as f:
labels = pickle.load(f)
with open(opt.cluster_weight_path, 'rb') as f:
att_weights = pickle.load(f)
emb_train, emb_test, att_train, att_test, l_train, l_test = data
att_weights = att_weights.squeeze(-1)
#print('Cluster Number:', opt.cluster_id)
cache = collections.defaultdict(list)
for cluster_id in range(opt.n_clusters):
idds = []
sents = []
# Calculate original ids
if cluster_id not in labels:
continue
chosen = np.where(labels == cluster_id)
for emb, weights in zip(emb_test[chosen], att_weights[chosen]):
index = np.argsort(weights)[-3:]
print(np.sort(weights)[-3:])
idd = cluster.emb2id[tuple(emb.tolist())]
sent = cluster.emb2sent[tuple(emb.tolist())]
toks = sent.split(' ')
toks = np.array(toks+['<PAD>']*(20-len(toks)))
idds.append(idd)
sents.append((idd,sent,toks[index]))
# Cluster:
uid = np.unique(idds)
lengths = [len(np.where(idds == uid[i])) for i in range(len(uid))]
cache[uid[np.argmax(lengths)]].append(sents)
cache = sorted(cache.items(), key = lambda x: x[0])
with open('clustering_results/result_atis_att.txt', 'w') as f:
for key, value in cache:
f.write("-"*15)
f.write("\n Original Label: {} \n".format(key))
for i, sents in enumerate(value):
f.write("*"*5+"Mini-cluster {}".format(i)+"*"*5+"\n")
for idd, sent, words in sents:
f.write("Ground Truth: {}, Attention Words: {} | {}\n".format(idd, " ".join(words[::-1]), sent))
f.write("-"*15+"\n\n")
if __name__ == '__main__':
import fire
fire.Fire()