brainstate 0.1.10__py2.py3-none-any.whl → 0.2.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 (163) hide show
  1. brainstate/__init__.py +169 -58
  2. brainstate/_compatible_import.py +340 -148
  3. brainstate/_compatible_import_test.py +681 -0
  4. brainstate/_deprecation.py +210 -0
  5. brainstate/_deprecation_test.py +2319 -0
  6. brainstate/{util/error.py → _error.py} +45 -55
  7. brainstate/_state.py +1652 -1605
  8. brainstate/_state_test.py +52 -52
  9. brainstate/_utils.py +47 -47
  10. brainstate/environ.py +1495 -563
  11. brainstate/environ_test.py +1223 -62
  12. brainstate/graph/__init__.py +22 -29
  13. brainstate/graph/_node.py +240 -0
  14. brainstate/graph/_node_test.py +589 -0
  15. brainstate/graph/{_graph_operation.py → _operation.py} +1624 -1738
  16. brainstate/graph/_operation_test.py +1147 -0
  17. brainstate/mixin.py +1433 -365
  18. brainstate/mixin_test.py +1017 -77
  19. brainstate/nn/__init__.py +137 -135
  20. brainstate/nn/_activations.py +1100 -808
  21. brainstate/nn/_activations_test.py +354 -331
  22. brainstate/nn/_collective_ops.py +633 -514
  23. brainstate/nn/_collective_ops_test.py +774 -43
  24. brainstate/nn/_common.py +226 -178
  25. brainstate/nn/_common_test.py +154 -0
  26. brainstate/nn/_conv.py +2010 -501
  27. brainstate/nn/_conv_test.py +849 -238
  28. brainstate/nn/_delay.py +575 -588
  29. brainstate/nn/_delay_test.py +243 -238
  30. brainstate/nn/_dropout.py +618 -426
  31. brainstate/nn/_dropout_test.py +477 -100
  32. brainstate/nn/_dynamics.py +1267 -1343
  33. brainstate/nn/_dynamics_test.py +67 -78
  34. brainstate/nn/_elementwise.py +1298 -1119
  35. brainstate/nn/_elementwise_test.py +830 -169
  36. brainstate/nn/_embedding.py +408 -58
  37. brainstate/nn/_embedding_test.py +156 -0
  38. brainstate/nn/{_fixedprob.py → _event_fixedprob.py} +233 -239
  39. brainstate/nn/{_fixedprob_test.py → _event_fixedprob_test.py} +115 -114
  40. brainstate/nn/{_linear_mv.py → _event_linear.py} +83 -83
  41. brainstate/nn/{_linear_mv_test.py → _event_linear_test.py} +121 -120
  42. brainstate/nn/_exp_euler.py +254 -92
  43. brainstate/nn/_exp_euler_test.py +377 -35
  44. brainstate/nn/_linear.py +744 -424
  45. brainstate/nn/_linear_test.py +475 -107
  46. brainstate/nn/_metrics.py +1070 -0
  47. brainstate/nn/_metrics_test.py +611 -0
  48. brainstate/nn/_module.py +384 -377
  49. brainstate/nn/_module_test.py +40 -40
  50. brainstate/nn/_normalizations.py +1334 -975
  51. brainstate/nn/_normalizations_test.py +699 -73
  52. brainstate/nn/_paddings.py +1020 -0
  53. brainstate/nn/_paddings_test.py +723 -0
  54. brainstate/nn/_poolings.py +2239 -1177
  55. brainstate/nn/_poolings_test.py +953 -217
  56. brainstate/nn/{_rate_rnns.py → _rnns.py} +946 -554
  57. brainstate/nn/_rnns_test.py +593 -0
  58. brainstate/nn/_utils.py +216 -89
  59. brainstate/nn/_utils_test.py +402 -0
  60. brainstate/{init/_random_inits.py → nn/init.py} +809 -553
  61. brainstate/{init/_random_inits_test.py → nn/init_test.py} +180 -149
  62. brainstate/random/__init__.py +270 -24
  63. brainstate/random/_rand_funs.py +3938 -3616
  64. brainstate/random/_rand_funs_test.py +640 -567
  65. brainstate/random/_rand_seed.py +675 -210
  66. brainstate/random/_rand_seed_test.py +48 -48
  67. brainstate/random/_rand_state.py +1617 -1409
  68. brainstate/random/_rand_state_test.py +551 -0
  69. brainstate/transform/__init__.py +59 -0
  70. brainstate/transform/_ad_checkpoint.py +176 -0
  71. brainstate/{compile → transform}/_ad_checkpoint_test.py +49 -49
  72. brainstate/{augment → transform}/_autograd.py +1025 -778
  73. brainstate/{augment → transform}/_autograd_test.py +1289 -1289
  74. brainstate/transform/_conditions.py +316 -0
  75. brainstate/{compile → transform}/_conditions_test.py +220 -220
  76. brainstate/{compile → transform}/_error_if.py +94 -92
  77. brainstate/{compile → transform}/_error_if_test.py +52 -52
  78. brainstate/transform/_eval_shape.py +145 -0
  79. brainstate/{augment → transform}/_eval_shape_test.py +38 -38
  80. brainstate/{compile → transform}/_jit.py +399 -346
  81. brainstate/{compile → transform}/_jit_test.py +143 -143
  82. brainstate/{compile → transform}/_loop_collect_return.py +675 -536
  83. brainstate/{compile → transform}/_loop_collect_return_test.py +58 -58
  84. brainstate/{compile → transform}/_loop_no_collection.py +283 -184
  85. brainstate/{compile → transform}/_loop_no_collection_test.py +50 -50
  86. brainstate/transform/_make_jaxpr.py +2016 -0
  87. brainstate/transform/_make_jaxpr_test.py +1510 -0
  88. brainstate/transform/_mapping.py +529 -0
  89. brainstate/transform/_mapping_test.py +194 -0
  90. brainstate/{compile → transform}/_progress_bar.py +255 -202
  91. brainstate/{augment → transform}/_random.py +171 -151
  92. brainstate/{compile → transform}/_unvmap.py +256 -159
  93. brainstate/transform/_util.py +286 -0
  94. brainstate/typing.py +837 -304
  95. brainstate/typing_test.py +780 -0
  96. brainstate/util/__init__.py +27 -50
  97. brainstate/util/_others.py +1025 -0
  98. brainstate/util/_others_test.py +962 -0
  99. brainstate/util/_pretty_pytree.py +1301 -0
  100. brainstate/util/_pretty_pytree_test.py +675 -0
  101. brainstate/util/{pretty_repr.py → _pretty_repr.py} +462 -328
  102. brainstate/util/_pretty_repr_test.py +696 -0
  103. brainstate/util/filter.py +945 -469
  104. brainstate/util/filter_test.py +912 -0
  105. brainstate/util/struct.py +910 -523
  106. brainstate/util/struct_test.py +602 -0
  107. {brainstate-0.1.10.dist-info → brainstate-0.2.1.dist-info}/METADATA +108 -91
  108. brainstate-0.2.1.dist-info/RECORD +111 -0
  109. {brainstate-0.1.10.dist-info → brainstate-0.2.1.dist-info}/licenses/LICENSE +202 -202
  110. brainstate/augment/__init__.py +0 -30
  111. brainstate/augment/_eval_shape.py +0 -99
  112. brainstate/augment/_mapping.py +0 -1060
  113. brainstate/augment/_mapping_test.py +0 -597
  114. brainstate/compile/__init__.py +0 -38
  115. brainstate/compile/_ad_checkpoint.py +0 -204
  116. brainstate/compile/_conditions.py +0 -256
  117. brainstate/compile/_make_jaxpr.py +0 -888
  118. brainstate/compile/_make_jaxpr_test.py +0 -156
  119. brainstate/compile/_util.py +0 -147
  120. brainstate/functional/__init__.py +0 -27
  121. brainstate/graph/_graph_node.py +0 -244
  122. brainstate/graph/_graph_node_test.py +0 -73
  123. brainstate/graph/_graph_operation_test.py +0 -563
  124. brainstate/init/__init__.py +0 -26
  125. brainstate/init/_base.py +0 -52
  126. brainstate/init/_generic.py +0 -244
  127. brainstate/init/_regular_inits.py +0 -105
  128. brainstate/init/_regular_inits_test.py +0 -50
  129. brainstate/nn/_inputs.py +0 -608
  130. brainstate/nn/_ltp.py +0 -28
  131. brainstate/nn/_neuron.py +0 -705
  132. brainstate/nn/_neuron_test.py +0 -161
  133. brainstate/nn/_others.py +0 -46
  134. brainstate/nn/_projection.py +0 -486
  135. brainstate/nn/_rate_rnns_test.py +0 -63
  136. brainstate/nn/_readout.py +0 -209
  137. brainstate/nn/_readout_test.py +0 -53
  138. brainstate/nn/_stp.py +0 -236
  139. brainstate/nn/_synapse.py +0 -505
  140. brainstate/nn/_synapse_test.py +0 -131
  141. brainstate/nn/_synaptic_projection.py +0 -423
  142. brainstate/nn/_synouts.py +0 -162
  143. brainstate/nn/_synouts_test.py +0 -57
  144. brainstate/nn/metrics.py +0 -388
  145. brainstate/optim/__init__.py +0 -38
  146. brainstate/optim/_base.py +0 -64
  147. brainstate/optim/_lr_scheduler.py +0 -448
  148. brainstate/optim/_lr_scheduler_test.py +0 -50
  149. brainstate/optim/_optax_optimizer.py +0 -152
  150. brainstate/optim/_optax_optimizer_test.py +0 -53
  151. brainstate/optim/_sgd_optimizer.py +0 -1104
  152. brainstate/random/_random_for_unit.py +0 -52
  153. brainstate/surrogate.py +0 -1957
  154. brainstate/transform.py +0 -23
  155. brainstate/util/caller.py +0 -98
  156. brainstate/util/others.py +0 -540
  157. brainstate/util/pretty_pytree.py +0 -945
  158. brainstate/util/pretty_pytree_test.py +0 -159
  159. brainstate/util/pretty_table.py +0 -2954
  160. brainstate/util/scaling.py +0 -258
  161. brainstate-0.1.10.dist-info/RECORD +0 -130
  162. {brainstate-0.1.10.dist-info → brainstate-0.2.1.dist-info}/WHEEL +0 -0
  163. {brainstate-0.1.10.dist-info → brainstate-0.2.1.dist-info}/top_level.txt +0 -0
@@ -1,100 +1,477 @@
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 numpy as np
20
-
21
- import brainstate
22
-
23
-
24
- class TestDropout(unittest.TestCase):
25
-
26
- def test_dropout(self):
27
- # Create a Dropout layer with a dropout rate of 0.5
28
- dropout_layer = brainstate.nn.Dropout(0.5)
29
-
30
- # Input data
31
- input_data = np.arange(20)
32
-
33
- with brainstate.environ.context(fit=True):
34
- # Apply dropout
35
- output_data = dropout_layer(input_data)
36
-
37
- # Check that the output has the same shape as the input
38
- self.assertEqual(input_data.shape, output_data.shape)
39
-
40
- # Check that some elements are zeroed out
41
- self.assertTrue(np.any(output_data == 0))
42
-
43
- # Check that the non-zero elements are scaled by 1/(1-rate)
44
- scale_factor = 1 / (1 - 0.5)
45
- non_zero_elements = output_data[output_data != 0]
46
- expected_non_zero_elements = input_data[output_data != 0] * scale_factor
47
- np.testing.assert_almost_equal(non_zero_elements, expected_non_zero_elements)
48
-
49
- def test_DropoutFixed(self):
50
- dropout_layer = brainstate.nn.DropoutFixed(in_size=(2, 3), prob=0.5)
51
- dropout_layer.init_state(batch_size=2)
52
- input_data = np.random.randn(2, 2, 3)
53
- with brainstate.environ.context(fit=True):
54
- output_data = dropout_layer.update(input_data)
55
- self.assertEqual(input_data.shape, output_data.shape)
56
- self.assertTrue(np.any(output_data == 0))
57
- scale_factor = 1 / (1 - 0.5)
58
- non_zero_elements = output_data[output_data != 0]
59
- expected_non_zero_elements = input_data[output_data != 0] * scale_factor
60
- np.testing.assert_almost_equal(non_zero_elements, expected_non_zero_elements)
61
-
62
- # def test_Dropout1d(self):
63
- # dropout_layer = brainstate.nn.Dropout1d(prob=0.5)
64
- # input_data = np.random.randn(2, 3, 4)
65
- # with brainstate.environ.context(fit=True):
66
- # output_data = dropout_layer(input_data)
67
- # self.assertEqual(input_data.shape, output_data.shape)
68
- # self.assertTrue(np.any(output_data == 0))
69
- # scale_factor = 1 / (1 - 0.5)
70
- # non_zero_elements = output_data[output_data != 0]
71
- # expected_non_zero_elements = input_data[output_data != 0] * scale_factor
72
- # np.testing.assert_almost_equal(non_zero_elements, expected_non_zero_elements, decimal=4)
73
-
74
- def test_Dropout2d(self):
75
- dropout_layer = brainstate.nn.Dropout2d(prob=0.5)
76
- input_data = np.random.randn(2, 3, 4, 5)
77
- with brainstate.environ.context(fit=True):
78
- output_data = dropout_layer(input_data)
79
- self.assertEqual(input_data.shape, output_data.shape)
80
- self.assertTrue(np.any(output_data == 0))
81
- scale_factor = 1 / (1 - 0.5)
82
- non_zero_elements = output_data[output_data != 0]
83
- expected_non_zero_elements = input_data[output_data != 0] * scale_factor
84
- np.testing.assert_almost_equal(non_zero_elements, expected_non_zero_elements, decimal=4)
85
-
86
- def test_Dropout3d(self):
87
- dropout_layer = brainstate.nn.Dropout3d(prob=0.5)
88
- input_data = np.random.randn(2, 3, 4, 5, 6)
89
- with brainstate.environ.context(fit=True):
90
- output_data = dropout_layer(input_data)
91
- self.assertEqual(input_data.shape, output_data.shape)
92
- self.assertTrue(np.any(output_data == 0))
93
- scale_factor = 1 / (1 - 0.5)
94
- non_zero_elements = output_data[output_data != 0]
95
- expected_non_zero_elements = input_data[output_data != 0] * scale_factor
96
- np.testing.assert_almost_equal(non_zero_elements, expected_non_zero_elements, decimal=4)
97
-
98
-
99
- if __name__ == '__main__':
100
- unittest.main()
1
+ # Copyright 2024 BrainX Ecosystem Limited. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ==============================================================================
15
+
16
+
17
+ import numpy as np
18
+ from absl.testing import absltest
19
+ from absl.testing import parameterized
20
+
21
+ import brainstate
22
+
23
+
24
+ class TestDropout(parameterized.TestCase):
25
+
26
+ def test_dropout_basic(self):
27
+ """Test basic dropout functionality."""
28
+ dropout_layer = brainstate.nn.Dropout(0.5)
29
+ input_data = np.arange(20, dtype=np.float32)
30
+
31
+ with brainstate.environ.context(fit=True):
32
+ output_data = dropout_layer(input_data)
33
+
34
+ # Check that the output has the same shape as the input
35
+ self.assertEqual(input_data.shape, output_data.shape)
36
+
37
+ # Check that some elements are zeroed out
38
+ self.assertTrue(np.any(output_data == 0))
39
+
40
+ # Check that the non-zero elements are scaled by 1/(1-rate)
41
+ scale_factor = 1 / (1 - 0.5)
42
+ non_zero_elements = output_data[output_data != 0]
43
+ expected_non_zero_elements = input_data[output_data != 0] * scale_factor
44
+ np.testing.assert_almost_equal(non_zero_elements, expected_non_zero_elements)
45
+
46
+ def test_dropout_eval_mode(self):
47
+ """Test that dropout is disabled in evaluation mode."""
48
+ dropout_layer = brainstate.nn.Dropout(0.5)
49
+ input_data = np.arange(20, dtype=np.float32)
50
+
51
+ with brainstate.environ.context(fit=False):
52
+ # Without fit context, dropout should be disabled
53
+ output_data = dropout_layer(input_data)
54
+ np.testing.assert_array_equal(input_data, output_data)
55
+
56
+ @parameterized.parameters(0.0, 0.2, 0.5, 0.8, 1.0)
57
+ def test_dropout_various_probs(self, prob):
58
+ """Test dropout with various probabilities."""
59
+ dropout_layer = brainstate.nn.Dropout(prob)
60
+ input_data = brainstate.random.randn(1000)
61
+
62
+ with brainstate.environ.context(fit=True):
63
+ output_data = dropout_layer(input_data)
64
+ self.assertEqual(input_data.shape, output_data.shape)
65
+
66
+ if prob == 1.0:
67
+ # All elements should be kept (no dropout)
68
+ np.testing.assert_array_equal(input_data, output_data)
69
+ elif prob == 0.0:
70
+ # All elements should be dropped
71
+ np.testing.assert_array_equal(np.zeros_like(input_data), output_data)
72
+
73
+ def test_dropout_broadcast_dims(self):
74
+ """Test dropout with broadcast dimensions."""
75
+ dropout_layer = brainstate.nn.Dropout(0.5, broadcast_dims=(1, 2))
76
+ input_data = brainstate.random.randn(4, 5, 6)
77
+
78
+ with brainstate.environ.context(fit=True):
79
+ output_data = dropout_layer(input_data)
80
+ self.assertEqual(input_data.shape, output_data.shape)
81
+
82
+ # Check that the same mask is applied across broadcast dimensions
83
+ for i in range(4):
84
+ channel_output = output_data[i]
85
+ # All elements in a channel should be either all zero or all scaled
86
+ is_zero = (channel_output == 0).all() or (channel_output != 0).all()
87
+ # Note: This might not always be true due to randomness, but generally should hold
88
+
89
+ def test_dropout_multidimensional(self):
90
+ """Test dropout with multidimensional input."""
91
+ dropout_layer = brainstate.nn.Dropout(0.5)
92
+ input_data = brainstate.random.randn(10, 20, 30)
93
+
94
+ with brainstate.environ.context(fit=True):
95
+ output_data = dropout_layer(input_data)
96
+ self.assertEqual(input_data.shape, output_data.shape)
97
+
98
+
99
+ class TestDropout1d(parameterized.TestCase):
100
+
101
+ def test_dropout1d_basic(self):
102
+ """Test basic Dropout1d functionality."""
103
+ dropout_layer = brainstate.nn.Dropout1d(prob=0.5)
104
+ input_data = brainstate.random.randn(2, 3, 4) # (N, C, L)
105
+
106
+ with brainstate.environ.context(fit=True):
107
+ output_data = dropout_layer(input_data)
108
+ self.assertEqual(input_data.shape, output_data.shape)
109
+
110
+ def test_dropout1d_channel_wise(self):
111
+ """Test that Dropout1d applies dropout."""
112
+ dropout_layer = brainstate.nn.Dropout1d(prob=0.5)
113
+ input_data = brainstate.random.randn(2, 8, 10) # (N, C, L)
114
+
115
+ with brainstate.environ.context(fit=True):
116
+ output_data = dropout_layer(input_data)
117
+ # Just verify that dropout is applied (shape preserved, some zeros present)
118
+ self.assertEqual(input_data.shape, output_data.shape)
119
+
120
+ def test_dropout1d_without_batch(self):
121
+ """Test Dropout1d with unbatched input (C, L)."""
122
+ dropout_layer = brainstate.nn.Dropout1d(prob=0.5)
123
+ input_data = brainstate.random.randn(3, 4) # (C, L)
124
+
125
+ with brainstate.environ.context(fit=True):
126
+ output_data = dropout_layer(input_data)
127
+ self.assertEqual(input_data.shape, output_data.shape)
128
+
129
+ def test_dropout1d_eval_mode(self):
130
+ """Test that Dropout1d is disabled in eval mode."""
131
+ dropout_layer = brainstate.nn.Dropout1d(prob=0.5)
132
+ input_data = brainstate.random.randn(2, 3, 4)
133
+
134
+ with brainstate.environ.context(fit=False):
135
+ output_data = dropout_layer(input_data)
136
+ np.testing.assert_array_equal(input_data, output_data)
137
+
138
+ def test_dropout1d_invalid_shape(self):
139
+ """Test that Dropout1d raises error for invalid input shape."""
140
+ dropout_layer = brainstate.nn.Dropout1d(prob=0.5)
141
+ input_data = brainstate.random.randn(2, 3, 4, 5) # 4D input is invalid for Dropout1d
142
+
143
+ with brainstate.environ.context(fit=True):
144
+ with self.assertRaises(RuntimeError):
145
+ dropout_layer(input_data)
146
+
147
+
148
+ class TestDropout2d(parameterized.TestCase):
149
+
150
+ def test_dropout2d_basic(self):
151
+ """Test basic Dropout2d functionality."""
152
+ dropout_layer = brainstate.nn.Dropout2d(prob=0.5)
153
+ input_data = brainstate.random.randn(2, 3, 4, 5) # (N, C, H, W)
154
+
155
+ with brainstate.environ.context(fit=True):
156
+ output_data = dropout_layer(input_data)
157
+ self.assertEqual(input_data.shape, output_data.shape)
158
+ self.assertTrue(np.any(output_data == 0))
159
+
160
+ def test_dropout2d_channel_wise(self):
161
+ """Test that Dropout2d applies dropout."""
162
+ dropout_layer = brainstate.nn.Dropout2d(prob=0.5)
163
+ input_data = brainstate.random.randn(2, 8, 4, 5) # (N, C, H, W)
164
+
165
+ with brainstate.environ.context(fit=True):
166
+ output_data = dropout_layer(input_data)
167
+ # Just verify that dropout is applied (shape preserved, some zeros present)
168
+ self.assertEqual(input_data.shape, output_data.shape)
169
+
170
+ def test_dropout2d_without_batch(self):
171
+ """Test Dropout2d with unbatched input (C, H, W)."""
172
+ dropout_layer = brainstate.nn.Dropout2d(prob=0.5)
173
+ input_data = brainstate.random.randn(3, 4, 5) # (C, H, W)
174
+
175
+ with brainstate.environ.context(fit=True):
176
+ output_data = dropout_layer(input_data)
177
+ self.assertEqual(input_data.shape, output_data.shape)
178
+
179
+ def test_dropout2d_scaling(self):
180
+ """Test that Dropout2d correctly scales non-dropped elements."""
181
+ dropout_layer = brainstate.nn.Dropout2d(prob=0.5)
182
+ input_data = brainstate.random.randn(2, 3, 4, 5)
183
+
184
+ with brainstate.environ.context(fit=True):
185
+ output_data = dropout_layer(input_data)
186
+ scale_factor = 1 / (1 - 0.5)
187
+ mask = ~np.isclose(output_data, 0)
188
+ non_zero_elements = output_data[mask]
189
+ expected_non_zero_elements = input_data[mask] * scale_factor
190
+ np.testing.assert_allclose(non_zero_elements, expected_non_zero_elements, rtol=1e-5)
191
+
192
+ def test_dropout2d_eval_mode(self):
193
+ """Test that Dropout2d is disabled in eval mode."""
194
+ dropout_layer = brainstate.nn.Dropout2d(prob=0.5)
195
+ input_data = brainstate.random.randn(2, 3, 4, 5)
196
+
197
+ with brainstate.environ.context(fit=False):
198
+ output_data = dropout_layer(input_data)
199
+ np.testing.assert_array_equal(input_data, output_data)
200
+
201
+
202
+ class TestDropout3d(parameterized.TestCase):
203
+
204
+ def test_dropout3d_basic(self):
205
+ """Test basic Dropout3d functionality."""
206
+ dropout_layer = brainstate.nn.Dropout3d(prob=0.5)
207
+ input_data = brainstate.random.randn(2, 3, 4, 5, 6) # (N, C, D, H, W)
208
+
209
+ with brainstate.environ.context(fit=True):
210
+ output_data = dropout_layer(input_data)
211
+ self.assertEqual(input_data.shape, output_data.shape)
212
+ self.assertTrue(np.any(output_data == 0))
213
+
214
+ def test_dropout3d_channel_wise(self):
215
+ """Test that Dropout3d applies dropout."""
216
+ dropout_layer = brainstate.nn.Dropout3d(prob=0.5)
217
+ input_data = brainstate.random.randn(2, 8, 4, 5, 6) # (N, C, D, H, W)
218
+
219
+ with brainstate.environ.context(fit=True):
220
+ output_data = dropout_layer(input_data)
221
+ # Just verify that dropout is applied (shape preserved, some zeros present)
222
+ self.assertEqual(input_data.shape, output_data.shape)
223
+
224
+ def test_dropout3d_without_batch(self):
225
+ """Test Dropout3d with unbatched input (C, D, H, W)."""
226
+ dropout_layer = brainstate.nn.Dropout3d(prob=0.5)
227
+ input_data = brainstate.random.randn(3, 4, 5, 6) # (C, D, H, W)
228
+
229
+ with brainstate.environ.context(fit=True):
230
+ output_data = dropout_layer(input_data)
231
+ self.assertEqual(input_data.shape, output_data.shape)
232
+
233
+ def test_dropout3d_scaling(self):
234
+ """Test that Dropout3d correctly scales non-dropped elements."""
235
+ dropout_layer = brainstate.nn.Dropout3d(prob=0.5)
236
+ input_data = brainstate.random.randn(2, 3, 4, 5, 6)
237
+
238
+ with brainstate.environ.context(fit=True):
239
+ output_data = dropout_layer(input_data)
240
+ scale_factor = 1 / (1 - 0.5)
241
+ mask = ~np.isclose(output_data, 0)
242
+ non_zero_elements = output_data[mask]
243
+ expected_non_zero_elements = input_data[mask] * scale_factor
244
+ np.testing.assert_allclose(non_zero_elements, expected_non_zero_elements, rtol=1e-5)
245
+
246
+ def test_dropout3d_eval_mode(self):
247
+ """Test that Dropout3d is disabled in eval mode."""
248
+ dropout_layer = brainstate.nn.Dropout3d(prob=0.5)
249
+ input_data = brainstate.random.randn(2, 3, 4, 5, 6)
250
+
251
+ with brainstate.environ.context(fit=False):
252
+ output_data = dropout_layer(input_data)
253
+ np.testing.assert_array_equal(input_data, output_data)
254
+
255
+
256
+ class TestAlphaDropout(parameterized.TestCase):
257
+
258
+ def test_alphadropout_basic(self):
259
+ """Test basic AlphaDropout functionality."""
260
+ dropout_layer = brainstate.nn.AlphaDropout(prob=0.5)
261
+ input_data = brainstate.random.randn(100, 50)
262
+
263
+ with brainstate.environ.context(fit=True):
264
+ output_data = dropout_layer(input_data)
265
+ self.assertEqual(input_data.shape, output_data.shape)
266
+
267
+ def test_alphadropout_self_normalizing(self):
268
+ """Test that AlphaDropout maintains zero mean and unit variance."""
269
+ dropout_layer = brainstate.nn.AlphaDropout(prob=0.5)
270
+ # Create input with zero mean and unit variance
271
+ input_data = brainstate.random.randn(10000)
272
+
273
+ with brainstate.environ.context(fit=True):
274
+ output_data = dropout_layer(input_data)
275
+
276
+ # The output should approximately maintain zero mean and unit variance
277
+ output_mean = np.mean(output_data)
278
+ output_std = np.std(output_data)
279
+
280
+ # Allow some tolerance due to randomness
281
+ self.assertAlmostEqual(output_mean, 0.0, delta=0.1)
282
+ self.assertAlmostEqual(output_std, 1.0, delta=0.2)
283
+
284
+ def test_alphadropout_alpha_value(self):
285
+ """Test that dropped values are set to alpha (not zero)."""
286
+ dropout_layer = brainstate.nn.AlphaDropout(prob=0.5)
287
+ input_data = brainstate.random.randn(1000) + 5.0 # Shift to avoid confusion with alpha
288
+
289
+ with brainstate.environ.context(fit=True):
290
+ output_data = dropout_layer(input_data)
291
+
292
+ # AlphaDropout should not have zeros, but values close to transformed alpha
293
+ # After affine transformation: alpha * a + b
294
+ expected_dropped_value = dropout_layer.alpha * dropout_layer.a + dropout_layer.b
295
+
296
+ # Check that we have some values close to the expected dropped value
297
+ # (within reasonable tolerance due to numerical precision)
298
+ unique_vals = np.unique(np.round(output_data, 3))
299
+ self.assertGreater(len(unique_vals), 1) # Should have both dropped and kept values
300
+
301
+ def test_alphadropout_eval_mode(self):
302
+ """Test that AlphaDropout is disabled in eval mode."""
303
+ dropout_layer = brainstate.nn.AlphaDropout(prob=0.5)
304
+ input_data = brainstate.random.randn(20, 16)
305
+
306
+ with brainstate.environ.context(fit=False):
307
+ output_data = dropout_layer(input_data)
308
+ np.testing.assert_array_equal(input_data, output_data)
309
+
310
+ @parameterized.parameters(0.2, 0.5, 0.8)
311
+ def test_alphadropout_various_probs(self, prob):
312
+ """Test AlphaDropout with various probabilities."""
313
+ dropout_layer = brainstate.nn.AlphaDropout(prob=prob)
314
+ input_data = brainstate.random.randn(1000)
315
+
316
+ with brainstate.environ.context(fit=True):
317
+ output_data = dropout_layer(input_data)
318
+ self.assertEqual(input_data.shape, output_data.shape)
319
+
320
+ def test_alphadropout_multidimensional(self):
321
+ """Test AlphaDropout with multidimensional input."""
322
+ dropout_layer = brainstate.nn.AlphaDropout(prob=0.5)
323
+ input_data = brainstate.random.randn(10, 20, 30)
324
+
325
+ with brainstate.environ.context(fit=True):
326
+ output_data = dropout_layer(input_data)
327
+ self.assertEqual(input_data.shape, output_data.shape)
328
+
329
+
330
+ class TestFeatureAlphaDropout(parameterized.TestCase):
331
+
332
+ def test_featurealphadropout_basic(self):
333
+ """Test basic FeatureAlphaDropout functionality."""
334
+ dropout_layer = brainstate.nn.FeatureAlphaDropout(prob=0.5)
335
+ input_data = brainstate.random.randn(2, 16, 4, 32, 32) # (N, C, D, H, W)
336
+
337
+ with brainstate.environ.context(fit=True):
338
+ output_data = dropout_layer(input_data)
339
+ self.assertEqual(input_data.shape, output_data.shape)
340
+
341
+ def test_featurealphadropout_channel_wise(self):
342
+ """Test that FeatureAlphaDropout drops entire channels."""
343
+ dropout_layer = brainstate.nn.FeatureAlphaDropout(prob=0.5, channel_axis=1)
344
+ input_data = brainstate.random.randn(2, 8, 10, 10) # (N, C, H, W)
345
+
346
+ with brainstate.environ.context(fit=True):
347
+ output_data = dropout_layer(input_data)
348
+
349
+ # Check that entire channels share the same dropout mask
350
+ for batch in range(2):
351
+ for channel in range(8):
352
+ channel_data = output_data[batch, channel, :, :]
353
+ # All elements in a channel should be either all from input or all alpha-transformed
354
+ unique_vals = np.unique(np.round(channel_data, 4))
355
+ # Due to the alpha transformation, we can't easily check, but shape should match
356
+ self.assertEqual(channel_data.shape, (10, 10))
357
+
358
+ def test_featurealphadropout_channel_axis(self):
359
+ """Test FeatureAlphaDropout with different channel axes."""
360
+ # Test with channel_axis=-1
361
+ dropout_layer = brainstate.nn.FeatureAlphaDropout(prob=0.5, channel_axis=-1)
362
+ input_data = brainstate.random.randn(2, 10, 10, 8) # (N, H, W, C)
363
+
364
+ with brainstate.environ.context(fit=True):
365
+ output_data = dropout_layer(input_data)
366
+ self.assertEqual(input_data.shape, output_data.shape)
367
+
368
+ def test_featurealphadropout_eval_mode(self):
369
+ """Test that FeatureAlphaDropout is disabled in eval mode."""
370
+ dropout_layer = brainstate.nn.FeatureAlphaDropout(prob=0.5)
371
+ input_data = brainstate.random.randn(2, 16, 4, 32, 32)
372
+
373
+ with brainstate.environ.context(fit=False):
374
+ output_data = dropout_layer(input_data)
375
+ np.testing.assert_array_equal(input_data, output_data)
376
+
377
+ def test_featurealphadropout_self_normalizing(self):
378
+ """Test that FeatureAlphaDropout maintains self-normalizing properties."""
379
+ dropout_layer = brainstate.nn.FeatureAlphaDropout(prob=0.5, channel_axis=1)
380
+ # Create input with zero mean and unit variance
381
+ input_data = brainstate.random.randn(10, 32, 20)
382
+
383
+ with brainstate.environ.context(fit=True):
384
+ output_data = dropout_layer(input_data)
385
+
386
+ # The output should approximately maintain zero mean and unit variance
387
+ output_mean = np.mean(output_data)
388
+ output_std = np.std(output_data)
389
+
390
+ # Allow some tolerance due to randomness
391
+ self.assertAlmostEqual(output_mean, 0.0, delta=0.2)
392
+ self.assertAlmostEqual(output_std, 1.0, delta=0.3)
393
+
394
+
395
+ class TestDropoutFixed(parameterized.TestCase):
396
+
397
+ def test_dropoutfixed_basic(self):
398
+ """Test basic DropoutFixed functionality."""
399
+ with brainstate.random.seed_context(42):
400
+ dropout_layer = brainstate.nn.DropoutFixed(in_size=(2, 3), prob=0.5)
401
+ dropout_layer.init_state(batch_size=2)
402
+ input_data = np.random.randn(2, 2, 3)
403
+
404
+ with brainstate.environ.context(fit=True):
405
+ output_data = dropout_layer.update(input_data)
406
+ self.assertEqual(input_data.shape, output_data.shape)
407
+ self.assertTrue(np.any(output_data == 0))
408
+
409
+ def test_dropoutfixed_mask_persistence(self):
410
+ """Test that DropoutFixed uses the same mask across multiple calls."""
411
+ with brainstate.random.seed_context(42):
412
+ dropout_layer = brainstate.nn.DropoutFixed(in_size=(10,), prob=0.5)
413
+ dropout_layer.init_state(batch_size=5)
414
+
415
+ input_data1 = brainstate.random.randn(5, 10)
416
+ input_data2 = brainstate.random.randn(5, 10)
417
+
418
+ with brainstate.environ.context(fit=True):
419
+ output_data1 = dropout_layer.update(input_data1)
420
+ output_data2 = dropout_layer.update(input_data2)
421
+
422
+ # The dropout mask should be the same for both calls
423
+ mask1 = (output_data1 == 0)
424
+ mask2 = (output_data2 == 0)
425
+ np.testing.assert_array_equal(mask1, mask2)
426
+
427
+ def test_dropoutfixed_scaling(self):
428
+ """Test that DropoutFixed correctly scales non-dropped elements."""
429
+ with brainstate.random.seed_context(42):
430
+ dropout_layer = brainstate.nn.DropoutFixed(in_size=(2, 3), prob=0.5)
431
+ dropout_layer.init_state(batch_size=2)
432
+ input_data = np.random.randn(2, 2, 3)
433
+
434
+ with brainstate.environ.context(fit=True):
435
+ output_data = dropout_layer.update(input_data)
436
+ scale_factor = 1 / (1 - 0.5)
437
+ non_zero_elements = output_data[output_data != 0]
438
+ expected_non_zero_elements = input_data[output_data != 0] * scale_factor
439
+ np.testing.assert_almost_equal(non_zero_elements, expected_non_zero_elements, decimal=3)
440
+
441
+ def test_dropoutfixed_eval_mode(self):
442
+ """Test that DropoutFixed is disabled in eval mode."""
443
+ with brainstate.random.seed_context(42):
444
+ dropout_layer = brainstate.nn.DropoutFixed(in_size=(2, 3), prob=0.5)
445
+ dropout_layer.init_state(batch_size=2)
446
+ input_data = np.random.randn(2, 2, 3)
447
+
448
+ with brainstate.environ.context(fit=False):
449
+ output_data = dropout_layer.update(input_data)
450
+ np.testing.assert_array_equal(input_data, output_data)
451
+
452
+ def test_dropoutfixed_shape_mismatch(self):
453
+ """Test that DropoutFixed raises error for shape mismatch."""
454
+ with brainstate.random.seed_context(42):
455
+ dropout_layer = brainstate.nn.DropoutFixed(in_size=(2, 3), prob=0.5)
456
+ dropout_layer.init_state(batch_size=2)
457
+ input_data = np.random.randn(3, 2, 3) # Wrong batch size
458
+
459
+ with brainstate.environ.context(fit=True):
460
+ with self.assertRaises(ValueError):
461
+ dropout_layer.update(input_data)
462
+
463
+ @parameterized.parameters(0.2, 0.5, 0.8)
464
+ def test_dropoutfixed_various_probs(self, prob):
465
+ """Test DropoutFixed with various probabilities."""
466
+ with brainstate.random.seed_context(42):
467
+ dropout_layer = brainstate.nn.DropoutFixed(in_size=(10,), prob=prob)
468
+ dropout_layer.init_state(batch_size=5)
469
+ input_data = brainstate.random.randn(5, 10)
470
+
471
+ with brainstate.environ.context(fit=True):
472
+ output_data = dropout_layer.update(input_data)
473
+ self.assertEqual(input_data.shape, output_data.shape)
474
+
475
+
476
+ if __name__ == '__main__':
477
+ absltest.main()