-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathscale_opt.py
146 lines (119 loc) · 4.82 KB
/
scale_opt.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
# 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.gp import get_analytic_scale
from MuyGPyS._test.optimize import BenchmarkTestCase
from MuyGPyS._test.utils import _sq_rel_err
from MuyGPyS.gp import MuyGPS
from MuyGPyS.gp.deformation import Isotropy, l2
from MuyGPyS.gp.hyperparameter import (
AnalyticScale,
DownSampleScale,
ScalarParam,
)
from MuyGPyS.gp.kernels import Matern
from MuyGPyS.gp.noise import HomoscedasticNoise
if config.state.backend != "numpy":
raise ValueError("optimize.py only supports the numpy backend at this time")
class ScaleTest(BenchmarkTestCase):
@classmethod
def setUpClass(cls):
super(ScaleTest, cls).setUpClass()
def test_scale(self):
mrse = 0.0
pairwise_tensor = self.sampler.gp.kernel.deformation.pairwise_tensor(
self.train_features, mm.arange(self.train_count)
)
Kin = self.sampler.gp.kernel(
pairwise_tensor
) + self.sampler.gp.noise() * mm.eye(self.train_count)
for i in range(self.its):
ss = get_analytic_scale(Kin, self.train_responses_list[i])
mrse += _sq_rel_err(self.params["scale"](), ss)
mrse /= self.its
print(f"optimizes with mean relative squared error {mrse}")
self.assertLessEqual(mrse, self.scale_tol)
class AnalyticOptimTest(BenchmarkTestCase):
@classmethod
def setUpClass(cls):
super(AnalyticOptimTest, cls).setUpClass()
def test_scale_optim(self):
mrse = 0.0
for i in range(self.its):
muygps = MuyGPS(
kernel=Matern(
smoothness=ScalarParam(self.params["smoothness"]()),
deformation=Isotropy(
metric=l2,
length_scale=ScalarParam(self.params["length_scale"]()),
),
),
noise=HomoscedasticNoise(self.params["noise"]()),
scale=AnalyticScale(),
)
muygps = muygps.optimize_scale(
self.batch_pairwise_dists_list[i], self.batch_nn_targets_list[i]
)
estimate = muygps.scale()
mrse += _sq_rel_err(self.params["scale"](), estimate)
mrse /= self.its
print(f"optimizes with mean relative squared error {mrse}")
self.assertLessEqual(mrse, self.scale_tol)
def test_iterative_scale_optim(self):
mrse = 0.0
for i in range(self.its):
muygps = MuyGPS(
kernel=Matern(
smoothness=ScalarParam(self.params["smoothness"]()),
deformation=Isotropy(
metric=l2,
length_scale=ScalarParam(self.params["length_scale"]()),
),
),
noise=HomoscedasticNoise(self.params["noise"]()),
scale=AnalyticScale(iteration_count=10),
)
muygps = muygps.optimize_scale(
self.batch_pairwise_dists_list[i], self.batch_nn_targets_list[i]
)
estimate = muygps.scale()
mrse += _sq_rel_err(self.params["scale"](), estimate)
mrse /= self.its
print(f"optimizes with mean relative squared error {mrse}")
self.assertLessEqual(mrse, self.scale_tol)
class DownSampleOptimTest(BenchmarkTestCase):
@classmethod
def setUpClass(cls):
super(DownSampleOptimTest, cls).setUpClass()
@parameterized.parameters(((d, i) for d in [8] for i in [10]))
def test_scale_optim(self, down_count, iteration_count):
mrse = 0.0
for i in range(self.its):
muygps = MuyGPS(
kernel=Matern(
smoothness=ScalarParam(self.params["smoothness"]()),
deformation=Isotropy(
metric=l2,
length_scale=ScalarParam(self.params["length_scale"]()),
),
),
noise=HomoscedasticNoise(self.params["noise"]()),
scale=DownSampleScale(
down_count=down_count, iteration_count=iteration_count
),
)
muygps = muygps.optimize_scale(
self.batch_pairwise_dists_list[i], self.batch_nn_targets_list[i]
)
estimate = muygps.scale()
mrse += _sq_rel_err(self.params["scale"](), estimate)
mrse /= self.its
print(f"optimizes with mean relative squared error {mrse}")
self.assertLessEqual(mrse, self.scale_tol)
if __name__ == "__main__":
absltest.main()