-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoptimize.py
206 lines (184 loc) · 6.38 KB
/
optimize.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
# 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 as mm
from MuyGPyS import config
from MuyGPyS._test.optimize import BenchmarkTestCase
from MuyGPyS._test.utils import (
# _advanced_opt_fn_and_kwarg_options,
_basic_opt_fn_and_kwarg_options,
)
from MuyGPyS.gp import MuyGPS
from MuyGPyS.gp.deformation import Isotropy, l2
from MuyGPyS.gp.hyperparameter import AnalyticScale, FixedScale, ScalarParam
from MuyGPyS.gp.kernels import Matern
from MuyGPyS.gp.noise import HomoscedasticNoise
from MuyGPyS.optimize.loss import lool_fn, mse_fn, pseudo_huber_fn, looph_fn
if config.state.backend != "numpy":
raise ValueError("optimize.py only supports the numpy backend at this time")
class BenchmarkTest(BenchmarkTestCase):
@classmethod
def setUpClass(cls):
super(BenchmarkTest, cls).setUpClass()
def test_types(self):
self._check_ndarray(
self.train_features,
mm.ftype,
ctype=mm.ndarray,
shape=(self.train_count, self.feature_count),
)
self._check_ndarray(
self.test_features,
mm.ftype,
ctype=mm.ndarray,
shape=(self.test_count, self.feature_count),
)
for i in range(self.its):
self._check_ndarray(
self.train_responses_list[i],
mm.ftype,
ctype=mm.ndarray,
shape=(self.train_count, self.response_count),
)
self._check_ndarray(
self.test_responses_list[i],
mm.ftype,
ctype=mm.ndarray,
shape=(self.test_count, self.response_count),
)
class SmoothnessTest(BenchmarkTestCase):
@classmethod
def setUpClass(cls):
super(SmoothnessTest, cls).setUpClass()
@parameterized.parameters(
(
(
loss_kwargs_and_scale,
opt_fn_and_kwargs,
)
for loss_kwargs_and_scale in [
["mse", mse_fn, dict(), FixedScale()],
[
"huber",
pseudo_huber_fn,
{"boundary_scale": 1.5},
FixedScale(),
],
["lool", lool_fn, dict(), AnalyticScale()],
["looph", looph_fn, {"boundary_scale": 3.0}, AnalyticScale()],
]
for opt_fn_and_kwargs in _basic_opt_fn_and_kwarg_options
# for opt_fn_and_kwargs in [_basic_opt_fn_and_kwarg_options[0]]
)
)
def test_smoothness(
self,
loss_kwargs_and_scale,
opt_fn_and_kwargs,
):
(
loss_name,
loss_fn,
loss_kwargs,
scale,
) = loss_kwargs_and_scale
opt_fn, opt_kwargs = opt_fn_and_kwargs
error_vector = list()
for i in range(self.its):
# set up MuyGPS object
muygps = MuyGPS(
kernel=Matern(
smoothness=ScalarParam(
"sample", self.params["smoothness"].get_bounds()
),
deformation=Isotropy(
metric=l2,
length_scale=ScalarParam(self.params["length_scale"]()),
),
),
noise=HomoscedasticNoise(self.params["noise"]()),
scale=scale,
)
error_vector.append(
self._optim_chassis(
muygps,
"smoothness",
i,
loss_fn,
opt_fn,
opt_kwargs,
loss_kwargs=loss_kwargs,
)
)
median_rse = mm.median(error_vector)
print(
f"optimizes smoothness with mean relative squared error {median_rse}"
)
# Is this a strong enough guarantee?
self.assertLessEqual(median_rse, self.smoothness_tol[loss_name])
class LengthScaleTest(BenchmarkTestCase):
@classmethod
def setUpClass(cls):
super(LengthScaleTest, cls).setUpClass()
@parameterized.parameters(
(
(
loss_and_scale,
opt_fn_and_kwargs,
)
for loss_and_scale in [
["lool", lool_fn, dict(), AnalyticScale()],
["looph", looph_fn, {"boundary_scale": 3.0}, AnalyticScale()],
]
# for opt_fn_and_kwargs in _advanced_opt_fn_and_kwarg_options
for opt_fn_and_kwargs in _basic_opt_fn_and_kwarg_options
# for opt_fn_and_kwargs in [_basic_opt_fn_and_kwarg_options[0]]
# for opt_fn_and_kwargs in [_basic_opt_fn_and_kwarg_options[1]]
)
)
def test_length_scale(
self,
loss_and_scale,
opt_fn_and_kwargs,
):
loss_name, loss_fn, loss_kwargs, scale = loss_and_scale
opt_fn, opt_kwargs = opt_fn_and_kwargs
error_vector = list()
for i in range(self.its):
# set up MuyGPS object
muygps = MuyGPS(
kernel=Matern(
smoothness=ScalarParam(self.params["smoothness"]()),
deformation=Isotropy(
metric=l2,
length_scale=ScalarParam(
"sample", self.params["length_scale"].get_bounds()
),
),
),
noise=HomoscedasticNoise(self.params["noise"]()),
scale=scale,
)
error_vector.append(
self._optim_chassis(
muygps,
"length_scale",
i,
loss_fn,
opt_fn,
opt_kwargs,
loss_kwargs=loss_kwargs,
)
)
median_error = mm.median(error_vector)
print(
"optimizes length_scale with "
f"median relative squared error {median_error}"
)
# Is this a strong enough guarantee?
self.assertLessEqual(median_error, self.length_scale_tol[loss_name])
if __name__ == "__main__":
absltest.main()