brainstate 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.
Files changed (79) hide show
  1. brainstate/__init__.py +45 -0
  2. brainstate/_module.py +1466 -0
  3. brainstate/_module_test.py +133 -0
  4. brainstate/_state.py +378 -0
  5. brainstate/_state_test.py +41 -0
  6. brainstate/_utils.py +21 -0
  7. brainstate/environ.py +375 -0
  8. brainstate/functional/__init__.py +25 -0
  9. brainstate/functional/_activations.py +754 -0
  10. brainstate/functional/_normalization.py +69 -0
  11. brainstate/functional/_spikes.py +90 -0
  12. brainstate/init/__init__.py +26 -0
  13. brainstate/init/_base.py +36 -0
  14. brainstate/init/_generic.py +175 -0
  15. brainstate/init/_random_inits.py +489 -0
  16. brainstate/init/_regular_inits.py +109 -0
  17. brainstate/math/__init__.py +21 -0
  18. brainstate/math/_einops.py +787 -0
  19. brainstate/math/_einops_parsing.py +169 -0
  20. brainstate/math/_einops_parsing_test.py +126 -0
  21. brainstate/math/_einops_test.py +346 -0
  22. brainstate/math/_misc.py +298 -0
  23. brainstate/math/_misc_test.py +58 -0
  24. brainstate/mixin.py +373 -0
  25. brainstate/mixin_test.py +73 -0
  26. brainstate/nn/__init__.py +68 -0
  27. brainstate/nn/_base.py +248 -0
  28. brainstate/nn/_connections.py +686 -0
  29. brainstate/nn/_dynamics.py +406 -0
  30. brainstate/nn/_elementwise.py +1437 -0
  31. brainstate/nn/_misc.py +132 -0
  32. brainstate/nn/_normalizations.py +389 -0
  33. brainstate/nn/_others.py +100 -0
  34. brainstate/nn/_poolings.py +1228 -0
  35. brainstate/nn/_poolings_test.py +231 -0
  36. brainstate/nn/_projection/__init__.py +32 -0
  37. brainstate/nn/_projection/_align_post.py +528 -0
  38. brainstate/nn/_projection/_align_pre.py +599 -0
  39. brainstate/nn/_projection/_delta.py +241 -0
  40. brainstate/nn/_projection/_utils.py +17 -0
  41. brainstate/nn/_projection/_vanilla.py +101 -0
  42. brainstate/nn/_rate_rnns.py +393 -0
  43. brainstate/nn/_readout.py +130 -0
  44. brainstate/nn/_synouts.py +166 -0
  45. brainstate/nn/functional/__init__.py +25 -0
  46. brainstate/nn/functional/_activations.py +754 -0
  47. brainstate/nn/functional/_normalization.py +69 -0
  48. brainstate/nn/functional/_spikes.py +90 -0
  49. brainstate/nn/init/__init__.py +26 -0
  50. brainstate/nn/init/_base.py +36 -0
  51. brainstate/nn/init/_generic.py +175 -0
  52. brainstate/nn/init/_random_inits.py +489 -0
  53. brainstate/nn/init/_regular_inits.py +109 -0
  54. brainstate/nn/surrogate.py +1740 -0
  55. brainstate/optim/__init__.py +23 -0
  56. brainstate/optim/_lr_scheduler.py +486 -0
  57. brainstate/optim/_lr_scheduler_test.py +36 -0
  58. brainstate/optim/_sgd_optimizer.py +1148 -0
  59. brainstate/random.py +5148 -0
  60. brainstate/random_test.py +576 -0
  61. brainstate/surrogate.py +1740 -0
  62. brainstate/transform/__init__.py +36 -0
  63. brainstate/transform/_autograd.py +585 -0
  64. brainstate/transform/_autograd_test.py +1183 -0
  65. brainstate/transform/_control.py +665 -0
  66. brainstate/transform/_controls_test.py +220 -0
  67. brainstate/transform/_jit.py +239 -0
  68. brainstate/transform/_jit_error.py +158 -0
  69. brainstate/transform/_jit_test.py +102 -0
  70. brainstate/transform/_make_jaxpr.py +573 -0
  71. brainstate/transform/_make_jaxpr_test.py +133 -0
  72. brainstate/transform/_progress_bar.py +113 -0
  73. brainstate/typing.py +69 -0
  74. brainstate/util.py +747 -0
  75. brainstate-0.0.1.dist-info/LICENSE +202 -0
  76. brainstate-0.0.1.dist-info/METADATA +101 -0
  77. brainstate-0.0.1.dist-info/RECORD +79 -0
  78. brainstate-0.0.1.dist-info/WHEEL +6 -0
  79. brainstate-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1437 @@
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 __future__ import annotations
19
+
20
+ from typing import Optional
21
+
22
+ import jax.numpy as jnp
23
+ import jax.typing
24
+
25
+ from ._base import ElementWiseBlock
26
+ from .. import math, environ, random, functional as F
27
+ from .._module import Module
28
+ from .._state import ParamState
29
+ from ..mixin import Mode
30
+ from ..typing import ArrayLike
31
+
32
+ __all__ = [
33
+ # activation functions
34
+ 'Threshold', 'ReLU', 'RReLU', 'Hardtanh', 'ReLU6', 'Sigmoid', 'Hardsigmoid',
35
+ 'Tanh', 'SiLU', 'Mish', 'Hardswish', 'ELU', 'CELU', 'SELU', 'GLU', 'GELU',
36
+ 'Hardshrink', 'LeakyReLU', 'LogSigmoid', 'Softplus', 'Softshrink', 'PReLU',
37
+ 'Softsign', 'Tanhshrink', 'Softmin', 'Softmax', 'Softmax2d', 'LogSoftmax',
38
+
39
+ # dropout
40
+ 'Dropout', 'Dropout1d', 'Dropout2d', 'Dropout3d', 'AlphaDropout', 'FeatureAlphaDropout',
41
+
42
+ # others
43
+ 'Identity', 'SpikeBitwise',
44
+ ]
45
+
46
+
47
+ class Threshold(Module, ElementWiseBlock):
48
+ r"""Thresholds each element of the input Tensor.
49
+
50
+ Threshold is defined as:
51
+
52
+ .. math::
53
+ y =
54
+ \begin{cases}
55
+ x, &\text{ if } x > \text{threshold} \\
56
+ \text{value}, &\text{ otherwise }
57
+ \end{cases}
58
+
59
+ Args:
60
+ threshold: The value to threshold at
61
+ value: The value to replace with
62
+
63
+ Shape:
64
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
65
+ - Output: :math:`(*)`, same shape as the input.
66
+
67
+ Examples::
68
+
69
+ >>> import brainstate.nn as nn
70
+ >>> import brainstate as bst
71
+ >>> m = nn.Threshold(0.1, 20)
72
+ >>> x = random.randn(2)
73
+ >>> output = m(x)
74
+ """
75
+ __module__ = 'brainstate.nn'
76
+ threshold: float
77
+ value: float
78
+
79
+ def __init__(self, threshold: float, value: float) -> None:
80
+ super().__init__()
81
+ self.threshold = threshold
82
+ self.value = value
83
+
84
+ def __call__(self, x: ArrayLike) -> ArrayLike:
85
+ dtype = math.get_dtype(x)
86
+ return jnp.where(x > jnp.asarray(self.threshold, dtype=dtype),
87
+ x,
88
+ jnp.asarray(self.value, dtype=dtype))
89
+
90
+ def __repr__(self):
91
+ return f'{self.__class__.__name__}(threshold={self.threshold}, value={self.value})'
92
+
93
+
94
+ class ReLU(Module, ElementWiseBlock):
95
+ r"""Applies the rectified linear unit function element-wise:
96
+
97
+ :math:`\text{ReLU}(x) = (x)^+ = \max(0, x)`
98
+
99
+ Shape:
100
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
101
+ - Output: :math:`(*)`, same shape as the input.
102
+
103
+ Examples::
104
+
105
+ >>> import brainstate.nn as nn
106
+ >>> import brainstate as bst
107
+ >>> m = nn.ReLU()
108
+ >>> x = random.randn(2)
109
+ >>> output = m(x)
110
+
111
+
112
+ An implementation of CReLU - https://arxiv.org/abs/1603.05201
113
+
114
+ >>> import brainstate.nn as nn
115
+ >>> import brainstate as bst
116
+ >>> m = nn.ReLU()
117
+ >>> x = random.randn(2).unsqueeze(0)
118
+ >>> output = jax.numpy.concat((m(x), m(-x)))
119
+ """
120
+ __module__ = 'brainstate.nn'
121
+
122
+ def __call__(self, x: ArrayLike) -> ArrayLike:
123
+ return F.relu(x)
124
+
125
+ def __repr__(self):
126
+ return f'{self.__class__.__name__}()'
127
+
128
+
129
+ class RReLU(Module, ElementWiseBlock):
130
+ r"""Applies the randomized leaky rectified liner unit function, element-wise,
131
+ as described in the paper:
132
+
133
+ `Empirical Evaluation of Rectified Activations in Convolutional Network`_.
134
+
135
+ The function is defined as:
136
+
137
+ .. math::
138
+ \text{RReLU}(x) =
139
+ \begin{cases}
140
+ x & \text{if } x \geq 0 \\
141
+ ax & \text{ otherwise }
142
+ \end{cases}
143
+
144
+ where :math:`a` is randomly sampled from uniform distribution
145
+ :math:`\mathcal{U}(\text{lower}, \text{upper})`.
146
+
147
+ See: https://arxiv.org/pdf/1505.00853.pdf
148
+
149
+ Args:
150
+ lower: lower bound of the uniform distribution. Default: :math:`\frac{1}{8}`
151
+ upper: upper bound of the uniform distribution. Default: :math:`\frac{1}{3}`
152
+
153
+ Shape:
154
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
155
+ - Output: :math:`(*)`, same shape as the input.
156
+
157
+ Examples::
158
+
159
+ >>> import brainstate.nn as nn
160
+ >>> import brainstate as bst
161
+ >>> m = nn.RReLU(0.1, 0.3)
162
+ >>> x = random.randn(2)
163
+ >>> output = m(x)
164
+
165
+ .. _`Empirical Evaluation of Rectified Activations in Convolutional Network`:
166
+ https://arxiv.org/abs/1505.00853
167
+ """
168
+ __module__ = 'brainstate.nn'
169
+ lower: float
170
+ upper: float
171
+
172
+ def __init__(
173
+ self,
174
+ lower: float = 1. / 8,
175
+ upper: float = 1. / 3,
176
+ ):
177
+ super().__init__()
178
+ self.lower = lower
179
+ self.upper = upper
180
+
181
+ def __call__(self, x: ArrayLike) -> ArrayLike:
182
+ return F.rrelu(x, self.lower, self.upper)
183
+
184
+ def extra_repr(self):
185
+ return f'{self.__class__.__name__}(lower={self.lower}, upper={self.upper})'
186
+
187
+
188
+ class Hardtanh(Module, ElementWiseBlock):
189
+ r"""Applies the HardTanh function element-wise.
190
+
191
+ HardTanh is defined as:
192
+
193
+ .. math::
194
+ \text{HardTanh}(x) = \begin{cases}
195
+ \text{max\_val} & \text{ if } x > \text{ max\_val } \\
196
+ \text{min\_val} & \text{ if } x < \text{ min\_val } \\
197
+ x & \text{ otherwise } \\
198
+ \end{cases}
199
+
200
+ Args:
201
+ min_val: minimum value of the linear region range. Default: -1
202
+ max_val: maximum value of the linear region range. Default: 1
203
+
204
+ Keyword arguments :attr:`min_value` and :attr:`max_value`
205
+ have been deprecated in favor of :attr:`min_val` and :attr:`max_val`.
206
+
207
+ Shape:
208
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
209
+ - Output: :math:`(*)`, same shape as the input.
210
+
211
+ Examples::
212
+
213
+ >>> import brainstate.nn as nn
214
+ >>> import brainstate as bst
215
+ >>> m = nn.Hardtanh(-2, 2)
216
+ >>> x = random.randn(2)
217
+ >>> output = m(x)
218
+ """
219
+ __module__ = 'brainstate.nn'
220
+ min_val: float
221
+ max_val: float
222
+
223
+ def __init__(
224
+ self,
225
+ min_val: float = -1.,
226
+ max_val: float = 1.,
227
+ ) -> None:
228
+ super().__init__()
229
+ self.min_val = min_val
230
+ self.max_val = max_val
231
+ assert self.max_val > self.min_val
232
+
233
+ def __call__(self, x: ArrayLike) -> ArrayLike:
234
+ return F.hard_tanh(x, self.min_val, self.max_val)
235
+
236
+ def extra_repr(self) -> str:
237
+ return f'{self.__class__.__name__}(min_val={self.min_val}, max_val={self.max_val})'
238
+
239
+
240
+ class ReLU6(Hardtanh, ElementWiseBlock):
241
+ r"""Applies the element-wise function:
242
+
243
+ .. math::
244
+ \text{ReLU6}(x) = \min(\max(0,x), 6)
245
+
246
+ Shape:
247
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
248
+ - Output: :math:`(*)`, same shape as the input.
249
+
250
+ Examples::
251
+
252
+ >>> import brainstate.nn as nn
253
+ >>> import brainstate as bst
254
+ >>> m = nn.ReLU6()
255
+ >>> x = random.randn(2)
256
+ >>> output = m(x)
257
+ """
258
+ __module__ = 'brainstate.nn'
259
+
260
+ def __init__(self):
261
+ super().__init__(0., 6.)
262
+
263
+
264
+ class Sigmoid(Module, ElementWiseBlock):
265
+ r"""Applies the element-wise function:
266
+
267
+ .. math::
268
+ \text{Sigmoid}(x) = \sigma(x) = \frac{1}{1 + \exp(-x)}
269
+
270
+
271
+ Shape:
272
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
273
+ - Output: :math:`(*)`, same shape as the input.
274
+
275
+ Examples::
276
+
277
+ >>> import brainstate.nn as nn
278
+ >>> import brainstate as bst
279
+ >>> m = nn.Sigmoid()
280
+ >>> x = random.randn(2)
281
+ >>> output = m(x)
282
+ """
283
+ __module__ = 'brainstate.nn'
284
+
285
+ def __call__(self, x: ArrayLike) -> ArrayLike:
286
+ return F.sigmoid(x)
287
+
288
+
289
+ class Hardsigmoid(Module, ElementWiseBlock):
290
+ r"""Applies the Hardsigmoid function element-wise.
291
+
292
+ Hardsigmoid is defined as:
293
+
294
+ .. math::
295
+ \text{Hardsigmoid}(x) = \begin{cases}
296
+ 0 & \text{if~} x \le -3, \\
297
+ 1 & \text{if~} x \ge +3, \\
298
+ x / 6 + 1 / 2 & \text{otherwise}
299
+ \end{cases}
300
+
301
+ Shape:
302
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
303
+ - Output: :math:`(*)`, same shape as the input.
304
+
305
+ Examples::
306
+
307
+ >>> import brainstate.nn as nn
308
+ >>> import brainstate as bst
309
+ >>> m = nn.Hardsigmoid()
310
+ >>> x = random.randn(2)
311
+ >>> output = m(x)
312
+ """
313
+ __module__ = 'brainstate.nn'
314
+
315
+ def __call__(self, x: ArrayLike) -> ArrayLike:
316
+ return F.hard_sigmoid(x)
317
+
318
+
319
+ class Tanh(Module, ElementWiseBlock):
320
+ r"""Applies the Hyperbolic Tangent (Tanh) function element-wise.
321
+
322
+ Tanh is defined as:
323
+
324
+ .. math::
325
+ \text{Tanh}(x) = \tanh(x) = \frac{\exp(x) - \exp(-x)} {\exp(x) + \exp(-x)}
326
+
327
+ Shape:
328
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
329
+ - Output: :math:`(*)`, same shape as the input.
330
+
331
+ Examples::
332
+
333
+ >>> import brainstate.nn as nn
334
+ >>> import brainstate as bst
335
+ >>> m = nn.Tanh()
336
+ >>> x = random.randn(2)
337
+ >>> output = m(x)
338
+ """
339
+ __module__ = 'brainstate.nn'
340
+
341
+ def __call__(self, x: ArrayLike) -> ArrayLike:
342
+ return F.tanh(x)
343
+
344
+
345
+ class SiLU(Module, ElementWiseBlock):
346
+ r"""Applies the Sigmoid Linear Unit (SiLU) function, element-wise.
347
+ The SiLU function is also known as the swish function.
348
+
349
+ .. math::
350
+ \text{silu}(x) = x * \sigma(x), \text{where } \sigma(x) \text{ is the logistic sigmoid.}
351
+
352
+ .. note::
353
+ See `Gaussian Error Linear Units (GELUs) <https://arxiv.org/abs/1606.08415>`_
354
+ where the SiLU (Sigmoid Linear Unit) was originally coined, and see
355
+ `Sigmoid-Weighted Linear Units for Neural Network Function Approximation
356
+ in Reinforcement Learning <https://arxiv.org/abs/1702.03118>`_ and `Swish:
357
+ a Self-Gated Activation Function <https://arxiv.org/abs/1710.05941v1>`_
358
+ where the SiLU was experimented with later.
359
+
360
+ Shape:
361
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
362
+ - Output: :math:`(*)`, same shape as the input.
363
+
364
+ Examples::
365
+
366
+ >>> import brainstate.nn as nn
367
+ >>> m = nn.SiLU()
368
+ >>> x = random.randn(2)
369
+ >>> output = m(x)
370
+ """
371
+ __module__ = 'brainstate.nn'
372
+
373
+ def __call__(self, x: ArrayLike) -> ArrayLike:
374
+ return F.silu(x)
375
+
376
+
377
+ class Mish(Module, ElementWiseBlock):
378
+ r"""Applies the Mish function, element-wise.
379
+ Mish: A Self Regularized Non-Monotonic Neural Activation Function.
380
+
381
+ .. math::
382
+ \text{Mish}(x) = x * \text{Tanh}(\text{Softplus}(x))
383
+
384
+ .. note::
385
+ See `Mish: A Self Regularized Non-Monotonic Neural Activation Function <https://arxiv.org/abs/1908.08681>`_
386
+
387
+
388
+ Shape:
389
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
390
+ - Output: :math:`(*)`, same shape as the input.
391
+
392
+ Examples::
393
+
394
+ >>> import brainstate.nn as nn
395
+ >>> import brainstate as bst
396
+ >>> m = nn.Mish()
397
+ >>> x = random.randn(2)
398
+ >>> output = m(x)
399
+ """
400
+ __module__ = 'brainstate.nn'
401
+
402
+ def __call__(self, x: ArrayLike) -> ArrayLike:
403
+ return F.mish(x)
404
+
405
+
406
+ class Hardswish(Module, ElementWiseBlock):
407
+ r"""Applies the Hardswish function, element-wise, as described in the paper:
408
+ `Searching for MobileNetV3 <https://arxiv.org/abs/1905.02244>`_.
409
+
410
+ Hardswish is defined as:
411
+
412
+ .. math::
413
+ \text{Hardswish}(x) = \begin{cases}
414
+ 0 & \text{if~} x \le -3, \\
415
+ x & \text{if~} x \ge +3, \\
416
+ x \cdot (x + 3) /6 & \text{otherwise}
417
+ \end{cases}
418
+
419
+
420
+ Shape:
421
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
422
+ - Output: :math:`(*)`, same shape as the input.
423
+
424
+ Examples::
425
+
426
+ >>> import brainstate.nn as nn
427
+ >>> import brainstate as bst
428
+ >>> m = nn.Hardswish()
429
+ >>> x = random.randn(2)
430
+ >>> output = m(x)
431
+ """
432
+ __module__ = 'brainstate.nn'
433
+
434
+ def __call__(self, x: ArrayLike) -> ArrayLike:
435
+ return F.hard_swish(x)
436
+
437
+
438
+ class ELU(Module, ElementWiseBlock):
439
+ r"""Applies the Exponential Linear Unit (ELU) function, element-wise, as described
440
+ in the paper: `Fast and Accurate Deep Network Learning by Exponential Linear
441
+ Units (ELUs) <https://arxiv.org/abs/1511.07289>`__.
442
+
443
+ ELU is defined as:
444
+
445
+ .. math::
446
+ \text{ELU}(x) = \begin{cases}
447
+ x, & \text{ if } x > 0\\
448
+ \alpha * (\exp(x) - 1), & \text{ if } x \leq 0
449
+ \end{cases}
450
+
451
+ Args:
452
+ alpha: the :math:`\alpha` value for the ELU formulation. Default: 1.0
453
+
454
+ Shape:
455
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
456
+ - Output: :math:`(*)`, same shape as the input.
457
+
458
+ Examples::
459
+
460
+ >>> import brainstate.nn as nn
461
+ >>> import brainstate as bst
462
+ >>> m = nn.ELU()
463
+ >>> x = random.randn(2)
464
+ >>> output = m(x)
465
+ """
466
+ __module__ = 'brainstate.nn'
467
+ alpha: float
468
+
469
+ def __init__(self, alpha: float = 1.) -> None:
470
+ super().__init__()
471
+ self.alpha = alpha
472
+
473
+ def __call__(self, x: ArrayLike) -> ArrayLike:
474
+ return F.elu(x, self.alpha)
475
+
476
+ def extra_repr(self) -> str:
477
+ return f'{self.__class__.__name__}(alpha={self.alpha})'
478
+
479
+
480
+ class CELU(Module, ElementWiseBlock):
481
+ r"""Applies the element-wise function:
482
+
483
+ .. math::
484
+ \text{CELU}(x) = \max(0,x) + \min(0, \alpha * (\exp(x/\alpha) - 1))
485
+
486
+ More details can be found in the paper `Continuously Differentiable Exponential Linear Units`_ .
487
+
488
+ Args:
489
+ alpha: the :math:`\alpha` value for the CELU formulation. Default: 1.0
490
+
491
+ Shape:
492
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
493
+ - Output: :math:`(*)`, same shape as the input.
494
+
495
+ Examples::
496
+
497
+ >>> import brainstate.nn as nn
498
+ >>> import brainstate as bst
499
+ >>> m = nn.CELU()
500
+ >>> x = random.randn(2)
501
+ >>> output = m(x)
502
+
503
+ .. _`Continuously Differentiable Exponential Linear Units`:
504
+ https://arxiv.org/abs/1704.07483
505
+ """
506
+ __module__ = 'brainstate.nn'
507
+ alpha: float
508
+
509
+ def __init__(self, alpha: float = 1.) -> None:
510
+ super().__init__()
511
+ self.alpha = alpha
512
+
513
+ def __call__(self, x: ArrayLike) -> ArrayLike:
514
+ return F.celu(x, self.alpha)
515
+
516
+ def extra_repr(self) -> str:
517
+ return f'{self.__class__.__name__}(alpha={self.alpha})'
518
+
519
+
520
+ class SELU(Module, ElementWiseBlock):
521
+ r"""Applied element-wise, as:
522
+
523
+ .. math::
524
+ \text{SELU}(x) = \text{scale} * (\max(0,x) + \min(0, \alpha * (\exp(x) - 1)))
525
+
526
+ with :math:`\alpha = 1.6732632423543772848170429916717` and
527
+ :math:`\text{scale} = 1.0507009873554804934193349852946`.
528
+
529
+ More details can be found in the paper `Self-Normalizing Neural Networks`_ .
530
+
531
+
532
+ Shape:
533
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
534
+ - Output: :math:`(*)`, same shape as the input.
535
+
536
+ Examples::
537
+
538
+ >>> import brainstate.nn as nn
539
+ >>> import brainstate as bst
540
+ >>> m = nn.SELU()
541
+ >>> x = random.randn(2)
542
+ >>> output = m(x)
543
+
544
+ .. _Self-Normalizing Neural Networks: https://arxiv.org/abs/1706.02515
545
+ """
546
+ __module__ = 'brainstate.nn'
547
+
548
+ def __call__(self, x: ArrayLike) -> ArrayLike:
549
+ return F.selu(x)
550
+
551
+
552
+ class GLU(Module, ElementWiseBlock):
553
+ r"""Applies the gated linear unit function
554
+ :math:`{GLU}(a, b)= a \otimes \sigma(b)` where :math:`a` is the first half
555
+ of the input matrices and :math:`b` is the second half.
556
+
557
+ Args:
558
+ dim (int): the dimension on which to split the input. Default: -1
559
+
560
+ Shape:
561
+ - Input: :math:`(\ast_1, N, \ast_2)` where `*` means, any number of additional
562
+ dimensions
563
+ - Output: :math:`(\ast_1, M, \ast_2)` where :math:`M=N/2`
564
+
565
+ Examples::
566
+
567
+ >>> import brainstate.nn as nn
568
+ >>> import brainstate as bst
569
+ >>> m = nn.GLU()
570
+ >>> x = random.randn(4, 2)
571
+ >>> output = m(x)
572
+ """
573
+ __module__ = 'brainstate.nn'
574
+ dim: int
575
+
576
+ def __init__(self, dim: int = -1) -> None:
577
+ super().__init__()
578
+ self.dim = dim
579
+
580
+ def __call__(self, x: ArrayLike) -> ArrayLike:
581
+ return F.glu(x, self.dim)
582
+
583
+ def __repr__(self):
584
+ return f'{self.__class__.__name__}(dim={self.dim})'
585
+
586
+
587
+ class GELU(Module, ElementWiseBlock):
588
+ r"""Applies the Gaussian Error Linear Units function:
589
+
590
+ .. math:: \text{GELU}(x) = x * \Phi(x)
591
+
592
+ where :math:`\Phi(x)` is the Cumulative Distribution Function for Gaussian Distribution.
593
+
594
+ When the approximate argument is 'tanh', Gelu is estimated with:
595
+
596
+ .. math:: \text{GELU}(x) = 0.5 * x * (1 + \text{Tanh}(\sqrt(2 / \pi) * (x + 0.044715 * x^3)))
597
+
598
+ Args:
599
+ approximate (str, optional): the gelu approximation algorithm to use:
600
+ ``'none'`` | ``'tanh'``. Default: ``'none'``
601
+
602
+ Shape:
603
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
604
+ - Output: :math:`(*)`, same shape as the input.
605
+
606
+ Examples::
607
+
608
+ >>> import brainstate.nn as nn
609
+ >>> import brainstate as bst
610
+ >>> m = nn.GELU()
611
+ >>> x = random.randn(2)
612
+ >>> output = m(x)
613
+ """
614
+ __module__ = 'brainstate.nn'
615
+ approximate: bool
616
+
617
+ def __init__(self, approximate: bool = False) -> None:
618
+ super().__init__()
619
+ self.approximate = approximate
620
+
621
+ def __call__(self, x: ArrayLike) -> ArrayLike:
622
+ return F.gelu(x, approximate=self.approximate)
623
+
624
+ def __repr__(self):
625
+ return f'{self.__class__.__name__}(approximate={self.approximate})'
626
+
627
+
628
+ class Hardshrink(Module, ElementWiseBlock):
629
+ r"""Applies the Hard Shrinkage (Hardshrink) function element-wise.
630
+
631
+ Hardshrink is defined as:
632
+
633
+ .. math::
634
+ \text{HardShrink}(x) =
635
+ \begin{cases}
636
+ x, & \text{ if } x > \lambda \\
637
+ x, & \text{ if } x < -\lambda \\
638
+ 0, & \text{ otherwise }
639
+ \end{cases}
640
+
641
+ Args:
642
+ lambd: the :math:`\lambda` value for the Hardshrink formulation. Default: 0.5
643
+
644
+ Shape:
645
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
646
+ - Output: :math:`(*)`, same shape as the input.
647
+
648
+ Examples::
649
+
650
+ >>> import brainstate.nn as nn
651
+ >>> import brainstate as bst
652
+ >>> m = nn.Hardshrink()
653
+ >>> x = random.randn(2)
654
+ >>> output = m(x)
655
+ """
656
+ __module__ = 'brainstate.nn'
657
+ lambd: float
658
+
659
+ def __init__(self, lambd: float = 0.5) -> None:
660
+ super().__init__()
661
+ self.lambd = lambd
662
+
663
+ def __call__(self, x: ArrayLike) -> ArrayLike:
664
+ return F.hard_shrink(x, self.lambd)
665
+
666
+ def __repr__(self):
667
+ return f'{self.__class__.__name__}(lambd={self.lambd})'
668
+
669
+
670
+ class LeakyReLU(Module, ElementWiseBlock):
671
+ r"""Applies the element-wise function:
672
+
673
+ .. math::
674
+ \text{LeakyReLU}(x) = \max(0, x) + \text{negative\_slope} * \min(0, x)
675
+
676
+
677
+ or
678
+
679
+ .. math::
680
+ \text{LeakyReLU}(x) =
681
+ \begin{cases}
682
+ x, & \text{ if } x \geq 0 \\
683
+ \text{negative\_slope} \times x, & \text{ otherwise }
684
+ \end{cases}
685
+
686
+ Args:
687
+ negative_slope: Controls the angle of the negative slope (which is used for
688
+ negative input values). Default: 1e-2
689
+
690
+ Shape:
691
+ - Input: :math:`(*)` where `*` means, any number of additional
692
+ dimensions
693
+ - Output: :math:`(*)`, same shape as the input
694
+
695
+ Examples::
696
+
697
+ >>> import brainstate.nn as nn
698
+ >>> import brainstate as bst
699
+ >>> m = nn.LeakyReLU(0.1)
700
+ >>> x = random.randn(2)
701
+ >>> output = m(x)
702
+ """
703
+ __module__ = 'brainstate.nn'
704
+ negative_slope: float
705
+
706
+ def __init__(self, negative_slope: float = 1e-2) -> None:
707
+ super().__init__()
708
+ self.negative_slope = negative_slope
709
+
710
+ def __call__(self, x: ArrayLike) -> ArrayLike:
711
+ return F.leaky_relu(x, self.negative_slope)
712
+
713
+ def __repr__(self):
714
+ return f'{self.__class__.__name__}(negative_slope={self.negative_slope})'
715
+
716
+
717
+ class LogSigmoid(Module, ElementWiseBlock):
718
+ r"""Applies the element-wise function:
719
+
720
+ .. math::
721
+ \text{LogSigmoid}(x) = \log\left(\frac{ 1 }{ 1 + \exp(-x)}\right)
722
+
723
+ Shape:
724
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
725
+ - Output: :math:`(*)`, same shape as the input.
726
+
727
+ Examples::
728
+
729
+ >>> import brainstate.nn as nn
730
+ >>> import brainstate as bst
731
+ >>> m = nn.LogSigmoid()
732
+ >>> x = random.randn(2)
733
+ >>> output = m(x)
734
+ """
735
+ __module__ = 'brainstate.nn'
736
+
737
+ def __call__(self, x: ArrayLike) -> ArrayLike:
738
+ return F.log_sigmoid(x)
739
+
740
+
741
+ class Softplus(Module, ElementWiseBlock):
742
+ r"""Applies the Softplus function :math:`\text{Softplus}(x) = \frac{1}{\beta} *
743
+ \log(1 + \exp(\beta * x))` element-wise.
744
+
745
+ SoftPlus is a smooth approximation to the ReLU function and can be used
746
+ to constrain the output of a machine to always be positive.
747
+
748
+ For numerical stability the implementation reverts to the linear function
749
+ when :math:`input \times \beta > threshold`.
750
+
751
+ Args:
752
+ beta: the :math:`\beta` value for the Softplus formulation. Default: 1
753
+ threshold: values above this revert to a linear function. Default: 20
754
+
755
+ Shape:
756
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
757
+ - Output: :math:`(*)`, same shape as the input.
758
+
759
+ Examples::
760
+
761
+ >>> import brainstate.nn as nn
762
+ >>> import brainstate as bst
763
+ >>> m = nn.Softplus()
764
+ >>> x = random.randn(2)
765
+ >>> output = m(x)
766
+ """
767
+ __module__ = 'brainstate.nn'
768
+ beta: float
769
+ threshold: float
770
+
771
+ def __init__(self, beta: float = 1, threshold: float = 20.) -> None:
772
+ super().__init__()
773
+ self.beta = beta
774
+ self.threshold = threshold
775
+
776
+ def __call__(self, x: ArrayLike) -> ArrayLike:
777
+ return F.softplus(x, self.beta, self.threshold)
778
+
779
+ def __repr__(self):
780
+ return f'{self.__class__.__name__}(beta={self.beta}, threshold={self.threshold})'
781
+
782
+
783
+ class Softshrink(Module, ElementWiseBlock):
784
+ r"""Applies the soft shrinkage function elementwise:
785
+
786
+ .. math::
787
+ \text{SoftShrinkage}(x) =
788
+ \begin{cases}
789
+ x - \lambda, & \text{ if } x > \lambda \\
790
+ x + \lambda, & \text{ if } x < -\lambda \\
791
+ 0, & \text{ otherwise }
792
+ \end{cases}
793
+
794
+ Args:
795
+ lambd: the :math:`\lambda` (must be no less than zero) value for the Softshrink formulation. Default: 0.5
796
+
797
+ Shape:
798
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
799
+ - Output: :math:`(*)`, same shape as the input.
800
+
801
+ Examples::
802
+
803
+ >>> import brainstate.nn as nn
804
+ >>> import brainstate as bst
805
+ >>> m = nn.Softshrink()
806
+ >>> x = random.randn(2)
807
+ >>> output = m(x)
808
+ """
809
+ __module__ = 'brainstate.nn'
810
+ lambd: float
811
+
812
+ def __init__(self, lambd: float = 0.5) -> None:
813
+ super().__init__()
814
+ self.lambd = lambd
815
+
816
+ def __call__(self, x: ArrayLike) -> ArrayLike:
817
+ return F.soft_shrink(x, self.lambd)
818
+
819
+ def __repr__(self):
820
+ return f'{self.__class__.__name__}(lambd={self.lambd})'
821
+
822
+
823
+ class PReLU(Module, ElementWiseBlock):
824
+ r"""Applies the element-wise function:
825
+
826
+ .. math::
827
+ \text{PReLU}(x) = \max(0,x) + a * \min(0,x)
828
+
829
+ or
830
+
831
+ .. math::
832
+ \text{PReLU}(x) =
833
+ \begin{cases}
834
+ x, & \text{ if } x \geq 0 \\
835
+ ax, & \text{ otherwise }
836
+ \end{cases}
837
+
838
+ Here :math:`a` is a learnable parameter. When called without arguments, `nn.PReLU()` uses a single
839
+ parameter :math:`a` across all input channels. If called with `nn.PReLU(nChannels)`,
840
+ a separate :math:`a` is used for each input channel.
841
+
842
+
843
+ .. note::
844
+ weight decay should not be used when learning :math:`a` for good performance.
845
+
846
+ .. note::
847
+ Channel dim is the 2nd dim of input. When input has dims < 2, then there is
848
+ no channel dim and the number of channels = 1.
849
+
850
+ Args:
851
+ num_parameters (int): number of :math:`a` to learn.
852
+ Although it takes an int as input, there is only two values are legitimate:
853
+ 1, or the number of channels at input. Default: 1
854
+ init (float): the initial value of :math:`a`. Default: 0.25
855
+
856
+ Shape:
857
+ - Input: :math:`( *)` where `*` means, any number of additional
858
+ dimensions.
859
+ - Output: :math:`(*)`, same shape as the input.
860
+
861
+ Attributes:
862
+ weight (Tensor): the learnable weights of shape (:attr:`num_parameters`).
863
+
864
+ Examples::
865
+
866
+ >>> import brainstate as bst
867
+ >>> m = bst.nn.PReLU()
868
+ >>> x = bst.random.randn(2)
869
+ >>> output = m(x)
870
+ """
871
+ __module__ = 'brainstate.nn'
872
+ num_parameters: int
873
+
874
+ def __init__(self, num_parameters: int = 1, init: float = 0.25, dtype=None) -> None:
875
+ super().__init__()
876
+ self.num_parameters = num_parameters
877
+ self.weight = ParamState(jnp.ones(num_parameters, dtype=dtype) * init)
878
+
879
+ def __call__(self, x: ArrayLike) -> ArrayLike:
880
+ return F.prelu(x, self.weight.value)
881
+
882
+ def __repr__(self):
883
+ return f'{self.__class__.__name__}(num_parameters={self.num_parameters})'
884
+
885
+
886
+ class Softsign(Module, ElementWiseBlock):
887
+ r"""Applies the element-wise function:
888
+
889
+ .. math::
890
+ \text{SoftSign}(x) = \frac{x}{ 1 + |x|}
891
+
892
+ Shape:
893
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
894
+ - Output: :math:`(*)`, same shape as the input.
895
+
896
+ Examples::
897
+
898
+ >>> import brainstate.nn as nn
899
+ >>> import brainstate as bst
900
+ >>> m = nn.Softsign()
901
+ >>> x = random.randn(2)
902
+ >>> output = m(x)
903
+ """
904
+ __module__ = 'brainstate.nn'
905
+
906
+ def __call__(self, x: ArrayLike) -> ArrayLike:
907
+ return F.soft_sign(x)
908
+
909
+
910
+ class Tanhshrink(Module, ElementWiseBlock):
911
+ r"""Applies the element-wise function:
912
+
913
+ .. math::
914
+ \text{Tanhshrink}(x) = x - \tanh(x)
915
+
916
+ Shape:
917
+ - Input: :math:`(*)`, where :math:`*` means any number of dimensions.
918
+ - Output: :math:`(*)`, same shape as the input.
919
+
920
+ Examples::
921
+
922
+ >>> import brainstate.nn as nn
923
+ >>> import brainstate as bst
924
+ >>> m = nn.Tanhshrink()
925
+ >>> x = random.randn(2)
926
+ >>> output = m(x)
927
+ """
928
+ __module__ = 'brainstate.nn'
929
+
930
+ def __call__(self, x: ArrayLike) -> ArrayLike:
931
+ return F.tanh_shrink(x)
932
+
933
+
934
+ class Softmin(Module, ElementWiseBlock):
935
+ r"""Applies the Softmin function to an n-dimensional input Tensor
936
+ rescaling them so that the elements of the n-dimensional output Tensor
937
+ lie in the range `[0, 1]` and sum to 1.
938
+
939
+ Softmin is defined as:
940
+
941
+ .. math::
942
+ \text{Softmin}(x_{i}) = \frac{\exp(-x_i)}{\sum_j \exp(-x_j)}
943
+
944
+ Shape:
945
+ - Input: :math:`(*)` where `*` means, any number of additional
946
+ dimensions
947
+ - Output: :math:`(*)`, same shape as the input
948
+
949
+ Args:
950
+ dim (int): A dimension along which Softmin will be computed (so every slice
951
+ along dim will sum to 1).
952
+
953
+ Returns:
954
+ a Tensor of the same dimension and shape as the input, with
955
+ values in the range [0, 1]
956
+
957
+ Examples::
958
+
959
+ >>> import brainstate.nn as nn
960
+ >>> import brainstate as bst
961
+ >>> m = nn.Softmin(dim=1)
962
+ >>> x = random.randn(2, 3)
963
+ >>> output = m(x)
964
+ """
965
+ __module__ = 'brainstate.nn'
966
+ dim: Optional[int]
967
+
968
+ def __init__(self, dim: Optional[int] = None) -> None:
969
+ super().__init__()
970
+ self.dim = dim
971
+
972
+ def __call__(self, x: ArrayLike) -> ArrayLike:
973
+ return F.softmin(x, self.dim)
974
+
975
+ def __repr__(self):
976
+ return f'{self.__class__.__name__}(dim={self.dim})'
977
+
978
+
979
+ class Softmax(Module, ElementWiseBlock):
980
+ r"""Applies the Softmax function to an n-dimensional input Tensor
981
+ rescaling them so that the elements of the n-dimensional output Tensor
982
+ lie in the range [0,1] and sum to 1.
983
+
984
+ Softmax is defined as:
985
+
986
+ .. math::
987
+ \text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}
988
+
989
+ When the input Tensor is a sparse tensor then the unspecified
990
+ values are treated as ``-inf``.
991
+
992
+ Shape:
993
+ - Input: :math:`(*)` where `*` means, any number of additional
994
+ dimensions
995
+ - Output: :math:`(*)`, same shape as the input
996
+
997
+ Returns:
998
+ a Tensor of the same dimension and shape as the input with
999
+ values in the range [0, 1]
1000
+
1001
+ Args:
1002
+ dim (int): A dimension along which Softmax will be computed (so every slice
1003
+ along dim will sum to 1).
1004
+
1005
+ .. note::
1006
+ This module doesn't work directly with NLLLoss,
1007
+ which expects the Log to be computed between the Softmax and itself.
1008
+ Use `LogSoftmax` instead (it's faster and has better numerical properties).
1009
+
1010
+ Examples::
1011
+
1012
+ >>> import brainstate.nn as nn
1013
+ >>> import brainstate as bst
1014
+ >>> m = nn.Softmax(dim=1)
1015
+ >>> x = random.randn(2, 3)
1016
+ >>> output = m(x)
1017
+
1018
+ """
1019
+ __module__ = 'brainstate.nn'
1020
+ dim: Optional[int]
1021
+
1022
+ def __init__(self, dim: Optional[int] = None) -> None:
1023
+ super().__init__()
1024
+ self.dim = dim
1025
+
1026
+ def __call__(self, x: ArrayLike) -> ArrayLike:
1027
+ return F.softmax(x, self.dim)
1028
+
1029
+ def __repr__(self):
1030
+ return f'{self.__class__.__name__}(dim={self.dim})'
1031
+
1032
+
1033
+ class Softmax2d(Module, ElementWiseBlock):
1034
+ r"""Applies SoftMax over features to each spatial location.
1035
+
1036
+ When given an image of ``Channels x Height x Width``, it will
1037
+ apply `Softmax` to each location :math:`(Channels, h_i, w_j)`
1038
+
1039
+ Shape:
1040
+ - Input: :math:`(N, C, H, W)` or :math:`(C, H, W)`.
1041
+ - Output: :math:`(N, C, H, W)` or :math:`(C, H, W)` (same shape as input)
1042
+
1043
+ Returns:
1044
+ a Tensor of the same dimension and shape as the input with
1045
+ values in the range [0, 1]
1046
+
1047
+ Examples::
1048
+
1049
+ >>> import brainstate.nn as nn
1050
+ >>> import brainstate as bst
1051
+ >>> m = nn.Softmax2d()
1052
+ >>> # you softmax over the 2nd dimension
1053
+ >>> x = random.randn(2, 3, 12, 13)
1054
+ >>> output = m(x)
1055
+ """
1056
+ __module__ = 'brainstate.nn'
1057
+
1058
+ def __call__(self, x: ArrayLike) -> ArrayLike:
1059
+ assert x.ndim == 4 or x.ndim == 3, 'Softmax2d requires a 3D or 4D tensor as input'
1060
+ return F.softmax(x, -3)
1061
+
1062
+
1063
+ class LogSoftmax(Module, ElementWiseBlock):
1064
+ r"""Applies the :math:`\log(\text{Softmax}(x))` function to an n-dimensional
1065
+ input Tensor. The LogSoftmax formulation can be simplified as:
1066
+
1067
+ .. math::
1068
+ \text{LogSoftmax}(x_{i}) = \log\left(\frac{\exp(x_i) }{ \sum_j \exp(x_j)} \right)
1069
+
1070
+ Shape:
1071
+ - Input: :math:`(*)` where `*` means, any number of additional
1072
+ dimensions
1073
+ - Output: :math:`(*)`, same shape as the input
1074
+
1075
+ Args:
1076
+ dim (int): A dimension along which LogSoftmax will be computed.
1077
+
1078
+ Returns:
1079
+ a Tensor of the same dimension and shape as the input with
1080
+ values in the range [-inf, 0)
1081
+
1082
+ Examples::
1083
+
1084
+ >>> import brainstate.nn as nn
1085
+ >>> import brainstate as bst
1086
+ >>> m = nn.LogSoftmax(dim=1)
1087
+ >>> x = random.randn(2, 3)
1088
+ >>> output = m(x)
1089
+ """
1090
+ __module__ = 'brainstate.nn'
1091
+ dim: Optional[int]
1092
+
1093
+ def __init__(self, dim: Optional[int] = None) -> None:
1094
+ super().__init__()
1095
+ self.dim = dim
1096
+
1097
+ def __call__(self, x: ArrayLike) -> ArrayLike:
1098
+ return F.log_softmax(x, self.dim)
1099
+
1100
+ def __repr__(self):
1101
+ return f'{self.__class__.__name__}(dim={self.dim})'
1102
+
1103
+
1104
+ class Identity(Module, ElementWiseBlock):
1105
+ r"""A placeholder identity operator that is argument-insensitive.
1106
+ """
1107
+ __module__ = 'brainstate.nn'
1108
+
1109
+ def __call__(self, x):
1110
+ return x
1111
+
1112
+
1113
+ class Dropout(Module, ElementWiseBlock):
1114
+ """A layer that stochastically ignores a subset of inputs each training step.
1115
+
1116
+ In training, to compensate for the fraction of input values dropped (`rate`),
1117
+ all surviving values are multiplied by `1 / (1 - rate)`.
1118
+
1119
+ This layer is active only during training (``mode=brainstate.mixin.Training``). In other
1120
+ circumstances it is a no-op.
1121
+
1122
+ .. [1] Srivastava, Nitish, et al. "Dropout: a simple way to prevent
1123
+ neural networks from overfitting." The journal of machine learning
1124
+ research 15.1 (2014): 1929-1958.
1125
+
1126
+ Args:
1127
+ prob: Probability to keep element of the tensor.
1128
+ mode: Mode. The computation mode of the object.
1129
+ name: str. The name of the dynamic system.
1130
+
1131
+ """
1132
+ __module__ = 'brainstate.nn'
1133
+
1134
+ def __init__(
1135
+ self,
1136
+ prob: float = 0.5,
1137
+ mode: Optional[Mode] = None,
1138
+ name: Optional[str] = None
1139
+ ) -> None:
1140
+ super().__init__(mode=mode, name=name)
1141
+ assert 0. <= prob < 1., f"Dropout probability must be in the range [0, 1). But got {prob}."
1142
+ self.prob = prob
1143
+
1144
+ def __call__(self, x):
1145
+ dtype = math.get_dtype(x)
1146
+ fit_phase = environ.get('fit', desc='Whether this is a fitting process. Bool.')
1147
+ if fit_phase:
1148
+ keep_mask = random.bernoulli(self.prob, x.shape)
1149
+ return jnp.where(keep_mask,
1150
+ jnp.asarray(x / self.prob, dtype=dtype),
1151
+ jnp.asarray(0., dtype=dtype))
1152
+ else:
1153
+ return x
1154
+
1155
+
1156
+ class _DropoutNd(Module, ElementWiseBlock):
1157
+ __module__ = 'brainstate.nn'
1158
+ prob: float
1159
+ channel_axis: int
1160
+ minimal_dim: int
1161
+
1162
+ def __init__(
1163
+ self,
1164
+ prob: float = 0.5,
1165
+ channel_axis: int = -1,
1166
+ mode: Optional[Mode] = None,
1167
+ name: Optional[str] = None
1168
+ ) -> None:
1169
+ super().__init__(mode=mode, name=name)
1170
+ assert 0. <= prob < 1., f"Dropout probability must be in the range [0, 1). But got {prob}."
1171
+ self.prob = prob
1172
+ self.channel_axis = channel_axis
1173
+
1174
+ def __call__(self, x):
1175
+ dtype = math.get_dtype(x)
1176
+ # get fit phase
1177
+ fit_phase = environ.get('fit', desc='Whether this is a fitting process. Bool.')
1178
+
1179
+ # check input shape
1180
+ if self.mode.is_nonbatch_mode():
1181
+ assert x.ndim == self.minimal_dim, f"Input tensor must be {self.minimal_dim}D. But got {x.ndim}D."
1182
+ channel_axis = self.channel_axis if self.channel_axis >= 0 else (x.ndim + self.channel_axis)
1183
+ mask_shape = [(dim if i == channel_axis else 1) for i, dim in enumerate(x.shape)]
1184
+ else:
1185
+ channel_axis = (self.channel_axis + 1) if self.channel_axis >= 0 else (x.ndim + self.channel_axis)
1186
+ assert channel_axis != 0, f"Channel axis must not be 0. But got {self.channel_axis}."
1187
+ mask_shape = [(dim if i in (channel_axis, 0) else 1) for i, dim in enumerate(x.shape)]
1188
+
1189
+ # generate mask
1190
+ if fit_phase:
1191
+ keep_mask = jnp.broadcast_to(random.bernoulli(self.prob, mask_shape), x.shape)
1192
+ return jnp.where(keep_mask,
1193
+ jnp.asarray(x / self.prob, dtype=dtype),
1194
+ jnp.asarray(0., dtype=dtype))
1195
+ else:
1196
+ return x
1197
+
1198
+ def __repr__(self) -> str:
1199
+ return f'{self.__class__.__name__}(prob={self.prob}, channel_axis={self.channel_axis})'
1200
+
1201
+
1202
+ class Dropout1d(_DropoutNd):
1203
+ r"""Randomly zero out entire channels (a channel is a 1D feature map,
1204
+ e.g., the :math:`j`-th channel of the :math:`i`-th sample in the
1205
+ batched input is a 1D tensor :math:`\text{input}[i, j]`).
1206
+ Each channel will be zeroed out independently on every forward call with
1207
+ probability :attr:`p` using samples from a Bernoulli distribution.
1208
+
1209
+ Usually the input comes from :class:`nn.Conv1d` modules.
1210
+
1211
+ As described in the paper
1212
+ `Efficient Object Localization Using Convolutional Networks`_ ,
1213
+ if adjacent pixels within feature maps are strongly correlated
1214
+ (as is normally the case in early convolution layers) then i.i.d. dropout
1215
+ will not regularize the activations and will otherwise just result
1216
+ in an effective learning rate decrease.
1217
+
1218
+ In this case, :func:`nn.Dropout1d` will help promote independence between
1219
+ feature maps and should be used instead.
1220
+
1221
+ Args:
1222
+ prob: float. probability of an element to be zero-ed.
1223
+
1224
+ Shape:
1225
+ - Input: :math:`(N, C, L)` or :math:`(C, L)`.
1226
+ - Output: :math:`(N, C, L)` or :math:`(C, L)` (same shape as input).
1227
+
1228
+ Examples::
1229
+
1230
+ >>> m = Dropout1d(p=0.2)
1231
+ >>> x = random.randn(20, 32, 16)
1232
+ >>> output = m(x)
1233
+ >>> output.shape
1234
+ (20, 32, 16)
1235
+
1236
+ .. _Efficient Object Localization Using Convolutional Networks:
1237
+ https://arxiv.org/abs/1411.4280
1238
+ """
1239
+ __module__ = 'brainstate.nn'
1240
+ minimal_dim: int = 2
1241
+
1242
+
1243
+ class Dropout2d(_DropoutNd):
1244
+ r"""Randomly zero out entire channels (a channel is a 2D feature map,
1245
+ e.g., the :math:`j`-th channel of the :math:`i`-th sample in the
1246
+ batched input is a 2D tensor :math:`\text{input}[i, j]`).
1247
+ Each channel will be zeroed out independently on every forward call with
1248
+ probability :attr:`p` using samples from a Bernoulli distribution.
1249
+
1250
+ Usually the input comes from :class:`nn.Conv2d` modules.
1251
+
1252
+ As described in the paper
1253
+ `Efficient Object Localization Using Convolutional Networks`_ ,
1254
+ if adjacent pixels within feature maps are strongly correlated
1255
+ (as is normally the case in early convolution layers) then i.i.d. dropout
1256
+ will not regularize the activations and will otherwise just result
1257
+ in an effective learning rate decrease.
1258
+
1259
+ In this case, :func:`nn.Dropout2d` will help promote independence between
1260
+ feature maps and should be used instead.
1261
+
1262
+ Args:
1263
+ prob: float. probability of an element to be kept.
1264
+
1265
+ Shape:
1266
+ - Input: :math:`(N, C, H, W)` or :math:`(N, C, L)`.
1267
+ - Output: :math:`(N, C, H, W)` or :math:`(N, C, L)` (same shape as input).
1268
+
1269
+ Examples::
1270
+
1271
+ >>> m = Dropout2d(p=0.2)
1272
+ >>> x = random.randn(20, 32, 32, 16)
1273
+ >>> output = m(x)
1274
+
1275
+ .. _Efficient Object Localization Using Convolutional Networks:
1276
+ https://arxiv.org/abs/1411.4280
1277
+ """
1278
+ __module__ = 'brainstate.nn'
1279
+ minimal_dim: int = 3
1280
+
1281
+
1282
+ class Dropout3d(_DropoutNd):
1283
+ r"""Randomly zero out entire channels (a channel is a 3D feature map,
1284
+ e.g., the :math:`j`-th channel of the :math:`i`-th sample in the
1285
+ batched input is a 3D tensor :math:`\text{input}[i, j]`).
1286
+ Each channel will be zeroed out independently on every forward call with
1287
+ probability :attr:`p` using samples from a Bernoulli distribution.
1288
+
1289
+ Usually the input comes from :class:`nn.Conv3d` modules.
1290
+
1291
+ As described in the paper
1292
+ `Efficient Object Localization Using Convolutional Networks`_ ,
1293
+ if adjacent pixels within feature maps are strongly correlated
1294
+ (as is normally the case in early convolution layers) then i.i.d. dropout
1295
+ will not regularize the activations and will otherwise just result
1296
+ in an effective learning rate decrease.
1297
+
1298
+ In this case, :func:`nn.Dropout3d` will help promote independence between
1299
+ feature maps and should be used instead.
1300
+
1301
+ Args:
1302
+ prob: float. probability of an element to be kept.
1303
+
1304
+ Shape:
1305
+ - Input: :math:`(N, C, D, H, W)` or :math:`(C, D, H, W)`.
1306
+ - Output: :math:`(N, C, D, H, W)` or :math:`(C, D, H, W)` (same shape as input).
1307
+
1308
+ Examples::
1309
+
1310
+ >>> m = Dropout3d(p=0.2)
1311
+ >>> x = random.randn(20, 16, 4, 32, 32)
1312
+ >>> output = m(x)
1313
+
1314
+ .. _Efficient Object Localization Using Convolutional Networks:
1315
+ https://arxiv.org/abs/1411.4280
1316
+ """
1317
+ __module__ = 'brainstate.nn'
1318
+ minimal_dim: int = 4
1319
+
1320
+
1321
+ class AlphaDropout(_DropoutNd):
1322
+ r"""Applies Alpha Dropout over the input.
1323
+
1324
+ Alpha Dropout is a type of Dropout that maintains the self-normalizing
1325
+ property.
1326
+ For an input with zero mean and unit standard deviation, the output of
1327
+ Alpha Dropout maintains the original mean and standard deviation of the
1328
+ input.
1329
+ Alpha Dropout goes hand-in-hand with SELU activation function, which ensures
1330
+ that the outputs have zero mean and unit standard deviation.
1331
+
1332
+ During training, it randomly masks some of the elements of the input
1333
+ tensor with probability *p* using samples from a bernoulli distribution.
1334
+ The elements to masked are randomized on every forward call, and scaled
1335
+ and shifted to maintain zero mean and unit standard deviation.
1336
+
1337
+ During evaluation the module simply computes an identity function.
1338
+
1339
+ More details can be found in the paper `Self-Normalizing Neural Networks`_ .
1340
+
1341
+ Args:
1342
+ prob: float. probability of an element to be kept.
1343
+
1344
+ Shape:
1345
+ - Input: :math:`(*)`. Input can be of any shape
1346
+ - Output: :math:`(*)`. Output is of the same shape as input
1347
+
1348
+ Examples::
1349
+
1350
+ >>> m = AlphaDropout(p=0.2)
1351
+ >>> x = random.randn(20, 16)
1352
+ >>> output = m(x)
1353
+
1354
+ .. _Self-Normalizing Neural Networks: https://arxiv.org/abs/1706.02515
1355
+ """
1356
+ __module__ = 'brainstate.nn'
1357
+
1358
+ def forward(self, x):
1359
+ return F.alpha_dropout(x, self.p, self.training)
1360
+
1361
+
1362
+ class FeatureAlphaDropout(_DropoutNd):
1363
+ r"""Randomly masks out entire channels (a channel is a feature map,
1364
+ e.g. the :math:`j`-th channel of the :math:`i`-th sample in the batch input
1365
+ is a tensor :math:`\text{input}[i, j]`) of the input tensor). Instead of
1366
+ setting activations to zero, as in regular Dropout, the activations are set
1367
+ to the negative saturation value of the SELU activation function. More details
1368
+ can be found in the paper `Self-Normalizing Neural Networks`_ .
1369
+
1370
+ Each element will be masked independently for each sample on every forward
1371
+ call with probability :attr:`p` using samples from a Bernoulli distribution.
1372
+ The elements to be masked are randomized on every forward call, and scaled
1373
+ and shifted to maintain zero mean and unit variance.
1374
+
1375
+ Usually the input comes from :class:`nn.AlphaDropout` modules.
1376
+
1377
+ As described in the paper
1378
+ `Efficient Object Localization Using Convolutional Networks`_ ,
1379
+ if adjacent pixels within feature maps are strongly correlated
1380
+ (as is normally the case in early convolution layers) then i.i.d. dropout
1381
+ will not regularize the activations and will otherwise just result
1382
+ in an effective learning rate decrease.
1383
+
1384
+ In this case, :func:`nn.AlphaDropout` will help promote independence between
1385
+ feature maps and should be used instead.
1386
+
1387
+ Args:
1388
+ prob: float. probability of an element to be kept.
1389
+
1390
+ Shape:
1391
+ - Input: :math:`(N, C, D, H, W)` or :math:`(C, D, H, W)`.
1392
+ - Output: :math:`(N, C, D, H, W)` or :math:`(C, D, H, W)` (same shape as input).
1393
+
1394
+ Examples::
1395
+
1396
+ >>> m = FeatureAlphaDropout(p=0.2)
1397
+ >>> x = random.randn(20, 16, 4, 32, 32)
1398
+ >>> output = m(x)
1399
+
1400
+ .. _Self-Normalizing Neural Networks: https://arxiv.org/abs/1706.02515
1401
+ .. _Efficient Object Localization Using Convolutional Networks:
1402
+ https://arxiv.org/abs/1411.4280
1403
+ """
1404
+ __module__ = 'brainstate.nn'
1405
+
1406
+ def forward(self, x):
1407
+ return F.feature_alpha_dropout(x, self.p, self.training)
1408
+
1409
+
1410
+ class SpikeBitwise(Module, ElementWiseBlock):
1411
+ r"""Bitwise addition for the spiking inputs.
1412
+
1413
+ .. math::
1414
+
1415
+ \begin{array}{ccc}
1416
+ \hline \text { Mode } & \text { Expression for } \mathrm{g}(\mathrm{x}, \mathrm{y}) & \text { Code for } \mathrm{g}(\mathrm{x}, \mathrm{y}) \\
1417
+ \hline \text { ADD } & x+y & x+y \\
1418
+ \text { AND } & x \cap y & x \cdot y \\
1419
+ \text { IAND } & (\neg x) \cap y & (1-x) \cdot y \\
1420
+ \text { OR } & x \cup y & (x+y)-(x \cdot y) \\
1421
+ \hline
1422
+ \end{array}
1423
+
1424
+ Args:
1425
+ op: str. The bitwise operation.
1426
+ name: str. The name of the dynamic system.
1427
+ """
1428
+ __module__ = 'brainstate.nn'
1429
+
1430
+ def __init__(self,
1431
+ op: str = 'add',
1432
+ name: Optional[str] = None) -> None:
1433
+ super().__init__(name=name)
1434
+ self.op = op
1435
+
1436
+ def __call__(self, x, y):
1437
+ return F.spike_bitwise(x, y, self.op)