forked from google/jaxopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplicit_diff_test.py
114 lines (97 loc) · 4.51 KB
/
implicit_diff_test.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
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from absl.testing import absltest
from absl.testing import parameterized
import jax
from jax import test_util as jtu
import jax.numpy as jnp
from jaxopt import implicit_diff as idf
from jaxopt._src import test_util
from sklearn import datasets
def ridge_objective(params, lam, X, y):
residuals = jnp.dot(X, params) - y
return 0.5 * jnp.mean(residuals ** 2) + 0.5 * lam * jnp.sum(params ** 2)
# def ridge_solver(init_params, lam, X, y):
def ridge_solver(init_params, lam, X, y):
del init_params # not used
XX = jnp.dot(X.T, X)
Xy = jnp.dot(X.T, y)
I = jnp.eye(X.shape[1])
return jnp.linalg.solve(XX + lam * len(y) * I, Xy)
class ImplicitDiffTest(jtu.JaxTestCase):
def test_root_vjp(self):
X, y = datasets.make_regression(n_samples=10, n_features=3, random_state=0)
optimality_fun = jax.grad(ridge_objective)
lam = 5.0
sol = ridge_solver(None, lam, X, y)
vjp = lambda g: idf.root_vjp(optimality_fun=optimality_fun,
sol=sol,
args=(lam, X, y),
cotangent=g)[0] # vjp w.r.t. lam
I = jnp.eye(len(sol))
J = jax.vmap(vjp)(I)
J_num = test_util.ridge_solver_jac(X, y, lam, eps=1e-4)
self.assertArraysAllClose(J, J_num, atol=5e-2)
def test_root_jvp(self):
X, y = datasets.make_regression(n_samples=10, n_features=3, random_state=0)
optimality_fun = jax.grad(ridge_objective)
lam = 5.0
sol = ridge_solver(None, lam, X, y)
J = idf.root_jvp(optimality_fun=optimality_fun,
sol=sol,
args=(lam, X, y),
tangents=(1.0, jnp.zeros_like(X), jnp.zeros_like(y)))
J_num = test_util.ridge_solver_jac(X, y, lam, eps=1e-4)
self.assertArraysAllClose(J, J_num, atol=5e-2)
def test_custom_root(self):
X, y = datasets.make_regression(n_samples=10, n_features=3, random_state=0)
optimality_fun = jax.grad(ridge_objective)
lam = 5.0
ridge_solver_decorated = idf.custom_root(optimality_fun)(ridge_solver)
sol = ridge_solver(None, lam=lam, X=X, y=y)
sol_decorated = ridge_solver_decorated(None, lam=lam, X=X, y=y)
self.assertArraysAllClose(sol, sol_decorated, atol=1e-4)
J_num = test_util.ridge_solver_jac(X, y, lam, eps=1e-4)
J = jax.jacrev(ridge_solver_decorated, argnums=1)(None, lam, X=X, y=y)
self.assertArraysAllClose(J, J_num, atol=5e-2)
def test_custom_root_with_has_aux(self):
def ridge_solver_with_aux(init_params, lam, X, y):
return ridge_solver(init_params, lam, X, y), None
X, y = datasets.make_regression(n_samples=10, n_features=3, random_state=0)
optimality_fun = jax.grad(ridge_objective)
lam = 5.0
decorator = idf.custom_root(optimality_fun, has_aux=True)
ridge_solver_decorated = decorator(ridge_solver_with_aux)
sol = ridge_solver(None, lam=lam, X=X, y=y)
sol_decorated = ridge_solver_decorated(None, lam=lam, X=X, y=y)[0]
self.assertArraysAllClose(sol, sol_decorated, atol=1e-4)
J_num = test_util.ridge_solver_jac(X, y, lam, eps=1e-4)
J, _ = jax.jacrev(ridge_solver_decorated, argnums=1)(None, lam, X=X, y=y)
self.assertArraysAllClose(J, J_num, atol=5e-2)
def test_custom_fixed_point(self):
X, y = datasets.make_regression(n_samples=10, n_features=3, random_state=0)
grad_fun = jax.grad(ridge_objective)
fp_fun = lambda x, *args: x - grad_fun(x, *args)
lam = 5.0
ridge_solver_decorated = idf.custom_fixed_point(fp_fun)(ridge_solver)
sol = ridge_solver(None, lam=lam, X=X, y=y)
sol_decorated = ridge_solver_decorated(None, lam=lam, X=X, y=y)
self.assertArraysAllClose(sol, sol_decorated, atol=1e-4)
J_num = test_util.ridge_solver_jac(X, y, lam, eps=1e-4)
J = jax.jacrev(ridge_solver_decorated, argnums=1)(None, lam, X=X, y=y)
self.assertArraysAllClose(J, J_num, atol=5e-2)
if __name__ == '__main__':
# Uncomment the line below in order to run in float64.
# jax.config.update("jax_enable_x64", True)
absltest.main(testLoader=jtu.JaxTestLoader())