-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpredict.py
234 lines (214 loc) · 7.29 KB
/
predict.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Copyright 2021-2024 Lawrence Livermore National Security, LLC and other
# MuyGPyS Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: MIT
from absl.testing import absltest
from absl.testing import parameterized
import MuyGPyS._src.math.numpy as np
from MuyGPyS import config
from MuyGPyS.examples.classify import classify_any
from MuyGPyS.examples.two_class_classify_uq import (
classify_two_class_uq,
train_two_class_interval,
example_lambdas,
make_masks,
do_uq,
)
from MuyGPyS.gp import MuyGPS
from MuyGPyS.gp.deformation import Isotropy, Anisotropy, l2
from MuyGPyS.gp.hyperparameter import ScalarParam, VectorParam
from MuyGPyS.gp.kernels import Matern, RBF
from MuyGPyS.gp.noise import HomoscedasticNoise
from MuyGPyS.neighbors import NN_Wrapper
from MuyGPyS.optimize.batch import (
get_balanced_batch,
)
from MuyGPyS._test.utils import (
_make_gaussian_data,
_basic_nn_kwarg_options,
)
from MuyGPyS._src.mpi_utils import _consistent_unchunk_tensor
if config.state.backend == "torch":
raise ValueError("conventional optimization does not support torch.")
class ClassifyTest(parameterized.TestCase):
@parameterized.parameters(
(
(1000, 200, 2, r, nn, nn_kwargs, k_kwargs)
for r in [10, 1]
for nn in [5, 50]
for nn_kwargs in _basic_nn_kwarg_options
# for f in [100]
# for r in [10]
# for nn in [10]
# for nn_kwargs in [_basic_nn_kwarg_options[0]]
for k_kwargs in (
{
"kernel": Matern(
smoothness=ScalarParam(0.38),
deformation=Isotropy(l2, length_scale=ScalarParam(1.5)),
),
"noise": HomoscedasticNoise(1e-5),
},
{
"kernel": Matern(
smoothness=ScalarParam(0.38),
deformation=Anisotropy(
l2,
length_scale=VectorParam(
ScalarParam(1.5), ScalarParam(0.5)
),
),
),
"noise": HomoscedasticNoise(1e-5),
},
)
)
)
def test_classify_any(
self,
train_count,
test_count,
feature_count,
response_count,
nn_count,
nn_kwargs,
k_kwargs,
):
muygps = MuyGPS(**k_kwargs)
train, test = _make_gaussian_data(
train_count,
test_count,
feature_count,
response_count,
categorical=True,
)
nbrs_lookup = NN_Wrapper(train["input"], nn_count, **nn_kwargs)
predictions, _ = classify_any(
muygps,
test["input"],
train["input"],
nbrs_lookup,
train["output"],
)
predictions = _consistent_unchunk_tensor(predictions)
self.assertEqual(predictions.shape, (test_count, response_count))
class ClassifyUQTest(parameterized.TestCase):
@parameterized.parameters(
(
(1000, 200, 2, r, nn, b, nn_kwargs, k_kwargs)
# for f in [100]
# for r in [2]
# for nn in [10]
# for b in [200]
# for nn_kwargs in [_basic_nn_kwarg_options[0]]
for r in [2]
for nn in [5, 50]
for b in [200]
for nn_kwargs in _basic_nn_kwarg_options
for k_kwargs in (
{
"kernel": Matern(
smoothness=ScalarParam(0.38),
deformation=Isotropy(l2, length_scale=ScalarParam(1.5)),
),
"noise": HomoscedasticNoise(1e-5),
},
{
"kernel": RBF(
deformation=Isotropy(l2, length_scale=ScalarParam(1.5))
),
"noise": HomoscedasticNoise(1e-5),
},
{
"kernel": Matern(
smoothness=ScalarParam(0.38),
deformation=Anisotropy(
l2,
length_scale=VectorParam(
ScalarParam(1.5), ScalarParam(0.5)
),
),
),
"noise": HomoscedasticNoise(1e-5),
},
{
"kernel": RBF(
deformation=Anisotropy(
l2,
length_scale=VectorParam(
ScalarParam(1.5), ScalarParam(0.5)
),
)
),
"noise": HomoscedasticNoise(1e-5),
},
)
)
)
def test_classify_uq(
self,
train_count,
test_count,
feature_count,
response_count,
nn_count,
batch_count,
nn_kwargs,
k_kwargs,
):
muygps = MuyGPS(**k_kwargs)
objective_count = len(example_lambdas)
train, test = _make_gaussian_data(
train_count,
test_count,
feature_count,
response_count,
categorical=True,
)
train["output"] *= 2
test["output"] *= 2
nbrs_lookup = NN_Wrapper(train["input"], nn_count, **nn_kwargs)
predictions, variances, _ = classify_two_class_uq(
muygps,
test["input"],
train["input"],
nbrs_lookup,
train["output"],
)
predictions = _consistent_unchunk_tensor(predictions)
variances = _consistent_unchunk_tensor(variances)
self.assertEqual(predictions.shape, (test_count, response_count))
self.assertEqual(variances.squeeze().shape, (test_count,))
train_labels = np.argmax(train["output"], axis=1)
indices, nn_indices = get_balanced_batch(
nbrs_lookup,
train_labels,
batch_count,
)
cutoffs = train_two_class_interval(
muygps,
indices,
nn_indices,
train["input"],
train["output"],
train_labels,
example_lambdas,
)
self.assertEqual(cutoffs.shape, (objective_count,))
min_label = np.min(train["output"][0, :])
max_label = np.max(train["output"][0, :])
if min_label == 0.0 and max_label == 1.0:
_ = np.argmax(predictions, axis=1)
elif min_label == -1.0 and max_label == 1.0:
_ = 2 * np.argmax(predictions, axis=1) - 1
else:
raise ("Unhandled label encoding min ({min_label}, {max_label})!")
mid_value = (min_label + max_label) / 2
masks = make_masks(predictions, cutoffs, variances, mid_value)
self.assertEqual(masks.shape, (objective_count, test_count))
acc, uq = do_uq(predictions, test["output"], masks)
self.assertGreaterEqual(acc, 0.0)
self.assertLessEqual(acc, 1.0)
self.assertEqual(uq.shape, (objective_count, 3))
if __name__ == "__main__":
absltest.main()