Debian Codegraph @

src/hmmlearn/tests/test_gmm_hmm_multisequence.py

raw symbols SCIP
1 import itertools
2
3 import numpy as np
4 from numpy.testing import assert_allclose
5 import pytest
6
7 from hmmlearn import hmm
8 from hmmlearn.base import ConvergenceMonitor
9
10
11 # Example multi-sequence data, arranged as shape
12 # (n_sequences, n_samples, n_features)
13 #
14 # This data is a reduced subset of data_training.npy
15 # from issue https://github.com/hmmlearn/hmmlearn/issues/410
16 # illustrating GMMHMM fit diverging during EM iterations.
17 #
18 # Transformations to reduce data volume:
19 # - keep only first 3 of many sequences, discard rest
20 # - keep only first 50 of many samples per sequence, discard rest
21 # - keep only first 4 of 17 features per sample, discard rest
22 EXAMPLE_SEQUENCES_ISSUE_410_PRUNED = np.array(
23 [
24 np.array(
25 [
26 [0.00992058, 0.44151747, 0.5395124, 0.40644765],
27 [0.00962487, 0.45613006, 0.52375835, 0.3899082],
28 [0.00915721, 0.47111648, 0.5103008, 0.3846845],
29 [0.00916073, 0.4749602, 0.5241155, 0.39899495],
30 [0.0090966, 0.47398633, 0.53792244, 0.41295874],
31 [0.00953476, 0.47201437, 0.5322343, 0.41661483],
32 [0.00916542, 0.4455471, 0.55598766, 0.40831617],
33 [0.00906925, 0.43173638, 0.56246823, 0.39109665],
34 [0.00826067, 0.4136997, 0.58712745, 0.39158684],
35 [0.00828806, 0.41975173, 0.60497123, 0.38206288],
36 [0.00788883, 0.397979, 0.63639283, 0.3627324],
37 [0.00765208, 0.38908702, 0.65764546, 0.3516956],
38 [0.00738148, 0.38130987, 0.6522844, 0.36725503],
39 [0.00717299, 0.37383446, 0.6722188, 0.37951013],
40 [0.0073711, 0.37058228, 0.6799041, 0.3860375],
41 [0.00728311, 0.37892842, 0.65606904, 0.39165357],
42 [0.00730301, 0.39218283, 0.6332023, 0.3976117],
43 [0.00713718, 0.38652796, 0.6423802, 0.34927416],
44 [0.00683423, 0.3656172, 0.68119335, 0.2912439],
45 [0.00663389, 0.34920084, 0.68535674, 0.28290597],
46 [0.00625478, 0.3525497, 0.6658849, 0.30369937],
47 [0.00614696, 0.35593832, 0.65440905, 0.3450122],
48 [0.00611069, 0.35073754, 0.6559732, 0.33597857],
49 [0.00635504, 0.3360095, 0.6800729, 0.32301348],
50 [0.00617533, 0.3444746, 0.6745925, 0.34155408],
51 [0.00592057, 0.35373318, 0.66947186, 0.32476413],
52 [0.00564618, 0.36178407, 0.6560819, 0.3297305],
53 [0.00572176, 0.37058342, 0.6551206, 0.2967357],
54 [0.00578371, 0.39031005, 0.64601576, 0.33421013],
55 [0.00577161, 0.41922286, 0.6089396, 0.3717376],
56 [0.00579954, 0.41518527, 0.60426843, 0.38774568],
57 [0.00578072, 0.40165138, 0.6203536, 0.34574744],
58 [0.00583212, 0.42201585, 0.60890085, 0.38103116],
59 [0.00572761, 0.40093482, 0.63888615, 0.36249077],
60 [0.00594841, 0.3804375, 0.6576098, 0.37927687],
61 [0.0059343, 0.34200934, 0.693946, 0.3007063],
62 [0.00591482, 0.3709248, 0.66136825, 0.32304856],
63 [0.0055425, 0.41159946, 0.62043166, 0.3460799],
64 [0.00548492, 0.40038764, 0.6440804, 0.33333993],
65 [0.00552325, 0.36867827, 0.6703099, 0.30612737],
66 [0.00553349, 0.35795027, 0.67543924, 0.27393535],
67 [0.00558642, 0.4015568, 0.62600005, 0.31275502],
68 [0.00565522, 0.40925154, 0.6178226, 0.3131643],
69 [0.0058172, 0.42638385, 0.6077434, 0.33476466],
70 [0.00585697, 0.40742254, 0.6218038, 0.37967283],
71 [0.00591527, 0.4296229, 0.6016123, 0.3985932],
72 [0.00604816, 0.43141186, 0.59317786, 0.42083132],
73 [0.00621391, 0.4110697, 0.6092669, 0.38827285],
74 [0.00656536, 0.39309287, 0.60035396, 0.41596898],
75 [0.00693208, 0.37821782, 0.59813255, 0.4394344],
76 ],
77 dtype=np.float32,
78 ),
79 np.array(
80 [
81 [0.00318667, 0.48804316, 0.52020603, 0.36232004],
82 [0.00322638, 0.48808283, 0.5341949, 0.37973505],
83 [0.00329762, 0.47688982, 0.5563834, 0.4047565],
84 [0.00321911, 0.48151806, 0.54239404, 0.38407174],
85 [0.00400121, 0.5309283, 0.49719027, 0.40301552],
86 [0.00461331, 0.5856188, 0.44557935, 0.40280044],
87 [0.0048873, 0.59214115, 0.4330637, 0.43839055],
88 [0.00411017, 0.53695357, 0.49013752, 0.3832056],
89 [0.00357234, 0.48548815, 0.54152006, 0.3475358],
90 [0.00341532, 0.46990934, 0.57406586, 0.33449954],
91 [0.00345838, 0.50714695, 0.51190466, 0.38789546],
92 [0.00341552, 0.526225, 0.48219037, 0.41689718],
93 [0.0034434, 0.5293161, 0.47968563, 0.4379618],
94 [0.00342251, 0.49587825, 0.53558546, 0.4021806],
95 [0.00329584, 0.5082987, 0.52165693, 0.4086313],
96 [0.0031443, 0.519554, 0.49245426, 0.3961157],
97 [0.0030792, 0.5455676, 0.45203313, 0.423718],
98 [0.00320465, 0.5504491, 0.43865028, 0.45784852],
99 [0.00329045, 0.52342314, 0.48308045, 0.455756],
100 [0.00342623, 0.50302553, 0.495605, 0.45614803],
101 [0.00347294, 0.48462915, 0.52492356, 0.41467494],
102 [0.00352737, 0.45118907, 0.53679097, 0.38664353],
103 [0.00349953, 0.40440804, 0.59128445, 0.3342833],
104 [0.00343233, 0.385624, 0.61139005, 0.29829293],
105 [0.00333838, 0.4443106, 0.55629146, 0.3360141],
106 [0.00341689, 0.47139308, 0.5338217, 0.34617022],
107 [0.00349784, 0.49085665, 0.5179333, 0.38301566],
108 [0.00354852, 0.45714432, 0.5581238, 0.3514836],
109 [0.00336909, 0.4432368, 0.5735479, 0.37587976],
110 [0.00334855, 0.42174804, 0.60338825, 0.36684743],
111 [0.00340526, 0.4299499, 0.5964124, 0.38658547],
112 [0.00355591, 0.44496518, 0.5775449, 0.37693518],
113 [0.00368621, 0.4641773, 0.54175687, 0.38873497],
114 [0.00392683, 0.48638234, 0.52129495, 0.40342456],
115 [0.00444013, 0.5311254, 0.4699971, 0.41572142],
116 [0.00559078, 0.5725662, 0.42817408, 0.42607802],
117 [0.00651121, 0.60758656, 0.3687854, 0.45583618],
118 [0.00731191, 0.60793436, 0.36194792, 0.46369436],
119 [0.0075791, 0.56968933, 0.409531, 0.40896642],
120 [0.00813889, 0.5602184, 0.43285215, 0.35258207],
121 [0.00887025, 0.5855726, 0.40227312, 0.35542902],
122 [0.00897418, 0.609533, 0.35594714, 0.3942479],
123 [0.01022383, 0.6129873, 0.3352133, 0.47778368],
124 [0.0095141, 0.5595026, 0.4162315, 0.45395738],
125 [0.00841102, 0.49951875, 0.48765272, 0.4517436],
126 [0.00826918, 0.45066664, 0.5471808, 0.3476721],
127 [0.01868482, 0.39936644, 0.55312306, 0.43391562],
128 [0.04211371, 0.3675188, 0.56271124, 0.53548455],
129 [0.07297403, 0.3576941, 0.57314515, 0.6578705],
130 [0.10756538, 0.413695, 0.5337025, 0.653491],
131 ],
132 dtype=np.float32,
133 ),
134 np.array(
135 [
136 [0.00302289, 0.47364405, 0.53537434, 0.45129624],
137 [0.00311589, 0.45328405, 0.5570029, 0.42494437],
138 [0.00315774, 0.44939527, 0.5723259, 0.4117937],
139 [0.00314883, 0.46795434, 0.54135257, 0.3953221],
140 [0.00309122, 0.49266115, 0.5156292, 0.4080569],
141 [0.00312682, 0.49728185, 0.51451075, 0.41902116],
142 [0.00323118, 0.49948877, 0.5244724, 0.45043528],
143 [0.00325324, 0.47703367, 0.5608516, 0.42876282],
144 [0.00322127, 0.4831656, 0.5520302, 0.43367508],
145 [0.00314415, 0.5081604, 0.5305388, 0.47354758],
146 [0.00307265, 0.51409185, 0.5208431, 0.46550223],
147 [0.00312928, 0.5049119, 0.5267695, 0.44442567],
148 [0.00313668, 0.47089654, 0.55917096, 0.37248307],
149 [0.00320294, 0.45442477, 0.57189673, 0.38878405],
150 [0.00317113, 0.43952465, 0.59469926, 0.3535012],
151 [0.00323172, 0.46230638, 0.5713085, 0.39267808],
152 [0.00323527, 0.50055844, 0.52773774, 0.4445253],
153 [0.00322912, 0.5141386, 0.501789, 0.488796],
154 [0.00312394, 0.53217465, 0.47047156, 0.49383518],
155 [0.00305265, 0.5208433, 0.50014937, 0.459741],
156 [0.00305853, 0.51386, 0.5120822, 0.43915167],
157 [0.00311382, 0.47278965, 0.57036394, 0.45423037],
158 [0.00318763, 0.46807718, 0.5674287, 0.43843716],
159 [0.00318154, 0.47042188, 0.5779207, 0.44580007],
160 [0.00303793, 0.46571356, 0.5818657, 0.4107192],
161 [0.00299003, 0.44406536, 0.60040486, 0.38005733],
162 [0.00296462, 0.44092676, 0.59199274, 0.3560812],
163 [0.00307418, 0.45244223, 0.56540257, 0.33132356],
164 [0.00302929, 0.47221994, 0.5470938, 0.36822143],
165 [0.00301338, 0.47062206, 0.54911035, 0.3680197],
166 [0.00291349, 0.45564204, 0.5619591, 0.3808152],
167 [0.00295025, 0.47307628, 0.54229873, 0.35219967],
168 [0.00289911, 0.4725018, 0.54288876, 0.38314143],
169 [0.00291358, 0.45272404, 0.5728319, 0.37214962],
170 [0.00294044, 0.4570347, 0.5649049, 0.42110214],
171 [0.00298315, 0.4561437, 0.55468136, 0.40142712],
172 [0.00303691, 0.49613172, 0.52464443, 0.42899716],
173 [0.00300609, 0.49105296, 0.5310325, 0.41341364],
174 [0.00305483, 0.532239, 0.485052, 0.46893936],
175 [0.00298801, 0.51202387, 0.49897632, 0.45285705],
176 [0.0030795, 0.5070261, 0.5062058, 0.44057184],
177 [0.00303104, 0.47002167, 0.56080073, 0.40074068],
178 [0.00308184, 0.4575324, 0.56803167, 0.3928257],
179 [0.00291516, 0.44490653, 0.58391964, 0.40129626],
180 [0.00289649, 0.4531514, 0.5818511, 0.407385],
181 [0.00284487, 0.4488143, 0.5850243, 0.41675568],
182 [0.00291576, 0.4616304, 0.57469726, 0.40793785],
183 [0.00287342, 0.46473294, 0.54888374, 0.41002542],
184 [0.00303153, 0.4966541, 0.5055506, 0.4316879],
185 [0.00327755, 0.45762977, 0.529044, 0.4493881],
186 ],
187 dtype=np.float32,
188 ),
189 ]
190 )
191
192
193 class StrictMonitor(ConvergenceMonitor):
194 @property
195 def converged(self):
196 # The default ConvergenceMonitor regards some scenarios
197 # as "converged" when they have not necessarily converged:
198 #
199 # 1. exhausting max iterations
200 # 2. decreases in log_prob between successive EM iterations
201 #
202 # This second behaviour should (ignoring numerical problems)
203 # never happen if the EM implementation is correct. EM is a
204 # local optimisation method, it may not find a global maxima,
205 # but log_prob should always be non-decreasing between each
206 # pair of successive iterations.
207
208 assert not np.isnan(self.history[-1]), "log_prob must not be nan"
209
210 if len(self.history) < 2:
211 return False
212
213 assert self.history[-1] >= self.history[-2] - self.tol, \
214 "log_prob must be non-decreasing"
215
216 return self.history[-1] - self.history[-2] < self.tol
217
218
219 def make_permutations(items):
220 sequence_indices = list(range(len(items)))
221 return [list(p) for p in itertools.permutations(sequence_indices)]
222
223
224 @pytest.mark.parametrize("covariance_type",
225 ["diag", "spherical", "tied", "full"])
226 @pytest.mark.parametrize("implementation", ["scaling", "log"])
227 def test_gmmhmm_multi_sequence_fit_invariant_to_sequence_ordering(
228 covariance_type, implementation, init_params='mcw', verbose=False
229 ):
230 """
231 Sanity check GMM-HMM fit behaviour when run on multiple sequences
232 aka multiple frames.
233
234 Training data consumed during GMM-HMM fit is packed into a single
235 array X containing one or more sequences. In the case where
236 there are two or more input sequences, the ordering that the
237 sequences are packed into X should not influence the results
238 of the fit. Major differences in convergence during EM
239 iterations by merely permuting sequence order in the input
240 indicates a likely defect in the fit implementation.
241
242 Note: the ordering of samples inside a given sequence
243 is very meaningful, permuting the order of samples would
244 destroy the the state transition structure in the input data.
245
246 See issue 410 on github:
247 https://github.com/hmmlearn/hmmlearn/issues/410
248 """
249 sequence_data = EXAMPLE_SEQUENCES_ISSUE_410_PRUNED
250
251 scores = []
252 for p in make_permutations(sequence_data):
253 sequences = sequence_data[p]
254 X = np.concatenate(sequences)
255 lengths = [len(seq) for seq in sequences]
256
257 model = hmm.GMMHMM(
258 n_components=2,
259 n_mix=2,
260 n_iter=100,
261 covariance_type=covariance_type,
262 verbose=verbose,
263 init_params=init_params,
264 random_state=1234,
265 implementation=implementation
266 )
267
268 # don't use random parameters for testing
269 init = 1. / model.n_components
270 model.startprob_ = np.full(model.n_components, init)
271 model.transmat_ = \
272 np.full((model.n_components, model.n_components), init)
273
274 model.monitor_ = StrictMonitor(
275 model.monitor_.tol,
276 model.monitor_.n_iter,
277 model.monitor_.verbose,
278 )
279
280 model.fit(X, lengths)
281
282 assert model.monitor_.converged
283 scores.append(model.score(X, lengths))
284
285 # Choice of rtol value is ad-hoc, no theoretical motivation.
286 assert_allclose(scores, np.mean(scores), rtol=5e-03)