-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_transforms.py
180 lines (143 loc) · 5.54 KB
/
test_transforms.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
# This file is part of Jaxley, a differentiable neuroscience simulator. Jaxley is
# licensed under the Apache License Version 2.0, see <https://www.apache.org/licenses/>
import jax
jax.config.update("jax_enable_x64", True)
jax.config.update("jax_platform_name", "cpu")
import jax.numpy as jnp
import numpy as np
import pytest
from jax import jit
import jaxley as jx
import jaxley.optimize.transforms as jt
from jaxley.optimize.transforms import ParamTransform
def test_joint_inverse():
# test forward(inverse(x))=x
tf_dict = [
{"param_array_1": jt.SigmoidTransform(-2, 2)},
{"param_array_2": jt.SoftplusTransform(2)},
{"param_array_3": jt.NegSoftplusTransform(-2)},
]
params = [
{"param_array_1": jnp.asarray(np.linspace(-1, 1, 4))},
{"param_array_2": jnp.asarray(np.linspace(-4, 1, 4))},
{"param_array_3": jnp.asarray(np.linspace(-1, 4, 4))},
]
tf = ParamTransform(tf_dict)
forward = tf.forward(params)
inverse = tf.inverse(forward)
assert np.allclose(
inverse[0]["param_array_1"], params[0]["param_array_1"]
), "SigmoidTransform forward, inverse failed."
assert np.allclose(
inverse[1]["param_array_2"], params[1]["param_array_2"]
), "SoftplusTransform forward, inverse failed."
assert np.allclose(
inverse[2]["param_array_3"], params[2]["param_array_3"]
), "NegSoftplusTransform forward, inverse failed."
def test_bounds():
# test if forward maps parameters into bounds
lowers = {"param_array_1": -2, "param_array_2": None, "param_array_3": -2}
uppers = {"param_array_1": 2, "param_array_2": 2, "param_array_3": None}
tf_dict = [
{"param_array_1": jt.SigmoidTransform(-2, 2)},
{"param_array_2": jt.NegSoftplusTransform(2)},
{"param_array_3": jt.SoftplusTransform(-2)},
]
params = [
{"param_array_1": jnp.asarray(np.linspace(-10, 10, 4))},
{"param_array_2": jnp.asarray(np.linspace(-10, 10, 4))},
{"param_array_3": jnp.asarray(np.linspace(-10, 10, 4))},
]
tf = ParamTransform(tf_dict)
forward = tf.forward(params)
assert all(
forward[0]["param_array_1"] > lowers["param_array_1"]
), "SigmoidTransform failed to match lower bound."
assert all(
forward[0]["param_array_1"] < uppers["param_array_1"]
), "SigmoidTransform failed to match upper bound."
assert all(
forward[1]["param_array_2"] < uppers["param_array_2"]
), "SoftplusTransform failed to match lower bound."
assert all(
forward[2]["param_array_3"] > lowers["param_array_3"]
), "NegSoftplusTransform failed to match lower bound."
@pytest.mark.parametrize(
"transform",
[
jt.SigmoidTransform(-2, 2),
jt.SoftplusTransform(2),
jt.NegSoftplusTransform(2),
jt.AffineTransform(1.0, 1.0),
jt.CustomTransform(lambda x: x, lambda x: x),
jt.ChainTransform([jt.SigmoidTransform(-2, 2), jt.SoftplusTransform(2)]),
],
)
def test_jit(transform):
# test jit-compilation:
tf_dict = [{"param_array_1": transform}]
params = [{"param_array_1": jnp.asarray(np.linspace(-1, 1, 4))}]
tf = ParamTransform(tf_dict)
@jit
def test_jit(params):
return tf.inverse(params)
_ = test_jit(params)
@pytest.mark.parametrize(
"transform",
[
jt.SigmoidTransform(-2, 2),
jt.SoftplusTransform(2),
jt.NegSoftplusTransform(2),
jt.AffineTransform(1.0, 1.0),
jt.CustomTransform(lambda x: x, lambda x: x),
jt.ChainTransform([jt.SigmoidTransform(-2, 2), jt.SoftplusTransform(2)]),
jt.MaskedTransform(
jnp.array([True, False, False, True]), jt.SigmoidTransform(-2, 2)
),
],
)
def test_correct(transform):
# Test correctness on "standard" PyTree
tf_dict = [{"param_array_1": transform}]
params = [{"param_array_1": jnp.asarray(np.linspace(-1, 1, 4))}]
tf = ParamTransform(tf_dict)
forward = tf.forward(params)
inverse = tf.inverse(forward)
assert np.allclose(
inverse[0]["param_array_1"], params[0]["param_array_1"]
), f"{transform} forward, inverse failed."
# Test correctness plain Array
for shape in [(4,), (4, 1), (4, 4)]:
tf_dict = transform
tf = ParamTransform(tf_dict)
x = jnp.ones(shape)
y = tf.forward(x)
x_inv = tf.inverse(y)
assert np.allclose(
x, x_inv
), f"{transform} forward, inverse failed on non PyTree."
@pytest.mark.parametrize(
"transform",
[jt.SigmoidTransform(-2, 2), jt.SoftplusTransform(2), jt.NegSoftplusTransform(2)],
)
def test_user_api(transform, SimpleCell):
cell = SimpleCell(3, 2)
cell.branch("all").make_trainable("radius")
cell.branch(2).make_trainable("radius")
cell.branch(1).make_trainable("length")
cell.branch(0).make_trainable("radius")
cell.branch("all").comp("all").make_trainable("axial_resistivity")
cell.make_trainable("capacitance")
params = cell.get_parameters()
# We scale it to something samll as axial_resistivity is large
# and then the transform becomes numerically uninvertible.
t = jt.ChainTransform([jt.AffineTransform(1e-3, 0.0), transform])
tf_dict = [{list(k.keys())[0]: t} for k in params]
tf = ParamTransform(tf_dict)
forward = tf.forward(params)
reverse = tf.inverse(forward)
flat_params, _ = jax.tree_util.tree_flatten(params)
flat_reverse, _ = jax.tree_util.tree_flatten(reverse)
assert all(
[np.allclose(a, b) for a, b in zip(flat_params, flat_reverse)]
), f"{transform} forward, inverse failed."