-
Notifications
You must be signed in to change notification settings - Fork 420
Expand file tree
/
Copy pathtests.py
More file actions
151 lines (133 loc) · 5.67 KB
/
tests.py
File metadata and controls
151 lines (133 loc) · 5.67 KB
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
import unittest
import logging
import numpy as np
from sklearn import pipeline, preprocessing, linear_model
from competition import AdversarialCompetition
from gradient_descent import GradientDescent
from models import GenerativeNormalModel, GenerativeNormalMixtureModel
from utils import randn
logging.basicConfig(level=logging.INFO)
__version__ = "0.1"
logger = logging.getLogger(__name__)
class TestDistribution(unittest.TestCase):
def test_normal(self):
return self._test(GenerativeNormalModel(1, 2))
def test_normal_mixture(self):
return self._test(GenerativeNormalMixtureModel([0, 1], [0.5, 2]))
def _test(self, model):
step = 0.01
x = np.arange(-100, 100, step)
y = model.predict_proba(x)
integral = np.sum(y) * step
self.assertLess(np.abs(integral - 1), 0.01)
class TestGradient(unittest.TestCase):
def test_normal(self):
return self._test(GenerativeNormalModel(1, 2))
def test_normal_mixture(self):
return self._test(GenerativeNormalMixtureModel([-3, 3], [1, 1]))
def _test(self, model):
np.random.seed(0)
logger.info("Starting test gradient for model %s" % model)
np.random.seed(1)
x = np.round(randn(1), 1)
grad = model.d_log_likelihood(x)
grad_approx = model.d_log_likelihood_approx(x)
np.testing.assert_allclose(grad, grad_approx, 0.01)
class TestConvergence(unittest.TestCase):
def test_normal_10(self):
np.random.seed(0)
size_batch = 10
adversarials = AdversarialCompetition(
size_batch=size_batch,
true_model=GenerativeNormalModel(1, 2),
discriminative=pipeline.make_pipeline(
preprocessing.PolynomialFeatures(4),
linear_model.LogisticRegression()),
generative=GenerativeNormalModel(0, 1, updates=["mu", "sigma"]),
gradient_descent=GradientDescent(
0.1, inertia=0.9, annealing=1000, last_learning_rate=0.01),
)
for i in range(500):
adversarials.iteration()
params = adversarials.generatives[-1]._params
true_params = adversarials.true_model._params
np.testing.assert_allclose(params, true_params, 0, 0.05)
def test_normal_100(self):
np.random.seed(0)
size_batch = 100
adversarials = AdversarialCompetition(
size_batch=size_batch,
true_model=GenerativeNormalModel(1, 2),
discriminative=pipeline.make_pipeline(
preprocessing.PolynomialFeatures(4),
linear_model.LogisticRegression()),
generative=GenerativeNormalModel(
0, 1, updates=["mu", "sigma"]),
gradient_descent=GradientDescent(
0.03, inertia=0.0, annealing=100),
)
for i in range(1000):
adversarials.iteration()
params = adversarials.generatives[-1]._params
true_params = adversarials.true_model._params
np.testing.assert_allclose(params, true_params, 0, 0.02)
def test_normal_1000(self):
np.random.seed(0)
size_batch = 1000
adversarials = AdversarialCompetition(
size_batch=size_batch,
true_model=GenerativeNormalModel(1, 2),
discriminative=pipeline.make_pipeline(
preprocessing.PolynomialFeatures(4),
linear_model.LogisticRegression()),
generative=GenerativeNormalModel(0, 1, updates=["mu", "sigma"]),
gradient_descent=GradientDescent(0.03, 0.9),
)
for i in range(200):
adversarials.iteration()
params = adversarials.generatives[-1]._params
true_params = adversarials.true_model._params
np.testing.assert_allclose(params, true_params, 0, 0.02)
def test_normal_mixture(self):
np.random.seed(0)
size_batch = 1000
competition = AdversarialCompetition(
size_batch=size_batch,
true_model=GenerativeNormalMixtureModel([-3, 3], [1, 1]),
discriminative=pipeline.make_pipeline(
preprocessing.PolynomialFeatures(4),
linear_model.LogisticRegression()),
generative=GenerativeNormalMixtureModel(
[-1, 1], [1, 1], updates=["mu", "sigma"]),
gradient_descent=GradientDescent(
0.1, inertia=0.9, annealing=1000, last_learning_rate=0.01),
)
for i in range(2000):
competition.iteration()
params = competition.generatives[-1]._params
true_params = competition.true_model._params
np.testing.assert_allclose(params, true_params, 0, 0.1)
def test_normal_mixture_hard(self):
np.random.seed(0)
size_batch = 1000
competition = AdversarialCompetition(
size_batch=size_batch,
true_model=GenerativeNormalMixtureModel(
np.arange(-3, 4), np.random.uniform(1, 2, 7).round(2)),
discriminative=pipeline.make_pipeline(
preprocessing.PolynomialFeatures(4),
linear_model.LogisticRegression()),
generative=GenerativeNormalMixtureModel(
np.arange(-3, 4) * 0.1, np.ones(7), updates=["mu", "sigma"]),
gradient_descent=GradientDescent(
np.array([0.3, 0.1, 0.3]).reshape((-1, 1)), inertia=0.9,
annealing=2000, last_learning_rate=0.001),
)
for i in range(5000):
competition.iteration()
params = competition.generatives[-1]._params
print params.shape
true_params = competition.true_model._params
np.testing.assert_allclose(params, true_params, 0, 0.2)
if __name__ == '__main__':
unittest.main()