forked from GPflow/GPflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_data_object.py
174 lines (148 loc) · 6.12 KB
/
test_data_object.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
# Copyright 2017 the GPflow authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import tensorflow as tf
import numpy as np
from numpy.testing import assert_array_equal
import gpflow
from gpflow import settings
from gpflow.test_util import GPflowTestCase
class Foo(gpflow.models.Model):
def _build_likelihood(self):
return tf.constant(0, dtype=gpflow.settings.float_type)
class TestDataHolderSimple(GPflowTestCase):
def prepare(self, autobuild=True):
rng = np.random.RandomState()
with gpflow.defer_build():
m = Foo()
m.X = gpflow.DataHolder(rng.randn(2, 2))
m.Y = gpflow.DataHolder(rng.randn(2, 2))
m.Z = gpflow.DataHolder(rng.randn(2, 2))
if autobuild:
m.compile()
return m, rng
def test_types(self):
with self.test_context():
m, _ = self.prepare(False)
self.assertEqual(m.X.dtype, settings.float_type)
self.assertEqual(m.Y.dtype, settings.float_type)
self.assertEqual(m.Z.dtype, settings.float_type)
self.assertEqual(m.X.read_value().dtype, settings.float_type)
self.assertEqual(m.Y.read_value().dtype, settings.float_type)
self.assertEqual(m.Z.read_value().dtype, settings.float_type)
m.compile()
self.assertEqual(m.X.dtype, settings.float_type)
self.assertEqual(m.Y.dtype, settings.float_type)
self.assertEqual(m.Z.dtype, settings.float_type)
self.assertEqual(m.X.read_value().dtype, settings.float_type)
self.assertEqual(m.Y.read_value().dtype, settings.float_type)
self.assertEqual(m.Z.read_value().dtype, settings.float_type)
def test_same_shape(self):
with self.test_context():
m, rng = self.prepare()
new_X = rng.randn(2, 2)
m.X = new_X
assert_array_equal(m.X.shape, new_X.shape)
assert_array_equal(m.X.read_value(), new_X)
new_Y = rng.randn(2, 2)
m.Y = new_Y
assert_array_equal(m.Y.shape, new_Y.shape)
assert_array_equal(m.Y.read_value(), new_Y)
new_Z = rng.randn(2, 2)
m.Z = new_Z
assert_array_equal(m.Z.shape, new_Z.shape)
assert_array_equal(m.Z.read_value(), new_Z)
def test_raise(self):
with self.test_context():
m, rng = self.prepare(True)
m.Y = rng.randn(3, 3)
class TestDataHolderIntegers(GPflowTestCase):
def prepare(self):
m = Foo(autobuild=False)
rng = np.random.RandomState()
m.X = gpflow.DataHolder(rng.randint(0, 10, (2, 2)), dtype=gpflow.settings.int_type)
m.compile()
return m, rng
def test_types(self):
with self.test_context():
m, _ = self.prepare()
self.assertEqual(m.X.read_value().dtype, gpflow.settings.int_type)
self.assertEqual(m.X.dtype, gpflow.settings.int_type)
def test_same_shape(self):
with self.test_context():
m, rng = self.prepare()
new_X = rng.randint(0, 10, (2, 2), dtype=gpflow.settings.int_type)
m.X = new_X
assert_array_equal(m.X.shape, new_X.shape)
assert_array_equal(m.X.read_value(), new_X)
class TestDataHolderModels(GPflowTestCase):
"""
We make test for Dataholder that enables to reuse model for different data
with the same shape to the original. We tested this for the six models.
"""
def prepare(self):
rng = np.random.RandomState(0)
X = rng.rand(20, 1)
Y = np.sin(X) + 0.9 * np.cos(X * 1.6) + rng.randn(*X.shape) * 0.8
kern = gpflow.kernels.Matern32(1)
return X, Y, kern, rng
def test_gpr(self):
with self.test_context():
X, Y, kern, rng = self.prepare()
m = gpflow.models.GPR(X, Y, kern)
m.compile()
m.X = rng.randn(*X.shape)
m.X = rng.randn(30, 1)
def test_sgpr(self):
with self.test_context():
X, Y, kern, rng = self.prepare()
m = gpflow.models.SGPR(X, Y, kern, Z=X[::2])
m.compile()
m.X = rng.randn(*X.shape)
m.X = rng.randn(30, 1)
def test_gpmc(self):
with self.test_context():
X, Y, kern, rng = self.prepare()
m = gpflow.models.GPMC(X, Y, kern, likelihood=gpflow.likelihoods.StudentT())
m.compile()
m.X = rng.randn(*X.shape)
Xnew = rng.randn(30, 1)
Ynew = np.sin(Xnew) + 0.9 * np.cos(Xnew*1.6) + rng.randn(*Xnew.shape)
m.X = Xnew
m.Y = Ynew
def test_sgpmc(self):
with self.test_context():
X, Y, kern, rng = self.prepare()
m = gpflow.models.SGPMC(X, Y, kern, likelihood=gpflow.likelihoods.StudentT(), Z=X[::2])
m.compile()
m.X = rng.randn(*X.shape)
m.X = rng.randn(30, 1)
def test_svgp(self):
with self.test_context():
X, Y, kern, rng = self.prepare()
m = gpflow.models.SVGP(X, Y, kern, likelihood=gpflow.likelihoods.StudentT(), Z=X[::2])
m.compile()
m.X = rng.randn(*X.shape)
m.X = rng.randn(30, 1)
def test_vgp(self):
with self.test_context():
X, Y, kern, rng = self.prepare()
m = gpflow.models.VGP(X, Y, kern, likelihood=gpflow.likelihoods.StudentT())
m.compile()
m.X = rng.randn(*X.shape)
Xnew = rng.randn(30, 1)
Ynew = np.sin(Xnew) + 0.9 * np.cos(Xnew * 1.6) + rng.randn(*Xnew.shape)
m.X = Xnew
m.Y = Ynew
if __name__ == '__main__':
tf.test.main()