-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_lda.py
148 lines (111 loc) · 3.99 KB
/
train_lda.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
import os
import sys
import logging
logging.basicConfig(
format='%(asctime)s : %(levelname)s : %(message)s',
level=logging.INFO)
import wandb
import utils
import gensim
from gensim.models import LdaModel
from gensim.models.callbacks import CoherenceMetric, ConvergenceMetric, PerplexityMetric, DiffMetric
from gensim.corpora import Dictionary
import pyLDAvis
import pyLDAvis.gensim
import plotly.express as px
class ConvergenceCallback(ConvergenceMetric):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.epoch = 0
def get_value(self, **kwargs):
value = super().get_value(**kwargs)
wandb.log({'convergence': value}, step=self.epoch)
self.epoch += 1
return value
class CoherenceCallback(CoherenceMetric):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.epoch = 0
def get_value(self, **kwargs):
value = super().get_value(**kwargs)
wandb.log({'coherence': value}, step=self.epoch)
self.epoch += 1
return value
class PerplexityCallback(PerplexityMetric):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.epoch = 0
def get_value(self, **kwargs):
value = super().get_value(**kwargs)
wandb.log({'perplexity': value}, step=self.epoch)
self.epoch += 1
return value
class DiffCallback(DiffMetric):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.epoch = 0
def get_value(self, **kwargs):
value = super().get_value(**kwargs)
wandb.log({'topic_diff': value}, step=self.epoch)
self.epoch += 1
return value
def get_hyperparameters():
return dict(
passes = 10,
iterations = 400,
decay = 0.7,
offset = 1024,
chunksize = 2000,
alpha = 'auto',
eta = 'auto',
random_state = 1024,
num_topics = 7,
minimum_probability = 0.01,
no_below = None,
no_above = None
)
def train(passes=1, iterations=50, num_topics=100, decay=0.5, offset=1.0, chunksize=2000, minimum_probability=0.01,
alpha='symmetric', eta=None, random_state=None, no_below=None, no_above=None):
texts, _, dictionary = utils.get_text_inputs_from_folder('model')
if no_above or no_below:
dictionary.filter_extremes(no_above=no_above, no_below=no_below)
corpus = [dictionary.doc2bow(text) for text in texts]
convergence_logger = ConvergenceCallback(logger='shell')
coherence_logger = CoherenceCallback(logger='shell', corpus=corpus, texts=texts, dictionary=dictionary, coherence='c_v')
perplexity_logger = PerplexityCallback(logger='shell', corpus=corpus)
lm = LdaModel(
corpus=corpus,
id2word=dictionary,
eval_every=1,
callbacks=[convergence_logger, coherence_logger, perplexity_logger],
passes=passes,
iterations=iterations,
num_topics=num_topics,
decay=decay,
offset=offset,
chunksize=chunksize,
alpha=alpha,
eta=eta,
random_state=random_state,
minimum_probability=minimum_probability,
)
return lm, corpus, dictionary
def main():
hyperparameters = get_hyperparameters()
if len(sys.argv) > 1:
args = vars(utils.parse_args())
args = {k: v for k, v in args.items() if v is not None}
hyperparameters.update(args)
wandb.init(project="bom-topic-modelling", config=hyperparameters)
lm, corpus, dictionary = train(**hyperparameters)
lm.save(os.path.join(wandb.run.dir, 'lda.model'))
# topic difference heatmap
mdiff, _ = lm.diff(lm, distance='jaccard', num_words=50)
fig = px.imshow(mdiff, origin='lower', color_continuous_scale='RdBu_r')
wandb.log({"topic_diff": fig})
# pyLDAvis
vis = pyLDAvis.gensim.prepare(lm, corpus, dictionary)
html = pyLDAvis.prepared_data_to_html(vis)
wandb.log({"pyLDAvis": wandb.Html(html, inject=False)})
if __name__ == "__main__":
main()