File: benchmark.py

package info (click to toggle)
python-hmmlearn 0.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 588 kB
  • sloc: python: 4,797; cpp: 321; makefile: 13
file content (298 lines) | stat: -rw-r--r-- 8,715 bytes parent folder | download
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
A script for testing / benchmarking HMM Implementations
"""

import argparse
import collections
import logging
import time

import hmmlearn.hmm

import numpy as np

import sklearn.base

LOG = logging.getLogger(__file__)


class Benchmark:
    def __init__(self, repeat, n_iter, verbose):
        self.repeat = repeat
        self.n_iter = n_iter
        self.verbose = verbose

    def benchmark(self, sequences, lengths, model, tag):
        elapsed = []
        for i in range(self.repeat):
            start = time.time()
            cloned = sklearn.base.clone(model)
            cloned.fit(sequences, lengths)
            end = time.time()
            elapsed.append(end-start)
            self.log_one_run(start, end, cloned, tag)
        return np.asarray(elapsed)

    def generate_training_sequences(self):
        pass

    def new_model(self, implementation):
        pass

    def run(self, results_file):
        runtimes = collections.defaultdict(dict)

        sequences, lengths = self.generate_training_sequences()

        for implementation in ["scaling", "log"]:
            model = self.new_model(implementation)
            LOG.info(f"{model.__class__.__name__}: testing {implementation}")
            key = f"{model.__class__.__name__}|EM|hmmlearn-{implementation}"
            elapsed = self.benchmark(sequences, lengths, model, key)
            runtimes[key]["mean"] = elapsed.mean()
            runtimes[key]["std"] = elapsed.std()

        with open(results_file, mode="w") as fd:
            fd.write("configuration,mean,std,n_iterations,repeat\n")
            for key, value in runtimes.items():
                fd.write(f"{key},{value['mean']},{value['std']},"
                         f"{self.n_iter},{self.repeat}\n")

    def log_one_run(self, start, end, model, tag):
        LOG.info(f"Training Took {end-start} seconds {tag}")
        LOG.info(f"startprob={model.startprob_}")
        LOG.info(f"transmat={model.transmat_}")


class GaussianBenchmark(Benchmark):

    def new_model(self, implementation):
        return hmmlearn.hmm.GaussianHMM(
            n_components=4,
            n_iter=self.n_iter,
            covariance_type="full",
            implementation=implementation,
            verbose=self.verbose
        )

    def generate_training_sequences(self):
        sampler = hmmlearn.hmm.GaussianHMM(
            n_components=4,
            covariance_type="full",
            init_params="",
            verbose=self.verbose
        )

        sampler.startprob_ = np.asarray([0, 0, 0, 1])
        sampler.transmat_ = np.asarray([
            [.2, .2, .3, .3],
            [.3, .2, .2, .3],
            [.2, .3, .3, .2],
            [.3, .3, .2, .2],
        ])
        sampler.means_ = np.asarray([
            -1.5,
            0,
            1.5,
            3
        ]).reshape(4, 1)
        sampler.covars_ = np.asarray([
            .5,
            .5,
            .5,
            .5
        ]).reshape(4, 1, 1,)

        sequences, states = sampler.sample(50000)
        lengths = [len(sequences)]
        return sequences, lengths

    def log_one_run(self, start, end, model, tag):
        super().log_one_run(start, end, model, tag)
        LOG.info(f"means={model.means_}")
        LOG.info(f"covars={model.covars_}")


class MultinomialBenchmark(Benchmark):

    def new_model(self, implementation):
        return hmmlearn.hmm.MultinomialHMM(
                n_components=3,
                n_iter=self.n_iter,
                verbose=self.verbose,
                implementation=implementation
            )

    def generate_training_sequences(self):

        sampler = hmmlearn.hmm.MultinomialHMM(n_components=3)
        sampler.startprob_ = np.array([0.6, 0.3, 0.1])
        sampler.transmat_ = np.array([[0.6, 0.2, 0.2],
                             [0.3, 0.5, 0.2],
                             [0.4, 0.3, 0.3]])

        sampler.emissionprob_ = np.array([
            [.1, .5, .1, .3],
            [.1, .2, .4, .3],
            [0, .5, .5, .0],
        ])

        sequences, states = sampler.sample(50000)
        lengths = [len(sequences)]
        return sequences, lengths

    def log_one_run(self, start, end, model, tag):
        super().log_one_run(start, end, model, tag)
        LOG.info(f"emissions={model.emissionprob_}")


class MultivariateGaussianBenchmark(GaussianBenchmark):
    def generate_training_sequences(self):
        sampler = hmmlearn.hmm.GaussianHMM(
            n_components=4,
            covariance_type="full",
            init_params=""
        )

        sampler.startprob_ = np.asarray([0, 0, 0, 1])
        sampler.transmat_ = np.asarray([
            [.2, .2, .3, .3],
            [.3, .2, .2, .3],
            [.2, .3, .3, .2],
            [.3, .3, .2, .2],
        ])
        sampler.means_ = np.asarray([
            [-1.5, 0],
            [0,  0],
            [1.5, 0],
            [3, 0]
        ])
        sampler.covars_ = np.asarray([
            [[.5, 0],
             [0, .5]],
            [[.5, 0],
             [0, 0.5]],

            [[.5, 0],
             [0, .5]],
            [[0.5, 0],
             [0, 0.5]],
        ])

        observed, hidden = sampler.sample(50000)
        lengths = [len(observed)]
        return observed, lengths


class GMMBenchmark(GaussianBenchmark):
    def generate_training_sequences(self):
        sampler = hmmlearn.hmm.GMMHMM(
            n_components=4,
            n_mix=3,
            covariance_type="full",
            init_params=""
        )

        sampler.startprob_ = [.25, .25, .25, .25]
        sampler.transmat_ = [
            [.1, .3, .3, .3],
            [.3, .1, .3, .3],
            [.3, .3, .1, .3],
            [.3, .3, .3, .1],
        ]
        sampler.weights_ = [
            [.2, .2, .6],
            [.6, .2, .2],
            [.2, .6, .2],
            [.1, .1, .8],
        ]
        sampler.means_ = np.asarray([
            [[-10], [-12], [-9]],
            [[-5], [-4], [-3]],
            [[-1.5], [0], [1.5]],
            [[5], [7], [9]],
        ])

        sampler.covars_ = np.asarray([
            [[[.125]], [[.125]], [[.125]]],
            [[[.125]], [[.125]], [[.125]]],
            [[[.125]], [[.125]], [[.125]]],
            [[[.125]], [[.125]], [[.125]]],
        ])

        n_sequences = 10
        length = 5_000
        sequences = []
        for i in range(n_sequences):
            sequences.append(sampler.sample(5000)[0])
        return np.concatenate(sequences), [length] * n_sequences

    def new_model(self, implementation):
        return hmmlearn.hmm.GMMHMM(
            n_components=4,
            n_mix=3,
            n_iter=self.n_iter,
            covariance_type="full",
            verbose=self.verbose,
            implementation=implementation
        )

    def log_one_run(self, start, end, model, tag):
        super().log_one_run(start, end, model, tag)
        LOG.info(f"weights_={model.weights_}")


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--all", action="store_true")
    parser.add_argument("--categorical", action="store_true")
    parser.add_argument("--gaussian", action="store_true")
    parser.add_argument("--multivariate-gaussian", action="store_true")
    parser.add_argument("--gaussian-mixture", action="store_true")
    parser.add_argument("--repeat", type=int, default=10)
    parser.add_argument("--verbose", action="store_true")
    parser.add_argument("--n-iter", type=int, default=100)

    args = parser.parse_args()
    if args.all:
        args.categorical = True
        args.gaussian = True
        args.multivariate_gaussian = True
        args.gaussian_mixture = True

    if args.categorical:
        bench = MultinomialBenchmark(
            repeat=args.repeat,
            n_iter=args.n_iter,
            verbose=args.verbose,
        )
        bench.run("categorical.benchmark.csv")
    if args.gaussian:
        bench = GaussianBenchmark(
            repeat=args.repeat,
            n_iter=args.n_iter,
            verbose=args.verbose,
        )
        bench.run("gaussian.benchmark.csv")
    if args.multivariate_gaussian:
        bench = MultivariateGaussianBenchmark(
            repeat=args.repeat,
            n_iter=args.n_iter,
            verbose=args.verbose,
        )
        bench.run("multivariate_gaussian.benchmark.csv")
    if args.gaussian_mixture:
        bench = GMMBenchmark(
            repeat=args.repeat,
            n_iter=args.n_iter,
            verbose=args.verbose,
        )
        bench.run("gmm.benchmark.csv")


if __name__ == "__main__":
    logging.basicConfig(
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        level=logging.DEBUG
    )
    main()