File: profile_online_em.py

package info (click to toggle)
python-scipy 0.5.2-0.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 33,888 kB
  • ctags: 44,231
  • sloc: ansic: 156,256; cpp: 90,347; python: 89,604; fortran: 73,083; sh: 1,318; objc: 424; makefile: 342
file content (241 lines) | stat: -rw-r--r-- 6,666 bytes parent folder | download | duplicates (2)
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
# /usr/bin/python
# Last Change: Wed Dec 06 08:00 PM 2006 J
import copy

import numpy as N

from gauss_mix import GM
from gmm_em import GMM

def _generate_data(nframes, d, k, mode = 'diag'):
    N.random.seed(0)
    w, mu, va   = GM.gen_param(d, k, mode, spread = 1.5)
    gm          = GM.fromvalues(w, mu, va)
    # Sample nframes frames  from the model
    data        = gm.sample(nframes)

    #++++++++++++++++++++++++++++++++++++++++++
    # Approximate the models with classical EM
    #++++++++++++++++++++++++++++++++++++++++++
    emiter  = 5
    # Init the model
    lgm = GM(d, k, mode)
    gmm = GMM(lgm, 'kmean')
    gmm.init(data)

    gm0    = copy.copy(gmm.gm)
    # The actual EM, with likelihood computation
    like    = N.zeros(emiter)
    for i in range(emiter):
        g, tgd  = gmm.sufficient_statistics(data)
        like[i] = N.sum(N.log(N.sum(tgd, 1)), axis = 0)
        gmm.update_em(data, g)

    return data, gm

nframes = int(5e3)
d       = 1
k       = 2
niter   = 1

def test_v1():
    # Generate test data
    data, gm    = _generate_data(nframes, d, k)
    for i in range(niter):
        iter_1(data, gm)

def test_v2():
    # Generate test data
    data, gm    = _generate_data(nframes, d, k)
    for i in range(niter):
        iter_2(data, gm)

def test_v3():
    # Generate test data
    data, gm    = _generate_data(nframes, d, k)
    for i in range(niter):
        iter_3(data, gm)

def test_v4():
    # Generate test data
    data, gm    = _generate_data(nframes, d, k)
    for i in range(niter):
        iter_4(data, gm)

def iter_1(data, gm):
    """Online EM with original densities + original API"""
    from online_em import OnGMM

    nframes     = data.shape[0]
    ogm         = copy.copy(gm)
    ogmm        = OnGMM(ogm, 'kmean')
    init_data   = data[0:nframes / 20, :]
    ogmm.init(init_data)

    # Forgetting param
    ku		= 0.005
    t0		= 200
    lamb	= 1 - 1/(N.arange(-1, nframes-1) * ku + t0)
    nu0		= 0.2
    nu		= N.zeros((len(lamb), 1))
    nu[0]	= nu0
    for i in range(1, len(lamb)):
        nu[i]	= 1./(1 + lamb[i] / nu[i-1])

    # object version of online EM
    for t in range(nframes):
        ogmm.compute_sufficient_statistics_frame(data[t], nu[t])
        ogmm.update_em_frame()

    ogmm.gm.set_param(ogmm.cw, ogmm.cmu, ogmm.cva)
    print ogmm.cw
    print ogmm.cmu
    print ogmm.cva

def iter_2(data, gm):
    """Online EM with densities2 + original API"""
    from online_em2 import OnGMM

    nframes     = data.shape[0]
    ogm         = copy.copy(gm)
    ogmm        = OnGMM(ogm, 'kmean')
    init_data   = data[0:nframes / 20, :]
    ogmm.init(init_data)

    # Forgetting param
    ku		= 0.005
    t0		= 200
    lamb	= 1 - 1/(N.arange(-1, nframes-1) * ku + t0)
    nu0		= 0.2
    nu		= N.zeros((len(lamb), 1))
    nu[0]	= nu0
    for i in range(1, len(lamb)):
        nu[i]	= 1./(1 + lamb[i] / nu[i-1])

    # object version of online EM
    for t in range(nframes):
        ogmm.compute_sufficient_statistics_frame(data[t], nu[t])
        ogmm.update_em_frame()

    ogmm.gm.set_param(ogmm.cw, ogmm.cmu, ogmm.cva)
    print ogmm.cw
    print ogmm.cmu
    print ogmm.cva

def iter_3(data, gm):
    """Online EM with densities + 1d API"""
    from online_em import OnGMM1d

    #def blop(self, frame, nu):
    #    self.compute_sufficient_statistics_frame(frame, nu)
    #OnGMM.blop  = blop

    nframes     = data.shape[0]
    ogm         = copy.copy(gm)
    ogmm        = OnGMM1d(ogm, 'kmean')
    init_data   = data[0:nframes / 20, :]
    ogmm.init(init_data[:, 0])

    # Forgetting param
    ku		= 0.005
    t0		= 200
    lamb	= 1 - 1/(N.arange(-1, nframes-1) * ku + t0)
    nu0		= 0.2
    nu		= N.zeros((len(lamb), 1))
    nu[0]	= nu0
    for i in range(1, len(lamb)):
        nu[i]	= 1./(1 + lamb[i] / nu[i-1])

    # object version of online EM
    for t in range(nframes):
        #assert ogmm.cw is ogmm.pw
        #assert ogmm.cva is ogmm.pva
        #assert ogmm.cmu is ogmm.pmu
        a, b, c = ogmm.compute_sufficient_statistics_frame(data[t, 0], nu[t])
        ##ogmm.blop(data[t,0], nu[t])
        ogmm.update_em_frame(a, b, c)

    #ogmm.gm.set_param(ogmm.cw, ogmm.cmu, ogmm.cva)
    print ogmm.cw
    print ogmm.cmu
    print ogmm.cva

def iter_4(data, gm):
    """Online EM with densities2 + 1d API"""
    from online_em2 import OnGMM1d

    #def blop(self, frame, nu):
    #    self.compute_sufficient_statistics_frame(frame, nu)
    #OnGMM.blop  = blop

    nframes     = data.shape[0]
    ogm         = copy.copy(gm)
    ogmm        = OnGMM1d(ogm, 'kmean')
    init_data   = data[0:nframes / 20, :]
    ogmm.init(init_data[:, 0])

    # Forgetting param
    ku		= 0.005
    t0		= 200
    lamb	= 1 - 1/(N.arange(-1, nframes-1) * ku + t0)
    nu0		= 0.2
    nu		= N.zeros((len(lamb), 1))
    nu[0]	= nu0
    for i in range(1, len(lamb)):
        nu[i]	= 1./(1 + lamb[i] / nu[i-1])

    # object version of online EM
    def blop():
        #for t in range(nframes):
        #    #assert ogmm.cw is ogmm.pw
        #    #assert ogmm.cva is ogmm.pva
        #    #assert ogmm.cmu is ogmm.pmu
        #    #a, b, c = ogmm.compute_sufficient_statistics_frame(data[t, 0], nu[t])
        #    ###ogmm.blop(data[t,0], nu[t])
        #    #ogmm.update_em_frame(a, b, c)
        #    ogmm.compute_em_frame(data[t, 0], nu[t])
        [ogmm.compute_em_frame(data[t, 0], nu[t]) for t in range(nframes)]
    blop()

    #ogmm.gm.set_param(ogmm.cw, ogmm.cmu, ogmm.cva)
    print ogmm.cw
    print ogmm.cmu
    print ogmm.cva



if __name__ == '__main__':
    #import hotshot, hotshot.stats
    #profile_file    = 'onem1.prof'
    #prof    = hotshot.Profile(profile_file, lineevents=1)
    #prof.runcall(test_v1)
    #p = hotshot.stats.load(profile_file)
    #print p.sort_stats('cumulative').print_stats(20)
    #prof.close()

    #import hotshot, hotshot.stats
    #profile_file    = 'onem2.prof'
    #prof    = hotshot.Profile(profile_file, lineevents=1)
    #prof.runcall(test_v2)
    #p = hotshot.stats.load(profile_file)
    #print p.sort_stats('cumulative').print_stats(20)
    #prof.close()

    import hotshot, hotshot.stats
    profile_file    = 'onem3.prof'
    prof    = hotshot.Profile(profile_file, lineevents=1)
    prof.runcall(test_v3)
    p = hotshot.stats.load(profile_file)
    print p.sort_stats('cumulative').print_stats(20)
    prof.close()

    import hotshot, hotshot.stats
    profile_file    = 'onem4.prof'
    prof    = hotshot.Profile(profile_file, lineevents=1)
    prof.runcall(test_v4)
    p = hotshot.stats.load(profile_file)
    print p.sort_stats('cumulative').print_stats(20)
    prof.close()
    #test_v1()
    #test_v2()
    #test_v3()