braintools 0.0.1__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.
@@ -0,0 +1,46 @@
1
+ from typing import Sequence, Optional
2
+
3
+ import jax
4
+ import jax.numpy as jnp
5
+ import numpy as np
6
+
7
+
8
+ __all__ = [
9
+ 'weight_standardization',
10
+ ]
11
+
12
+
13
+ def weight_standardization(
14
+ w: jax.typing.ArrayLike,
15
+ axes: Sequence[int],
16
+ eps: float,
17
+ gain: Optional[jax.Array] = None
18
+ ):
19
+ """
20
+ Scaled Weight Standardization.
21
+
22
+ Parameters
23
+ ----------
24
+ w : jax.typing.ArrayLike
25
+ The weight tensor.
26
+ axes : Sequence[int]
27
+ The axes to calculate the mean and variance.
28
+ eps : float
29
+ A small value to avoid division by zero.
30
+ gain : Array
31
+ The gain function, by default None.
32
+
33
+ Returns
34
+ -------
35
+ jax.typing.ArrayLike
36
+ The scaled weight tensor.
37
+
38
+ """
39
+ # Get Scaled WS weight HWIO;
40
+ fan_in = np.prod(w.shape[:-1])
41
+ mean = jnp.mean(w, axis=axes[:-1], keepdims=True)
42
+ var = jnp.var(w, axis=axes[:-1], keepdims=True)
43
+ weight = (w - mean) / (var * fan_in + eps) ** 0.5
44
+ if gain is not None:
45
+ weight = gain * weight
46
+ return weight
@@ -0,0 +1,73 @@
1
+ __all__ = [
2
+ 'spike_bitwise_or',
3
+ 'spike_bitwise_and',
4
+ 'spike_bitwise_iand',
5
+ 'spike_bitwise_not',
6
+ 'spike_bitwise_xor',
7
+ 'spike_bitwise_ixor',
8
+ 'spike_bitwise',
9
+ ]
10
+
11
+
12
+ def spike_bitwise_or(x, y):
13
+ """Bitwise OR operation for spike tensors."""
14
+ return x + y - x * y
15
+
16
+
17
+ def spike_bitwise_and(x, y):
18
+ """Bitwise AND operation for spike tensors."""
19
+ return x * y
20
+
21
+
22
+ def spike_bitwise_iand(x, y):
23
+ """Bitwise IAND operation for spike tensors."""
24
+ return (1 - x) * y
25
+
26
+
27
+ def spike_bitwise_not(x):
28
+ """Bitwise NOT operation for spike tensors."""
29
+ return 1 - x
30
+
31
+
32
+ def spike_bitwise_xor(x, y):
33
+ """Bitwise XOR operation for spike tensors."""
34
+ return x + y - 2 * x * y
35
+
36
+
37
+ def spike_bitwise_ixor(x, y):
38
+ """Bitwise IXOR operation for spike tensors."""
39
+ return x * (1 - y) + (1 - x) * y
40
+
41
+
42
+ def spike_bitwise(x, y, op: str):
43
+ r"""Bitwise operation for spike tensors.
44
+
45
+ .. math::
46
+
47
+ \begin{array}{ccc}
48
+ \hline \text { Mode } & \text { Expression for } \mathrm{g}(\mathrm{x}, \mathrm{y}) & \text { Code for } \mathrm{g}(\mathrm{x}, \mathrm{y}) \\
49
+ \hline \text { ADD } & x+y & x+y \\
50
+ \text { AND } & x \cap y & x \cdot y \\
51
+ \text { IAND } & (\neg x) \cap y & (1-x) \cdot y \\
52
+ \text { OR } & x \cup y & (x+y)-(x \cdot y) \\
53
+ \hline
54
+ \end{array}
55
+
56
+ Args:
57
+ x: A spike tensor.
58
+ y: A spike tensor.
59
+ op: A string indicating the bitwise operation to perform.
60
+ """
61
+ if op == 'or':
62
+ return spike_bitwise_or(x, y)
63
+ elif op == 'and':
64
+ return spike_bitwise_and(x, y)
65
+ elif op == 'iand':
66
+ return spike_bitwise_iand(x, y)
67
+ elif op == 'xor':
68
+ return spike_bitwise_xor(x, y)
69
+ elif op == 'ixor':
70
+ return spike_bitwise_ixor(x, y)
71
+ else:
72
+ raise NotImplementedError(f"Unsupported bitwise operation: {op}.")
73
+
@@ -0,0 +1,10 @@
1
+ from ._base import *
2
+ from ._base import __all__ as _base_all
3
+ from ._generic import *
4
+ from ._generic import __all__ as _generic_all
5
+ from ._random_inits import *
6
+ from ._random_inits import __all__ as _random_inits_all
7
+ from ._regular_inits import *
8
+ from ._regular_inits import __all__ as _regular_inits_all
9
+
10
+ __all__ = _generic_all + _base_all + _regular_inits_all + _random_inits_all
@@ -0,0 +1,8 @@
1
+ __all__ = ['Initializer']
2
+
3
+
4
+ class Initializer(object):
5
+ def __call__(self, *args, **kwargs):
6
+ raise NotImplementedError
7
+
8
+
@@ -0,0 +1,164 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from typing import Union, Callable, Optional, Sequence
4
+
5
+ import braincore as bc
6
+ import jax
7
+ import jax.numpy as jnp
8
+ import numpy as np
9
+ from brainpy.tools import to_size
10
+
11
+ __all__ = [
12
+ 'parameter',
13
+ 'state',
14
+ 'noise',
15
+ ]
16
+
17
+
18
+ def _is_scalar(x):
19
+ return isinstance(x, (float, int, bool, complex))
20
+
21
+
22
+ def parameter(
23
+ param: Union[Callable, np.ndarray, jax.Array, float, int, bool],
24
+ sizes: Union[int, Sequence[int]],
25
+ allow_none: bool = True,
26
+ allow_scalar: bool = True,
27
+ ):
28
+ """Initialize parameters.
29
+
30
+ Parameters
31
+ ----------
32
+ param: callable, Initializer, bm.ndarray, jnp.ndarray, onp.ndarray, float, int, bool
33
+ The initialization of the parameter.
34
+ - If it is None, the created parameter will be None.
35
+ - If it is a callable function :math:`f`, the ``f(size)`` will be returned.
36
+ - If it is an instance of :py:class:`brainonline.init.Initializer``, the ``f(size)`` will be returned.
37
+ - If it is a tensor, then this function check whether ``tensor.shape`` is equal to the given ``size``.
38
+ sizes: int, sequence of int
39
+ The shape of the parameter.
40
+ allow_none: bool
41
+ Whether allow the parameter is None.
42
+ allow_scalar: bool
43
+ Whether allow the parameter is a scalar value.
44
+
45
+ Returns
46
+ -------
47
+ param: ArrayType, float, int, bool, None
48
+ The initialized parameter.
49
+
50
+ See Also
51
+ --------
52
+ variable_, noise, delay
53
+ """
54
+ if param is None:
55
+ if allow_none:
56
+ return None
57
+ else:
58
+ raise ValueError(f'Expect a parameter with type of float, ArrayType, Initializer, or '
59
+ f'Callable function, but we got None. ')
60
+ sizes = to_size(sizes)
61
+ if allow_scalar and _is_scalar(param):
62
+ return param
63
+
64
+ if callable(param):
65
+ return param(*sizes)
66
+ elif isinstance(param, np.ndarray):
67
+ param = jnp.asarray(param)
68
+ elif isinstance(param, bc.State):
69
+ param = param
70
+ else:
71
+ param = param
72
+
73
+ if allow_scalar:
74
+ if param.shape == () or param.shape == (1,):
75
+ return param
76
+ if param.shape != sizes:
77
+ raise ValueError(f'The shape of the parameters should be {sizes}, but we got {param.shape}')
78
+ return param
79
+
80
+
81
+ def state(
82
+ init: Union[Callable, np.ndarray, jax.Array],
83
+ sizes: Union[int, Sequence[int]] = None,
84
+ batch_or_mode: Optional[Union[int, bool, bc.mixin.Mode]] = None,
85
+ ):
86
+ """
87
+ Initialize a :math:`~.State` from a callable function or a data.
88
+ """
89
+ sizes = list(to_size(sizes))
90
+
91
+ if callable(init):
92
+ if sizes is None:
93
+ raise ValueError('"varshape" cannot be None when data is a callable function.')
94
+ if isinstance(batch_or_mode, bc.mixin.Mode):
95
+ if batch_or_mode.has(bc.mixin.Batching):
96
+ sizes.insert(batch_or_mode.batch_axis, batch_or_mode.batch_size)
97
+ elif batch_or_mode in (None, False):
98
+ pass
99
+ elif isinstance(batch_or_mode, int):
100
+ sizes.insert(0, batch_or_mode)
101
+ else:
102
+ raise ValueError(f'Unknown batch_size_or_mode: {batch_or_mode}')
103
+ data = bc.State(init(*sizes))
104
+
105
+ else:
106
+ if sizes is not None:
107
+ if jnp.shape(init) != sizes:
108
+ raise ValueError(f'The shape of "data" {jnp.shape(init)} does not match with "var_shape" {sizes}')
109
+ batch_axis = None
110
+ batch_size = None
111
+ if isinstance(batch_or_mode, bc.mixin.Mode):
112
+ if batch_or_mode.has(bc.mixin.Batching):
113
+ batch_axis = batch_or_mode.batch_axis
114
+ batch_size = batch_or_mode.batch_size
115
+ elif batch_or_mode in (None, False):
116
+ pass
117
+ elif isinstance(batch_or_mode, int):
118
+ batch_axis = 0
119
+ batch_size = batch_or_mode
120
+ else:
121
+ raise ValueError('Unknown batch_size_or_mode.')
122
+ data = bc.State(jnp.repeat(jnp.expand_dims(init, axis=batch_axis), batch_size, axis=batch_axis))
123
+ return data
124
+
125
+
126
+ def noise(
127
+ noises: Optional[Union[int, float, np.ndarray, jax.Array, Callable]],
128
+ size: Union[int, Sequence[int]],
129
+ num_vars: int = 1,
130
+ noise_idx: int = 0,
131
+ ) -> Optional[Callable]:
132
+ """Initialize a noise function.
133
+
134
+ Parameters
135
+ ----------
136
+ noises: Any
137
+ size: Shape
138
+ The size of the noise.
139
+ num_vars: int
140
+ The number of variables.
141
+ noise_idx: int
142
+ The index of the current noise among all noise variables.
143
+
144
+ Returns
145
+ -------
146
+ noise_func: function, None
147
+ The noise function.
148
+
149
+ See Also
150
+ --------
151
+ variable_, parameter, delay
152
+
153
+ """
154
+ if callable(noises):
155
+ return noises
156
+ elif noises is None:
157
+ return None
158
+ else:
159
+ noises = parameter(noises, size, allow_none=False)
160
+ if num_vars > 1:
161
+ noises_ = [None] * num_vars
162
+ noises_[noise_idx] = noises
163
+ noises = tuple(noises_)
164
+ return lambda *args, **kwargs: noises