-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserving.coco
44 lines (31 loc) · 1.36 KB
/
serving.coco
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
"""
The serving backend. Selects the best existing data point.
"""
from bbopt.util import best_example
from bbopt.backends.util import Backend
# Backend:
class ServingBackend(Backend):
"""The serving backend uses the parameter values from the best example."""
backend_name = "serving"
@override
def attempt_update(self, examples, params, allow_missing_data=False):
"""Update the serving backend with new parameters."""
# since we're serving, ignore params and just extract the best example
self.current_values = best_example(examples)["values"]
# set new allow_missing_data and call init_fallback_backend if necessary
self.allow_missing_data = allow_missing_data
if not self.fallback_backend and self.allow_missing_data:
self.init_fallback_backend()
return True
@override
def fallback_func(self, name, func, *args, **kwargs):
if self.allow_missing_data:
return super().fallback_func(name, func, *args, **kwargs)
else:
raise ValueError(f"missing data for parameter {name} while serving and no guess")
# Registered names:
ServingBackend.register()
# allow_missing_data=False not included to help bb._backend_store
ServingBackend.register_alg(None)
ServingBackend.register_alg("serving")
ServingBackend.register_alg("max_greedy", allow_missing_data=True)