-
Notifications
You must be signed in to change notification settings - Fork 2
/
kmeans_bert.py
185 lines (168 loc) · 6.66 KB
/
kmeans_bert.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
from bert_serving.client import BertClient
import numpy as np
import csv
import os
from sklearn.cluster import KMeans
from sklearn.metrics import normalized_mutual_info_score
from sklearn.metrics import adjusted_mutual_info_score
bc = BertClient()
def encode_sentences(sents):
data_size = len(sents)
all_emb = bc.encode(sents)
result = []
for i in range(data_size):
emb = all_emb[i]
emb = emb[np.sum(emb > 0, axis=-1) > 0]
result.append(emb)
return result
def cluster_acc(y_true, y_pred):
"""
Calculate clustering accuracy. Require scikit-learn installed
# Arguments
y: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
accuracy, in [0,1]
"""
y_true = y_true.astype(np.int64)
y_pred = y_pred.astype(np.int64)
assert y_pred.size == y_true.size, 'y_pred.size {} y_true.size {}'.format(y_pred.size, y_true.size)
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
from sklearn.utils.linear_assignment_ import linear_assignment
ind = linear_assignment(w.max() - w)
return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
def load_csv_corpus(path):
labels = []
sents = []
with open(path, 'r') as f:
reader = csv.reader(f)
for item in reader:
labels.append(int(item[0]))
tmp_doc = item[1].strip()
sents.append(tmp_doc)
ids = range(len(sents))
return sents, labels, ids
def feat_extraction(sents, batch_size=32):
data_size = len(sents)
feat_max = []
feat_mean = []
feat_last = []
for i in range(0, data_size, batch_size):
batch_sents = sents[i: i + batch_size]
# batch_feat_lst = encoder.embed_batch([s.split() for s in batch_sents])
batch_feat_lst = encode_sentences(batch_sents)
feat_max.extend([np.max(tmp, axis=0) for tmp in batch_feat_lst])
feat_mean.extend([np.mean(tmp, axis=0) for tmp in batch_feat_lst])
feat_last.extend([tmp[0] for tmp in batch_feat_lst])
print(i)
return np.stack(feat_max), np.stack(feat_mean), np.stack(feat_last)
def cluster_alg(feat, n_clusters):
kmeans = KMeans(n_clusters=n_clusters, n_init=10, n_jobs=10, verbose=True)
pred = kmeans.fit_predict(feat)
return pred
def ln(feat):
return (feat - feat.mean(axis=1, keepdims=True)) / feat.std(axis=1, keepdims=True)
def norm(feat):
return feat / np.linalg.norm(feat, axis=1, keepdims=True)
def dump_mongo(corpora, feat_name, n_topics, acc, pred, all_pred, all_acc, all_nmi, all_ari):
acc_std = np.std(all_acc)
acc_mean = np.mean(all_acc)
nmi_std = np.std(all_nmi)
nmi_mean = np.mean(all_nmi)
ari_std = np.std(all_ari)
ari_mean = np.mean(all_ari)
best_nmi = np.max(all_nmi)
best_ari = np.max(all_ari)
tmp = {
'corpora': corpora,
'feat_name': feat_name,
'n_topics': n_topics,
'best_pred': pred,
'best_acc': acc,
'best_nmi':best_nmi,
'best_ari':best_ari,
'all_pred': all_pred,
'all_acc': all_acc,
'acc_std':acc_std,
'acc_mean':acc_mean,
'all_nmi':all_nmi,
'nmi_std':nmi_std,
'nmi_mean':nmi_mean,
'all_ari':all_ari,
'ari_std':ari_std,
'ari_mean':ari_mean}
with open('bert_results.txt','a') as f:
import json
f.write(json.dumps(tmp) + '\n')
if False:
from pymongo import MongoClient
client = MongoClient('59.72.109.90', 27017)
cluster_db = client.cluster_db
results = cluster_db.elmo_results
results.insert_one(tmp)
client.close()
feat_func_dict = {'ln': ln, 'n': norm, 'i': lambda x: x}
data_dict = {0:'ag_news',1:'dbpedia', 2:'yahoo_answers', 3:'R2', 4:'R5',5:'R10', 6:'R19'}
n_cluster_dict = {0: 4, 1: 14, 2: 10, 3:2, 4:5, 5:10, 6:19}
if __name__ == '__main__':
if False:
def get_args():
import argparse
parser = argparse.ArgumentParser(description='ElMo')
parser.add_argument('--corpora_id', type=int, default=0, help='corpora id')
parser.add_argument('--batch_size', type=int, default=32, help='batch_size')
args = parser.parse_args()
return args
args = get_args()
assert 0 <= args.corpora_id <= 6
from collections import namedtuple
ARGS= namedtuple('ARGS', ['corpora_id', 'batch_size'])
for corpora_id in range(0, 7):
args = ARGS(corpora_id=corpora_id, batch_size=32)
corpora_name = data_dict[args.corpora_id]
n_clusters = n_cluster_dict[args.corpora_id]
train_path = os.path.join('data', corpora_name, 'train.csv')
sents, labels, _ = load_csv_corpus(train_path)
labels = np.array(labels)
feat_max, feat_mean, feat_last = feat_extraction(sents, batch_size=args.batch_size)
all_feat = {'bert_max':feat_max, 'bert_mean':feat_mean, 'bert_first': feat_last}
#
trial_num = 10
#
for feat_name, feat in all_feat.items():
for func_name, feat_trans_func in feat_func_dict.items():
best_acc = 0.0
best_pred = None
feat_tmp = feat_trans_func(feat)
all_pred = []
all_acc = []
all_nmi = []
all_ari = []
for i in range(trial_num):
pred = cluster_alg(feat_tmp, n_clusters)
acc = cluster_acc(labels, pred)
nmi = normalized_mutual_info_score(labels, pred)
ari = adjusted_mutual_info_score(labels, pred)
all_pred.append(pred.tolist())
all_acc.append(acc)
all_nmi.append(nmi)
all_ari.append(ari)
if acc > best_acc:
best_pred = pred
best_acc = acc
tmp_feat_name = feat_name + '_{}'.format(func_name)
print('{} {} best acc is {}'.format(tmp_feat_name, func_name, best_acc))
pred_std = np.std(all_acc)
pred_mean = np.mean(all_acc)
dump_mongo(corpora=corpora_name,
feat_name=tmp_feat_name,
n_topics=n_clusters,
acc=best_acc,
pred=best_pred.tolist(),
all_pred=all_pred,
all_acc=all_acc,
all_nmi=all_nmi,
all_ari=all_ari)