-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathacquisition_functions.py
277 lines (239 loc) · 10.1 KB
/
acquisition_functions.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from __future__ import annotations
import time
from copy import deepcopy
from typing import Any, Optional
import torch
from botorch.acquisition import AcquisitionFunction, MCAcquisitionFunction
from botorch.acquisition.objective import MCAcquisitionObjective
from botorch.exceptions.errors import UnsupportedError
from botorch.models.model import Model
from botorch.sampling.samplers import MCSampler, SobolQMCNormalSampler
from botorch.utils.gp_sampling import get_gp_samples
from botorch.utils.transforms import (concatenate_pending_points,
match_batch_shape,
t_batch_mode_transform)
from torch import Tensor
from torch.distributions import Bernoulli, Normal
from constants import * # noqa: F403, F401
from helper_classes import LearnedPrefereceObjective, PosteriorMeanDummySampler
def get_rff_sample(outcome_model):
om_without_transforms = deepcopy(outcome_model)
om_without_transforms.input_transform = None
om_without_transforms.outcome_transform = None
gp_samples = get_gp_samples(
model=om_without_transforms,
num_outputs=om_without_transforms.num_outputs,
n_samples=1,
num_rff_features=500,
)
gp_samples.input_transform = deepcopy(outcome_model.input_transform)
gp_samples.outcome_transform = deepcopy(outcome_model.outcome_transform)
return gp_samples
class BALD(MCAcquisitionFunction):
r"""Bayesian Active Learning by Disagreement"""
def __init__(
self,
outcome_model: Model,
pref_model: Model,
search_space_type: str,
sampler: Optional[MCSampler] = None,
objective: Optional[MCAcquisitionObjective] = None,
X_pending: Optional[Tensor] = None,
**kwargs: Any,
) -> None:
# sampler and objectives are placeholders and not used
if sampler is None:
sampler = PosteriorMeanDummySampler(model=outcome_model)
if objective is None:
preference_sampler = SobolQMCNormalSampler(
num_samples=64, resample=False, collapse_batch_dims=True
)
objective = LearnedPrefereceObjective(
pref_model=pref_model,
sampler=preference_sampler,
use_mean=False,
)
super().__init__(
model=outcome_model,
sampler=sampler,
objective=objective,
X_pending=X_pending,
)
pref_model.eval()
self.pref_model = pref_model
self.search_space_type = search_space_type
if search_space_type == "rff":
self.gp_samples = get_rff_sample(outcome_model)
@concatenate_pending_points
@t_batch_mode_transform()
def forward(self, X: Tensor) -> Tensor:
# only work with q = 2
assert X.shape[-2] == 2
if self.search_space_type == "rff":
batch_q_shape = X.shape[:-1]
X_dim = X.shape[-1]
Y = self.gp_samples.posterior(X.reshape(-1, X_dim)).mean.reshape(batch_q_shape + (-1,))
elif self.search_space_type == "f_mean":
outcome_posterior = self.outcome_model.posterior(X)
Y = outcome_posterior.mean
elif self.search_space_type == "y":
Y = X
else:
raise UnsupportedError("Unsupported search_space_type!")
preference_posterior = self.pref_model(Y)
preference_mean = preference_posterior.mean
preference_cov = preference_posterior.covariance_matrix
mu = preference_mean[..., 0] - preference_mean[..., 1]
var = (
2.0
+ preference_cov[..., 0, 0]
+ preference_cov[..., 1, 1]
- preference_cov[..., 0, 1]
- preference_cov[..., 1, 0]
)
sigma = torch.sqrt(var)
obj_samples = Normal(0, 1).cdf(Normal(mu, sigma).rsample(torch.Size([2048])))
posterior_entropies = (
Bernoulli(Normal(0, 1).cdf(mu / torch.sqrt(var + 1))).entropy().squeeze(-1)
)
sample_entropies = Bernoulli(obj_samples).entropy()
conditional_entropies = sample_entropies.mean(dim=0).squeeze(-1)
return posterior_entropies - conditional_entropies
class qPreferentialOptimal(MCAcquisitionFunction):
r"""
MC EUBO
(y_1, y_2)^* = argmax_{y_1,y_2 \in Y} E[max{g(y_1), g(y_2)}]
"""
def __init__(
self,
outcome_model: Model,
pref_model: Model,
sampler: Optional[MCSampler] = None,
objective: Optional[MCAcquisitionObjective] = None,
X_pending: Optional[Tensor] = None,
**kwargs: Any,
) -> None:
r"""q-Preferential Noisy Expected Improvement.
Args:
outcome_model (Model): .
pref_model (Model): .
sampler (Optional[MCSampler], optional): . Defaults to None.
objective (Optional[MCAcquisitionObjective], optional): . Defaults to None.
X_pending (Optional[Tensor], optional): . Defaults to None.
"""
if sampler is None:
sampler = PosteriorMeanDummySampler(model=outcome_model)
if objective is None:
preference_sampler = SobolQMCNormalSampler(
num_samples=64, resample=False, collapse_batch_dims=True
)
objective = LearnedPrefereceObjective(
pref_model=pref_model,
sampler=preference_sampler,
use_mean=False,
)
super().__init__(
model=outcome_model,
sampler=sampler,
objective=objective,
X_pending=X_pending,
)
@concatenate_pending_points
@t_batch_mode_transform()
def forward(self, X: Tensor) -> Tensor:
r"""Evaluate qNoisyExpectedImprovement on the candidate set `X`.
Args:
X: A `batch_shape x q x d`-dim Tensor of t-batches with `q` `d`-dim design
points each.
Returns:
A `batch_shape'`-dim Tensor of Noisy Expected Improvement values at the
given design points `X`, where `batch_shape'` is the broadcasted batch shape
of model and input `X`.
"""
Y_posterior = self.model.posterior(X)
Y_samples = self.sampler(Y_posterior)
obj = self.objective(Y_samples)
max_util_samples = obj.max(dim=-1).values
exp_max_util = max_util_samples.mean(dim=0)
return exp_max_util
class ExpectedUtility(AcquisitionFunction):
r"""Analytic Prefential Expected Utility, i.e., Analytical EUBO"""
def __init__(
self,
preference_model: Model,
outcome_model: Model,
search_space_type: str = "f_mean",
previous_winner: Optional[Tensor] = None,
) -> None:
r"""Analytic Preferential Expected Utility.
Args:
preference_model (Model): .
outcome_model (Model): .
search_space_type (str, optional): "f_mean", "rff", or "one_sample". Defaults to "f_mean".
previous_winner (Optional[Tensor], optional): Tensor representing the previous winner in Y space.
Defaults to None.
"""
super().__init__(model=outcome_model)
self.preference_model = preference_model
self.outcome_model = outcome_model
self.register_buffer("previous_winner", previous_winner)
self.preference_model.eval() # make sure model is in eval mode
if self.outcome_model is not None:
self.outcome_model.eval()
self.search_space_type = search_space_type
dtype = preference_model.datapoints.dtype
device = preference_model.datapoints.device
self.std_norm = torch.distributions.normal.Normal(
torch.zeros(1, dtype=dtype, device=device),
torch.ones(1, dtype=dtype, device=device),
)
if search_space_type == "rff":
self.gp_samples = get_rff_sample(outcome_model)
elif search_space_type == "one_sample":
Y_dim = preference_model.datapoints.shape[-1]
self.w = self.std_norm.rsample((Y_dim,)).squeeze(-1)
elif search_space_type not in ("y", "f_mean"):
raise UnsupportedError("Unsupported search space!")
@t_batch_mode_transform()
def forward(self, X: Tensor) -> Tensor:
r"""Evaluate PreferentialOneStepLookahead on the candidate set X.
Args:
X: A `batch_shape x q x d`-dim Tensor, where `q = 2` if `previous_winner` is
not `None`, and `q = 1` otherwise.
Returns:
The acquisition value for each batch as a tensor of shape `batch_shape`.
"""
assert (X.shape[-2] == 2) or ((X.shape[-2] == 1) and (self.previous_winner is not None))
if self.search_space_type == "rff":
batch_q_shape = X.shape[:-1]
X_dim = X.shape[-1]
Y = self.gp_samples.posterior(X.reshape(-1, X_dim)).mean.reshape(batch_q_shape + (-1,))
elif self.search_space_type == "f_mean":
outcome_posterior = self.outcome_model.posterior(X)
Y = outcome_posterior.mean
elif self.search_space_type == "one_sample":
post = self.outcome_model.posterior(X)
Y = post.mean + post.variance.sqrt() * self.w
elif self.search_space_type == "y":
Y = X
else:
raise UnsupportedError("Unsupported search_space_type!")
if self.previous_winner is not None:
Y = torch.cat([Y, match_batch_shape(self.previous_winner, Y)], dim=-2)
preference_posterior = self.preference_model(Y)
preference_mean = preference_posterior.mean
preference_cov = preference_posterior.covariance_matrix
delta = preference_mean[..., 0] - preference_mean[..., 1]
sigma = torch.sqrt(
preference_cov[..., 0, 0]
+ preference_cov[..., 1, 1]
- preference_cov[..., 0, 1]
- preference_cov[..., 1, 0]
)
u = delta / sigma
ucdf = self.std_norm.cdf(u)
updf = torch.exp(self.std_norm.log_prob(u))
acqf_val = sigma * (updf + u * ucdf)
if self.previous_winner is None:
acqf_val += preference_mean[..., 1]
return acqf_val