brainstate 0.2.1__py2.py3-none-any.whl → 0.2.2__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- brainstate/__init__.py +167 -169
- brainstate/_compatible_import.py +340 -340
- brainstate/_compatible_import_test.py +681 -681
- brainstate/_deprecation.py +210 -210
- brainstate/_deprecation_test.py +2297 -2319
- brainstate/_error.py +45 -45
- brainstate/_state.py +2157 -1652
- brainstate/_state_test.py +1129 -52
- brainstate/_utils.py +47 -47
- brainstate/environ.py +1495 -1495
- brainstate/environ_test.py +1223 -1223
- brainstate/graph/__init__.py +22 -22
- brainstate/graph/_node.py +240 -240
- brainstate/graph/_node_test.py +589 -589
- brainstate/graph/_operation.py +1620 -1624
- brainstate/graph/_operation_test.py +1147 -1147
- brainstate/mixin.py +1447 -1433
- brainstate/mixin_test.py +1017 -1017
- brainstate/nn/__init__.py +146 -137
- brainstate/nn/_activations.py +1100 -1100
- brainstate/nn/_activations_test.py +354 -354
- brainstate/nn/_collective_ops.py +635 -633
- brainstate/nn/_collective_ops_test.py +774 -774
- brainstate/nn/_common.py +226 -226
- brainstate/nn/_common_test.py +134 -154
- brainstate/nn/_conv.py +2010 -2010
- brainstate/nn/_conv_test.py +849 -849
- brainstate/nn/_delay.py +575 -575
- brainstate/nn/_delay_test.py +243 -243
- brainstate/nn/_dropout.py +618 -618
- brainstate/nn/_dropout_test.py +480 -477
- brainstate/nn/_dynamics.py +870 -1267
- brainstate/nn/_dynamics_test.py +53 -67
- brainstate/nn/_elementwise.py +1298 -1298
- brainstate/nn/_elementwise_test.py +829 -829
- brainstate/nn/_embedding.py +408 -408
- brainstate/nn/_embedding_test.py +156 -156
- brainstate/nn/_event_fixedprob.py +233 -233
- brainstate/nn/_event_fixedprob_test.py +115 -115
- brainstate/nn/_event_linear.py +83 -83
- brainstate/nn/_event_linear_test.py +121 -121
- brainstate/nn/_exp_euler.py +254 -254
- brainstate/nn/_exp_euler_test.py +377 -377
- brainstate/nn/_linear.py +744 -744
- brainstate/nn/_linear_test.py +475 -475
- brainstate/nn/_metrics.py +1070 -1070
- brainstate/nn/_metrics_test.py +611 -611
- brainstate/nn/_module.py +391 -384
- brainstate/nn/_module_test.py +40 -40
- brainstate/nn/_normalizations.py +1334 -1334
- brainstate/nn/_normalizations_test.py +699 -699
- brainstate/nn/_paddings.py +1020 -1020
- brainstate/nn/_paddings_test.py +722 -722
- brainstate/nn/_poolings.py +2239 -2239
- brainstate/nn/_poolings_test.py +952 -952
- brainstate/nn/_rnns.py +946 -946
- brainstate/nn/_rnns_test.py +592 -592
- brainstate/nn/_utils.py +216 -216
- brainstate/nn/_utils_test.py +401 -401
- brainstate/nn/init.py +809 -809
- brainstate/nn/init_test.py +180 -180
- brainstate/random/__init__.py +270 -270
- brainstate/random/{_rand_funs.py → _fun.py} +3938 -3938
- brainstate/random/{_rand_funs_test.py → _fun_test.py} +638 -640
- brainstate/random/_impl.py +672 -0
- brainstate/random/{_rand_seed.py → _seed.py} +675 -675
- brainstate/random/{_rand_seed_test.py → _seed_test.py} +48 -48
- brainstate/random/{_rand_state.py → _state.py} +1320 -1617
- brainstate/random/{_rand_state_test.py → _state_test.py} +551 -551
- brainstate/transform/__init__.py +56 -59
- brainstate/transform/_ad_checkpoint.py +176 -176
- brainstate/transform/_ad_checkpoint_test.py +49 -49
- brainstate/transform/_autograd.py +1025 -1025
- brainstate/transform/_autograd_test.py +1289 -1289
- brainstate/transform/_conditions.py +316 -316
- brainstate/transform/_conditions_test.py +220 -220
- brainstate/transform/_error_if.py +94 -94
- brainstate/transform/_error_if_test.py +52 -52
- brainstate/transform/_find_state.py +200 -0
- brainstate/transform/_find_state_test.py +84 -0
- brainstate/transform/_jit.py +399 -399
- brainstate/transform/_jit_test.py +143 -143
- brainstate/transform/_loop_collect_return.py +675 -675
- brainstate/transform/_loop_collect_return_test.py +58 -58
- brainstate/transform/_loop_no_collection.py +283 -283
- brainstate/transform/_loop_no_collection_test.py +50 -50
- brainstate/transform/_make_jaxpr.py +2176 -2016
- brainstate/transform/_make_jaxpr_test.py +1634 -1510
- brainstate/transform/_mapping.py +607 -529
- brainstate/transform/_mapping_test.py +104 -194
- brainstate/transform/_progress_bar.py +255 -255
- brainstate/transform/_unvmap.py +256 -256
- brainstate/transform/_util.py +286 -286
- brainstate/typing.py +837 -837
- brainstate/typing_test.py +780 -780
- brainstate/util/__init__.py +27 -27
- brainstate/util/_others.py +1024 -1024
- brainstate/util/_others_test.py +962 -962
- brainstate/util/_pretty_pytree.py +1301 -1301
- brainstate/util/_pretty_pytree_test.py +675 -675
- brainstate/util/_pretty_repr.py +462 -462
- brainstate/util/_pretty_repr_test.py +696 -696
- brainstate/util/filter.py +945 -945
- brainstate/util/filter_test.py +911 -911
- brainstate/util/struct.py +910 -910
- brainstate/util/struct_test.py +602 -602
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/METADATA +108 -108
- brainstate-0.2.2.dist-info/RECORD +111 -0
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/licenses/LICENSE +202 -202
- brainstate/transform/_eval_shape.py +0 -145
- brainstate/transform/_eval_shape_test.py +0 -38
- brainstate/transform/_random.py +0 -171
- brainstate-0.2.1.dist-info/RECORD +0 -111
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/WHEEL +0 -0
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/top_level.txt +0 -0
brainstate/nn/_dropout_test.py
CHANGED
@@ -1,477 +1,480 @@
|
|
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
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
np.
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
np.
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
with
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
#
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
with
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
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
|
+
def setUp(self):
|
150
|
+
brainstate.random.seed(0)
|
151
|
+
|
152
|
+
def test_dropout2d_basic(self):
|
153
|
+
"""Test basic Dropout2d functionality."""
|
154
|
+
with brainstate.random.seed_context(42):
|
155
|
+
dropout_layer = brainstate.nn.Dropout2d(prob=0.5)
|
156
|
+
input_data = brainstate.random.randn(2, 3, 4, 5) # (N, C, H, W)
|
157
|
+
|
158
|
+
with brainstate.environ.context(fit=True):
|
159
|
+
output_data = dropout_layer(input_data)
|
160
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
161
|
+
self.assertTrue(np.any(output_data == 0))
|
162
|
+
|
163
|
+
def test_dropout2d_channel_wise(self):
|
164
|
+
"""Test that Dropout2d applies dropout."""
|
165
|
+
dropout_layer = brainstate.nn.Dropout2d(prob=0.5)
|
166
|
+
input_data = brainstate.random.randn(2, 8, 4, 5) # (N, C, H, W)
|
167
|
+
|
168
|
+
with brainstate.environ.context(fit=True):
|
169
|
+
output_data = dropout_layer(input_data)
|
170
|
+
# Just verify that dropout is applied (shape preserved, some zeros present)
|
171
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
172
|
+
|
173
|
+
def test_dropout2d_without_batch(self):
|
174
|
+
"""Test Dropout2d with unbatched input (C, H, W)."""
|
175
|
+
dropout_layer = brainstate.nn.Dropout2d(prob=0.5)
|
176
|
+
input_data = brainstate.random.randn(3, 4, 5) # (C, H, W)
|
177
|
+
|
178
|
+
with brainstate.environ.context(fit=True):
|
179
|
+
output_data = dropout_layer(input_data)
|
180
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
181
|
+
|
182
|
+
def test_dropout2d_scaling(self):
|
183
|
+
"""Test that Dropout2d correctly scales non-dropped elements."""
|
184
|
+
dropout_layer = brainstate.nn.Dropout2d(prob=0.5)
|
185
|
+
input_data = brainstate.random.randn(2, 3, 4, 5)
|
186
|
+
|
187
|
+
with brainstate.environ.context(fit=True):
|
188
|
+
output_data = dropout_layer(input_data)
|
189
|
+
scale_factor = 1 / (1 - 0.5)
|
190
|
+
mask = ~np.isclose(output_data, 0)
|
191
|
+
non_zero_elements = output_data[mask]
|
192
|
+
expected_non_zero_elements = input_data[mask] * scale_factor
|
193
|
+
np.testing.assert_allclose(non_zero_elements, expected_non_zero_elements, rtol=1e-5)
|
194
|
+
|
195
|
+
def test_dropout2d_eval_mode(self):
|
196
|
+
"""Test that Dropout2d is disabled in eval mode."""
|
197
|
+
dropout_layer = brainstate.nn.Dropout2d(prob=0.5)
|
198
|
+
input_data = brainstate.random.randn(2, 3, 4, 5)
|
199
|
+
|
200
|
+
with brainstate.environ.context(fit=False):
|
201
|
+
output_data = dropout_layer(input_data)
|
202
|
+
np.testing.assert_array_equal(input_data, output_data)
|
203
|
+
|
204
|
+
|
205
|
+
class TestDropout3d(parameterized.TestCase):
|
206
|
+
|
207
|
+
def test_dropout3d_basic(self):
|
208
|
+
"""Test basic Dropout3d functionality."""
|
209
|
+
dropout_layer = brainstate.nn.Dropout3d(prob=0.5)
|
210
|
+
input_data = brainstate.random.randn(2, 3, 4, 5, 6) # (N, C, D, H, W)
|
211
|
+
|
212
|
+
with brainstate.environ.context(fit=True):
|
213
|
+
output_data = dropout_layer(input_data)
|
214
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
215
|
+
self.assertTrue(np.any(output_data == 0))
|
216
|
+
|
217
|
+
def test_dropout3d_channel_wise(self):
|
218
|
+
"""Test that Dropout3d applies dropout."""
|
219
|
+
dropout_layer = brainstate.nn.Dropout3d(prob=0.5)
|
220
|
+
input_data = brainstate.random.randn(2, 8, 4, 5, 6) # (N, C, D, H, W)
|
221
|
+
|
222
|
+
with brainstate.environ.context(fit=True):
|
223
|
+
output_data = dropout_layer(input_data)
|
224
|
+
# Just verify that dropout is applied (shape preserved, some zeros present)
|
225
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
226
|
+
|
227
|
+
def test_dropout3d_without_batch(self):
|
228
|
+
"""Test Dropout3d with unbatched input (C, D, H, W)."""
|
229
|
+
dropout_layer = brainstate.nn.Dropout3d(prob=0.5)
|
230
|
+
input_data = brainstate.random.randn(3, 4, 5, 6) # (C, D, H, W)
|
231
|
+
|
232
|
+
with brainstate.environ.context(fit=True):
|
233
|
+
output_data = dropout_layer(input_data)
|
234
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
235
|
+
|
236
|
+
def test_dropout3d_scaling(self):
|
237
|
+
"""Test that Dropout3d correctly scales non-dropped elements."""
|
238
|
+
dropout_layer = brainstate.nn.Dropout3d(prob=0.5)
|
239
|
+
input_data = brainstate.random.randn(2, 3, 4, 5, 6)
|
240
|
+
|
241
|
+
with brainstate.environ.context(fit=True):
|
242
|
+
output_data = dropout_layer(input_data)
|
243
|
+
scale_factor = 1 / (1 - 0.5)
|
244
|
+
mask = ~np.isclose(output_data, 0)
|
245
|
+
non_zero_elements = output_data[mask]
|
246
|
+
expected_non_zero_elements = input_data[mask] * scale_factor
|
247
|
+
np.testing.assert_allclose(non_zero_elements, expected_non_zero_elements, rtol=1e-5)
|
248
|
+
|
249
|
+
def test_dropout3d_eval_mode(self):
|
250
|
+
"""Test that Dropout3d is disabled in eval mode."""
|
251
|
+
dropout_layer = brainstate.nn.Dropout3d(prob=0.5)
|
252
|
+
input_data = brainstate.random.randn(2, 3, 4, 5, 6)
|
253
|
+
|
254
|
+
with brainstate.environ.context(fit=False):
|
255
|
+
output_data = dropout_layer(input_data)
|
256
|
+
np.testing.assert_array_equal(input_data, output_data)
|
257
|
+
|
258
|
+
|
259
|
+
class TestAlphaDropout(parameterized.TestCase):
|
260
|
+
|
261
|
+
def test_alphadropout_basic(self):
|
262
|
+
"""Test basic AlphaDropout functionality."""
|
263
|
+
dropout_layer = brainstate.nn.AlphaDropout(prob=0.5)
|
264
|
+
input_data = brainstate.random.randn(100, 50)
|
265
|
+
|
266
|
+
with brainstate.environ.context(fit=True):
|
267
|
+
output_data = dropout_layer(input_data)
|
268
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
269
|
+
|
270
|
+
def test_alphadropout_self_normalizing(self):
|
271
|
+
"""Test that AlphaDropout maintains zero mean and unit variance."""
|
272
|
+
dropout_layer = brainstate.nn.AlphaDropout(prob=0.5)
|
273
|
+
# Create input with zero mean and unit variance
|
274
|
+
input_data = brainstate.random.randn(10000)
|
275
|
+
|
276
|
+
with brainstate.environ.context(fit=True):
|
277
|
+
output_data = dropout_layer(input_data)
|
278
|
+
|
279
|
+
# The output should approximately maintain zero mean and unit variance
|
280
|
+
output_mean = np.mean(output_data)
|
281
|
+
output_std = np.std(output_data)
|
282
|
+
|
283
|
+
# Allow some tolerance due to randomness
|
284
|
+
self.assertAlmostEqual(output_mean, 0.0, delta=0.1)
|
285
|
+
self.assertAlmostEqual(output_std, 1.0, delta=0.2)
|
286
|
+
|
287
|
+
def test_alphadropout_alpha_value(self):
|
288
|
+
"""Test that dropped values are set to alpha (not zero)."""
|
289
|
+
dropout_layer = brainstate.nn.AlphaDropout(prob=0.5)
|
290
|
+
input_data = brainstate.random.randn(1000) + 5.0 # Shift to avoid confusion with alpha
|
291
|
+
|
292
|
+
with brainstate.environ.context(fit=True):
|
293
|
+
output_data = dropout_layer(input_data)
|
294
|
+
|
295
|
+
# AlphaDropout should not have zeros, but values close to transformed alpha
|
296
|
+
# After affine transformation: alpha * a + b
|
297
|
+
expected_dropped_value = dropout_layer.alpha * dropout_layer.a + dropout_layer.b
|
298
|
+
|
299
|
+
# Check that we have some values close to the expected dropped value
|
300
|
+
# (within reasonable tolerance due to numerical precision)
|
301
|
+
unique_vals = np.unique(np.round(output_data, 3))
|
302
|
+
self.assertGreater(len(unique_vals), 1) # Should have both dropped and kept values
|
303
|
+
|
304
|
+
def test_alphadropout_eval_mode(self):
|
305
|
+
"""Test that AlphaDropout is disabled in eval mode."""
|
306
|
+
dropout_layer = brainstate.nn.AlphaDropout(prob=0.5)
|
307
|
+
input_data = brainstate.random.randn(20, 16)
|
308
|
+
|
309
|
+
with brainstate.environ.context(fit=False):
|
310
|
+
output_data = dropout_layer(input_data)
|
311
|
+
np.testing.assert_array_equal(input_data, output_data)
|
312
|
+
|
313
|
+
@parameterized.parameters(0.2, 0.5, 0.8)
|
314
|
+
def test_alphadropout_various_probs(self, prob):
|
315
|
+
"""Test AlphaDropout with various probabilities."""
|
316
|
+
dropout_layer = brainstate.nn.AlphaDropout(prob=prob)
|
317
|
+
input_data = brainstate.random.randn(1000)
|
318
|
+
|
319
|
+
with brainstate.environ.context(fit=True):
|
320
|
+
output_data = dropout_layer(input_data)
|
321
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
322
|
+
|
323
|
+
def test_alphadropout_multidimensional(self):
|
324
|
+
"""Test AlphaDropout with multidimensional input."""
|
325
|
+
dropout_layer = brainstate.nn.AlphaDropout(prob=0.5)
|
326
|
+
input_data = brainstate.random.randn(10, 20, 30)
|
327
|
+
|
328
|
+
with brainstate.environ.context(fit=True):
|
329
|
+
output_data = dropout_layer(input_data)
|
330
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
331
|
+
|
332
|
+
|
333
|
+
class TestFeatureAlphaDropout(parameterized.TestCase):
|
334
|
+
|
335
|
+
def test_featurealphadropout_basic(self):
|
336
|
+
"""Test basic FeatureAlphaDropout functionality."""
|
337
|
+
dropout_layer = brainstate.nn.FeatureAlphaDropout(prob=0.5)
|
338
|
+
input_data = brainstate.random.randn(2, 16, 4, 32, 32) # (N, C, D, H, W)
|
339
|
+
|
340
|
+
with brainstate.environ.context(fit=True):
|
341
|
+
output_data = dropout_layer(input_data)
|
342
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
343
|
+
|
344
|
+
def test_featurealphadropout_channel_wise(self):
|
345
|
+
"""Test that FeatureAlphaDropout drops entire channels."""
|
346
|
+
dropout_layer = brainstate.nn.FeatureAlphaDropout(prob=0.5, channel_axis=1)
|
347
|
+
input_data = brainstate.random.randn(2, 8, 10, 10) # (N, C, H, W)
|
348
|
+
|
349
|
+
with brainstate.environ.context(fit=True):
|
350
|
+
output_data = dropout_layer(input_data)
|
351
|
+
|
352
|
+
# Check that entire channels share the same dropout mask
|
353
|
+
for batch in range(2):
|
354
|
+
for channel in range(8):
|
355
|
+
channel_data = output_data[batch, channel, :, :]
|
356
|
+
# All elements in a channel should be either all from input or all alpha-transformed
|
357
|
+
unique_vals = np.unique(np.round(channel_data, 4))
|
358
|
+
# Due to the alpha transformation, we can't easily check, but shape should match
|
359
|
+
self.assertEqual(channel_data.shape, (10, 10))
|
360
|
+
|
361
|
+
def test_featurealphadropout_channel_axis(self):
|
362
|
+
"""Test FeatureAlphaDropout with different channel axes."""
|
363
|
+
# Test with channel_axis=-1
|
364
|
+
dropout_layer = brainstate.nn.FeatureAlphaDropout(prob=0.5, channel_axis=-1)
|
365
|
+
input_data = brainstate.random.randn(2, 10, 10, 8) # (N, H, W, C)
|
366
|
+
|
367
|
+
with brainstate.environ.context(fit=True):
|
368
|
+
output_data = dropout_layer(input_data)
|
369
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
370
|
+
|
371
|
+
def test_featurealphadropout_eval_mode(self):
|
372
|
+
"""Test that FeatureAlphaDropout is disabled in eval mode."""
|
373
|
+
dropout_layer = brainstate.nn.FeatureAlphaDropout(prob=0.5)
|
374
|
+
input_data = brainstate.random.randn(2, 16, 4, 32, 32)
|
375
|
+
|
376
|
+
with brainstate.environ.context(fit=False):
|
377
|
+
output_data = dropout_layer(input_data)
|
378
|
+
np.testing.assert_array_equal(input_data, output_data)
|
379
|
+
|
380
|
+
def test_featurealphadropout_self_normalizing(self):
|
381
|
+
"""Test that FeatureAlphaDropout maintains self-normalizing properties."""
|
382
|
+
dropout_layer = brainstate.nn.FeatureAlphaDropout(prob=0.5, channel_axis=1)
|
383
|
+
# Create input with zero mean and unit variance
|
384
|
+
input_data = brainstate.random.randn(10, 32, 20)
|
385
|
+
|
386
|
+
with brainstate.environ.context(fit=True):
|
387
|
+
output_data = dropout_layer(input_data)
|
388
|
+
|
389
|
+
# The output should approximately maintain zero mean and unit variance
|
390
|
+
output_mean = np.mean(output_data)
|
391
|
+
output_std = np.std(output_data)
|
392
|
+
|
393
|
+
# Allow some tolerance due to randomness
|
394
|
+
self.assertAlmostEqual(output_mean, 0.0, delta=0.2)
|
395
|
+
self.assertAlmostEqual(output_std, 1.0, delta=0.3)
|
396
|
+
|
397
|
+
|
398
|
+
class TestDropoutFixed(parameterized.TestCase):
|
399
|
+
|
400
|
+
def test_dropoutfixed_basic(self):
|
401
|
+
"""Test basic DropoutFixed functionality."""
|
402
|
+
with brainstate.random.seed_context(42):
|
403
|
+
dropout_layer = brainstate.nn.DropoutFixed(in_size=(2, 3), prob=0.5)
|
404
|
+
dropout_layer.init_state(batch_size=2)
|
405
|
+
input_data = np.random.randn(2, 2, 3)
|
406
|
+
|
407
|
+
with brainstate.environ.context(fit=True):
|
408
|
+
output_data = dropout_layer.update(input_data)
|
409
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
410
|
+
self.assertTrue(np.any(output_data == 0))
|
411
|
+
|
412
|
+
def test_dropoutfixed_mask_persistence(self):
|
413
|
+
"""Test that DropoutFixed uses the same mask across multiple calls."""
|
414
|
+
with brainstate.random.seed_context(42):
|
415
|
+
dropout_layer = brainstate.nn.DropoutFixed(in_size=(10,), prob=0.5)
|
416
|
+
dropout_layer.init_state(batch_size=5)
|
417
|
+
|
418
|
+
input_data1 = brainstate.random.randn(5, 10)
|
419
|
+
input_data2 = brainstate.random.randn(5, 10)
|
420
|
+
|
421
|
+
with brainstate.environ.context(fit=True):
|
422
|
+
output_data1 = dropout_layer.update(input_data1)
|
423
|
+
output_data2 = dropout_layer.update(input_data2)
|
424
|
+
|
425
|
+
# The dropout mask should be the same for both calls
|
426
|
+
mask1 = (output_data1 == 0)
|
427
|
+
mask2 = (output_data2 == 0)
|
428
|
+
np.testing.assert_array_equal(mask1, mask2)
|
429
|
+
|
430
|
+
def test_dropoutfixed_scaling(self):
|
431
|
+
"""Test that DropoutFixed correctly scales non-dropped elements."""
|
432
|
+
with brainstate.random.seed_context(42):
|
433
|
+
dropout_layer = brainstate.nn.DropoutFixed(in_size=(2, 3), prob=0.5)
|
434
|
+
dropout_layer.init_state(batch_size=2)
|
435
|
+
input_data = np.random.randn(2, 2, 3)
|
436
|
+
|
437
|
+
with brainstate.environ.context(fit=True):
|
438
|
+
output_data = dropout_layer.update(input_data)
|
439
|
+
scale_factor = 1 / (1 - 0.5)
|
440
|
+
non_zero_elements = output_data[output_data != 0]
|
441
|
+
expected_non_zero_elements = input_data[output_data != 0] * scale_factor
|
442
|
+
np.testing.assert_almost_equal(non_zero_elements, expected_non_zero_elements, decimal=3)
|
443
|
+
|
444
|
+
def test_dropoutfixed_eval_mode(self):
|
445
|
+
"""Test that DropoutFixed is disabled in eval mode."""
|
446
|
+
with brainstate.random.seed_context(42):
|
447
|
+
dropout_layer = brainstate.nn.DropoutFixed(in_size=(2, 3), prob=0.5)
|
448
|
+
dropout_layer.init_state(batch_size=2)
|
449
|
+
input_data = np.random.randn(2, 2, 3)
|
450
|
+
|
451
|
+
with brainstate.environ.context(fit=False):
|
452
|
+
output_data = dropout_layer.update(input_data)
|
453
|
+
np.testing.assert_array_equal(input_data, output_data)
|
454
|
+
|
455
|
+
def test_dropoutfixed_shape_mismatch(self):
|
456
|
+
"""Test that DropoutFixed raises error for shape mismatch."""
|
457
|
+
with brainstate.random.seed_context(42):
|
458
|
+
dropout_layer = brainstate.nn.DropoutFixed(in_size=(2, 3), prob=0.5)
|
459
|
+
dropout_layer.init_state(batch_size=2)
|
460
|
+
input_data = np.random.randn(3, 2, 3) # Wrong batch size
|
461
|
+
|
462
|
+
with brainstate.environ.context(fit=True):
|
463
|
+
with self.assertRaises(ValueError):
|
464
|
+
dropout_layer.update(input_data)
|
465
|
+
|
466
|
+
@parameterized.parameters(0.2, 0.5, 0.8)
|
467
|
+
def test_dropoutfixed_various_probs(self, prob):
|
468
|
+
"""Test DropoutFixed with various probabilities."""
|
469
|
+
with brainstate.random.seed_context(42):
|
470
|
+
dropout_layer = brainstate.nn.DropoutFixed(in_size=(10,), prob=prob)
|
471
|
+
dropout_layer.init_state(batch_size=5)
|
472
|
+
input_data = brainstate.random.randn(5, 10)
|
473
|
+
|
474
|
+
with brainstate.environ.context(fit=True):
|
475
|
+
output_data = dropout_layer.update(input_data)
|
476
|
+
self.assertEqual(input_data.shape, output_data.shape)
|
477
|
+
|
478
|
+
|
479
|
+
if __name__ == '__main__':
|
480
|
+
absltest.main()
|