brainstate 0.1.8__py2.py3-none-any.whl → 0.1.9__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- brainstate/__init__.py +58 -51
- brainstate/_compatible_import.py +148 -148
- brainstate/_state.py +1605 -1663
- brainstate/_state_test.py +52 -52
- brainstate/_utils.py +47 -47
- brainstate/augment/__init__.py +30 -30
- brainstate/augment/_autograd.py +778 -778
- brainstate/augment/_autograd_test.py +1289 -1289
- brainstate/augment/_eval_shape.py +99 -99
- brainstate/augment/_eval_shape_test.py +38 -38
- brainstate/augment/_mapping.py +1060 -1060
- brainstate/augment/_mapping_test.py +597 -597
- brainstate/augment/_random.py +151 -151
- brainstate/compile/__init__.py +38 -38
- brainstate/compile/_ad_checkpoint.py +204 -204
- brainstate/compile/_ad_checkpoint_test.py +49 -49
- brainstate/compile/_conditions.py +256 -256
- brainstate/compile/_conditions_test.py +220 -220
- brainstate/compile/_error_if.py +92 -92
- brainstate/compile/_error_if_test.py +52 -52
- brainstate/compile/_jit.py +346 -346
- brainstate/compile/_jit_test.py +143 -143
- brainstate/compile/_loop_collect_return.py +536 -536
- brainstate/compile/_loop_collect_return_test.py +58 -58
- brainstate/compile/_loop_no_collection.py +184 -184
- brainstate/compile/_loop_no_collection_test.py +50 -50
- brainstate/compile/_make_jaxpr.py +888 -888
- brainstate/compile/_make_jaxpr_test.py +156 -156
- brainstate/compile/_progress_bar.py +202 -202
- brainstate/compile/_unvmap.py +159 -159
- brainstate/compile/_util.py +147 -147
- brainstate/environ.py +563 -563
- brainstate/environ_test.py +62 -62
- brainstate/functional/__init__.py +27 -26
- brainstate/graph/__init__.py +29 -29
- brainstate/graph/_graph_node.py +244 -244
- brainstate/graph/_graph_node_test.py +73 -73
- brainstate/graph/_graph_operation.py +1738 -1738
- brainstate/graph/_graph_operation_test.py +563 -563
- brainstate/init/__init__.py +26 -26
- brainstate/init/_base.py +52 -52
- brainstate/init/_generic.py +244 -244
- brainstate/init/_random_inits.py +553 -553
- brainstate/init/_random_inits_test.py +149 -149
- brainstate/init/_regular_inits.py +105 -105
- brainstate/init/_regular_inits_test.py +50 -50
- brainstate/mixin.py +365 -363
- brainstate/mixin_test.py +77 -73
- brainstate/nn/__init__.py +135 -131
- brainstate/{functional → nn}/_activations.py +808 -813
- brainstate/{functional → nn}/_activations_test.py +331 -331
- brainstate/nn/_collective_ops.py +514 -514
- brainstate/nn/_collective_ops_test.py +43 -43
- brainstate/nn/_common.py +178 -178
- brainstate/nn/_conv.py +501 -501
- brainstate/nn/_conv_test.py +238 -238
- brainstate/nn/_delay.py +509 -502
- brainstate/nn/_delay_test.py +238 -184
- brainstate/nn/_dropout.py +426 -426
- brainstate/nn/_dropout_test.py +100 -100
- brainstate/nn/_dynamics.py +1343 -1343
- brainstate/nn/_dynamics_test.py +78 -78
- brainstate/nn/_elementwise.py +1119 -1119
- brainstate/nn/_elementwise_test.py +169 -169
- brainstate/nn/_embedding.py +58 -58
- brainstate/nn/_exp_euler.py +92 -92
- brainstate/nn/_exp_euler_test.py +35 -35
- brainstate/nn/_fixedprob.py +239 -239
- brainstate/nn/_fixedprob_test.py +114 -114
- brainstate/nn/_inputs.py +608 -608
- brainstate/nn/_linear.py +424 -424
- brainstate/nn/_linear_mv.py +83 -83
- brainstate/nn/_linear_mv_test.py +120 -120
- brainstate/nn/_linear_test.py +107 -107
- brainstate/nn/_ltp.py +28 -28
- brainstate/nn/_module.py +377 -377
- brainstate/nn/_module_test.py +40 -40
- brainstate/nn/_neuron.py +705 -705
- brainstate/nn/_neuron_test.py +161 -161
- brainstate/nn/_normalizations.py +975 -918
- brainstate/nn/_normalizations_test.py +73 -73
- brainstate/{functional → nn}/_others.py +46 -46
- brainstate/nn/_poolings.py +1177 -1177
- brainstate/nn/_poolings_test.py +217 -217
- brainstate/nn/_projection.py +486 -486
- brainstate/nn/_rate_rnns.py +554 -554
- brainstate/nn/_rate_rnns_test.py +63 -63
- brainstate/nn/_readout.py +209 -209
- brainstate/nn/_readout_test.py +53 -53
- brainstate/nn/_stp.py +236 -236
- brainstate/nn/_synapse.py +505 -505
- brainstate/nn/_synapse_test.py +131 -131
- brainstate/nn/_synaptic_projection.py +423 -423
- brainstate/nn/_synouts.py +162 -162
- brainstate/nn/_synouts_test.py +57 -57
- brainstate/nn/_utils.py +89 -89
- brainstate/nn/metrics.py +388 -388
- brainstate/optim/__init__.py +38 -38
- brainstate/optim/_base.py +64 -64
- brainstate/optim/_lr_scheduler.py +448 -448
- brainstate/optim/_lr_scheduler_test.py +50 -50
- brainstate/optim/_optax_optimizer.py +152 -152
- brainstate/optim/_optax_optimizer_test.py +53 -53
- brainstate/optim/_sgd_optimizer.py +1104 -1104
- brainstate/random/__init__.py +24 -24
- brainstate/random/_rand_funs.py +3616 -3616
- brainstate/random/_rand_funs_test.py +567 -567
- brainstate/random/_rand_seed.py +210 -210
- brainstate/random/_rand_seed_test.py +48 -48
- brainstate/random/_rand_state.py +1409 -1409
- brainstate/random/_random_for_unit.py +52 -52
- brainstate/surrogate.py +1957 -1957
- brainstate/transform.py +23 -23
- brainstate/typing.py +304 -304
- brainstate/util/__init__.py +50 -50
- brainstate/util/caller.py +98 -98
- brainstate/util/error.py +55 -55
- brainstate/util/filter.py +469 -469
- brainstate/util/others.py +540 -540
- brainstate/util/pretty_pytree.py +945 -945
- brainstate/util/pretty_pytree_test.py +159 -159
- brainstate/util/pretty_repr.py +328 -328
- brainstate/util/pretty_table.py +2954 -2954
- brainstate/util/scaling.py +258 -258
- brainstate/util/struct.py +523 -523
- {brainstate-0.1.8.dist-info → brainstate-0.1.9.dist-info}/METADATA +91 -99
- brainstate-0.1.9.dist-info/RECORD +130 -0
- {brainstate-0.1.8.dist-info → brainstate-0.1.9.dist-info}/WHEEL +1 -1
- {brainstate-0.1.8.dist-info → brainstate-0.1.9.dist-info/licenses}/LICENSE +202 -202
- brainstate/functional/_normalization.py +0 -81
- brainstate/functional/_spikes.py +0 -204
- brainstate-0.1.8.dist-info/RECORD +0 -132
- {brainstate-0.1.8.dist-info → brainstate-0.1.9.dist-info}/top_level.txt +0 -0
@@ -1,149 +1,149 @@
|
|
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
|
-
import unittest
|
19
|
-
|
20
|
-
import brainstate
|
21
|
-
|
22
|
-
|
23
|
-
class TestNormalInit(unittest.TestCase):
|
24
|
-
|
25
|
-
def test_normal_init1(self):
|
26
|
-
init = brainstate.init.Normal()
|
27
|
-
for size in [(100,), (10, 20), (10, 20, 30)]:
|
28
|
-
weights = init(size)
|
29
|
-
assert weights.shape == size
|
30
|
-
|
31
|
-
def test_normal_init2(self):
|
32
|
-
init = brainstate.init.Normal(scale=0.5)
|
33
|
-
for size in [(100,), (10, 20)]:
|
34
|
-
weights = init(size)
|
35
|
-
assert weights.shape == size
|
36
|
-
|
37
|
-
def test_normal_init3(self):
|
38
|
-
init1 = brainstate.init.Normal(scale=0.5, seed=10)
|
39
|
-
init2 = brainstate.init.Normal(scale=0.5, seed=10)
|
40
|
-
size = (10,)
|
41
|
-
weights1 = init1(size)
|
42
|
-
weights2 = init2(size)
|
43
|
-
assert weights1.shape == size
|
44
|
-
assert (weights1 == weights2).all()
|
45
|
-
|
46
|
-
|
47
|
-
class TestUniformInit(unittest.TestCase):
|
48
|
-
def test_uniform_init1(self):
|
49
|
-
init = brainstate.init.Normal()
|
50
|
-
for size in [(100,), (10, 20), (10, 20, 30)]:
|
51
|
-
weights = init(size)
|
52
|
-
assert weights.shape == size
|
53
|
-
|
54
|
-
def test_uniform_init2(self):
|
55
|
-
init = brainstate.init.Uniform(min_val=10, max_val=20)
|
56
|
-
for size in [(100,), (10, 20)]:
|
57
|
-
weights = init(size)
|
58
|
-
assert weights.shape == size
|
59
|
-
|
60
|
-
|
61
|
-
class TestVarianceScaling(unittest.TestCase):
|
62
|
-
def test_var_scaling1(self):
|
63
|
-
init = brainstate.init.VarianceScaling(scale=1., mode='fan_in', distribution='truncated_normal')
|
64
|
-
for size in [(10, 20), (10, 20, 30)]:
|
65
|
-
weights = init(size)
|
66
|
-
assert weights.shape == size
|
67
|
-
|
68
|
-
def test_var_scaling2(self):
|
69
|
-
init = brainstate.init.VarianceScaling(scale=2, mode='fan_out', distribution='normal')
|
70
|
-
for size in [(10, 20), (10, 20, 30)]:
|
71
|
-
weights = init(size)
|
72
|
-
assert weights.shape == size
|
73
|
-
|
74
|
-
def test_var_scaling3(self):
|
75
|
-
init = brainstate.init.VarianceScaling(scale=2 / 4, mode='fan_avg', in_axis=0, out_axis=1,
|
76
|
-
distribution='uniform')
|
77
|
-
for size in [(10, 20), (10, 20, 30)]:
|
78
|
-
weights = init(size)
|
79
|
-
assert weights.shape == size
|
80
|
-
|
81
|
-
|
82
|
-
class TestKaimingUniformUnit(unittest.TestCase):
|
83
|
-
def test_kaiming_uniform_init(self):
|
84
|
-
init = brainstate.init.KaimingUniform()
|
85
|
-
for size in [(10, 20), (10, 20, 30)]:
|
86
|
-
weights = init(size)
|
87
|
-
assert weights.shape == size
|
88
|
-
|
89
|
-
|
90
|
-
class TestKaimingNormalUnit(unittest.TestCase):
|
91
|
-
def test_kaiming_normal_init(self):
|
92
|
-
init = brainstate.init.KaimingNormal()
|
93
|
-
for size in [(10, 20), (10, 20, 30)]:
|
94
|
-
weights = init(size)
|
95
|
-
assert weights.shape == size
|
96
|
-
|
97
|
-
|
98
|
-
class TestXavierUniformUnit(unittest.TestCase):
|
99
|
-
def test_xavier_uniform_init(self):
|
100
|
-
init = brainstate.init.XavierUniform()
|
101
|
-
for size in [(10, 20), (10, 20, 30)]:
|
102
|
-
weights = init(size)
|
103
|
-
assert weights.shape == size
|
104
|
-
|
105
|
-
|
106
|
-
class TestXavierNormalUnit(unittest.TestCase):
|
107
|
-
def test_xavier_normal_init(self):
|
108
|
-
init = brainstate.init.XavierNormal()
|
109
|
-
for size in [(10, 20), (10, 20, 30)]:
|
110
|
-
weights = init(size)
|
111
|
-
assert weights.shape == size
|
112
|
-
|
113
|
-
|
114
|
-
class TestLecunUniformUnit(unittest.TestCase):
|
115
|
-
def test_lecun_uniform_init(self):
|
116
|
-
init = brainstate.init.LecunUniform()
|
117
|
-
for size in [(10, 20), (10, 20, 30)]:
|
118
|
-
weights = init(size)
|
119
|
-
assert weights.shape == size
|
120
|
-
|
121
|
-
|
122
|
-
class TestLecunNormalUnit(unittest.TestCase):
|
123
|
-
def test_lecun_normal_init(self):
|
124
|
-
init = brainstate.init.LecunNormal()
|
125
|
-
for size in [(10, 20), (10, 20, 30)]:
|
126
|
-
weights = init(size)
|
127
|
-
assert weights.shape == size
|
128
|
-
|
129
|
-
|
130
|
-
class TestOrthogonalUnit(unittest.TestCase):
|
131
|
-
def test_orthogonal_init1(self):
|
132
|
-
init = brainstate.init.Orthogonal()
|
133
|
-
for size in [(20, 20), (10, 20, 30)]:
|
134
|
-
weights = init(size)
|
135
|
-
assert weights.shape == size
|
136
|
-
|
137
|
-
def test_orthogonal_init2(self):
|
138
|
-
init = brainstate.init.Orthogonal(scale=2., axis=0)
|
139
|
-
for size in [(10, 20), (10, 20, 30)]:
|
140
|
-
weights = init(size)
|
141
|
-
assert weights.shape == size
|
142
|
-
|
143
|
-
|
144
|
-
class TestDeltaOrthogonalUnit(unittest.TestCase):
|
145
|
-
def test_delta_orthogonal_init1(self):
|
146
|
-
init = brainstate.init.DeltaOrthogonal()
|
147
|
-
for size in [(20, 20, 20), (10, 20, 30, 40), (50, 40, 30, 20, 20)]:
|
148
|
-
weights = init(size)
|
149
|
-
assert weights.shape == size
|
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
|
+
import unittest
|
19
|
+
|
20
|
+
import brainstate
|
21
|
+
|
22
|
+
|
23
|
+
class TestNormalInit(unittest.TestCase):
|
24
|
+
|
25
|
+
def test_normal_init1(self):
|
26
|
+
init = brainstate.init.Normal()
|
27
|
+
for size in [(100,), (10, 20), (10, 20, 30)]:
|
28
|
+
weights = init(size)
|
29
|
+
assert weights.shape == size
|
30
|
+
|
31
|
+
def test_normal_init2(self):
|
32
|
+
init = brainstate.init.Normal(scale=0.5)
|
33
|
+
for size in [(100,), (10, 20)]:
|
34
|
+
weights = init(size)
|
35
|
+
assert weights.shape == size
|
36
|
+
|
37
|
+
def test_normal_init3(self):
|
38
|
+
init1 = brainstate.init.Normal(scale=0.5, seed=10)
|
39
|
+
init2 = brainstate.init.Normal(scale=0.5, seed=10)
|
40
|
+
size = (10,)
|
41
|
+
weights1 = init1(size)
|
42
|
+
weights2 = init2(size)
|
43
|
+
assert weights1.shape == size
|
44
|
+
assert (weights1 == weights2).all()
|
45
|
+
|
46
|
+
|
47
|
+
class TestUniformInit(unittest.TestCase):
|
48
|
+
def test_uniform_init1(self):
|
49
|
+
init = brainstate.init.Normal()
|
50
|
+
for size in [(100,), (10, 20), (10, 20, 30)]:
|
51
|
+
weights = init(size)
|
52
|
+
assert weights.shape == size
|
53
|
+
|
54
|
+
def test_uniform_init2(self):
|
55
|
+
init = brainstate.init.Uniform(min_val=10, max_val=20)
|
56
|
+
for size in [(100,), (10, 20)]:
|
57
|
+
weights = init(size)
|
58
|
+
assert weights.shape == size
|
59
|
+
|
60
|
+
|
61
|
+
class TestVarianceScaling(unittest.TestCase):
|
62
|
+
def test_var_scaling1(self):
|
63
|
+
init = brainstate.init.VarianceScaling(scale=1., mode='fan_in', distribution='truncated_normal')
|
64
|
+
for size in [(10, 20), (10, 20, 30)]:
|
65
|
+
weights = init(size)
|
66
|
+
assert weights.shape == size
|
67
|
+
|
68
|
+
def test_var_scaling2(self):
|
69
|
+
init = brainstate.init.VarianceScaling(scale=2, mode='fan_out', distribution='normal')
|
70
|
+
for size in [(10, 20), (10, 20, 30)]:
|
71
|
+
weights = init(size)
|
72
|
+
assert weights.shape == size
|
73
|
+
|
74
|
+
def test_var_scaling3(self):
|
75
|
+
init = brainstate.init.VarianceScaling(scale=2 / 4, mode='fan_avg', in_axis=0, out_axis=1,
|
76
|
+
distribution='uniform')
|
77
|
+
for size in [(10, 20), (10, 20, 30)]:
|
78
|
+
weights = init(size)
|
79
|
+
assert weights.shape == size
|
80
|
+
|
81
|
+
|
82
|
+
class TestKaimingUniformUnit(unittest.TestCase):
|
83
|
+
def test_kaiming_uniform_init(self):
|
84
|
+
init = brainstate.init.KaimingUniform()
|
85
|
+
for size in [(10, 20), (10, 20, 30)]:
|
86
|
+
weights = init(size)
|
87
|
+
assert weights.shape == size
|
88
|
+
|
89
|
+
|
90
|
+
class TestKaimingNormalUnit(unittest.TestCase):
|
91
|
+
def test_kaiming_normal_init(self):
|
92
|
+
init = brainstate.init.KaimingNormal()
|
93
|
+
for size in [(10, 20), (10, 20, 30)]:
|
94
|
+
weights = init(size)
|
95
|
+
assert weights.shape == size
|
96
|
+
|
97
|
+
|
98
|
+
class TestXavierUniformUnit(unittest.TestCase):
|
99
|
+
def test_xavier_uniform_init(self):
|
100
|
+
init = brainstate.init.XavierUniform()
|
101
|
+
for size in [(10, 20), (10, 20, 30)]:
|
102
|
+
weights = init(size)
|
103
|
+
assert weights.shape == size
|
104
|
+
|
105
|
+
|
106
|
+
class TestXavierNormalUnit(unittest.TestCase):
|
107
|
+
def test_xavier_normal_init(self):
|
108
|
+
init = brainstate.init.XavierNormal()
|
109
|
+
for size in [(10, 20), (10, 20, 30)]:
|
110
|
+
weights = init(size)
|
111
|
+
assert weights.shape == size
|
112
|
+
|
113
|
+
|
114
|
+
class TestLecunUniformUnit(unittest.TestCase):
|
115
|
+
def test_lecun_uniform_init(self):
|
116
|
+
init = brainstate.init.LecunUniform()
|
117
|
+
for size in [(10, 20), (10, 20, 30)]:
|
118
|
+
weights = init(size)
|
119
|
+
assert weights.shape == size
|
120
|
+
|
121
|
+
|
122
|
+
class TestLecunNormalUnit(unittest.TestCase):
|
123
|
+
def test_lecun_normal_init(self):
|
124
|
+
init = brainstate.init.LecunNormal()
|
125
|
+
for size in [(10, 20), (10, 20, 30)]:
|
126
|
+
weights = init(size)
|
127
|
+
assert weights.shape == size
|
128
|
+
|
129
|
+
|
130
|
+
class TestOrthogonalUnit(unittest.TestCase):
|
131
|
+
def test_orthogonal_init1(self):
|
132
|
+
init = brainstate.init.Orthogonal()
|
133
|
+
for size in [(20, 20), (10, 20, 30)]:
|
134
|
+
weights = init(size)
|
135
|
+
assert weights.shape == size
|
136
|
+
|
137
|
+
def test_orthogonal_init2(self):
|
138
|
+
init = brainstate.init.Orthogonal(scale=2., axis=0)
|
139
|
+
for size in [(10, 20), (10, 20, 30)]:
|
140
|
+
weights = init(size)
|
141
|
+
assert weights.shape == size
|
142
|
+
|
143
|
+
|
144
|
+
class TestDeltaOrthogonalUnit(unittest.TestCase):
|
145
|
+
def test_delta_orthogonal_init1(self):
|
146
|
+
init = brainstate.init.DeltaOrthogonal()
|
147
|
+
for size in [(20, 20, 20), (10, 20, 30, 40), (50, 40, 30, 20, 20)]:
|
148
|
+
weights = init(size)
|
149
|
+
assert weights.shape == size
|
@@ -1,105 +1,105 @@
|
|
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
|
-
import brainunit as u
|
19
|
-
|
20
|
-
from brainstate import environ
|
21
|
-
from ._base import Initializer, to_size
|
22
|
-
|
23
|
-
__all__ = [
|
24
|
-
'ZeroInit',
|
25
|
-
'Constant',
|
26
|
-
'Identity',
|
27
|
-
]
|
28
|
-
|
29
|
-
|
30
|
-
class ZeroInit(Initializer):
|
31
|
-
"""Zero initializer.
|
32
|
-
|
33
|
-
Initialize the weights with zeros.
|
34
|
-
"""
|
35
|
-
__module__ = 'brainstate.init'
|
36
|
-
|
37
|
-
def __init__(self, unit: u.Unit = u.UNITLESS):
|
38
|
-
super(ZeroInit, self).__init__()
|
39
|
-
self.unit = unit
|
40
|
-
|
41
|
-
def __call__(self, shape, dtype=None):
|
42
|
-
dtype = dtype or environ.dftype()
|
43
|
-
shape = to_size(shape)
|
44
|
-
return u.maybe_decimal(u.math.zeros(shape, dtype=dtype, unit=self.unit))
|
45
|
-
|
46
|
-
|
47
|
-
class Constant(Initializer):
|
48
|
-
"""Constant initializer.
|
49
|
-
|
50
|
-
Initialize the weights with the given values.
|
51
|
-
|
52
|
-
Parameters
|
53
|
-
----------
|
54
|
-
value : float, int, bm.ndarray
|
55
|
-
The value to specify.
|
56
|
-
"""
|
57
|
-
__module__ = 'brainstate.init'
|
58
|
-
|
59
|
-
def __init__(self, value=1., ):
|
60
|
-
super(Constant, self).__init__()
|
61
|
-
self.value = value
|
62
|
-
|
63
|
-
def __call__(self, shape, dtype=None):
|
64
|
-
dtype = dtype or environ.dftype()
|
65
|
-
shape = to_size(shape)
|
66
|
-
return u.maybe_decimal(u.math.full(shape, self.value, dtype=dtype))
|
67
|
-
|
68
|
-
|
69
|
-
class Identity(Initializer):
|
70
|
-
"""Returns the identity matrix.
|
71
|
-
|
72
|
-
This initializer was proposed in (Le, et al., 2015) [1]_.
|
73
|
-
|
74
|
-
Parameters
|
75
|
-
----------
|
76
|
-
value : float
|
77
|
-
The optional scaling factor.
|
78
|
-
|
79
|
-
Returns
|
80
|
-
-------
|
81
|
-
shape: tuple of int
|
82
|
-
The weight shape/size.
|
83
|
-
|
84
|
-
References
|
85
|
-
----------
|
86
|
-
.. [1] Le, Quoc V., Navdeep Jaitly, and Geoffrey E. Hinton. "A simple way to
|
87
|
-
initialize recurrent networks of rectified linear units." arXiv preprint
|
88
|
-
arXiv:1504.00941 (2015).
|
89
|
-
"""
|
90
|
-
__module__ = 'brainstate.init'
|
91
|
-
|
92
|
-
def __init__(self, value=1., unit: u.Unit = u.UNITLESS):
|
93
|
-
super(Identity, self).__init__()
|
94
|
-
self.value = value
|
95
|
-
self.unit = unit
|
96
|
-
|
97
|
-
def __call__(self, shape, dtype=None):
|
98
|
-
dtype = dtype or environ.dftype()
|
99
|
-
shape = to_size(shape)
|
100
|
-
if isinstance(shape, (tuple, list)):
|
101
|
-
if len(shape) > 2:
|
102
|
-
raise ValueError(f'Only support initialize 2D weights for {self.__class__.__name__}.')
|
103
|
-
r = u.math.eye(*shape, dtype=dtype)
|
104
|
-
r = u.math.fill_diagonal(r, self.value)
|
105
|
-
return u.maybe_decimal(u.Quantity(r, unit=self.unit))
|
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
|
+
import brainunit as u
|
19
|
+
|
20
|
+
from brainstate import environ
|
21
|
+
from ._base import Initializer, to_size
|
22
|
+
|
23
|
+
__all__ = [
|
24
|
+
'ZeroInit',
|
25
|
+
'Constant',
|
26
|
+
'Identity',
|
27
|
+
]
|
28
|
+
|
29
|
+
|
30
|
+
class ZeroInit(Initializer):
|
31
|
+
"""Zero initializer.
|
32
|
+
|
33
|
+
Initialize the weights with zeros.
|
34
|
+
"""
|
35
|
+
__module__ = 'brainstate.init'
|
36
|
+
|
37
|
+
def __init__(self, unit: u.Unit = u.UNITLESS):
|
38
|
+
super(ZeroInit, self).__init__()
|
39
|
+
self.unit = unit
|
40
|
+
|
41
|
+
def __call__(self, shape, dtype=None):
|
42
|
+
dtype = dtype or environ.dftype()
|
43
|
+
shape = to_size(shape)
|
44
|
+
return u.maybe_decimal(u.math.zeros(shape, dtype=dtype, unit=self.unit))
|
45
|
+
|
46
|
+
|
47
|
+
class Constant(Initializer):
|
48
|
+
"""Constant initializer.
|
49
|
+
|
50
|
+
Initialize the weights with the given values.
|
51
|
+
|
52
|
+
Parameters
|
53
|
+
----------
|
54
|
+
value : float, int, bm.ndarray
|
55
|
+
The value to specify.
|
56
|
+
"""
|
57
|
+
__module__ = 'brainstate.init'
|
58
|
+
|
59
|
+
def __init__(self, value=1., ):
|
60
|
+
super(Constant, self).__init__()
|
61
|
+
self.value = value
|
62
|
+
|
63
|
+
def __call__(self, shape, dtype=None):
|
64
|
+
dtype = dtype or environ.dftype()
|
65
|
+
shape = to_size(shape)
|
66
|
+
return u.maybe_decimal(u.math.full(shape, self.value, dtype=dtype))
|
67
|
+
|
68
|
+
|
69
|
+
class Identity(Initializer):
|
70
|
+
"""Returns the identity matrix.
|
71
|
+
|
72
|
+
This initializer was proposed in (Le, et al., 2015) [1]_.
|
73
|
+
|
74
|
+
Parameters
|
75
|
+
----------
|
76
|
+
value : float
|
77
|
+
The optional scaling factor.
|
78
|
+
|
79
|
+
Returns
|
80
|
+
-------
|
81
|
+
shape: tuple of int
|
82
|
+
The weight shape/size.
|
83
|
+
|
84
|
+
References
|
85
|
+
----------
|
86
|
+
.. [1] Le, Quoc V., Navdeep Jaitly, and Geoffrey E. Hinton. "A simple way to
|
87
|
+
initialize recurrent networks of rectified linear units." arXiv preprint
|
88
|
+
arXiv:1504.00941 (2015).
|
89
|
+
"""
|
90
|
+
__module__ = 'brainstate.init'
|
91
|
+
|
92
|
+
def __init__(self, value=1., unit: u.Unit = u.UNITLESS):
|
93
|
+
super(Identity, self).__init__()
|
94
|
+
self.value = value
|
95
|
+
self.unit = unit
|
96
|
+
|
97
|
+
def __call__(self, shape, dtype=None):
|
98
|
+
dtype = dtype or environ.dftype()
|
99
|
+
shape = to_size(shape)
|
100
|
+
if isinstance(shape, (tuple, list)):
|
101
|
+
if len(shape) > 2:
|
102
|
+
raise ValueError(f'Only support initialize 2D weights for {self.__class__.__name__}.')
|
103
|
+
r = u.math.eye(*shape, dtype=dtype)
|
104
|
+
r = u.math.fill_diagonal(r, self.value)
|
105
|
+
return u.maybe_decimal(u.Quantity(r, unit=self.unit))
|
@@ -1,50 +1,50 @@
|
|
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
|
-
import unittest
|
19
|
-
|
20
|
-
import brainstate
|
21
|
-
|
22
|
-
|
23
|
-
class TestZeroInit(unittest.TestCase):
|
24
|
-
def test_zero_init(self):
|
25
|
-
init = brainstate.init.ZeroInit()
|
26
|
-
for size in [(100,), (10, 20), (10, 20, 30)]:
|
27
|
-
weights = init(size)
|
28
|
-
assert weights.shape == size
|
29
|
-
|
30
|
-
|
31
|
-
class TestOneInit(unittest.TestCase):
|
32
|
-
def test_one_init(self):
|
33
|
-
for size in [(100,), (10, 20), (10, 20, 30)]:
|
34
|
-
for value in [0., 1., -1.]:
|
35
|
-
init = brainstate.init.Constant(value=value)
|
36
|
-
weights = init(size)
|
37
|
-
assert weights.shape == size
|
38
|
-
assert (weights == value).all()
|
39
|
-
|
40
|
-
|
41
|
-
class TestIdentityInit(unittest.TestCase):
|
42
|
-
def test_identity_init(self):
|
43
|
-
for size in [(100,), (10, 20)]:
|
44
|
-
for value in [0., 1., -1.]:
|
45
|
-
init = brainstate.init.Identity(value=value)
|
46
|
-
weights = init(size)
|
47
|
-
if len(size) == 1:
|
48
|
-
assert weights.shape == (size[0], size[0])
|
49
|
-
else:
|
50
|
-
assert weights.shape == size
|
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
|
+
import unittest
|
19
|
+
|
20
|
+
import brainstate
|
21
|
+
|
22
|
+
|
23
|
+
class TestZeroInit(unittest.TestCase):
|
24
|
+
def test_zero_init(self):
|
25
|
+
init = brainstate.init.ZeroInit()
|
26
|
+
for size in [(100,), (10, 20), (10, 20, 30)]:
|
27
|
+
weights = init(size)
|
28
|
+
assert weights.shape == size
|
29
|
+
|
30
|
+
|
31
|
+
class TestOneInit(unittest.TestCase):
|
32
|
+
def test_one_init(self):
|
33
|
+
for size in [(100,), (10, 20), (10, 20, 30)]:
|
34
|
+
for value in [0., 1., -1.]:
|
35
|
+
init = brainstate.init.Constant(value=value)
|
36
|
+
weights = init(size)
|
37
|
+
assert weights.shape == size
|
38
|
+
assert (weights == value).all()
|
39
|
+
|
40
|
+
|
41
|
+
class TestIdentityInit(unittest.TestCase):
|
42
|
+
def test_identity_init(self):
|
43
|
+
for size in [(100,), (10, 20)]:
|
44
|
+
for value in [0., 1., -1.]:
|
45
|
+
init = brainstate.init.Identity(value=value)
|
46
|
+
weights = init(size)
|
47
|
+
if len(size) == 1:
|
48
|
+
assert weights.shape == (size[0], size[0])
|
49
|
+
else:
|
50
|
+
assert weights.shape == size
|