-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel_example.py
More file actions
169 lines (120 loc) · 5.88 KB
/
model_example.py
File metadata and controls
169 lines (120 loc) · 5.88 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
# coding: utf-8
from __future__ import print_function
import os, glob
import time
import numpy as np
import artm
from document_helper import pn_folder, vw_folder, files_total, domain_path
from measures_utils import ResultStorage, record_results
print(artm.version())
def create_model(dictionary, num_tokens, num_document_passes):
specific_topics = ['topic {}'.format(i) for i in range(1, 20)]
scores = [artm.PerplexityScore(name='PerplexityScore', dictionary=dictionary),
artm.TopTokensScore(name='TopTokensScore', num_tokens=10), # web version of Palmetto works only with <= 10 tokens
artm.SparsityPhiScore(name='SparsityPhiScore'),
artm.SparsityThetaScore(name='SparsityThetaScore')]
model = artm.ARTM(topic_names=specific_topics,
regularizers=[], cache_theta=True, scores=scores,
class_ids={'plain_text': 1.0})
model.initialize(dictionary=dictionary)
model.num_document_passes = num_document_passes
return model
def create_model_with_background(dictionary, num_tokens, num_document_passes):
sm_phi_tau = 0.0001 * 1e-4
sp_phi_tau = -0.0001 * 1e-4
decor_phi_tau = 1
specific_topics = ['topic {}'.format(i) for i in range(1, 20)]
topic_names = specific_topics + ["background"]
scores = [
artm.PerplexityScore(name='PerplexityScore', dictionary=dictionary),
artm.TopTokensScore(name='TopTokensScore', num_tokens=10, class_id='plain_text'), # web version of Palmetto works only with <= 10 tokens
artm.SparsityPhiScore(name='SparsityPhiScore'),
artm.SparsityThetaScore(name='SparsityThetaScore'),
artm.TopicKernelScore(name='TopicKernelScore', probability_mass_threshold=0.3, class_id='plain_text')
]
model = artm.ARTM(topic_names=specific_topics + ["background"],
regularizers=[], cache_theta=True, scores=scores,
class_ids={'plain_text': 1.0})
model.regularizers.add(artm.SmoothSparsePhiRegularizer(name='SparsePhi', tau=-sp_phi_tau, topic_names=specific_topics))
model.regularizers.add(artm.SmoothSparsePhiRegularizer(name='SmoothPhi', tau=sm_phi_tau, topic_names=["background"]))
# model.regularizers.add(artm.DecorrelatorPhiRegularizer(name='DecorrelatorPhi', tau=decor_phi_tau))
model.initialize(dictionary=dictionary)
model.num_document_passes = num_document_passes
return model
coh_names = ['newman', 'mimno',
'semantic', 'toplen', "focon"]
#coh_names = ['newman', 'mimno', 'toplen']
intra_coherence_params = {
"window": 10, "threshold": 0.02, "focon_threshold": 5, "cosine_num_top_tokens": 10, "num_top_tokens": 10,
"general_penalty": 0.005
}
num_passes_list = range(1, 20)
num_passes_list = range(1, 10)
num_passes_list = range(1, 2)
num_top_tokens = 10
# Vowpal Wabbit
batch_vectorizer = None
if len(glob.glob(os.path.join(pn_folder, vw_folder, '*.batch'))) < 1:
batch_vectorizer = artm.BatchVectorizer(data_path=os.path.join(pn_folder, vw_folder, 'vw_bimodal.txt'),
data_format='vowpal_wabbit',
target_folder=os.path.join(pn_folder, vw_folder))
else:
batch_vectorizer = artm.BatchVectorizer(data_path=os.path.join(pn_folder, vw_folder),
data_format='batches')
vw_file = os.path.join(pn_folder, vw_folder, 'vw_bimodal.txt')
dictionary = artm.Dictionary()
dict_path = os.path.join(pn_folder, vw_folder, 'dict.dict')
if not os.path.isfile(dict_path):
dictionary.gather(data_path=batch_vectorizer.data_path)
dictionary.save(dictionary_path=dict_path)
dictionary.load(dictionary_path=dict_path)
N = 1
# model
model = create_model_with_background(dictionary=dictionary,
num_tokens=num_top_tokens,
num_document_passes=N)
# number of cycles
num_of_restarts = 3
def print_status(t0, indent_number, what_is_happening):
print('({0:>2d}:{1:>2d}){2} {3}'.format(
int(time.time()-t0)//60//60,
int(time.time()-t0)//60%60,
indent*indent_number,
what_is_happening)
)
def randomize_model(restart_num, model):
np.random.seed(restart_num * restart_num + 42)
topic_model_data, phi_numpy_matrix = model.master.attach_model('pwt')
random_init = np.random.random_sample(phi_numpy_matrix.shape)
random_init /= np.sum(random_init, axis=0)
np.copyto(phi_numpy_matrix, random_init)
return topic_model_data, phi_numpy_matrix
t0 = time.time()
indent = ' '
indent_number = 0
data_storage = ResultStorage(coh_names, domain_path=domain_path)
for restart_num in range(num_of_restarts):
topic_model_data, phi_numpy_matrix = randomize_model(restart_num, model)
# range of models with different segmentation qualities
num_passes_last = 0
for num_passes_total in num_passes_list:
print('************************')
print_status(t0, indent_number, "teaching model at iter {}".format(num_passes_total))
indent_number += 1
model.fit_offline(batch_vectorizer=batch_vectorizer,
num_collection_passes=num_passes_total-num_passes_last
)
num_passes_last = num_passes_total
model_id = " {} ".format({"name": "PLSA", "restart_num": restart_num, "iter": num_passes_total})
with record_results(model=model, vw_file=vw_file, at=model_id, save_in=data_storage) as recorder:
for coh_name in coh_names:
print_status(t0, indent_number, coh_name)
recorder.evaluate(coh_name, intra_coherence_params)
indent_number -= 1
indent_number -= 1
print_status(t0, indent_number, "segmentation evaluation")
recorder.evaluate_segmentation_quality()
indent_number += 1
indent_number -= 1
#print(data_storage.segm_quality.items())
data_storage.data_results_save()