-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_model_selectors.py
256 lines (211 loc) · 10.7 KB
/
my_model_selectors.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
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
import math
import statistics
import warnings
import numpy as np
from hmmlearn.hmm import GaussianHMM
from sklearn.model_selection import KFold
from asl_utils import combine_sequences
import pickle
class ModelSelector(object):
'''
base class for model selection (strategy design pattern)
'''
def __init__(self, all_word_sequences: dict, all_word_Xlengths: dict, this_word: str,
n_constant=3,
min_n_components=2, max_n_components=10,
random_state=14, verbose=False):
self.words = all_word_sequences
self.hwords = all_word_Xlengths
self.sequences = all_word_sequences[this_word]
self.X, self.lengths = all_word_Xlengths[this_word]
self.this_word = this_word
self.n_constant = n_constant
self.min_n_components = min_n_components
self.max_n_components = max_n_components
self.random_state = random_state
self.verbose = verbose
def select(self):
raise NotImplementedError
def base_model(self, num_states):
# with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
# warnings.filterwarnings("ignore", category=RuntimeWarning)
try:
hmm_model = GaussianHMM(n_components=num_states, covariance_type="diag", n_iter=1000,
random_state=self.random_state, verbose=False).fit(self.X, self.lengths)
if self.verbose:
print("model created for {} with {} states".format(self.this_word, num_states))
return hmm_model
except:
if self.verbose:
print("failure on {} with {} states".format(self.this_word, num_states))
return None
class SelectorConstant(ModelSelector):
""" select the model with value self.n_constant
"""
def select(self):
""" select based on n_constant value
:return: GaussianHMM object
"""
best_num_components = self.n_constant
return self.base_model(best_num_components)
class SelectorBIC(ModelSelector):
""" select the model with the lowest Bayesian Information Criterion(BIC) score
http://www2.imm.dtu.dk/courses/02433/doc/ch6_slides.pdf
Bayesian information criteria: BIC = -2 * logL + p * logN
"""
def select(self):
""" select the best model for self.this_word based on
BIC score for n between self.min_n_components and self.max_n_components
:return: GaussianHMM object
"""
warnings.filterwarnings("ignore", category=DeprecationWarning)
# DONE implement model selection based on BIC scores
min_bic_score = float("inf")
best_model = None
for number_of_components in range(self.min_n_components, self.max_n_components + 1):
# bic = -2 * log_l + p * log_n where
# log_l is the model score
# p = num params = number_of_components**2 + 2 * number_of_features * number_of_components - 1
# logN = log of the number of data points
# NOTE=X.shape[1] is the number of features, X.shape[0] is the number of examples
try:
p = np.power(number_of_components, 2) + 2 * self.X.shape[1] * number_of_components - 1
model = GaussianHMM(n_components=number_of_components, covariance_type="diag", n_iter=1000,
random_state=self.random_state, verbose=False)
model.fit(self.X, self.lengths)
log_l = model.score(self.X, self.lengths)
bic = -2 * log_l + p * np.log(self.X.shape[0])
if bic < min_bic_score:
min_bic_score = bic
best_model = self.base_model(number_of_components)
except:
continue
return best_model
class SelectorDIC(ModelSelector):
''' select best model based on Discriminative Information Criterion
Biem, Alain. "A model selection criterion for classification: Application to hmm topology optimization."
Document Analysis and Recognition, 2003. Proceedings. Seventh International Conference on. IEEE, 2003.
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.58.6208&rep=rep1&type=pdf
DIC = log(P(X(i)) - 1/(M-1)SUM(log(P(X(all but i))
My Original DIC selector
Doesn't assume we'll be reusing the words. However runtime can get really long.
'''
def select(self):
warnings.filterwarnings("ignore", category=DeprecationWarning)
# DONE implement model selection based on DIC scores
max_dic_score = float("-inf")
best_model = None
for number_of_components in range(self.min_n_components, self.max_n_components + 1):
anti_probabilities = []
try:
model = GaussianHMM(n_components=number_of_components, covariance_type="diag", n_iter=1000,
random_state=self.random_state, verbose=False)
model.fit(self.X, self.lengths)
log_l = model.score(self.X, self.lengths)
except:
continue
for word in self.words:
if word is not self.this_word:
try:
anti_model = GaussianHMM(n_components=number_of_components, covariance_type="diag", n_iter=1000,
random_state=self.random_state, verbose=False)
x, lengths = self.hwords[word]
anti_model.fit(x, lengths)
anti_probabilities.append(anti_model.score(x, lengths))
except:
continue
# Want a high log likelihood compared to average log likelihood of other words for this model
dic_score = log_l - np.mean(anti_probabilities)
if dic_score > max_dic_score:
max_dic_score = dic_score
best_model = self.base_model(number_of_components)
return best_model
class SelectorCV(ModelSelector):
''' select best model based on average log Likelihood of cross-validation folds
'''
def select(self):
warnings.filterwarnings("ignore", category=DeprecationWarning)
# DONE implement model selection using CV
n_splits = 2
split_method = KFold(n_splits)
best_score = float("-inf")
best_model = None
# Try a model with number_of_components
for number_of_components in range(self.min_n_components, self.max_n_components + 1):
model = GaussianHMM(n_components=number_of_components, covariance_type="diag", n_iter=1000,
random_state=self.random_state, verbose=False)
fold_scores = []
if len(self.sequences) < n_splits:
break
for cv_train_idx, cv_test_idx in split_method.split(self.sequences):
train_x, train_lengths = combine_sequences(cv_train_idx, self.sequences)
test_x, test_lengths = combine_sequences(cv_test_idx, self.sequences)
try:
# Fit/Train HMM model with train data set
model.fit(train_x, train_lengths)
# Score with the test data set
fold_scores.append(model.score(test_x, test_lengths))
except:
break
if len(fold_scores) > 0:
average_score = np.average(fold_scores)
else:
average_score = float("-inf")
if average_score > best_score:
best_score = average_score
best_model = self.base_model(number_of_components)
return best_model
class FastSelectorDIC(ModelSelector):
''' select best model based on Discriminative Information Criterion
Biem, Alain. "A model selection criterion for classification: Application to hmm topology optimization."
Document Analysis and Recognition, 2003. Proceedings. Seventh International Conference on. IEEE, 2003.
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.58.6208&rep=rep1&type=pdf
DIC = log(P(X(i)) - 1/(M-1)SUM(log(P(X(all but i))
Faster DIC selector based on the following assumption: reusing the same word dictionary.
If word dictionary changes delete the reference_dictionary.pkl file
'''
def select(self):
warnings.filterwarnings("ignore", category=DeprecationWarning)
# Try to load reference dictionary of word (anti)probabilities
reference_dictionary = {}
try:
reference_dictionary = pickle.load(open("reference_dictionary.pkl", "rb"))
# Except if this is the first run (ie no reference_dictionary.pkl file found)
except:
for number_of_components in range(self.min_n_components, self.max_n_components + 1):
for word in self.words:
identifier = '{}_{}'.format(word, number_of_components)
try:
anti_model = GaussianHMM(n_components=number_of_components, covariance_type="diag", n_iter=1000,
random_state=self.random_state, verbose=False)
x, lengths = self.hwords[word]
anti_model.fit(x, lengths)
reference_dictionary[identifier] = anti_model.score(x, lengths)
except:
reference_dictionary[identifier] = 0
pickle.dump(reference_dictionary, open("reference_dictionary.pkl", "wb"))
# implement model selection based on DIC scores
max_dic_score = float("-inf")
best_model = None
for number_of_components in range(self.min_n_components, self.max_n_components + 1):
anti_probabilities = []
try:
model = GaussianHMM(n_components=number_of_components, covariance_type="diag", n_iter=1000,
random_state=self.random_state, verbose=False)
model.fit(self.X, self.lengths)
log_l = model.score(self.X, self.lengths)
except:
continue
for word in self.words:
if word is not self.this_word:
try:
anti_probabilities.append(reference_dictionary['{}_{}'.format(word, number_of_components)])
except:
continue
# Want a high log likelihood compared to average log likelihood of other words for this model
dic_score = log_l - np.mean(anti_probabilities)
if dic_score > max_dic_score:
max_dic_score = dic_score
best_model = self.base_model(number_of_components)
return best_model