forked from ldeecke/gmm-torch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
213 lines (166 loc) · 6.72 KB
/
test.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
import numpy as np
import sklearn.mixture
import torch
from gmm import GaussianMixture
import unittest
class CpuCheck(unittest.TestCase):
"""
Basic tests for CPU.
"""
def testPredictClasses(self):
"""
Assert that torch.FloatTensor is handled correctly.
"""
x = torch.randn(400, 2)
n_components = np.random.randint(1, 100)
model = GaussianMixture(n_components, x.size(1))
model.fit(x)
y = model.predict(x)
# check that dimensionality of class memberships is (n)
self.assertEqual(torch.Tensor(x.size(0)).size(), y.size())
def testPredictProbabilities(self):
"""
Assert that torch.FloatTensor is handled correctly when returning class probabilities.
"""
x = torch.randn(400, 2)
n_components = np.random.randint(1, 100)
model = GaussianMixture(n_components, x.size(1))
model.fit(x)
# check that y_p has dimensions (n, k)
y_p = model.predict(x, probs=True)
self.assertEqual(torch.Tensor(x.size(0), n_components).size(), y_p.size())
def testEmMatchesDiagSkLearn(self):
"""
Assert that log-probabilities (E-step) and parameter updates (M-step) approximately match those of sklearn.
"""
d = 20
n_components = np.random.randint(1, 100)
# (n, k, d)
x = torch.randn(400, 1, d).double()
# (n, d)
x_np = np.squeeze(x.data.numpy())
mu_init = torch.ones(1, n_components, d) - .2
var_init = torch.ones(1, n_components, d) - .4
model = GaussianMixture(n_components, d, mu_init=mu_init, var_init=var_init, covariance_type="diag")
model_sk = sklearn.mixture.GaussianMixture(n_components,
covariance_type="diag",
init_params="random",
means_init=np.squeeze(model.mu.data.numpy()),
precisions_init=np.squeeze(1. / np.sqrt(var_init.data.numpy())),
weights_init=np.squeeze(model.pi.data.numpy()))
model_sk._initialize_parameters(x_np, np.random.RandomState())
log_prob_sk = model_sk._estimate_log_prob(x_np)
log_prob = model._estimate_log_prob(x)
# Test whether log-probabilities are approximately equal
np.testing.assert_almost_equal(np.squeeze(log_prob.data.numpy()),
log_prob_sk,
decimal=2,
verbose=True)
_, log_resp_sk = model_sk._e_step(x_np)
_, log_resp = model._e_step(x)
# Test whether E-steps are approximately equal
np.testing.assert_almost_equal(np.squeeze(log_resp.data.numpy()),
log_resp_sk,
decimal=0,
verbose=True)
# Compute pi weight var update in sk, then compare against gmm
model_sk._m_step(x_np, log_resp_sk)
pi_sk = model_sk.weights_
mu_sk = model_sk.means_
var_sk = model_sk.covariances_
pi, mu, var = model._m_step(x, log_resp)
# Test whether pi ..
np.testing.assert_almost_equal(np.squeeze(pi.data.numpy()),
pi_sk,
decimal=1,
verbose=True)
# .. mu ..
np.testing.assert_almost_equal(np.squeeze(mu.data.numpy()),
mu_sk,
decimal=1,
verbose=True)
# .. and var are approximately equal
np.testing.assert_almost_equal(np.squeeze(var.data.numpy()),
var_sk,
decimal=1,
verbose=True)
def testEmMatchesFullSkLearn(self):
"""
Assert that log-probabilities (E-step) and parameter updates (M-step) approximately match those of sklearn.
"""
d = 20
n_components = np.random.randint(1, 100)
# (n, k, d)
x = torch.randn(400, 1, d).double()
# (n, d)
x_np = np.squeeze(x.data.numpy())
var_init = torch.eye(d,dtype=torch.float64).reshape(1, 1, d, d).repeat(1,n_components,1, 1)
model = GaussianMixture(n_components, d, init_params="random", var_init=var_init, covariance_type="full")
model_sk = sklearn.mixture.GaussianMixture(n_components,
covariance_type="full",
init_params="random",
means_init=np.squeeze(model.mu.data.numpy()),
precisions_init=np.squeeze(np.linalg.inv(var_init)))
model_sk._initialize_parameters(x_np, np.random.RandomState())
log_prob_sk = model_sk._estimate_log_prob(x_np)
log_prob = model._estimate_log_prob(x)
# Test whether log-probabilities are approximately equal
np.testing.assert_almost_equal(np.squeeze(log_prob.data.numpy()),
log_prob_sk,
decimal=2,
verbose=True)
_, log_resp_sk = model_sk._e_step(x_np)
_, log_resp = model._e_step(x)
# Test whether E-steps are approximately equal
np.testing.assert_almost_equal(np.squeeze(log_resp.data.numpy()),
log_resp_sk,
decimal=0,
verbose=True)
model_sk._m_step(x_np, log_resp_sk)
pi_sk = model_sk.weights_
mu_sk = model_sk.means_
var_sk = model_sk.covariances_
pi, mu, var = model._m_step(x, log_resp)
# Test whether pi ..
np.testing.assert_almost_equal(np.squeeze(pi.data.numpy()),
pi_sk,
decimal=1,
verbose=True)
# .. mu ..
np.testing.assert_almost_equal(np.squeeze(mu.data.numpy()),
mu_sk,
decimal=1,
verbose=True)
# .. and var are approximately equal
np.testing.assert_almost_equal(np.squeeze(var.data.numpy()),
var_sk,
decimal=1,
verbose=True)
class GpuCheck(unittest.TestCase):
"""
Basic tests for GPU.
"""
def testPredictClasses(self):
"""
Assert that torch.cuda.FloatTensor is handled correctly.
"""
x = torch.randn(400, 2).cuda()
n_components = np.random.randint(1, 100)
model = GaussianMixture(n_components, x.size(1), covariance_type="diag").cuda()
model.fit(x)
y = model.predict(x)
# check that dimensionality of class memberships is (n)
self.assertEqual(torch.Tensor(x.size(0)).size(), y.size())
def testPredictProbabilities(self):
"""
Assert that torch.cuda.FloatTensor is handled correctly when returning class probabilities.
"""
x = torch.randn(400, 2).cuda()
n_components = np.random.randint(1, 100)
model = GaussianMixture(n_components, x.size(1), covariance_type="diag").cuda()
model.fit(x)
# check that y_p has dimensions (n, k)
y_p = model.predict(x, probs=True)
self.assertEqual(torch.Tensor(x.size(0), n_components).size(), y_p.size())
if __name__ == "__main__":
unittest.main()