brainstate 0.1.8__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 -156
- 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 -502
- brainstate/nn/_delay_test.py +238 -184
- brainstate/nn/_dropout.py +426 -426
- brainstate/nn/_dropout_test.py +100 -100
- brainstate/nn/_dynamics.py +1343 -1343
- brainstate/nn/_dynamics_test.py +78 -78
- brainstate/nn/_elementwise.py +1119 -1119
- 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 -40
- 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.8.dist-info → brainstate-0.1.9.dist-info}/METADATA +91 -99
- brainstate-0.1.9.dist-info/RECORD +130 -0
- {brainstate-0.1.8.dist-info → brainstate-0.1.9.dist-info}/WHEEL +1 -1
- {brainstate-0.1.8.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.8.dist-info/RECORD +0 -132
- {brainstate-0.1.8.dist-info → brainstate-0.1.9.dist-info}/top_level.txt +0 -0
brainstate/init/__init__.py
CHANGED
@@ -1,26 +1,26 @@
|
|
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
|
-
from ._base import *
|
18
|
-
from ._base import __all__ as _base_all
|
19
|
-
from ._generic import *
|
20
|
-
from ._generic import __all__ as _generic_all
|
21
|
-
from ._random_inits import *
|
22
|
-
from ._random_inits import __all__ as _random_inits_all
|
23
|
-
from ._regular_inits import *
|
24
|
-
from ._regular_inits import __all__ as _regular_inits_all
|
25
|
-
|
26
|
-
__all__ = _generic_all + _base_all + _regular_inits_all + _random_inits_all
|
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
|
+
from ._base import *
|
18
|
+
from ._base import __all__ as _base_all
|
19
|
+
from ._generic import *
|
20
|
+
from ._generic import __all__ as _generic_all
|
21
|
+
from ._random_inits import *
|
22
|
+
from ._random_inits import __all__ as _random_inits_all
|
23
|
+
from ._regular_inits import *
|
24
|
+
from ._regular_inits import __all__ as _regular_inits_all
|
25
|
+
|
26
|
+
__all__ = _generic_all + _base_all + _regular_inits_all + _random_inits_all
|
brainstate/init/_base.py
CHANGED
@@ -1,52 +1,52 @@
|
|
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 Optional, Tuple
|
17
|
-
|
18
|
-
import numpy as np
|
19
|
-
|
20
|
-
from brainstate.util import PrettyRepr, PrettyType, PrettyAttr
|
21
|
-
|
22
|
-
__all__ = ['Initializer', 'to_size']
|
23
|
-
|
24
|
-
|
25
|
-
class Initializer(PrettyRepr):
|
26
|
-
"""
|
27
|
-
Base class for initializers.
|
28
|
-
"""
|
29
|
-
__module__ = 'brainstate.init'
|
30
|
-
|
31
|
-
def __call__(self, *args, **kwargs):
|
32
|
-
raise NotImplementedError
|
33
|
-
|
34
|
-
def __pretty_repr__(self):
|
35
|
-
"""
|
36
|
-
Pretty repr for the object.
|
37
|
-
"""
|
38
|
-
yield PrettyType(type=type(self))
|
39
|
-
for name, value in vars(self).items():
|
40
|
-
if name.startswith('_'):
|
41
|
-
continue
|
42
|
-
yield PrettyAttr(name, repr(value))
|
43
|
-
|
44
|
-
|
45
|
-
def to_size(x) -> Optional[Tuple[int]]:
|
46
|
-
if isinstance(x, (tuple, list)):
|
47
|
-
return tuple(x)
|
48
|
-
if isinstance(x, (int, np.integer)):
|
49
|
-
return (x,)
|
50
|
-
if x is None:
|
51
|
-
return x
|
52
|
-
raise ValueError(f'Cannot make a size for {x}')
|
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 Optional, Tuple
|
17
|
+
|
18
|
+
import numpy as np
|
19
|
+
|
20
|
+
from brainstate.util import PrettyRepr, PrettyType, PrettyAttr
|
21
|
+
|
22
|
+
__all__ = ['Initializer', 'to_size']
|
23
|
+
|
24
|
+
|
25
|
+
class Initializer(PrettyRepr):
|
26
|
+
"""
|
27
|
+
Base class for initializers.
|
28
|
+
"""
|
29
|
+
__module__ = 'brainstate.init'
|
30
|
+
|
31
|
+
def __call__(self, *args, **kwargs):
|
32
|
+
raise NotImplementedError
|
33
|
+
|
34
|
+
def __pretty_repr__(self):
|
35
|
+
"""
|
36
|
+
Pretty repr for the object.
|
37
|
+
"""
|
38
|
+
yield PrettyType(type=type(self))
|
39
|
+
for name, value in vars(self).items():
|
40
|
+
if name.startswith('_'):
|
41
|
+
continue
|
42
|
+
yield PrettyAttr(name, repr(value))
|
43
|
+
|
44
|
+
|
45
|
+
def to_size(x) -> Optional[Tuple[int]]:
|
46
|
+
if isinstance(x, (tuple, list)):
|
47
|
+
return tuple(x)
|
48
|
+
if isinstance(x, (int, np.integer)):
|
49
|
+
return (x,)
|
50
|
+
if x is None:
|
51
|
+
return x
|
52
|
+
raise ValueError(f'Cannot make a size for {x}')
|
brainstate/init/_generic.py
CHANGED
@@ -1,244 +1,244 @@
|
|
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
|
-
# -*- coding: utf-8 -*-
|
17
|
-
|
18
|
-
from typing import Union, Callable, Optional, Sequence
|
19
|
-
|
20
|
-
import brainunit as bu
|
21
|
-
import jax
|
22
|
-
import numpy as np
|
23
|
-
|
24
|
-
from brainstate._state import State
|
25
|
-
from brainstate._utils import set_module_as
|
26
|
-
from brainstate.typing import ArrayLike
|
27
|
-
from ._base import to_size
|
28
|
-
|
29
|
-
__all__ = [
|
30
|
-
'param',
|
31
|
-
'state',
|
32
|
-
'noise',
|
33
|
-
]
|
34
|
-
|
35
|
-
|
36
|
-
def _is_scalar(x):
|
37
|
-
return bu.math.isscalar(x)
|
38
|
-
|
39
|
-
|
40
|
-
def are_broadcastable_shapes(shape1, shape2):
|
41
|
-
"""
|
42
|
-
Check if two shapes are broadcastable.
|
43
|
-
|
44
|
-
Parameters:
|
45
|
-
- shape1: Tuple[int], the shape of the first array.
|
46
|
-
- shape2: Tuple[int], the shape of the second array.
|
47
|
-
|
48
|
-
Returns:
|
49
|
-
- bool: True if shapes are broadcastable, False otherwise.
|
50
|
-
"""
|
51
|
-
# Reverse the shapes to compare from the last dimension
|
52
|
-
shape1_reversed = shape1[::-1]
|
53
|
-
shape2_reversed = shape2[::-1]
|
54
|
-
|
55
|
-
# Iterate over the dimensions of the shorter shape
|
56
|
-
for dim1, dim2 in zip(shape1_reversed, shape2_reversed):
|
57
|
-
# Check if the dimensions are not equal and neither is 1
|
58
|
-
if dim1 != dim2 and 1 not in (dim1, dim2):
|
59
|
-
return False
|
60
|
-
|
61
|
-
# If all dimensions are compatible, the shapes are broadcastable
|
62
|
-
return True
|
63
|
-
|
64
|
-
|
65
|
-
def _expand_params_to_match_sizes(params, sizes):
|
66
|
-
"""
|
67
|
-
Expand the dimensions of params to match the dimensions of sizes.
|
68
|
-
|
69
|
-
Parameters:
|
70
|
-
- params: jax.Array or np.ndarray, the parameter array to be expanded.
|
71
|
-
- sizes: tuple[int] or list[int], the target shape dimensions.
|
72
|
-
|
73
|
-
Returns:
|
74
|
-
- Expanded params with dimensions matching sizes.
|
75
|
-
"""
|
76
|
-
params_dim = params.ndim
|
77
|
-
sizes_dim = len(sizes)
|
78
|
-
dim_diff = sizes_dim - params_dim
|
79
|
-
|
80
|
-
# Add new axes to params if it has fewer dimensions than sizes
|
81
|
-
for _ in range(dim_diff):
|
82
|
-
params = bu.math.expand_dims(params, axis=0) # Add new axis at the last dimension
|
83
|
-
return params
|
84
|
-
|
85
|
-
|
86
|
-
@set_module_as('brainstate.init')
|
87
|
-
def param(
|
88
|
-
parameter: Union[Callable, ArrayLike, State],
|
89
|
-
sizes: Union[int, Sequence[int]],
|
90
|
-
batch_size: Optional[int] = None,
|
91
|
-
allow_none: bool = True,
|
92
|
-
allow_scalar: bool = True,
|
93
|
-
):
|
94
|
-
"""Initialize parameters.
|
95
|
-
|
96
|
-
Parameters
|
97
|
-
----------
|
98
|
-
parameter: callable, ArrayLike, State
|
99
|
-
The initialization of the parameter.
|
100
|
-
- If it is None, the created parameter will be None.
|
101
|
-
- If it is a callable function :math:`f`, the ``f(size)`` will be returned.
|
102
|
-
- If it is an instance of :py:class:`init.Initializer``, the ``f(size)`` will be returned.
|
103
|
-
- If it is a tensor, then this function check whether ``tensor.shape`` is equal to the given ``size``.
|
104
|
-
sizes: int, sequence of int
|
105
|
-
The shape of the parameter.
|
106
|
-
batch_size: int
|
107
|
-
The batch size.
|
108
|
-
allow_none: bool
|
109
|
-
Whether allow the parameter is None.
|
110
|
-
allow_scalar: bool
|
111
|
-
Whether allow the parameter is a scalar value.
|
112
|
-
|
113
|
-
Returns
|
114
|
-
-------
|
115
|
-
param: ArrayType, float, int, bool, None
|
116
|
-
The initialized parameter.
|
117
|
-
|
118
|
-
See Also
|
119
|
-
--------
|
120
|
-
noise, state
|
121
|
-
"""
|
122
|
-
# Check if the parameter is None
|
123
|
-
if parameter is None:
|
124
|
-
if allow_none:
|
125
|
-
return None
|
126
|
-
else:
|
127
|
-
raise ValueError(f'Expect a parameter with type of float, ArrayType, Initializer, or '
|
128
|
-
f'Callable function, but we got None. ')
|
129
|
-
|
130
|
-
# Check if the parameter is a scalar value
|
131
|
-
if allow_scalar and _is_scalar(parameter):
|
132
|
-
return parameter
|
133
|
-
|
134
|
-
# Convert sizes to a tuple
|
135
|
-
sizes = tuple(to_size(sizes))
|
136
|
-
|
137
|
-
# Check if the parameter is a callable function
|
138
|
-
if callable(parameter):
|
139
|
-
if batch_size is not None:
|
140
|
-
sizes = (batch_size,) + sizes
|
141
|
-
return parameter(sizes)
|
142
|
-
elif isinstance(parameter, (np.ndarray, jax.Array, bu.Quantity, State)):
|
143
|
-
parameter = parameter
|
144
|
-
else:
|
145
|
-
raise ValueError(f'Unknown parameter type: {type(parameter)}')
|
146
|
-
|
147
|
-
# Check if the shape of the parameter matches the given size
|
148
|
-
if not are_broadcastable_shapes(parameter.shape, sizes):
|
149
|
-
raise ValueError(f'The shape of the parameter {parameter.shape} does not match with the given size {sizes}')
|
150
|
-
|
151
|
-
# Expand the parameter to match the given batch size
|
152
|
-
param_value = parameter.value if isinstance(parameter, State) else parameter
|
153
|
-
if batch_size is not None:
|
154
|
-
if param_value.ndim <= len(sizes):
|
155
|
-
# add a new axis to the params so that it matches the dimensionality of the given shape ``sizes``
|
156
|
-
param_value = _expand_params_to_match_sizes(param_value, sizes)
|
157
|
-
param_value = bu.math.repeat(
|
158
|
-
bu.math.expand_dims(param_value, axis=0),
|
159
|
-
batch_size,
|
160
|
-
axis=0
|
161
|
-
)
|
162
|
-
else:
|
163
|
-
if param_value.shape[0] != batch_size:
|
164
|
-
raise ValueError(f'The batch size of the parameter {param_value.shape[0]} '
|
165
|
-
f'does not match with the given batch size {batch_size}')
|
166
|
-
return type(parameter)(param_value) if isinstance(parameter, State) else param_value
|
167
|
-
|
168
|
-
|
169
|
-
@set_module_as('brainstate.init')
|
170
|
-
def state(
|
171
|
-
init: Union[Callable, jax.typing.ArrayLike],
|
172
|
-
sizes: Union[int, Sequence[int]] = None,
|
173
|
-
batch_size: Optional[int] = None,
|
174
|
-
):
|
175
|
-
"""
|
176
|
-
Initialize a :math:`~.State` from a callable function or a data.
|
177
|
-
"""
|
178
|
-
sizes = to_size(sizes)
|
179
|
-
if callable(init):
|
180
|
-
if sizes is None:
|
181
|
-
raise ValueError('"varshape" cannot be None when data is a callable function.')
|
182
|
-
sizes = list(sizes)
|
183
|
-
if isinstance(batch_size, int):
|
184
|
-
sizes.insert(0, batch_size)
|
185
|
-
return State(init(sizes))
|
186
|
-
|
187
|
-
else:
|
188
|
-
if sizes is not None:
|
189
|
-
if bu.math.shape(init) != sizes:
|
190
|
-
raise ValueError(f'The shape of "data" {bu.math.shape(init)} does not match with "var_shape" {sizes}')
|
191
|
-
if isinstance(batch_size, int):
|
192
|
-
batch_size = batch_size
|
193
|
-
data = State(
|
194
|
-
bu.math.repeat(
|
195
|
-
bu.math.expand_dims(init, axis=0),
|
196
|
-
batch_size,
|
197
|
-
axis=0
|
198
|
-
)
|
199
|
-
)
|
200
|
-
else:
|
201
|
-
data = State(init)
|
202
|
-
return data
|
203
|
-
|
204
|
-
|
205
|
-
@set_module_as('brainstate.init')
|
206
|
-
def noise(
|
207
|
-
noises: Optional[Union[ArrayLike, Callable]],
|
208
|
-
size: Union[int, Sequence[int]],
|
209
|
-
num_vars: int = 1,
|
210
|
-
noise_idx: int = 0,
|
211
|
-
) -> Optional[Callable]:
|
212
|
-
"""Initialize a noise function.
|
213
|
-
|
214
|
-
Parameters
|
215
|
-
----------
|
216
|
-
noises: Any
|
217
|
-
size: Shape
|
218
|
-
The size of the noise.
|
219
|
-
num_vars: int
|
220
|
-
The number of variables.
|
221
|
-
noise_idx: int
|
222
|
-
The index of the current noise among all noise variables.
|
223
|
-
|
224
|
-
Returns
|
225
|
-
-------
|
226
|
-
noise_func: function, None
|
227
|
-
The noise function.
|
228
|
-
|
229
|
-
See Also
|
230
|
-
--------
|
231
|
-
variable_, parameter, delay
|
232
|
-
|
233
|
-
"""
|
234
|
-
if callable(noises):
|
235
|
-
return noises
|
236
|
-
elif noises is None:
|
237
|
-
return None
|
238
|
-
else:
|
239
|
-
noises = param(noises, size, allow_none=False)
|
240
|
-
if num_vars > 1:
|
241
|
-
noises_ = [None] * num_vars
|
242
|
-
noises_[noise_idx] = noises
|
243
|
-
noises = tuple(noises_)
|
244
|
-
return lambda *args, **kwargs: noises
|
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
|
+
# -*- coding: utf-8 -*-
|
17
|
+
|
18
|
+
from typing import Union, Callable, Optional, Sequence
|
19
|
+
|
20
|
+
import brainunit as bu
|
21
|
+
import jax
|
22
|
+
import numpy as np
|
23
|
+
|
24
|
+
from brainstate._state import State
|
25
|
+
from brainstate._utils import set_module_as
|
26
|
+
from brainstate.typing import ArrayLike
|
27
|
+
from ._base import to_size
|
28
|
+
|
29
|
+
__all__ = [
|
30
|
+
'param',
|
31
|
+
'state',
|
32
|
+
'noise',
|
33
|
+
]
|
34
|
+
|
35
|
+
|
36
|
+
def _is_scalar(x):
|
37
|
+
return bu.math.isscalar(x)
|
38
|
+
|
39
|
+
|
40
|
+
def are_broadcastable_shapes(shape1, shape2):
|
41
|
+
"""
|
42
|
+
Check if two shapes are broadcastable.
|
43
|
+
|
44
|
+
Parameters:
|
45
|
+
- shape1: Tuple[int], the shape of the first array.
|
46
|
+
- shape2: Tuple[int], the shape of the second array.
|
47
|
+
|
48
|
+
Returns:
|
49
|
+
- bool: True if shapes are broadcastable, False otherwise.
|
50
|
+
"""
|
51
|
+
# Reverse the shapes to compare from the last dimension
|
52
|
+
shape1_reversed = shape1[::-1]
|
53
|
+
shape2_reversed = shape2[::-1]
|
54
|
+
|
55
|
+
# Iterate over the dimensions of the shorter shape
|
56
|
+
for dim1, dim2 in zip(shape1_reversed, shape2_reversed):
|
57
|
+
# Check if the dimensions are not equal and neither is 1
|
58
|
+
if dim1 != dim2 and 1 not in (dim1, dim2):
|
59
|
+
return False
|
60
|
+
|
61
|
+
# If all dimensions are compatible, the shapes are broadcastable
|
62
|
+
return True
|
63
|
+
|
64
|
+
|
65
|
+
def _expand_params_to_match_sizes(params, sizes):
|
66
|
+
"""
|
67
|
+
Expand the dimensions of params to match the dimensions of sizes.
|
68
|
+
|
69
|
+
Parameters:
|
70
|
+
- params: jax.Array or np.ndarray, the parameter array to be expanded.
|
71
|
+
- sizes: tuple[int] or list[int], the target shape dimensions.
|
72
|
+
|
73
|
+
Returns:
|
74
|
+
- Expanded params with dimensions matching sizes.
|
75
|
+
"""
|
76
|
+
params_dim = params.ndim
|
77
|
+
sizes_dim = len(sizes)
|
78
|
+
dim_diff = sizes_dim - params_dim
|
79
|
+
|
80
|
+
# Add new axes to params if it has fewer dimensions than sizes
|
81
|
+
for _ in range(dim_diff):
|
82
|
+
params = bu.math.expand_dims(params, axis=0) # Add new axis at the last dimension
|
83
|
+
return params
|
84
|
+
|
85
|
+
|
86
|
+
@set_module_as('brainstate.init')
|
87
|
+
def param(
|
88
|
+
parameter: Union[Callable, ArrayLike, State],
|
89
|
+
sizes: Union[int, Sequence[int]],
|
90
|
+
batch_size: Optional[int] = None,
|
91
|
+
allow_none: bool = True,
|
92
|
+
allow_scalar: bool = True,
|
93
|
+
):
|
94
|
+
"""Initialize parameters.
|
95
|
+
|
96
|
+
Parameters
|
97
|
+
----------
|
98
|
+
parameter: callable, ArrayLike, State
|
99
|
+
The initialization of the parameter.
|
100
|
+
- If it is None, the created parameter will be None.
|
101
|
+
- If it is a callable function :math:`f`, the ``f(size)`` will be returned.
|
102
|
+
- If it is an instance of :py:class:`init.Initializer``, the ``f(size)`` will be returned.
|
103
|
+
- If it is a tensor, then this function check whether ``tensor.shape`` is equal to the given ``size``.
|
104
|
+
sizes: int, sequence of int
|
105
|
+
The shape of the parameter.
|
106
|
+
batch_size: int
|
107
|
+
The batch size.
|
108
|
+
allow_none: bool
|
109
|
+
Whether allow the parameter is None.
|
110
|
+
allow_scalar: bool
|
111
|
+
Whether allow the parameter is a scalar value.
|
112
|
+
|
113
|
+
Returns
|
114
|
+
-------
|
115
|
+
param: ArrayType, float, int, bool, None
|
116
|
+
The initialized parameter.
|
117
|
+
|
118
|
+
See Also
|
119
|
+
--------
|
120
|
+
noise, state
|
121
|
+
"""
|
122
|
+
# Check if the parameter is None
|
123
|
+
if parameter is None:
|
124
|
+
if allow_none:
|
125
|
+
return None
|
126
|
+
else:
|
127
|
+
raise ValueError(f'Expect a parameter with type of float, ArrayType, Initializer, or '
|
128
|
+
f'Callable function, but we got None. ')
|
129
|
+
|
130
|
+
# Check if the parameter is a scalar value
|
131
|
+
if allow_scalar and _is_scalar(parameter):
|
132
|
+
return parameter
|
133
|
+
|
134
|
+
# Convert sizes to a tuple
|
135
|
+
sizes = tuple(to_size(sizes))
|
136
|
+
|
137
|
+
# Check if the parameter is a callable function
|
138
|
+
if callable(parameter):
|
139
|
+
if batch_size is not None:
|
140
|
+
sizes = (batch_size,) + sizes
|
141
|
+
return parameter(sizes)
|
142
|
+
elif isinstance(parameter, (np.ndarray, jax.Array, bu.Quantity, State)):
|
143
|
+
parameter = parameter
|
144
|
+
else:
|
145
|
+
raise ValueError(f'Unknown parameter type: {type(parameter)}')
|
146
|
+
|
147
|
+
# Check if the shape of the parameter matches the given size
|
148
|
+
if not are_broadcastable_shapes(parameter.shape, sizes):
|
149
|
+
raise ValueError(f'The shape of the parameter {parameter.shape} does not match with the given size {sizes}')
|
150
|
+
|
151
|
+
# Expand the parameter to match the given batch size
|
152
|
+
param_value = parameter.value if isinstance(parameter, State) else parameter
|
153
|
+
if batch_size is not None:
|
154
|
+
if param_value.ndim <= len(sizes):
|
155
|
+
# add a new axis to the params so that it matches the dimensionality of the given shape ``sizes``
|
156
|
+
param_value = _expand_params_to_match_sizes(param_value, sizes)
|
157
|
+
param_value = bu.math.repeat(
|
158
|
+
bu.math.expand_dims(param_value, axis=0),
|
159
|
+
batch_size,
|
160
|
+
axis=0
|
161
|
+
)
|
162
|
+
else:
|
163
|
+
if param_value.shape[0] != batch_size:
|
164
|
+
raise ValueError(f'The batch size of the parameter {param_value.shape[0]} '
|
165
|
+
f'does not match with the given batch size {batch_size}')
|
166
|
+
return type(parameter)(param_value) if isinstance(parameter, State) else param_value
|
167
|
+
|
168
|
+
|
169
|
+
@set_module_as('brainstate.init')
|
170
|
+
def state(
|
171
|
+
init: Union[Callable, jax.typing.ArrayLike],
|
172
|
+
sizes: Union[int, Sequence[int]] = None,
|
173
|
+
batch_size: Optional[int] = None,
|
174
|
+
):
|
175
|
+
"""
|
176
|
+
Initialize a :math:`~.State` from a callable function or a data.
|
177
|
+
"""
|
178
|
+
sizes = to_size(sizes)
|
179
|
+
if callable(init):
|
180
|
+
if sizes is None:
|
181
|
+
raise ValueError('"varshape" cannot be None when data is a callable function.')
|
182
|
+
sizes = list(sizes)
|
183
|
+
if isinstance(batch_size, int):
|
184
|
+
sizes.insert(0, batch_size)
|
185
|
+
return State(init(sizes))
|
186
|
+
|
187
|
+
else:
|
188
|
+
if sizes is not None:
|
189
|
+
if bu.math.shape(init) != sizes:
|
190
|
+
raise ValueError(f'The shape of "data" {bu.math.shape(init)} does not match with "var_shape" {sizes}')
|
191
|
+
if isinstance(batch_size, int):
|
192
|
+
batch_size = batch_size
|
193
|
+
data = State(
|
194
|
+
bu.math.repeat(
|
195
|
+
bu.math.expand_dims(init, axis=0),
|
196
|
+
batch_size,
|
197
|
+
axis=0
|
198
|
+
)
|
199
|
+
)
|
200
|
+
else:
|
201
|
+
data = State(init)
|
202
|
+
return data
|
203
|
+
|
204
|
+
|
205
|
+
@set_module_as('brainstate.init')
|
206
|
+
def noise(
|
207
|
+
noises: Optional[Union[ArrayLike, Callable]],
|
208
|
+
size: Union[int, Sequence[int]],
|
209
|
+
num_vars: int = 1,
|
210
|
+
noise_idx: int = 0,
|
211
|
+
) -> Optional[Callable]:
|
212
|
+
"""Initialize a noise function.
|
213
|
+
|
214
|
+
Parameters
|
215
|
+
----------
|
216
|
+
noises: Any
|
217
|
+
size: Shape
|
218
|
+
The size of the noise.
|
219
|
+
num_vars: int
|
220
|
+
The number of variables.
|
221
|
+
noise_idx: int
|
222
|
+
The index of the current noise among all noise variables.
|
223
|
+
|
224
|
+
Returns
|
225
|
+
-------
|
226
|
+
noise_func: function, None
|
227
|
+
The noise function.
|
228
|
+
|
229
|
+
See Also
|
230
|
+
--------
|
231
|
+
variable_, parameter, delay
|
232
|
+
|
233
|
+
"""
|
234
|
+
if callable(noises):
|
235
|
+
return noises
|
236
|
+
elif noises is None:
|
237
|
+
return None
|
238
|
+
else:
|
239
|
+
noises = param(noises, size, allow_none=False)
|
240
|
+
if num_vars > 1:
|
241
|
+
noises_ = [None] * num_vars
|
242
|
+
noises_[noise_idx] = noises
|
243
|
+
noises = tuple(noises_)
|
244
|
+
return lambda *args, **kwargs: noises
|