-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
222 lines (191 loc) · 6.93 KB
/
main.py
File metadata and controls
222 lines (191 loc) · 6.93 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
import sys
from postagger.utils.classifier import MaximumEntropyClassifier
from postagger.utils.common import timeit, get_data_path
from postagger.utils.common import get_tags
from postagger.utils.preprocess import load_save_preprocessed_data
from postagger.utils.decoder import CompData
from postagger.utils.classifier import save_load_init_model
# params
load_model = False
load_matrices = False
load_preprocess = False
model_name = 'model2.pickle'
model_matrices = 'model2_matrices.pickle'
model_preprocess = 'model2_preprocess.pickle'
verbose = 1
# data files
train = 'train.wtag'
test = 'train2.wtag'
comp = 'comp.words'
# hyper params
# features
min_occurrence_dict = {
'wordtag-f100': 0,
'suffix-f101': 0,
'prefix-f102': 0,
'trigram-f103': 0,
'bigram-f104': 0,
'unigram-f105': 0,
'previousword-f106': 2,
'nextword-f107': 2,
'starting_capital': 0,
'capital_inside': 0,
'number_inside': 0,
'hyphen_inside': 0,
'pre_pre_word': 2,
'next_next_word': 2
}
# model
regularization = 1
@timeit
def main():
train_path = get_data_path(train)
train_sentences = CompData(train_path)
if load_model:
clf = save_load_init_model(clf=None, filename=model_name)
else:
if load_matrices:
clf = save_load_init_model(clf=None, filename=model_matrices)
else:
# count features occurrences
preprocessor = load_save_preprocessed_data(model_preprocess, train_sentences, load=load_preprocess)
# apply filtering
pdict = preprocessor.summarize_counts(method='cut', dict=min_occurrence_dict)
# init classifier with known tags
tags = get_tags(train)
clf = MaximumEntropyClassifier(train_sentences, pdict, tags)
save_load_init_model(clf=clf, filename=model_matrices)
print("Start fitting %d features" % clf.get_num_features())
print("Top enabled features per tag: " + str(clf.get_enabled_features_per_tag()))
clf.fit(reg=regularization, verbose=verbose)
save_load_init_model(clf=clf, filename=model_name)
# evaluate
# train
print("Evaluate train:")
train_predict = clf.predict(train_sentences)
print(train_predict)
# test
print("Evaluate test:")
test_path = get_data_path(test)
test_sentences = CompData(test_path)
t_predict = clf.predict(test_sentences)
print(t_predict)
"""
comp_path = get_data_path(comp)
comp_sentences = CompData(comp_path, comp=True)
comp_predict = clf.predict(comp_sentences)
print(comp_predict)
"""
def training():
train_path = get_data_path(train)
train_sentences = CompData(train_path)
test_path = get_data_path(test)
test_sentences = CompData(test_path)
preprocessor = load_save_preprocessed_data(model_preprocess, train_sentences, load=load_preprocess)
# apply filtering
pdict = preprocessor.summarize_counts(method='cut', dict=min_occurrence_dict)
# init classifier with known tags
tags = get_tags(train)
clf = MaximumEntropyClassifier(train_sentences, pdict, tags)
reg = [5e-3, 1e-2, 5e-2, 1e-1, 1, 3, 5, 10, 25, 50, 100, 500, 1000]
best_model = 'best_model.pickle'
best_acc = 0
test_acc = 0
results = {}
for r in reg:
print("Start fitting model, reg: ", str(r))
clf.fit(reg=r)
try:
print("Evaluate train:")
train_pred = clf.predict(train_sentences)
train_acc = train_pred['accuracy']
print("Evaluate test:")
test_pred = clf.predict(test_sentences)
test_acc = test_pred['accuracy']
results[('reg', r)] = {'train_acc': train_acc, 'test_acc': test_acc}
if test_acc > best_acc:
best_acc = test_acc
save_load_init_model(clf=clf, filename=best_model)
except:
pass
print("Current results", results)
print("\n\n")
print("Final results")
print(results)
def training2():
# data files
train = 'train2.wtag'
train_path = get_data_path(train)
train_sentences = CompData(train_path, slice=(0, 630))
validation_sentences = CompData(train_path, slice=(630, 700))
preprocessor = load_save_preprocessed_data(model_preprocess, train_sentences, load=load_preprocess)
min_occurrence_dict = {
'wordtag-f100': 0,
'suffix-f101': 0,
'prefix-f102': 0,
'trigram-f103': 0,
'bigram-f104': 0,
'unigram-f105': 0,
'previousword-f106': 0,
'nextword-f107': 0,
'starting_capital': 0,
'capital_inside': 0,
'number_inside': 0,
'hyphen_inside': 0,
'pre_pre_word': 0,
'next_next_word': 0
}
reg = [1e-3, 1e-2, 1e-1]
min_occur = [ {'previousword-f106': 1, 'nextword-f107':1, 'pre_pre_word': 0, 'next_next_word': 0},
{'previousword-f106': 1, 'nextword-f107':1, 'pre_pre_word': 1, 'next_next_word': 1},
{'previousword-f106': 1, 'nextword-f107': 1, 'pre_pre_word': 2, 'next_next_word': 2},
{'previousword-f106': 0, 'nextword-f107': 0, 'pre_pre_word': 2, 'next_next_word': 2},
{'previousword-f106': 0, 'nextword-f107': 0, 'pre_pre_word': 1, 'next_next_word': 1},
{'previousword-f106': 2, 'nextword-f107': 2, 'pre_pre_word': 0, 'next_next_word': 0}]
best_model = 'best_model.pickle'
best_acc = 0
test_acc = 0
results = {}
for occur_dict in min_occur:
# apply filtering
# update dict
print("Init new classifier with updated occurrence dict")
print(occur_dict)
for key, value in occur_dict.items():
min_occurrence_dict[key] = value
pdict = preprocessor.summarize_counts(method='cut', dict=min_occurrence_dict)
# init classifier with known tags
tags = get_tags(train)
clf = MaximumEntropyClassifier(train_sentences, pdict, tags)
for r in reg:
print("Start fitting model, reg: ", str(r))
clf.fit(reg=r)
try:
print("Evaluate train:")
train_pred = clf.predict(train_sentences)
train_acc = train_pred['accuracy']
print("Evaluate validation:")
test_pred = clf.predict(validation_sentences)
test_acc = test_pred['accuracy']
results[('reg', r, str(occur_dict))] = {'train_acc': train_acc, 'validation_acc': test_acc}
if test_acc > best_acc:
best_acc = test_acc
save_load_init_model(clf=clf, filename=best_model)
except:
pass
print("Current results", results)
print("\n\n")
print("Final results")
print(results)
if __name__ == '__main__':
mode = None
try:
mode = sys.argv[1]
except Exception as e:
pass
if mode == '-t':
training()
elif mode == '-t2':
training2()
else:
main()