brainstate 0.2.1__py2.py3-none-any.whl → 0.2.2__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- brainstate/__init__.py +167 -169
- brainstate/_compatible_import.py +340 -340
- brainstate/_compatible_import_test.py +681 -681
- brainstate/_deprecation.py +210 -210
- brainstate/_deprecation_test.py +2297 -2319
- brainstate/_error.py +45 -45
- brainstate/_state.py +2157 -1652
- brainstate/_state_test.py +1129 -52
- brainstate/_utils.py +47 -47
- brainstate/environ.py +1495 -1495
- brainstate/environ_test.py +1223 -1223
- brainstate/graph/__init__.py +22 -22
- brainstate/graph/_node.py +240 -240
- brainstate/graph/_node_test.py +589 -589
- brainstate/graph/_operation.py +1620 -1624
- brainstate/graph/_operation_test.py +1147 -1147
- brainstate/mixin.py +1447 -1433
- brainstate/mixin_test.py +1017 -1017
- brainstate/nn/__init__.py +146 -137
- brainstate/nn/_activations.py +1100 -1100
- brainstate/nn/_activations_test.py +354 -354
- brainstate/nn/_collective_ops.py +635 -633
- brainstate/nn/_collective_ops_test.py +774 -774
- brainstate/nn/_common.py +226 -226
- brainstate/nn/_common_test.py +134 -154
- brainstate/nn/_conv.py +2010 -2010
- brainstate/nn/_conv_test.py +849 -849
- brainstate/nn/_delay.py +575 -575
- brainstate/nn/_delay_test.py +243 -243
- brainstate/nn/_dropout.py +618 -618
- brainstate/nn/_dropout_test.py +480 -477
- brainstate/nn/_dynamics.py +870 -1267
- brainstate/nn/_dynamics_test.py +53 -67
- brainstate/nn/_elementwise.py +1298 -1298
- brainstate/nn/_elementwise_test.py +829 -829
- brainstate/nn/_embedding.py +408 -408
- brainstate/nn/_embedding_test.py +156 -156
- brainstate/nn/_event_fixedprob.py +233 -233
- brainstate/nn/_event_fixedprob_test.py +115 -115
- brainstate/nn/_event_linear.py +83 -83
- brainstate/nn/_event_linear_test.py +121 -121
- brainstate/nn/_exp_euler.py +254 -254
- brainstate/nn/_exp_euler_test.py +377 -377
- brainstate/nn/_linear.py +744 -744
- brainstate/nn/_linear_test.py +475 -475
- brainstate/nn/_metrics.py +1070 -1070
- brainstate/nn/_metrics_test.py +611 -611
- brainstate/nn/_module.py +391 -384
- brainstate/nn/_module_test.py +40 -40
- brainstate/nn/_normalizations.py +1334 -1334
- brainstate/nn/_normalizations_test.py +699 -699
- brainstate/nn/_paddings.py +1020 -1020
- brainstate/nn/_paddings_test.py +722 -722
- brainstate/nn/_poolings.py +2239 -2239
- brainstate/nn/_poolings_test.py +952 -952
- brainstate/nn/_rnns.py +946 -946
- brainstate/nn/_rnns_test.py +592 -592
- brainstate/nn/_utils.py +216 -216
- brainstate/nn/_utils_test.py +401 -401
- brainstate/nn/init.py +809 -809
- brainstate/nn/init_test.py +180 -180
- brainstate/random/__init__.py +270 -270
- brainstate/random/{_rand_funs.py → _fun.py} +3938 -3938
- brainstate/random/{_rand_funs_test.py → _fun_test.py} +638 -640
- brainstate/random/_impl.py +672 -0
- brainstate/random/{_rand_seed.py → _seed.py} +675 -675
- brainstate/random/{_rand_seed_test.py → _seed_test.py} +48 -48
- brainstate/random/{_rand_state.py → _state.py} +1320 -1617
- brainstate/random/{_rand_state_test.py → _state_test.py} +551 -551
- brainstate/transform/__init__.py +56 -59
- brainstate/transform/_ad_checkpoint.py +176 -176
- brainstate/transform/_ad_checkpoint_test.py +49 -49
- brainstate/transform/_autograd.py +1025 -1025
- brainstate/transform/_autograd_test.py +1289 -1289
- brainstate/transform/_conditions.py +316 -316
- brainstate/transform/_conditions_test.py +220 -220
- brainstate/transform/_error_if.py +94 -94
- brainstate/transform/_error_if_test.py +52 -52
- brainstate/transform/_find_state.py +200 -0
- brainstate/transform/_find_state_test.py +84 -0
- brainstate/transform/_jit.py +399 -399
- brainstate/transform/_jit_test.py +143 -143
- brainstate/transform/_loop_collect_return.py +675 -675
- brainstate/transform/_loop_collect_return_test.py +58 -58
- brainstate/transform/_loop_no_collection.py +283 -283
- brainstate/transform/_loop_no_collection_test.py +50 -50
- brainstate/transform/_make_jaxpr.py +2176 -2016
- brainstate/transform/_make_jaxpr_test.py +1634 -1510
- brainstate/transform/_mapping.py +607 -529
- brainstate/transform/_mapping_test.py +104 -194
- brainstate/transform/_progress_bar.py +255 -255
- brainstate/transform/_unvmap.py +256 -256
- brainstate/transform/_util.py +286 -286
- brainstate/typing.py +837 -837
- brainstate/typing_test.py +780 -780
- brainstate/util/__init__.py +27 -27
- brainstate/util/_others.py +1024 -1024
- brainstate/util/_others_test.py +962 -962
- brainstate/util/_pretty_pytree.py +1301 -1301
- brainstate/util/_pretty_pytree_test.py +675 -675
- brainstate/util/_pretty_repr.py +462 -462
- brainstate/util/_pretty_repr_test.py +696 -696
- brainstate/util/filter.py +945 -945
- brainstate/util/filter_test.py +911 -911
- brainstate/util/struct.py +910 -910
- brainstate/util/struct_test.py +602 -602
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/METADATA +108 -108
- brainstate-0.2.2.dist-info/RECORD +111 -0
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/licenses/LICENSE +202 -202
- brainstate/transform/_eval_shape.py +0 -145
- brainstate/transform/_eval_shape_test.py +0 -38
- brainstate/transform/_random.py +0 -171
- brainstate-0.2.1.dist-info/RECORD +0 -111
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/WHEEL +0 -0
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/top_level.txt +0 -0
@@ -1,115 +1,115 @@
|
|
1
|
-
# Copyright 2024 BrainX Ecosystem Limited. All Rights Reserved.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
# ==============================================================================
|
15
|
-
|
16
|
-
|
17
|
-
import jax.numpy
|
18
|
-
import jax.numpy as jnp
|
19
|
-
import pytest
|
20
|
-
|
21
|
-
import brainstate
|
22
|
-
import braintools
|
23
|
-
|
24
|
-
|
25
|
-
class TestFixedProbCSR:
|
26
|
-
@pytest.mark.parametrize('allow_multi_conn', [True, False, ])
|
27
|
-
def test1(self, allow_multi_conn):
|
28
|
-
x = brainstate.random.rand(20) < 0.1
|
29
|
-
# x = brainstate.random.rand(20)
|
30
|
-
m = brainstate.nn.EventFixedProb(20, 40, 0.1, 1.0, seed=123, allow_multi_conn=allow_multi_conn)
|
31
|
-
y = m(x)
|
32
|
-
print(y)
|
33
|
-
|
34
|
-
m2 = brainstate.nn.EventFixedProb(20, 40, 0.1, braintools.init.KaimingUniform(), seed=123)
|
35
|
-
print(m2(x))
|
36
|
-
|
37
|
-
def test_grad_bool(self):
|
38
|
-
n_in = 20
|
39
|
-
n_out = 30
|
40
|
-
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
41
|
-
fn = brainstate.nn.EventFixedProb(n_in, n_out, 0.1, braintools.init.KaimingUniform(), seed=123)
|
42
|
-
|
43
|
-
def f(x):
|
44
|
-
return fn(x).sum()
|
45
|
-
|
46
|
-
print(jax.grad(f)(x))
|
47
|
-
|
48
|
-
@pytest.mark.parametrize('homo_w', [True, False])
|
49
|
-
def test_vjp(self, homo_w):
|
50
|
-
n_in = 20
|
51
|
-
n_out = 30
|
52
|
-
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
53
|
-
|
54
|
-
if homo_w:
|
55
|
-
fn = brainstate.nn.EventFixedProb(n_in, n_out, 0.1, 1.5, seed=123)
|
56
|
-
else:
|
57
|
-
fn = brainstate.nn.EventFixedProb(n_in, n_out, 0.1, braintools.init.KaimingUniform(), seed=123)
|
58
|
-
w = fn.weight.value
|
59
|
-
|
60
|
-
def f(x, w):
|
61
|
-
fn.weight.value = w
|
62
|
-
return fn(x).sum()
|
63
|
-
|
64
|
-
r = brainstate.augment.grad(f, argnums=(0, 1))(x, w)
|
65
|
-
|
66
|
-
# -------------------
|
67
|
-
# TRUE gradients
|
68
|
-
|
69
|
-
def true_fn(x, w, indices, n_post):
|
70
|
-
post = jnp.zeros((n_post,))
|
71
|
-
for i in range(n_in):
|
72
|
-
post = post.at[indices[i]].add(w * x[i] if homo_w else w[i] * x[i])
|
73
|
-
return post
|
74
|
-
|
75
|
-
def f2(x, w):
|
76
|
-
return true_fn(x, w, fn.conn.indices, n_out).sum()
|
77
|
-
|
78
|
-
r2 = jax.grad(f2, argnums=(0, 1))(x, w)
|
79
|
-
assert (jnp.allclose(r[0], r2[0]))
|
80
|
-
assert (jnp.allclose(r[1], r2[1]))
|
81
|
-
|
82
|
-
@pytest.mark.parametrize('homo_w', [True, False])
|
83
|
-
def test_jvp(self, homo_w):
|
84
|
-
n_in = 20
|
85
|
-
n_out = 30
|
86
|
-
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
87
|
-
|
88
|
-
fn = brainstate.nn.EventFixedProb(
|
89
|
-
n_in, n_out, 0.1, 1.5 if homo_w else braintools.init.KaimingUniform(),
|
90
|
-
seed=123,
|
91
|
-
)
|
92
|
-
w = fn.weight.value
|
93
|
-
|
94
|
-
def f(x, w):
|
95
|
-
fn.weight.value = w
|
96
|
-
return fn(x)
|
97
|
-
|
98
|
-
o1, r1 = jax.jvp(f, (x, w), (jnp.ones_like(x), jnp.ones_like(w)))
|
99
|
-
|
100
|
-
# -------------------
|
101
|
-
# TRUE gradients
|
102
|
-
|
103
|
-
def true_fn(x, w, indices, n_post):
|
104
|
-
post = jnp.zeros((n_post,))
|
105
|
-
for i in range(n_in):
|
106
|
-
post = post.at[indices[i]].add(w * x[i] if homo_w else w[i] * x[i])
|
107
|
-
return post
|
108
|
-
|
109
|
-
def f2(x, w):
|
110
|
-
return true_fn(x, w, fn.conn.indices, n_out)
|
111
|
-
|
112
|
-
o2, r2 = jax.jvp(f2, (x, w), (jnp.ones_like(x), jnp.ones_like(w)))
|
113
|
-
assert (jnp.allclose(o1, o2))
|
114
|
-
# assert jnp.allclose(r1, r2), f'r1={r1}, r2={r2}'
|
115
|
-
assert (jnp.allclose(r1, r2, rtol=1e-4, atol=1e-4))
|
1
|
+
# Copyright 2024 BrainX Ecosystem Limited. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
15
|
+
|
16
|
+
|
17
|
+
import jax.numpy
|
18
|
+
import jax.numpy as jnp
|
19
|
+
import pytest
|
20
|
+
|
21
|
+
import brainstate
|
22
|
+
import braintools
|
23
|
+
|
24
|
+
|
25
|
+
class TestFixedProbCSR:
|
26
|
+
@pytest.mark.parametrize('allow_multi_conn', [True, False, ])
|
27
|
+
def test1(self, allow_multi_conn):
|
28
|
+
x = brainstate.random.rand(20) < 0.1
|
29
|
+
# x = brainstate.random.rand(20)
|
30
|
+
m = brainstate.nn.EventFixedProb(20, 40, 0.1, 1.0, seed=123, allow_multi_conn=allow_multi_conn)
|
31
|
+
y = m(x)
|
32
|
+
print(y)
|
33
|
+
|
34
|
+
m2 = brainstate.nn.EventFixedProb(20, 40, 0.1, braintools.init.KaimingUniform(), seed=123)
|
35
|
+
print(m2(x))
|
36
|
+
|
37
|
+
def test_grad_bool(self):
|
38
|
+
n_in = 20
|
39
|
+
n_out = 30
|
40
|
+
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
41
|
+
fn = brainstate.nn.EventFixedProb(n_in, n_out, 0.1, braintools.init.KaimingUniform(), seed=123)
|
42
|
+
|
43
|
+
def f(x):
|
44
|
+
return fn(x).sum()
|
45
|
+
|
46
|
+
print(jax.grad(f)(x))
|
47
|
+
|
48
|
+
@pytest.mark.parametrize('homo_w', [True, False])
|
49
|
+
def test_vjp(self, homo_w):
|
50
|
+
n_in = 20
|
51
|
+
n_out = 30
|
52
|
+
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
53
|
+
|
54
|
+
if homo_w:
|
55
|
+
fn = brainstate.nn.EventFixedProb(n_in, n_out, 0.1, 1.5, seed=123)
|
56
|
+
else:
|
57
|
+
fn = brainstate.nn.EventFixedProb(n_in, n_out, 0.1, braintools.init.KaimingUniform(), seed=123)
|
58
|
+
w = fn.weight.value
|
59
|
+
|
60
|
+
def f(x, w):
|
61
|
+
fn.weight.value = w
|
62
|
+
return fn(x).sum()
|
63
|
+
|
64
|
+
r = brainstate.augment.grad(f, argnums=(0, 1))(x, w)
|
65
|
+
|
66
|
+
# -------------------
|
67
|
+
# TRUE gradients
|
68
|
+
|
69
|
+
def true_fn(x, w, indices, n_post):
|
70
|
+
post = jnp.zeros((n_post,))
|
71
|
+
for i in range(n_in):
|
72
|
+
post = post.at[indices[i]].add(w * x[i] if homo_w else w[i] * x[i])
|
73
|
+
return post
|
74
|
+
|
75
|
+
def f2(x, w):
|
76
|
+
return true_fn(x, w, fn.conn.indices, n_out).sum()
|
77
|
+
|
78
|
+
r2 = jax.grad(f2, argnums=(0, 1))(x, w)
|
79
|
+
assert (jnp.allclose(r[0], r2[0]))
|
80
|
+
assert (jnp.allclose(r[1], r2[1]))
|
81
|
+
|
82
|
+
@pytest.mark.parametrize('homo_w', [True, False])
|
83
|
+
def test_jvp(self, homo_w):
|
84
|
+
n_in = 20
|
85
|
+
n_out = 30
|
86
|
+
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
87
|
+
|
88
|
+
fn = brainstate.nn.EventFixedProb(
|
89
|
+
n_in, n_out, 0.1, 1.5 if homo_w else braintools.init.KaimingUniform(),
|
90
|
+
seed=123,
|
91
|
+
)
|
92
|
+
w = fn.weight.value
|
93
|
+
|
94
|
+
def f(x, w):
|
95
|
+
fn.weight.value = w
|
96
|
+
return fn(x)
|
97
|
+
|
98
|
+
o1, r1 = jax.jvp(f, (x, w), (jnp.ones_like(x), jnp.ones_like(w)))
|
99
|
+
|
100
|
+
# -------------------
|
101
|
+
# TRUE gradients
|
102
|
+
|
103
|
+
def true_fn(x, w, indices, n_post):
|
104
|
+
post = jnp.zeros((n_post,))
|
105
|
+
for i in range(n_in):
|
106
|
+
post = post.at[indices[i]].add(w * x[i] if homo_w else w[i] * x[i])
|
107
|
+
return post
|
108
|
+
|
109
|
+
def f2(x, w):
|
110
|
+
return true_fn(x, w, fn.conn.indices, n_out)
|
111
|
+
|
112
|
+
o2, r2 = jax.jvp(f2, (x, w), (jnp.ones_like(x), jnp.ones_like(w)))
|
113
|
+
assert (jnp.allclose(o1, o2))
|
114
|
+
# assert jnp.allclose(r1, r2), f'r1={r1}, r2={r2}'
|
115
|
+
assert (jnp.allclose(r1, r2, rtol=1e-4, atol=1e-4))
|
brainstate/nn/_event_linear.py
CHANGED
@@ -1,83 +1,83 @@
|
|
1
|
-
# Copyright 2024 BrainX Ecosystem Limited. All Rights Reserved.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
# ==============================================================================
|
15
|
-
|
16
|
-
from typing import Union, Callable, Optional
|
17
|
-
|
18
|
-
import brainevent
|
19
|
-
import brainunit as u
|
20
|
-
import jax
|
21
|
-
|
22
|
-
from brainstate._state import ParamState
|
23
|
-
from brainstate.typing import Size, ArrayLike
|
24
|
-
from . import init as init
|
25
|
-
from ._module import Module
|
26
|
-
|
27
|
-
__all__ = [
|
28
|
-
'EventLinear',
|
29
|
-
]
|
30
|
-
|
31
|
-
|
32
|
-
class EventLinear(Module):
|
33
|
-
"""
|
34
|
-
|
35
|
-
Parameters
|
36
|
-
----------
|
37
|
-
in_size : Size
|
38
|
-
Number of pre-synaptic neurons, i.e., input size.
|
39
|
-
out_size : Size
|
40
|
-
Number of post-synaptic neurons, i.e., output size.
|
41
|
-
weight : float or callable or jax.Array or brainunit.Quantity
|
42
|
-
Maximum synaptic conductance.
|
43
|
-
block_size : int, optional
|
44
|
-
Block size for parallel computation.
|
45
|
-
float_as_event : bool, optional
|
46
|
-
Whether to treat float as event.
|
47
|
-
name : str, optional
|
48
|
-
Name of the module.
|
49
|
-
"""
|
50
|
-
|
51
|
-
__module__ = 'brainstate.nn'
|
52
|
-
|
53
|
-
def __init__(
|
54
|
-
self,
|
55
|
-
in_size: Size,
|
56
|
-
out_size: Size,
|
57
|
-
weight: Union[Callable, ArrayLike],
|
58
|
-
float_as_event: bool = True,
|
59
|
-
block_size: int = 64,
|
60
|
-
name: Optional[str] = None,
|
61
|
-
param_type: type = ParamState,
|
62
|
-
):
|
63
|
-
super().__init__(name=name)
|
64
|
-
|
65
|
-
# network parameters
|
66
|
-
self.in_size = in_size
|
67
|
-
self.out_size = out_size
|
68
|
-
self.float_as_event = float_as_event
|
69
|
-
self.block_size = block_size
|
70
|
-
|
71
|
-
# maximum synaptic conductance
|
72
|
-
weight = init.param(weight, (self.in_size[-1], self.out_size[-1]), allow_none=False)
|
73
|
-
self.weight = param_type(weight)
|
74
|
-
|
75
|
-
def update(self, spk: jax.Array) -> Union[jax.Array, u.Quantity]:
|
76
|
-
weight = self.weight.value
|
77
|
-
if u.math.size(weight) == 1:
|
78
|
-
return u.math.ones(self.out_size) * (u.math.sum(spk) * weight)
|
79
|
-
|
80
|
-
if self.float_as_event:
|
81
|
-
return brainevent.EventArray(spk) @ weight
|
82
|
-
else:
|
83
|
-
return spk @ weight
|
1
|
+
# Copyright 2024 BrainX Ecosystem Limited. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
15
|
+
|
16
|
+
from typing import Union, Callable, Optional
|
17
|
+
|
18
|
+
import brainevent
|
19
|
+
import brainunit as u
|
20
|
+
import jax
|
21
|
+
|
22
|
+
from brainstate._state import ParamState
|
23
|
+
from brainstate.typing import Size, ArrayLike
|
24
|
+
from . import init as init
|
25
|
+
from ._module import Module
|
26
|
+
|
27
|
+
__all__ = [
|
28
|
+
'EventLinear',
|
29
|
+
]
|
30
|
+
|
31
|
+
|
32
|
+
class EventLinear(Module):
|
33
|
+
"""
|
34
|
+
|
35
|
+
Parameters
|
36
|
+
----------
|
37
|
+
in_size : Size
|
38
|
+
Number of pre-synaptic neurons, i.e., input size.
|
39
|
+
out_size : Size
|
40
|
+
Number of post-synaptic neurons, i.e., output size.
|
41
|
+
weight : float or callable or jax.Array or brainunit.Quantity
|
42
|
+
Maximum synaptic conductance.
|
43
|
+
block_size : int, optional
|
44
|
+
Block size for parallel computation.
|
45
|
+
float_as_event : bool, optional
|
46
|
+
Whether to treat float as event.
|
47
|
+
name : str, optional
|
48
|
+
Name of the module.
|
49
|
+
"""
|
50
|
+
|
51
|
+
__module__ = 'brainstate.nn'
|
52
|
+
|
53
|
+
def __init__(
|
54
|
+
self,
|
55
|
+
in_size: Size,
|
56
|
+
out_size: Size,
|
57
|
+
weight: Union[Callable, ArrayLike],
|
58
|
+
float_as_event: bool = True,
|
59
|
+
block_size: int = 64,
|
60
|
+
name: Optional[str] = None,
|
61
|
+
param_type: type = ParamState,
|
62
|
+
):
|
63
|
+
super().__init__(name=name)
|
64
|
+
|
65
|
+
# network parameters
|
66
|
+
self.in_size = in_size
|
67
|
+
self.out_size = out_size
|
68
|
+
self.float_as_event = float_as_event
|
69
|
+
self.block_size = block_size
|
70
|
+
|
71
|
+
# maximum synaptic conductance
|
72
|
+
weight = init.param(weight, (self.in_size[-1], self.out_size[-1]), allow_none=False)
|
73
|
+
self.weight = param_type(weight)
|
74
|
+
|
75
|
+
def update(self, spk: jax.Array) -> Union[jax.Array, u.Quantity]:
|
76
|
+
weight = self.weight.value
|
77
|
+
if u.math.size(weight) == 1:
|
78
|
+
return u.math.ones(self.out_size) * (u.math.sum(spk) * weight)
|
79
|
+
|
80
|
+
if self.float_as_event:
|
81
|
+
return brainevent.EventArray(spk) @ weight
|
82
|
+
else:
|
83
|
+
return spk @ weight
|
@@ -1,121 +1,121 @@
|
|
1
|
-
# Copyright 2024 BrainX Ecosystem Limited. All Rights Reserved.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
# ==============================================================================
|
15
|
-
|
16
|
-
|
17
|
-
import jax
|
18
|
-
import jax.numpy as jnp
|
19
|
-
import pytest
|
20
|
-
|
21
|
-
import braintools
|
22
|
-
import brainstate
|
23
|
-
|
24
|
-
|
25
|
-
class TestEventLinear:
|
26
|
-
@pytest.mark.parametrize('bool_x', [True, False])
|
27
|
-
@pytest.mark.parametrize('homo_w', [True, False])
|
28
|
-
def test1(self, homo_w, bool_x):
|
29
|
-
x = brainstate.random.rand(20) < 0.1
|
30
|
-
if not bool_x:
|
31
|
-
x = jnp.asarray(x, dtype=float)
|
32
|
-
m = brainstate.nn.EventLinear(
|
33
|
-
20, 40,
|
34
|
-
1.5 if homo_w else braintools.init.KaimingUniform(),
|
35
|
-
float_as_event=bool_x
|
36
|
-
)
|
37
|
-
y = m(x)
|
38
|
-
print(y)
|
39
|
-
|
40
|
-
assert (jnp.allclose(y, (x.sum() * m.weight.value) if homo_w else (x @ m.weight.value)))
|
41
|
-
|
42
|
-
def test_grad_bool(self):
|
43
|
-
n_in = 20
|
44
|
-
n_out = 30
|
45
|
-
x = brainstate.random.rand(n_in) < 0.3
|
46
|
-
fn = brainstate.nn.EventLinear(n_in, n_out, braintools.init.KaimingUniform())
|
47
|
-
|
48
|
-
with pytest.raises(TypeError):
|
49
|
-
print(jax.grad(lambda x: fn(x).sum())(x))
|
50
|
-
|
51
|
-
@pytest.mark.parametrize('bool_x', [True, False])
|
52
|
-
@pytest.mark.parametrize('homo_w', [True, False])
|
53
|
-
def test_vjp(self, bool_x, homo_w):
|
54
|
-
n_in = 20
|
55
|
-
n_out = 30
|
56
|
-
if bool_x:
|
57
|
-
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
58
|
-
else:
|
59
|
-
x = brainstate.random.rand(n_in)
|
60
|
-
|
61
|
-
fn = brainstate.nn.EventLinear(
|
62
|
-
n_in,
|
63
|
-
n_out,
|
64
|
-
1.5 if homo_w else braintools.init.KaimingUniform(),
|
65
|
-
float_as_event=bool_x
|
66
|
-
)
|
67
|
-
w = fn.weight.value
|
68
|
-
|
69
|
-
def f(x, w):
|
70
|
-
fn.weight.value = w
|
71
|
-
return fn(x).sum()
|
72
|
-
|
73
|
-
r1 = jax.grad(f, argnums=(0, 1))(x, w)
|
74
|
-
|
75
|
-
# -------------------
|
76
|
-
# TRUE gradients
|
77
|
-
|
78
|
-
def f2(x, w):
|
79
|
-
y = (x @ (jnp.ones([n_in, n_out]) * w)) if homo_w else (x @ w)
|
80
|
-
return y.sum()
|
81
|
-
|
82
|
-
r2 = jax.grad(f2, argnums=(0, 1))(x, w)
|
83
|
-
assert (jnp.allclose(r1[0], r2[0]))
|
84
|
-
|
85
|
-
if not jnp.allclose(r1[1], r2[1]):
|
86
|
-
print(r1[1] - r2[1])
|
87
|
-
|
88
|
-
assert (jnp.allclose(r1[1], r2[1]))
|
89
|
-
|
90
|
-
@pytest.mark.parametrize('bool_x', [True, False])
|
91
|
-
@pytest.mark.parametrize('homo_w', [True, False])
|
92
|
-
def test_jvp(self, bool_x, homo_w):
|
93
|
-
n_in = 20
|
94
|
-
n_out = 30
|
95
|
-
if bool_x:
|
96
|
-
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
97
|
-
else:
|
98
|
-
x = brainstate.random.rand(n_in)
|
99
|
-
|
100
|
-
fn = brainstate.nn.EventLinear(
|
101
|
-
n_in, n_out, 1.5 if homo_w else braintools.init.KaimingUniform(),
|
102
|
-
float_as_event=bool_x
|
103
|
-
)
|
104
|
-
w = fn.weight.value
|
105
|
-
|
106
|
-
def f(x, w):
|
107
|
-
fn.weight.value = w
|
108
|
-
return fn(x)
|
109
|
-
|
110
|
-
o1, r1 = jax.jvp(f, (x, w), (jnp.ones_like(x), jnp.ones_like(w)))
|
111
|
-
|
112
|
-
# -------------------
|
113
|
-
# TRUE gradients
|
114
|
-
|
115
|
-
def f2(x, w):
|
116
|
-
y = (x @ (jnp.ones([n_in, n_out]) * w)) if homo_w else (x @ w)
|
117
|
-
return y
|
118
|
-
|
119
|
-
o2, r2 = jax.jvp(f, (x, w), (jnp.ones_like(x), jnp.ones_like(w)))
|
120
|
-
assert (jnp.allclose(o1, o2))
|
121
|
-
assert (jnp.allclose(r1, r2))
|
1
|
+
# Copyright 2024 BrainX Ecosystem Limited. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
15
|
+
|
16
|
+
|
17
|
+
import jax
|
18
|
+
import jax.numpy as jnp
|
19
|
+
import pytest
|
20
|
+
|
21
|
+
import braintools
|
22
|
+
import brainstate
|
23
|
+
|
24
|
+
|
25
|
+
class TestEventLinear:
|
26
|
+
@pytest.mark.parametrize('bool_x', [True, False])
|
27
|
+
@pytest.mark.parametrize('homo_w', [True, False])
|
28
|
+
def test1(self, homo_w, bool_x):
|
29
|
+
x = brainstate.random.rand(20) < 0.1
|
30
|
+
if not bool_x:
|
31
|
+
x = jnp.asarray(x, dtype=float)
|
32
|
+
m = brainstate.nn.EventLinear(
|
33
|
+
20, 40,
|
34
|
+
1.5 if homo_w else braintools.init.KaimingUniform(),
|
35
|
+
float_as_event=bool_x
|
36
|
+
)
|
37
|
+
y = m(x)
|
38
|
+
print(y)
|
39
|
+
|
40
|
+
assert (jnp.allclose(y, (x.sum() * m.weight.value) if homo_w else (x @ m.weight.value)))
|
41
|
+
|
42
|
+
def test_grad_bool(self):
|
43
|
+
n_in = 20
|
44
|
+
n_out = 30
|
45
|
+
x = brainstate.random.rand(n_in) < 0.3
|
46
|
+
fn = brainstate.nn.EventLinear(n_in, n_out, braintools.init.KaimingUniform())
|
47
|
+
|
48
|
+
with pytest.raises(TypeError):
|
49
|
+
print(jax.grad(lambda x: fn(x).sum())(x))
|
50
|
+
|
51
|
+
@pytest.mark.parametrize('bool_x', [True, False])
|
52
|
+
@pytest.mark.parametrize('homo_w', [True, False])
|
53
|
+
def test_vjp(self, bool_x, homo_w):
|
54
|
+
n_in = 20
|
55
|
+
n_out = 30
|
56
|
+
if bool_x:
|
57
|
+
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
58
|
+
else:
|
59
|
+
x = brainstate.random.rand(n_in)
|
60
|
+
|
61
|
+
fn = brainstate.nn.EventLinear(
|
62
|
+
n_in,
|
63
|
+
n_out,
|
64
|
+
1.5 if homo_w else braintools.init.KaimingUniform(),
|
65
|
+
float_as_event=bool_x
|
66
|
+
)
|
67
|
+
w = fn.weight.value
|
68
|
+
|
69
|
+
def f(x, w):
|
70
|
+
fn.weight.value = w
|
71
|
+
return fn(x).sum()
|
72
|
+
|
73
|
+
r1 = jax.grad(f, argnums=(0, 1))(x, w)
|
74
|
+
|
75
|
+
# -------------------
|
76
|
+
# TRUE gradients
|
77
|
+
|
78
|
+
def f2(x, w):
|
79
|
+
y = (x @ (jnp.ones([n_in, n_out]) * w)) if homo_w else (x @ w)
|
80
|
+
return y.sum()
|
81
|
+
|
82
|
+
r2 = jax.grad(f2, argnums=(0, 1))(x, w)
|
83
|
+
assert (jnp.allclose(r1[0], r2[0]))
|
84
|
+
|
85
|
+
if not jnp.allclose(r1[1], r2[1]):
|
86
|
+
print(r1[1] - r2[1])
|
87
|
+
|
88
|
+
assert (jnp.allclose(r1[1], r2[1]))
|
89
|
+
|
90
|
+
@pytest.mark.parametrize('bool_x', [True, False])
|
91
|
+
@pytest.mark.parametrize('homo_w', [True, False])
|
92
|
+
def test_jvp(self, bool_x, homo_w):
|
93
|
+
n_in = 20
|
94
|
+
n_out = 30
|
95
|
+
if bool_x:
|
96
|
+
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
97
|
+
else:
|
98
|
+
x = brainstate.random.rand(n_in)
|
99
|
+
|
100
|
+
fn = brainstate.nn.EventLinear(
|
101
|
+
n_in, n_out, 1.5 if homo_w else braintools.init.KaimingUniform(),
|
102
|
+
float_as_event=bool_x
|
103
|
+
)
|
104
|
+
w = fn.weight.value
|
105
|
+
|
106
|
+
def f(x, w):
|
107
|
+
fn.weight.value = w
|
108
|
+
return fn(x)
|
109
|
+
|
110
|
+
o1, r1 = jax.jvp(f, (x, w), (jnp.ones_like(x), jnp.ones_like(w)))
|
111
|
+
|
112
|
+
# -------------------
|
113
|
+
# TRUE gradients
|
114
|
+
|
115
|
+
def f2(x, w):
|
116
|
+
y = (x @ (jnp.ones([n_in, n_out]) * w)) if homo_w else (x @ w)
|
117
|
+
return y
|
118
|
+
|
119
|
+
o2, r2 = jax.jvp(f, (x, w), (jnp.ones_like(x), jnp.ones_like(w)))
|
120
|
+
assert (jnp.allclose(o1, o2))
|
121
|
+
assert (jnp.allclose(r1, r2))
|