-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_models.py
More file actions
283 lines (251 loc) · 12.5 KB
/
run_models.py
File metadata and controls
283 lines (251 loc) · 12.5 KB
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
import os
import math
import random
from gensim import corpora
from gensim.models import FastText
from gensim.models.fasttext import save_facebook_model
from settings.common import save_topics, load_flat_dataset
from tm_pipeline.tndmallet import TndMallet
from tm_pipeline.ldamallet import LdaMallet
from tm_pipeline.etndmallet import eTndMallet
def run_LDA(dataset, dataset_name, mallet_path, param_combos, results_path):
'''
Run an instance of LDA for each parameter setting
:param dataset:
:param dataset_name:
:param mallet_path:
:param param_combos:
:return:
'''
if not os.path.exists('{}/{}/lda/'.format(results_path, dataset_name)):
os.makedirs('{}/{}/lda/'.format(results_path, dataset_name))
dictionary = corpora.Dictionary(dataset)
dictionary.filter_extremes()
corpus = [dictionary.doc2bow(doc) for doc in dataset]
for param_combo in param_combos:
k = param_combo[0]
model = LdaMallet(mallet_path, corpus, num_topics=k, id2word=dictionary)
topics = model.show_topics(num_topics=k, num_words=20, formatted=False)
topic_words = []
for topic in topics:
t = [w for (w, _) in topic[1]]
topic_words.append(t)
save_topics(topic_words, '{}/{}/lda/topics_{}.csv'.format(results_path, dataset_name, k))
def run_TND_MALLET(dataset, dataset_name, mallet_path, param_combos, results_path):
if not os.path.exists('{}/{}/tnd/'.format(results_path, dataset_name)):
os.makedirs('{}/{}/tnd/'.format(results_path, dataset_name))
dictionary = corpora.Dictionary(dataset)
dictionary.filter_extremes()
corpus = [dictionary.doc2bow(doc) for doc in dataset]
model = None
for param_combo in param_combos:
k = param_combo[0]
alpha = param_combo[1]
beta = param_combo[2]
skew = param_combo[3]
nwm = param_combo[4]
iterations = param_combo[5]
model = TndMallet(mallet_path, corpus, num_topics=k, id2word=dictionary, workers=4,
alpha=alpha, beta=beta, skew=skew, noise_words_max=nwm, iterations=iterations)
topics = model.show_topics(num_topics=k, num_words=20, formatted=False)
noise = model.load_noise_dist()
noise_list = sorted([(x, noise[x]) for x in noise.keys()], key=lambda x: x[1], reverse=True)
topic_words = []
for topic in topics:
t = [w for (w, _) in topic[1]]
topic_words.append(t)
save_topics(topic_words, '{}/{}/tnd/topics_{}_{}.csv'.format(results_path, dataset_name, k, skew))
with open('{}/{}/tnd/noise_{}_{}.csv'.format(results_path, dataset_name, k, skew), 'w') as f:
for pair in noise_list:
f.write('{},{}\n'.format(pair[0], pair[1]))
return model
def run_ETND_MALLET(dataset, dataset_name, mallet_path, param_combos, results_path):
if not os.path.exists('{}/{}/etnd/'.format(results_path, dataset_name)):
os.makedirs('{}/{}/etnd/'.format(results_path, dataset_name))
dictionary = corpora.Dictionary(dataset)
dictionary.filter_extremes()
corpus = [dictionary.doc2bow(doc) for doc in dataset]
model = None
for param_combo in param_combos:
k = param_combo[0]
alpha = param_combo[1]
beta = param_combo[2]
skew = param_combo[3]
nwm = param_combo[4]
embedding_path = param_combo[5].format(dataset_name)
closest_x_words = param_combo[6]
tau = param_combo[7]
iterations = param_combo[8]
model = eTndMallet(mallet_path, corpus, num_topics=k, id2word=dictionary, workers=4,
alpha=alpha, beta=beta, skew=skew, noise_words_max=nwm, tau=tau, embedding_path=embedding_path,
closest_x_words=closest_x_words, iterations=iterations)
topics = model.show_topics(num_topics=k, num_words=20, formatted=False)
noise = model.load_noise_dist()
noise_list = sorted([(x, noise[x]) for x in noise.keys()], key=lambda x: x[1], reverse=True)
topic_words = []
for topic in topics:
t = [w for (w, _) in topic[1]]
topic_words.append(t)
save_topics(topic_words, '{}/{}/etnd/topics_{}_{}_{}.csv'.format(results_path, dataset_name, k, skew, closest_x_words))
with open('{}/{}/etnd/noise_{}_{}_{}.csv'.format(results_path, dataset_name, k, skew, closest_x_words), 'w') as f:
for pair in noise_list:
f.write('{},{}\n'.format(pair[0], pair[1]))
return model
def run_NLDA(dataset, dataset_name, mallet_path, nft_mallet_path, param_combos, results_path, noise_dist=None):
'''
:param dataset:
:param dataset_name:
:param mallet_path:
:param param_combos:
:return:
'''
if not os.path.exists('{}/{}/nlda/'.format(results_path, dataset_name)):
os.makedirs('{}/{}/nlda/'.format(results_path, dataset_name))
dictionary = corpora.Dictionary(dataset)
dictionary.filter_extremes()
corpus = [dictionary.doc2bow(doc) for doc in dataset]
model_tuple = [noise_dist, None]
for param_combo in param_combos:
noise_params = param_combo[1]
lda_param_combos = param_combo[0]
topic_weights = param_combo[2]
if noise_dist is None:
model = run_TND_MALLET(dataset, dataset_name, nft_mallet_path, [noise_params], results_path=results_path)
noise_dist = model.load_noise_dist()
model_tuple[0] = noise_dist
for lda_params in lda_param_combos:
lda_k = lda_params[0]
model = LdaMallet(mallet_path, corpus, num_topics=lda_k, id2word=dictionary)
model_tuple[1] = model
topic_word_distribution = model.load_word_topics()
topics = model.show_topics(num_topics=lda_k, num_words=100, formatted=False)
for topic_weight in topic_weights:
final_topics = []
for i in range(0, len(topics)):
topic = [w for (w, _) in topics[i][1]]
final_topic = []
j = 0
while len(final_topic) < 20 and j < 100 and j < len(topic):
w = topic[j]
id = dictionary.token2id[w]
beta = 2
if w in noise_dist:
beta += noise_dist[w]
beta = max(2, beta * (topic_weight / lda_k))
alpha = 2 + topic_word_distribution[i, id]
roll = random.betavariate(alpha=math.sqrt(alpha), beta=math.sqrt(beta))
if roll >= 0.5:
final_topic.append(w)
if not w in noise_dist:
noise_dist[w] = 0
noise_dist[w] += (alpha - 2)
j += 1
final_topics.append(final_topic)
param_string = '_'.join([str(x).replace('.', '-')
for x in lda_params])
save_topics(final_topics, '{}/{}/nlda/topics_{}_{}.csv'
.format(results_path, dataset_name, param_string, topic_weight))
return model_tuple
def run_eNLDA(dataset, dataset_name, mallet_path, nft_mallet_path, param_combos, results_path, noise_dist=None):
'''
:param dataset:
:param dataset_name:
:param mallet_path:
:param param_combos:
:return:
'''
if not os.path.exists('{}/{}/enlda/'.format(results_path, dataset_name)):
os.makedirs('{}/{}/enlda/'.format(results_path, dataset_name))
dictionary = corpora.Dictionary(dataset)
dictionary.filter_extremes()
corpus = [dictionary.doc2bow(doc) for doc in dataset]
model_tuple = [noise_dist, None]
for param_combo in param_combos:
noise_params = param_combo[1]
lda_param_combos = param_combo[0]
topic_weights = param_combo[2] # tuple of topic weights to try
if noise_dist is None:
model = run_ETND_MALLET(dataset, dataset_name, nft_mallet_path, [noise_params], results_path=results_path)
noise_dist = model.load_noise_dist()
model_tuple[0] = noise_dist
for lda_params in lda_param_combos:
lda_k = lda_params[0]
model = LdaMallet(mallet_path, corpus, num_topics=lda_k, id2word=dictionary)
model_tuple[1] = model
topic_word_distribution = model.load_word_topics()
topics = model.show_topics(num_topics=lda_k, num_words=100, formatted=False)
for topic_weight in topic_weights:
final_topics = []
for i in range(0, len(topics)):
topic = [w for (w, _) in topics[i][1]]
final_topic = []
j = 0
while len(final_topic) < 20 and j < 100 and j < len(topic):
w = topic[j]
id = dictionary.token2id[w]
beta = 2
if w in noise_dist:
beta += noise_dist[w]
beta = max(2, beta * (topic_weight / lda_k))
alpha = 2 + topic_word_distribution[i, id]
roll = random.betavariate(alpha=math.sqrt(alpha), beta=math.sqrt(beta))
if roll >= 0.5:
final_topic.append(w)
if not w in noise_dist:
noise_dist[w] = 0
noise_dist[w] += (alpha - 2)
j += 1
final_topics.append(final_topic)
param_string = '_'.join([str(x).replace('.', '-')
for x in lda_params])
save_topics(final_topics, '{}/{}/enlda/topics_{}_{}_{}.csv'
.format(results_path, dataset_name, param_string, topic_weight, noise_params[6])) # noise_params[6] = closest x words
return model_tuple
def main():
mallet_path = 'mallet-2.0.8/bin/mallet'
tnd_path = 'mallet-tnd/bin/mallet'
etnd_path = 'mallet-etnd/bin/mallet'
results_path = 'results'
dataset_names = ['sample_tweets']
lda_params = [
# k
(30,)
]
tnd_params = [
# k, alpha, beta, skew, noise_words_max, iterations
(30, 50, 0.01, 25, 200, 1000),
]
etnd_params = [
# k, alpha, beta, skew, noise_words_max, embedding_path, closest_x_words, tau (number of iterations before embedding activation), iterations
# (30, 50, 0.01, 25, 200, 'local_{}_ft.bin', 3, 200, 1000),
(30, 50, 0.01, 25, 200, 'local_{}_ft.bin', 5, 200, 1000),
# (30, 50, 0.01, 25, 200, 'local_{}_ft.bin', 10, 200, 1000),
]
nlda_params = [
# lda_k, nft_k, alpha, beta, skew, noise_words_max, iterations, phi values tuple
(((10,), (20,), (30,)), (30, 0.1, 0.01, 25, 200, 1000), (1, 5, 10, 15, 20, 25, 30)),
]
enlda_params = [
# lda_k, nft_k, alpha, beta, skew, noise_words_max, embedding_path, closest_x_words, tau (number of iterations before embedding activation), iterations, phi values tuple
# (((30,),), (30, 50, 0.01, 25, 200, 'local_{}_ft.bin', 3, 200, 1000), (1, 5, 10, 15, 20, 25, 30)),
(((30,),), (30, 50, 0.01, 25, 200, 'local_{}_ft.bin', 5, 200, 1000), (1, 5, 10, 15, 20, 25, 30)),
# (((30,),), (30, 50, 0.01, 25, 200, 'local_{}_ft.bin', 10, 200, 1000), (1, 5, 10, 15, 20, 25, 30)),
]
for dataset_name in dataset_names:
dataset = load_flat_dataset('data/{}.csv'.format(dataset_name))
# train fasttext vectors if needed
ft = FastText(sentences=dataset, vector_size=100, min_count=50)
save_facebook_model(ft, 'local_{}_ft.bin'.format(dataset_name))
# run LDA
model0 = run_LDA(dataset, dataset_name, mallet_path, lda_params, results_path)
# run TND without embeddings
model1 = run_TND_MALLET(dataset, dataset_name, tnd_path, tnd_params, results_path)
# run TND with embeddings
model2 = run_ETND_MALLET(dataset, dataset_name, etnd_path, etnd_params, results_path)
# this will compute TND and LDA from scratch
model3 = run_NLDA(dataset, dataset_name, mallet_path, tnd_path, nlda_params, results_path)
# this will compute LDA from scratch, but use the noise distribution calculated in model2 to save computation time
model4 = run_eNLDA(dataset, dataset_name, mallet_path, etnd_path, enlda_params, results_path,
noise_dist=model2.load_noise_dist())
if __name__ == '__main__':
main()