brainstate 0.1.7__py2.py3-none-any.whl → 0.1.9__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 +58 -51
- brainstate/_compatible_import.py +148 -148
- brainstate/_state.py +1605 -1663
- brainstate/_state_test.py +52 -52
- brainstate/_utils.py +47 -47
- brainstate/augment/__init__.py +30 -30
- brainstate/augment/_autograd.py +778 -778
- brainstate/augment/_autograd_test.py +1289 -1289
- brainstate/augment/_eval_shape.py +99 -99
- brainstate/augment/_eval_shape_test.py +38 -38
- brainstate/augment/_mapping.py +1060 -1060
- brainstate/augment/_mapping_test.py +597 -597
- brainstate/augment/_random.py +151 -151
- brainstate/compile/__init__.py +38 -38
- brainstate/compile/_ad_checkpoint.py +204 -204
- brainstate/compile/_ad_checkpoint_test.py +49 -49
- brainstate/compile/_conditions.py +256 -256
- brainstate/compile/_conditions_test.py +220 -220
- brainstate/compile/_error_if.py +92 -92
- brainstate/compile/_error_if_test.py +52 -52
- brainstate/compile/_jit.py +346 -346
- brainstate/compile/_jit_test.py +143 -143
- brainstate/compile/_loop_collect_return.py +536 -536
- brainstate/compile/_loop_collect_return_test.py +58 -58
- brainstate/compile/_loop_no_collection.py +184 -184
- brainstate/compile/_loop_no_collection_test.py +50 -50
- brainstate/compile/_make_jaxpr.py +888 -888
- brainstate/compile/_make_jaxpr_test.py +156 -146
- brainstate/compile/_progress_bar.py +202 -202
- brainstate/compile/_unvmap.py +159 -159
- brainstate/compile/_util.py +147 -147
- brainstate/environ.py +563 -563
- brainstate/environ_test.py +62 -62
- brainstate/functional/__init__.py +27 -26
- brainstate/graph/__init__.py +29 -29
- brainstate/graph/_graph_node.py +244 -244
- brainstate/graph/_graph_node_test.py +73 -73
- brainstate/graph/_graph_operation.py +1738 -1738
- brainstate/graph/_graph_operation_test.py +563 -563
- brainstate/init/__init__.py +26 -26
- brainstate/init/_base.py +52 -52
- brainstate/init/_generic.py +244 -244
- brainstate/init/_random_inits.py +553 -553
- brainstate/init/_random_inits_test.py +149 -149
- brainstate/init/_regular_inits.py +105 -105
- brainstate/init/_regular_inits_test.py +50 -50
- brainstate/mixin.py +365 -363
- brainstate/mixin_test.py +77 -73
- brainstate/nn/__init__.py +135 -131
- brainstate/{functional → nn}/_activations.py +808 -813
- brainstate/{functional → nn}/_activations_test.py +331 -331
- brainstate/nn/_collective_ops.py +514 -514
- brainstate/nn/_collective_ops_test.py +43 -43
- brainstate/nn/_common.py +178 -178
- brainstate/nn/_conv.py +501 -501
- brainstate/nn/_conv_test.py +238 -238
- brainstate/nn/_delay.py +509 -470
- brainstate/nn/_delay_test.py +238 -0
- brainstate/nn/_dropout.py +426 -426
- brainstate/nn/_dropout_test.py +100 -100
- brainstate/nn/_dynamics.py +1343 -1361
- brainstate/nn/_dynamics_test.py +78 -78
- brainstate/nn/_elementwise.py +1119 -1120
- brainstate/nn/_elementwise_test.py +169 -169
- brainstate/nn/_embedding.py +58 -58
- brainstate/nn/_exp_euler.py +92 -92
- brainstate/nn/_exp_euler_test.py +35 -35
- brainstate/nn/_fixedprob.py +239 -239
- brainstate/nn/_fixedprob_test.py +114 -114
- brainstate/nn/_inputs.py +608 -608
- brainstate/nn/_linear.py +424 -424
- brainstate/nn/_linear_mv.py +83 -83
- brainstate/nn/_linear_mv_test.py +120 -120
- brainstate/nn/_linear_test.py +107 -107
- brainstate/nn/_ltp.py +28 -28
- brainstate/nn/_module.py +377 -377
- brainstate/nn/_module_test.py +40 -208
- brainstate/nn/_neuron.py +705 -705
- brainstate/nn/_neuron_test.py +161 -161
- brainstate/nn/_normalizations.py +975 -918
- brainstate/nn/_normalizations_test.py +73 -73
- brainstate/{functional → nn}/_others.py +46 -46
- brainstate/nn/_poolings.py +1177 -1177
- brainstate/nn/_poolings_test.py +217 -217
- brainstate/nn/_projection.py +486 -486
- brainstate/nn/_rate_rnns.py +554 -554
- brainstate/nn/_rate_rnns_test.py +63 -63
- brainstate/nn/_readout.py +209 -209
- brainstate/nn/_readout_test.py +53 -53
- brainstate/nn/_stp.py +236 -236
- brainstate/nn/_synapse.py +505 -505
- brainstate/nn/_synapse_test.py +131 -131
- brainstate/nn/_synaptic_projection.py +423 -423
- brainstate/nn/_synouts.py +162 -162
- brainstate/nn/_synouts_test.py +57 -57
- brainstate/nn/_utils.py +89 -89
- brainstate/nn/metrics.py +388 -388
- brainstate/optim/__init__.py +38 -38
- brainstate/optim/_base.py +64 -64
- brainstate/optim/_lr_scheduler.py +448 -448
- brainstate/optim/_lr_scheduler_test.py +50 -50
- brainstate/optim/_optax_optimizer.py +152 -152
- brainstate/optim/_optax_optimizer_test.py +53 -53
- brainstate/optim/_sgd_optimizer.py +1104 -1104
- brainstate/random/__init__.py +24 -24
- brainstate/random/_rand_funs.py +3616 -3616
- brainstate/random/_rand_funs_test.py +567 -567
- brainstate/random/_rand_seed.py +210 -210
- brainstate/random/_rand_seed_test.py +48 -48
- brainstate/random/_rand_state.py +1409 -1409
- brainstate/random/_random_for_unit.py +52 -52
- brainstate/surrogate.py +1957 -1957
- brainstate/transform.py +23 -23
- brainstate/typing.py +304 -304
- brainstate/util/__init__.py +50 -50
- brainstate/util/caller.py +98 -98
- brainstate/util/error.py +55 -55
- brainstate/util/filter.py +469 -469
- brainstate/util/others.py +540 -540
- brainstate/util/pretty_pytree.py +945 -945
- brainstate/util/pretty_pytree_test.py +159 -159
- brainstate/util/pretty_repr.py +328 -328
- brainstate/util/pretty_table.py +2954 -2954
- brainstate/util/scaling.py +258 -258
- brainstate/util/struct.py +523 -523
- {brainstate-0.1.7.dist-info → brainstate-0.1.9.dist-info}/METADATA +91 -99
- brainstate-0.1.9.dist-info/RECORD +130 -0
- {brainstate-0.1.7.dist-info → brainstate-0.1.9.dist-info}/WHEEL +1 -1
- {brainstate-0.1.7.dist-info → brainstate-0.1.9.dist-info/licenses}/LICENSE +202 -202
- brainstate/functional/_normalization.py +0 -81
- brainstate/functional/_spikes.py +0 -204
- brainstate-0.1.7.dist-info/RECORD +0 -131
- {brainstate-0.1.7.dist-info → brainstate-0.1.9.dist-info}/top_level.txt +0 -0
brainstate/nn/_linear_mv.py
CHANGED
@@ -1,83 +1,83 @@
|
|
1
|
-
# Copyright 2024 BDP 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
|
19
|
-
import
|
20
|
-
|
21
|
-
|
22
|
-
import
|
23
|
-
from brainstate._state import ParamState
|
24
|
-
from brainstate.typing import Size, ArrayLike
|
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 BDP 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 import init
|
23
|
+
from brainstate._state import ParamState
|
24
|
+
from brainstate.typing import Size, ArrayLike
|
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
|
brainstate/nn/_linear_mv_test.py
CHANGED
@@ -1,120 +1,120 @@
|
|
1
|
-
# Copyright 2024 BDP 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 brainstate
|
22
|
-
|
23
|
-
|
24
|
-
class TestEventLinear:
|
25
|
-
@pytest.mark.parametrize('bool_x', [True, False])
|
26
|
-
@pytest.mark.parametrize('homo_w', [True, False])
|
27
|
-
def test1(self, homo_w, bool_x):
|
28
|
-
x = brainstate.random.rand(20) < 0.1
|
29
|
-
if not bool_x:
|
30
|
-
x = jnp.asarray(x, dtype=float)
|
31
|
-
m = brainstate.nn.EventLinear(
|
32
|
-
20, 40,
|
33
|
-
1.5 if homo_w else brainstate.init.KaimingUniform(),
|
34
|
-
float_as_event=bool_x
|
35
|
-
)
|
36
|
-
y = m(x)
|
37
|
-
print(y)
|
38
|
-
|
39
|
-
assert (jnp.allclose(y, (x.sum() * m.weight.value) if homo_w else (x @ m.weight.value)))
|
40
|
-
|
41
|
-
def test_grad_bool(self):
|
42
|
-
n_in = 20
|
43
|
-
n_out = 30
|
44
|
-
x = brainstate.random.rand(n_in) < 0.3
|
45
|
-
fn = brainstate.nn.EventLinear(n_in, n_out, brainstate.init.KaimingUniform())
|
46
|
-
|
47
|
-
with pytest.raises(TypeError):
|
48
|
-
print(jax.grad(lambda x: fn(x).sum())(x))
|
49
|
-
|
50
|
-
@pytest.mark.parametrize('bool_x', [True, False])
|
51
|
-
@pytest.mark.parametrize('homo_w', [True, False])
|
52
|
-
def test_vjp(self, bool_x, homo_w):
|
53
|
-
n_in = 20
|
54
|
-
n_out = 30
|
55
|
-
if bool_x:
|
56
|
-
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
57
|
-
else:
|
58
|
-
x = brainstate.random.rand(n_in)
|
59
|
-
|
60
|
-
fn = brainstate.nn.EventLinear(
|
61
|
-
n_in,
|
62
|
-
n_out,
|
63
|
-
1.5 if homo_w else brainstate.init.KaimingUniform(),
|
64
|
-
float_as_event=bool_x
|
65
|
-
)
|
66
|
-
w = fn.weight.value
|
67
|
-
|
68
|
-
def f(x, w):
|
69
|
-
fn.weight.value = w
|
70
|
-
return fn(x).sum()
|
71
|
-
|
72
|
-
r1 = jax.grad(f, argnums=(0, 1))(x, w)
|
73
|
-
|
74
|
-
# -------------------
|
75
|
-
# TRUE gradients
|
76
|
-
|
77
|
-
def f2(x, w):
|
78
|
-
y = (x @ (jnp.ones([n_in, n_out]) * w)) if homo_w else (x @ w)
|
79
|
-
return y.sum()
|
80
|
-
|
81
|
-
r2 = jax.grad(f2, argnums=(0, 1))(x, w)
|
82
|
-
assert (jnp.allclose(r1[0], r2[0]))
|
83
|
-
|
84
|
-
if not jnp.allclose(r1[1], r2[1]):
|
85
|
-
print(r1[1] - r2[1])
|
86
|
-
|
87
|
-
assert (jnp.allclose(r1[1], r2[1]))
|
88
|
-
|
89
|
-
@pytest.mark.parametrize('bool_x', [True, False])
|
90
|
-
@pytest.mark.parametrize('homo_w', [True, False])
|
91
|
-
def test_jvp(self, bool_x, homo_w):
|
92
|
-
n_in = 20
|
93
|
-
n_out = 30
|
94
|
-
if bool_x:
|
95
|
-
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
96
|
-
else:
|
97
|
-
x = brainstate.random.rand(n_in)
|
98
|
-
|
99
|
-
fn = brainstate.nn.EventLinear(
|
100
|
-
n_in, n_out, 1.5 if homo_w else brainstate.init.KaimingUniform(),
|
101
|
-
float_as_event=bool_x
|
102
|
-
)
|
103
|
-
w = fn.weight.value
|
104
|
-
|
105
|
-
def f(x, w):
|
106
|
-
fn.weight.value = w
|
107
|
-
return fn(x)
|
108
|
-
|
109
|
-
o1, r1 = jax.jvp(f, (x, w), (jnp.ones_like(x), jnp.ones_like(w)))
|
110
|
-
|
111
|
-
# -------------------
|
112
|
-
# TRUE gradients
|
113
|
-
|
114
|
-
def f2(x, w):
|
115
|
-
y = (x @ (jnp.ones([n_in, n_out]) * w)) if homo_w else (x @ w)
|
116
|
-
return y
|
117
|
-
|
118
|
-
o2, r2 = jax.jvp(f, (x, w), (jnp.ones_like(x), jnp.ones_like(w)))
|
119
|
-
assert (jnp.allclose(o1, o2))
|
120
|
-
assert (jnp.allclose(r1, r2))
|
1
|
+
# Copyright 2024 BDP 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 brainstate
|
22
|
+
|
23
|
+
|
24
|
+
class TestEventLinear:
|
25
|
+
@pytest.mark.parametrize('bool_x', [True, False])
|
26
|
+
@pytest.mark.parametrize('homo_w', [True, False])
|
27
|
+
def test1(self, homo_w, bool_x):
|
28
|
+
x = brainstate.random.rand(20) < 0.1
|
29
|
+
if not bool_x:
|
30
|
+
x = jnp.asarray(x, dtype=float)
|
31
|
+
m = brainstate.nn.EventLinear(
|
32
|
+
20, 40,
|
33
|
+
1.5 if homo_w else brainstate.init.KaimingUniform(),
|
34
|
+
float_as_event=bool_x
|
35
|
+
)
|
36
|
+
y = m(x)
|
37
|
+
print(y)
|
38
|
+
|
39
|
+
assert (jnp.allclose(y, (x.sum() * m.weight.value) if homo_w else (x @ m.weight.value)))
|
40
|
+
|
41
|
+
def test_grad_bool(self):
|
42
|
+
n_in = 20
|
43
|
+
n_out = 30
|
44
|
+
x = brainstate.random.rand(n_in) < 0.3
|
45
|
+
fn = brainstate.nn.EventLinear(n_in, n_out, brainstate.init.KaimingUniform())
|
46
|
+
|
47
|
+
with pytest.raises(TypeError):
|
48
|
+
print(jax.grad(lambda x: fn(x).sum())(x))
|
49
|
+
|
50
|
+
@pytest.mark.parametrize('bool_x', [True, False])
|
51
|
+
@pytest.mark.parametrize('homo_w', [True, False])
|
52
|
+
def test_vjp(self, bool_x, homo_w):
|
53
|
+
n_in = 20
|
54
|
+
n_out = 30
|
55
|
+
if bool_x:
|
56
|
+
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
57
|
+
else:
|
58
|
+
x = brainstate.random.rand(n_in)
|
59
|
+
|
60
|
+
fn = brainstate.nn.EventLinear(
|
61
|
+
n_in,
|
62
|
+
n_out,
|
63
|
+
1.5 if homo_w else brainstate.init.KaimingUniform(),
|
64
|
+
float_as_event=bool_x
|
65
|
+
)
|
66
|
+
w = fn.weight.value
|
67
|
+
|
68
|
+
def f(x, w):
|
69
|
+
fn.weight.value = w
|
70
|
+
return fn(x).sum()
|
71
|
+
|
72
|
+
r1 = jax.grad(f, argnums=(0, 1))(x, w)
|
73
|
+
|
74
|
+
# -------------------
|
75
|
+
# TRUE gradients
|
76
|
+
|
77
|
+
def f2(x, w):
|
78
|
+
y = (x @ (jnp.ones([n_in, n_out]) * w)) if homo_w else (x @ w)
|
79
|
+
return y.sum()
|
80
|
+
|
81
|
+
r2 = jax.grad(f2, argnums=(0, 1))(x, w)
|
82
|
+
assert (jnp.allclose(r1[0], r2[0]))
|
83
|
+
|
84
|
+
if not jnp.allclose(r1[1], r2[1]):
|
85
|
+
print(r1[1] - r2[1])
|
86
|
+
|
87
|
+
assert (jnp.allclose(r1[1], r2[1]))
|
88
|
+
|
89
|
+
@pytest.mark.parametrize('bool_x', [True, False])
|
90
|
+
@pytest.mark.parametrize('homo_w', [True, False])
|
91
|
+
def test_jvp(self, bool_x, homo_w):
|
92
|
+
n_in = 20
|
93
|
+
n_out = 30
|
94
|
+
if bool_x:
|
95
|
+
x = jax.numpy.asarray(brainstate.random.rand(n_in) < 0.3, dtype=float)
|
96
|
+
else:
|
97
|
+
x = brainstate.random.rand(n_in)
|
98
|
+
|
99
|
+
fn = brainstate.nn.EventLinear(
|
100
|
+
n_in, n_out, 1.5 if homo_w else brainstate.init.KaimingUniform(),
|
101
|
+
float_as_event=bool_x
|
102
|
+
)
|
103
|
+
w = fn.weight.value
|
104
|
+
|
105
|
+
def f(x, w):
|
106
|
+
fn.weight.value = w
|
107
|
+
return fn(x)
|
108
|
+
|
109
|
+
o1, r1 = jax.jvp(f, (x, w), (jnp.ones_like(x), jnp.ones_like(w)))
|
110
|
+
|
111
|
+
# -------------------
|
112
|
+
# TRUE gradients
|
113
|
+
|
114
|
+
def f2(x, w):
|
115
|
+
y = (x @ (jnp.ones([n_in, n_out]) * w)) if homo_w else (x @ w)
|
116
|
+
return y
|
117
|
+
|
118
|
+
o2, r2 = jax.jvp(f, (x, w), (jnp.ones_like(x), jnp.ones_like(w)))
|
119
|
+
assert (jnp.allclose(o1, o2))
|
120
|
+
assert (jnp.allclose(r1, r2))
|
brainstate/nn/_linear_test.py
CHANGED
@@ -1,107 +1,107 @@
|
|
1
|
-
# Copyright 2024 BDP 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 unittest
|
18
|
-
|
19
|
-
import brainunit as u
|
20
|
-
from absl.testing import parameterized
|
21
|
-
|
22
|
-
import brainstate
|
23
|
-
|
24
|
-
|
25
|
-
class TestDense(parameterized.TestCase):
|
26
|
-
@parameterized.product(
|
27
|
-
size=[(10,),
|
28
|
-
(20, 10),
|
29
|
-
(5, 8, 10)],
|
30
|
-
num_out=[20, ]
|
31
|
-
)
|
32
|
-
def test_Dense1(self, size, num_out):
|
33
|
-
f = brainstate.nn.Linear(10, num_out)
|
34
|
-
x = brainstate.random.random(size)
|
35
|
-
y = f(x)
|
36
|
-
self.assertTrue(y.shape == size[:-1] + (num_out,))
|
37
|
-
|
38
|
-
|
39
|
-
class TestSparseMatrix(unittest.TestCase):
|
40
|
-
def test_csr(self):
|
41
|
-
data = brainstate.random.rand(10, 20)
|
42
|
-
data = data * (data > 0.9)
|
43
|
-
f = brainstate.nn.SparseLinear(u.sparse.CSR.fromdense(data))
|
44
|
-
|
45
|
-
x = brainstate.random.rand(10)
|
46
|
-
y = f(x)
|
47
|
-
self.assertTrue(
|
48
|
-
u.math.allclose(
|
49
|
-
y,
|
50
|
-
x @ data
|
51
|
-
)
|
52
|
-
)
|
53
|
-
|
54
|
-
x = brainstate.random.rand(5, 10)
|
55
|
-
y = f(x)
|
56
|
-
self.assertTrue(
|
57
|
-
u.math.allclose(
|
58
|
-
y,
|
59
|
-
x @ data
|
60
|
-
)
|
61
|
-
)
|
62
|
-
|
63
|
-
def test_csc(self):
|
64
|
-
data = brainstate.random.rand(10, 20)
|
65
|
-
data = data * (data > 0.9)
|
66
|
-
f = brainstate.nn.SparseLinear(u.sparse.CSC.fromdense(data))
|
67
|
-
|
68
|
-
x = brainstate.random.rand(10)
|
69
|
-
y = f(x)
|
70
|
-
self.assertTrue(
|
71
|
-
u.math.allclose(
|
72
|
-
y,
|
73
|
-
x @ data
|
74
|
-
)
|
75
|
-
)
|
76
|
-
|
77
|
-
x = brainstate.random.rand(5, 10)
|
78
|
-
y = f(x)
|
79
|
-
self.assertTrue(
|
80
|
-
u.math.allclose(
|
81
|
-
y,
|
82
|
-
x @ data
|
83
|
-
)
|
84
|
-
)
|
85
|
-
|
86
|
-
def test_coo(self):
|
87
|
-
data = brainstate.random.rand(10, 20)
|
88
|
-
data = data * (data > 0.9)
|
89
|
-
f = brainstate.nn.SparseLinear(u.sparse.COO.fromdense(data))
|
90
|
-
|
91
|
-
x = brainstate.random.rand(10)
|
92
|
-
y = f(x)
|
93
|
-
self.assertTrue(
|
94
|
-
u.math.allclose(
|
95
|
-
y,
|
96
|
-
x @ data
|
97
|
-
)
|
98
|
-
)
|
99
|
-
|
100
|
-
x = brainstate.random.rand(5, 10)
|
101
|
-
y = f(x)
|
102
|
-
self.assertTrue(
|
103
|
-
u.math.allclose(
|
104
|
-
y,
|
105
|
-
x @ data
|
106
|
-
)
|
107
|
-
)
|
1
|
+
# Copyright 2024 BDP 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 unittest
|
18
|
+
|
19
|
+
import brainunit as u
|
20
|
+
from absl.testing import parameterized
|
21
|
+
|
22
|
+
import brainstate
|
23
|
+
|
24
|
+
|
25
|
+
class TestDense(parameterized.TestCase):
|
26
|
+
@parameterized.product(
|
27
|
+
size=[(10,),
|
28
|
+
(20, 10),
|
29
|
+
(5, 8, 10)],
|
30
|
+
num_out=[20, ]
|
31
|
+
)
|
32
|
+
def test_Dense1(self, size, num_out):
|
33
|
+
f = brainstate.nn.Linear(10, num_out)
|
34
|
+
x = brainstate.random.random(size)
|
35
|
+
y = f(x)
|
36
|
+
self.assertTrue(y.shape == size[:-1] + (num_out,))
|
37
|
+
|
38
|
+
|
39
|
+
class TestSparseMatrix(unittest.TestCase):
|
40
|
+
def test_csr(self):
|
41
|
+
data = brainstate.random.rand(10, 20)
|
42
|
+
data = data * (data > 0.9)
|
43
|
+
f = brainstate.nn.SparseLinear(u.sparse.CSR.fromdense(data))
|
44
|
+
|
45
|
+
x = brainstate.random.rand(10)
|
46
|
+
y = f(x)
|
47
|
+
self.assertTrue(
|
48
|
+
u.math.allclose(
|
49
|
+
y,
|
50
|
+
x @ data
|
51
|
+
)
|
52
|
+
)
|
53
|
+
|
54
|
+
x = brainstate.random.rand(5, 10)
|
55
|
+
y = f(x)
|
56
|
+
self.assertTrue(
|
57
|
+
u.math.allclose(
|
58
|
+
y,
|
59
|
+
x @ data
|
60
|
+
)
|
61
|
+
)
|
62
|
+
|
63
|
+
def test_csc(self):
|
64
|
+
data = brainstate.random.rand(10, 20)
|
65
|
+
data = data * (data > 0.9)
|
66
|
+
f = brainstate.nn.SparseLinear(u.sparse.CSC.fromdense(data))
|
67
|
+
|
68
|
+
x = brainstate.random.rand(10)
|
69
|
+
y = f(x)
|
70
|
+
self.assertTrue(
|
71
|
+
u.math.allclose(
|
72
|
+
y,
|
73
|
+
x @ data
|
74
|
+
)
|
75
|
+
)
|
76
|
+
|
77
|
+
x = brainstate.random.rand(5, 10)
|
78
|
+
y = f(x)
|
79
|
+
self.assertTrue(
|
80
|
+
u.math.allclose(
|
81
|
+
y,
|
82
|
+
x @ data
|
83
|
+
)
|
84
|
+
)
|
85
|
+
|
86
|
+
def test_coo(self):
|
87
|
+
data = brainstate.random.rand(10, 20)
|
88
|
+
data = data * (data > 0.9)
|
89
|
+
f = brainstate.nn.SparseLinear(u.sparse.COO.fromdense(data))
|
90
|
+
|
91
|
+
x = brainstate.random.rand(10)
|
92
|
+
y = f(x)
|
93
|
+
self.assertTrue(
|
94
|
+
u.math.allclose(
|
95
|
+
y,
|
96
|
+
x @ data
|
97
|
+
)
|
98
|
+
)
|
99
|
+
|
100
|
+
x = brainstate.random.rand(5, 10)
|
101
|
+
y = f(x)
|
102
|
+
self.assertTrue(
|
103
|
+
u.math.allclose(
|
104
|
+
y,
|
105
|
+
x @ data
|
106
|
+
)
|
107
|
+
)
|
brainstate/nn/_ltp.py
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
# Copyright 2025 BDP 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
|
-
# -*- coding: utf-8 -*-
|
17
|
-
|
18
|
-
|
19
|
-
from ._synapse import Synapse
|
20
|
-
|
21
|
-
__all__ = [
|
22
|
-
'LongTermPlasticity',
|
23
|
-
]
|
24
|
-
|
25
|
-
|
26
|
-
class LongTermPlasticity(Synapse):
|
27
|
-
pass
|
28
|
-
|
1
|
+
# Copyright 2025 BDP 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
|
+
# -*- coding: utf-8 -*-
|
17
|
+
|
18
|
+
|
19
|
+
from ._synapse import Synapse
|
20
|
+
|
21
|
+
__all__ = [
|
22
|
+
'LongTermPlasticity',
|
23
|
+
]
|
24
|
+
|
25
|
+
|
26
|
+
class LongTermPlasticity(Synapse):
|
27
|
+
pass
|
28
|
+
|