-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathobjective.py
90 lines (77 loc) · 2.79 KB
/
objective.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
from benchopt import BaseObjective
from benchopt import safe_import_context
with safe_import_context() as import_ctx:
import operator
import jax
import jax.numpy as jnp
class Objective(BaseObjective):
name = "Bilevel Optimization"
url = "https://github.com/benchopt/benchmark_bilevel"
requirements = ["scikit-learn", "jax", "jaxlib"]
min_benchopt_version = "1.6.1"
parameters = {"random_state": [2442]}
def __init__(self, random_state=2442):
self.random_state = random_state
def get_one_result(self):
inner_shape, outer_shape = (
self.dim_inner,
self.dim_outer,
)
return dict(
inner_var=jax.tree_util.tree_map(
jnp.zeros,
inner_shape,
is_leaf=lambda x: isinstance(x, tuple),
), # can be a pytree
outer_var=jax.tree_util.tree_map(
jnp.zeros,
outer_shape,
is_leaf=lambda x: isinstance(x, tuple),
), # build a new tree full of zeros with the outer_shape structure
)
def set_data(self, pb_inner, pb_outer, metrics, init_var=None):
(
self.f_inner,
self.n_samples_inner,
self.dim_inner,
self.f_inner_fb,
) = pb_inner
(
self.f_outer,
self.n_samples_outer,
self.dim_outer,
self.f_outer_fb,
) = pb_outer
self.metrics = metrics
key = jax.random.PRNGKey(self.random_state)
if init_var is not None:
# Define random inits per datasets
self.inner_var0, self.outer_var0 = init_var(key)
else:
self.inner_var0 = jax.tree_util.tree_map(
jnp.zeros,
self.dim_inner,
is_leaf=lambda x: isinstance(x, tuple),
) # build a new tree full of zeros with the dim_inner structure
self.outer_var0 = jax.tree_util.tree_map(
lambda x: -2 * jnp.ones(x),
self.dim_outer,
is_leaf=lambda x: isinstance(x, tuple),
) # build a new tree full of zeros with the dim_outer structure
def evaluate_result(self, inner_var, outer_var):
if jax.tree_util.tree_reduce(
operator.or_,
jax.tree_util.tree_map(lambda x: jnp.isnan(x).any(), outer_var),
):
raise ValueError
metrics = self.metrics(inner_var, outer_var)
return metrics
def get_objective(self):
return dict(
f_inner=self.f_inner,
f_outer=self.f_outer,
n_inner_samples=self.n_samples_inner,
n_outer_samples=self.n_samples_outer,
inner_var0=self.inner_var0,
outer_var0=self.outer_var0,
)