brainstate 0.1.10__py2.py3-none-any.whl → 0.2.0__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 +130 -19
  2. brainstate/_compatible_import.py +201 -9
  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} +10 -20
  7. brainstate/_state.py +94 -47
  8. brainstate/_state_test.py +1 -1
  9. brainstate/_utils.py +1 -1
  10. brainstate/environ.py +1279 -347
  11. brainstate/environ_test.py +1187 -26
  12. brainstate/graph/__init__.py +6 -13
  13. brainstate/graph/_node.py +240 -0
  14. brainstate/graph/_node_test.py +589 -0
  15. brainstate/graph/{_graph_operation.py → _operation.py} +632 -746
  16. brainstate/graph/_operation_test.py +1147 -0
  17. brainstate/mixin.py +1209 -141
  18. brainstate/mixin_test.py +991 -51
  19. brainstate/nn/__init__.py +74 -72
  20. brainstate/nn/_activations.py +587 -295
  21. brainstate/nn/_activations_test.py +109 -86
  22. brainstate/nn/_collective_ops.py +393 -274
  23. brainstate/nn/_collective_ops_test.py +746 -15
  24. brainstate/nn/_common.py +114 -66
  25. brainstate/nn/_common_test.py +154 -0
  26. brainstate/nn/_conv.py +1652 -143
  27. brainstate/nn/_conv_test.py +838 -227
  28. brainstate/nn/_delay.py +15 -28
  29. brainstate/nn/_delay_test.py +25 -20
  30. brainstate/nn/_dropout.py +359 -167
  31. brainstate/nn/_dropout_test.py +429 -52
  32. brainstate/nn/_dynamics.py +14 -90
  33. brainstate/nn/_dynamics_test.py +1 -12
  34. brainstate/nn/_elementwise.py +492 -313
  35. brainstate/nn/_elementwise_test.py +806 -145
  36. brainstate/nn/_embedding.py +369 -19
  37. brainstate/nn/_embedding_test.py +156 -0
  38. brainstate/nn/{_fixedprob.py → _event_fixedprob.py} +10 -16
  39. brainstate/nn/{_fixedprob_test.py → _event_fixedprob_test.py} +6 -5
  40. brainstate/nn/{_linear_mv.py → _event_linear.py} +2 -2
  41. brainstate/nn/{_linear_mv_test.py → _event_linear_test.py} +6 -5
  42. brainstate/nn/_exp_euler.py +200 -38
  43. brainstate/nn/_exp_euler_test.py +350 -8
  44. brainstate/nn/_linear.py +391 -71
  45. brainstate/nn/_linear_test.py +427 -59
  46. brainstate/nn/_metrics.py +1070 -0
  47. brainstate/nn/_metrics_test.py +611 -0
  48. brainstate/nn/_module.py +10 -3
  49. brainstate/nn/_module_test.py +1 -1
  50. brainstate/nn/_normalizations.py +688 -329
  51. brainstate/nn/_normalizations_test.py +663 -37
  52. brainstate/nn/_paddings.py +1020 -0
  53. brainstate/nn/_paddings_test.py +723 -0
  54. brainstate/nn/_poolings.py +1404 -342
  55. brainstate/nn/_poolings_test.py +828 -92
  56. brainstate/nn/{_rate_rnns.py → _rnns.py} +446 -54
  57. brainstate/nn/_rnns_test.py +593 -0
  58. brainstate/nn/_utils.py +132 -5
  59. brainstate/nn/_utils_test.py +402 -0
  60. brainstate/{init/_random_inits.py → nn/init.py} +301 -45
  61. brainstate/{init/_random_inits_test.py → nn/init_test.py} +51 -20
  62. brainstate/random/__init__.py +247 -1
  63. brainstate/random/_rand_funs.py +668 -346
  64. brainstate/random/_rand_funs_test.py +74 -1
  65. brainstate/random/_rand_seed.py +541 -76
  66. brainstate/random/_rand_seed_test.py +1 -1
  67. brainstate/random/_rand_state.py +601 -393
  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 +1 -1
  72. brainstate/{augment → transform}/_autograd.py +360 -113
  73. brainstate/{augment → transform}/_autograd_test.py +2 -2
  74. brainstate/transform/_conditions.py +316 -0
  75. brainstate/{compile → transform}/_conditions_test.py +11 -11
  76. brainstate/{compile → transform}/_error_if.py +22 -20
  77. brainstate/{compile → transform}/_error_if_test.py +1 -1
  78. brainstate/transform/_eval_shape.py +145 -0
  79. brainstate/{augment → transform}/_eval_shape_test.py +1 -1
  80. brainstate/{compile → transform}/_jit.py +99 -46
  81. brainstate/{compile → transform}/_jit_test.py +3 -3
  82. brainstate/{compile → transform}/_loop_collect_return.py +219 -80
  83. brainstate/{compile → transform}/_loop_collect_return_test.py +1 -1
  84. brainstate/{compile → transform}/_loop_no_collection.py +133 -34
  85. brainstate/{compile → transform}/_loop_no_collection_test.py +2 -2
  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 +78 -25
  91. brainstate/{augment → transform}/_random.py +65 -45
  92. brainstate/{compile → transform}/_unvmap.py +102 -5
  93. brainstate/transform/_util.py +286 -0
  94. brainstate/typing.py +594 -61
  95. brainstate/typing_test.py +780 -0
  96. brainstate/util/__init__.py +9 -32
  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} +161 -27
  102. brainstate/util/_pretty_repr_test.py +696 -0
  103. brainstate/util/filter.py +557 -81
  104. brainstate/util/filter_test.py +912 -0
  105. brainstate/util/struct.py +769 -382
  106. brainstate/util/struct_test.py +602 -0
  107. {brainstate-0.1.10.dist-info → brainstate-0.2.0.dist-info}/METADATA +34 -17
  108. brainstate-0.2.0.dist-info/RECORD +111 -0
  109. brainstate/augment/__init__.py +0 -30
  110. brainstate/augment/_eval_shape.py +0 -99
  111. brainstate/augment/_mapping.py +0 -1060
  112. brainstate/augment/_mapping_test.py +0 -597
  113. brainstate/compile/__init__.py +0 -38
  114. brainstate/compile/_ad_checkpoint.py +0 -204
  115. brainstate/compile/_conditions.py +0 -256
  116. brainstate/compile/_make_jaxpr.py +0 -888
  117. brainstate/compile/_make_jaxpr_test.py +0 -156
  118. brainstate/compile/_util.py +0 -147
  119. brainstate/functional/__init__.py +0 -27
  120. brainstate/graph/_graph_node.py +0 -244
  121. brainstate/graph/_graph_node_test.py +0 -73
  122. brainstate/graph/_graph_operation_test.py +0 -563
  123. brainstate/init/__init__.py +0 -26
  124. brainstate/init/_base.py +0 -52
  125. brainstate/init/_generic.py +0 -244
  126. brainstate/init/_regular_inits.py +0 -105
  127. brainstate/init/_regular_inits_test.py +0 -50
  128. brainstate/nn/_inputs.py +0 -608
  129. brainstate/nn/_ltp.py +0 -28
  130. brainstate/nn/_neuron.py +0 -705
  131. brainstate/nn/_neuron_test.py +0 -161
  132. brainstate/nn/_others.py +0 -46
  133. brainstate/nn/_projection.py +0 -486
  134. brainstate/nn/_rate_rnns_test.py +0 -63
  135. brainstate/nn/_readout.py +0 -209
  136. brainstate/nn/_readout_test.py +0 -53
  137. brainstate/nn/_stp.py +0 -236
  138. brainstate/nn/_synapse.py +0 -505
  139. brainstate/nn/_synapse_test.py +0 -131
  140. brainstate/nn/_synaptic_projection.py +0 -423
  141. brainstate/nn/_synouts.py +0 -162
  142. brainstate/nn/_synouts_test.py +0 -57
  143. brainstate/nn/metrics.py +0 -388
  144. brainstate/optim/__init__.py +0 -38
  145. brainstate/optim/_base.py +0 -64
  146. brainstate/optim/_lr_scheduler.py +0 -448
  147. brainstate/optim/_lr_scheduler_test.py +0 -50
  148. brainstate/optim/_optax_optimizer.py +0 -152
  149. brainstate/optim/_optax_optimizer_test.py +0 -53
  150. brainstate/optim/_sgd_optimizer.py +0 -1104
  151. brainstate/random/_random_for_unit.py +0 -52
  152. brainstate/surrogate.py +0 -1957
  153. brainstate/transform.py +0 -23
  154. brainstate/util/caller.py +0 -98
  155. brainstate/util/others.py +0 -540
  156. brainstate/util/pretty_pytree.py +0 -945
  157. brainstate/util/pretty_pytree_test.py +0 -159
  158. brainstate/util/pretty_table.py +0 -2954
  159. brainstate/util/scaling.py +0 -258
  160. brainstate-0.1.10.dist-info/RECORD +0 -130
  161. {brainstate-0.1.10.dist-info → brainstate-0.2.0.dist-info}/WHEEL +0 -0
  162. {brainstate-0.1.10.dist-info → brainstate-0.2.0.dist-info}/licenses/LICENSE +0 -0
  163. {brainstate-0.1.10.dist-info → brainstate-0.2.0.dist-info}/top_level.txt +0 -0
brainstate/nn/metrics.py DELETED
@@ -1,388 +0,0 @@
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 typing as tp
18
- from dataclasses import dataclass
19
- from functools import partial
20
-
21
- import jax
22
- import jax.numpy as jnp
23
- import numpy as np
24
-
25
- from brainstate._state import State
26
-
27
- __all__ = [
28
- 'Average',
29
- 'Statistics',
30
- 'Welford',
31
- 'Accuracy',
32
- 'MultiMetric',
33
- ]
34
-
35
-
36
- class MetricState(State):
37
- """Wrapper class for Metric Variables."""
38
- pass
39
-
40
-
41
- class Metric(object):
42
- """Base class for metrics. Any class that subclasses ``Metric`` should
43
- implement a ``compute``, ``reset`` and ``update`` method."""
44
-
45
- def reset(self) -> None:
46
- """In-place reset the ``Metric``."""
47
- raise NotImplementedError('Must override `reset()` method.')
48
-
49
- def update(self, **kwargs) -> None:
50
- """In-place update the ``Metric``."""
51
- raise NotImplementedError('Must override `update()` method.')
52
-
53
- def compute(self):
54
- """Compute and return the value of the ``Metric``."""
55
- raise NotImplementedError('Must override `compute()` method.')
56
-
57
-
58
- class Average(Metric):
59
- """Average metric.
60
-
61
- Example usage::
62
-
63
- >>> import jax.numpy as jnp
64
- >>> import brainstate as brainstate
65
-
66
- >>> batch_loss = jnp.array([1, 2, 3, 4])
67
- >>> batch_loss2 = jnp.array([3, 2, 1, 0])
68
-
69
- >>> metrics = brainstate.nn.metrics.Average()
70
- >>> metrics.compute()
71
- Array(nan, dtype=float32)
72
- >>> metrics.update(values=batch_loss)
73
- >>> metrics.compute()
74
- Array(2.5, dtype=float32)
75
- >>> metrics.update(values=batch_loss2)
76
- >>> metrics.compute()
77
- Array(2., dtype=float32)
78
- >>> metrics.reset()
79
- >>> metrics.compute()
80
- Array(nan, dtype=float32)
81
- """
82
-
83
- def __init__(self, argname: str = 'values'):
84
- """Pass in a string denoting the key-word argument that :func:`update` will use to derive the new value.
85
- For example, constructing the metric as ``avg = Average('test')`` would allow you to make updates with
86
- ``avg.update(test=new_value)``.
87
-
88
- Args:
89
- argname: an optional string denoting the key-word argument that
90
- :func:`update` will use to derive the new value. Defaults to
91
- ``'values'``.
92
- """
93
- self.argname = argname
94
- self.total = MetricState(jnp.array(0, dtype=jnp.float32))
95
- self.count = MetricState(jnp.array(0, dtype=jnp.int32))
96
-
97
- def reset(self) -> None:
98
- """Reset this ``Metric``."""
99
- self.total.value = jnp.array(0, dtype=jnp.float32)
100
- self.count.value = jnp.array(0, dtype=jnp.int32)
101
-
102
- def update(self, **kwargs) -> None:
103
- """In-place update this ``Metric``. This method will use the value from
104
- ``kwargs[self.argname]`` to update the metric, where ``self.argname`` is
105
- defined on construction.
106
-
107
- Args:
108
- **kwargs: the key-word arguments that contains a ``self.argname``
109
- entry that maps to the value we want to use to update this metric.
110
- """
111
- if self.argname not in kwargs:
112
- raise TypeError(f"Expected keyword argument '{self.argname}'")
113
- values: tp.Union[int, float, jax.Array] = kwargs[self.argname]
114
- self.total.value += (
115
- values if isinstance(values, (int, float)) else values.sum()
116
- )
117
- self.count.value += 1 if isinstance(values, (int, float)) else values.size
118
-
119
- def compute(self) -> jax.Array:
120
- """Compute and return the average."""
121
- return self.total.value / self.count.value
122
-
123
-
124
- @partial(jax.tree_util.register_dataclass,
125
- data_fields=['mean', 'standard_error_of_mean', 'standard_deviation'],
126
- meta_fields=[])
127
- @dataclass
128
- class Statistics:
129
- mean: jnp.float32
130
- standard_error_of_mean: jnp.float32
131
- standard_deviation: jnp.float32
132
-
133
-
134
- class Welford(Metric):
135
- """Uses Welford's algorithm to compute the mean and variance of a stream of data.
136
-
137
- Example usage::
138
-
139
- >>> import jax.numpy as jnp
140
- >>> from brainstate import nn
141
-
142
- >>> batch_loss = jnp.array([1, 2, 3, 4])
143
- >>> batch_loss2 = jnp.array([3, 2, 1, 0])
144
-
145
- >>> metrics = nn.metrics.Welford()
146
- >>> metrics.compute()
147
- Statistics(mean=Array(0., dtype=float32), standard_error_of_mean=Array(nan, dtype=float32), standard_deviation=Array(nan, dtype=float32))
148
- >>> metrics.update(values=batch_loss)
149
- >>> metrics.compute()
150
- Statistics(mean=Array(2.5, dtype=float32), standard_error_of_mean=Array(0.559017, dtype=float32), standard_deviation=Array(1.118034, dtype=float32))
151
- >>> metrics.update(values=batch_loss2)
152
- >>> metrics.compute()
153
- Statistics(mean=Array(2., dtype=float32), standard_error_of_mean=Array(0.43301272, dtype=float32), standard_deviation=Array(1.2247449, dtype=float32))
154
- >>> metrics.reset()
155
- >>> metrics.compute()
156
- Statistics(mean=Array(0., dtype=float32), standard_error_of_mean=Array(nan, dtype=float32), standard_deviation=Array(nan, dtype=float32))
157
- """
158
-
159
- def __init__(self, argname: str = 'values'):
160
- """Pass in a string denoting the key-word argument that :func:`update` will use to derive the new value.
161
- For example, constructing the metric as ``wf = Welford('test')`` would allow you to make updates with
162
- ``wf.update(test=new_value)``.
163
-
164
- Args:
165
- argname: an optional string denoting the key-word argument that
166
- :func:`update` will use to derive the new value. Defaults to
167
- ``'values'``.
168
- """
169
- self.argname = argname
170
- self.count = MetricState(jnp.array(0, dtype=jnp.int32))
171
- self.mean = MetricState(jnp.array(0, dtype=jnp.float32))
172
- self.m2 = MetricState(jnp.array(0, dtype=jnp.float32))
173
-
174
- def reset(self) -> None:
175
- """Reset this ``Metric``."""
176
- self.count.value = jnp.array(0, dtype=jnp.uint32)
177
- self.mean.value = jnp.array(0, dtype=jnp.float32)
178
- self.m2.value = jnp.array(0, dtype=jnp.float32)
179
-
180
- def update(self, **kwargs) -> None:
181
- """In-place update this ``Metric``. This method will use the value from
182
- ``kwargs[self.argname]`` to update the metric, where ``self.argname`` is
183
- defined on construction.
184
-
185
- Args:
186
- **kwargs: the key-word arguments that contains a ``self.argname``
187
- entry that maps to the value we want to use to update this metric.
188
- """
189
- if self.argname not in kwargs:
190
- raise TypeError(f"Expected keyword argument '{self.argname}'")
191
- values: tp.Union[int, float, jax.Array] = kwargs[self.argname]
192
- count = 1 if isinstance(values, (int, float)) else values.size
193
- original_count = self.count.value
194
- self.count.value += count
195
- delta = (
196
- values if isinstance(values, (int, float)) else values.mean()
197
- ) - self.mean.value
198
- self.mean.value += delta * count / self.count.value
199
- m2 = 0.0 if isinstance(values, (int, float)) else values.var() * count
200
- self.m2.value += (
201
- m2 + delta * delta * count * original_count / self.count
202
- )
203
-
204
- def compute(self) -> Statistics:
205
- """Compute and return the mean and variance statistics in a
206
- ``Statistics`` dataclass object.
207
- """
208
- variance = self.m2.value / self.count.value
209
- standard_deviation = variance ** 0.5
210
- sem = standard_deviation / (self.count.value ** 0.5)
211
- return Statistics(
212
- mean=self.mean.value,
213
- standard_error_of_mean=sem,
214
- standard_deviation=standard_deviation,
215
- )
216
-
217
-
218
- class Accuracy(Average):
219
- """Accuracy metric. This metric subclasses :class:`Average`,
220
- and so they share the same ``reset`` and ``compute`` method
221
- implementations. Unlike :class:`Average`, no string needs to
222
- be passed to ``Accuracy`` during construction.
223
-
224
- Example usage::
225
-
226
- >>> import brainstate as brainstate
227
- >>> import jax, jax.numpy as jnp
228
-
229
- >>> logits = jax.random.normal(jax.random.key(0), (5, 2))
230
- >>> labels = jnp.array([1, 1, 0, 1, 0])
231
- >>> logits2 = jax.random.normal(jax.random.key(1), (5, 2))
232
- >>> labels2 = jnp.array([0, 1, 1, 1, 1])
233
-
234
- >>> metrics = brainstate.nn.metrics.Accuracy()
235
- >>> metrics.compute()
236
- Array(nan, dtype=float32)
237
- >>> metrics.update(logits=logits, labels=labels)
238
- >>> metrics.compute()
239
- Array(0.6, dtype=float32)
240
- >>> metrics.update(logits=logits2, labels=labels2)
241
- >>> metrics.compute()
242
- Array(0.7, dtype=float32)
243
- >>> metrics.reset()
244
- >>> metrics.compute()
245
- Array(nan, dtype=float32)
246
- """
247
-
248
- def update(self, *, logits: jax.Array, labels: jax.Array, **_) -> None: # type: ignore[override]
249
- """In-place update this ``Metric``.
250
-
251
- Args:
252
- logits: the outputted predicted activations. These values are
253
- argmax-ed (on the trailing dimension), before comparing them
254
- to the labels.
255
- labels: the ground truth integer labels.
256
- """
257
- if logits.ndim != labels.ndim + 1:
258
- raise ValueError(
259
- f'Expected logits.ndim==labels.ndim+1, got {logits.ndim} and {labels.ndim}'
260
- )
261
- elif labels.dtype in (jnp.int64, np.int32, np.int64):
262
- labels = jnp.astype(labels, jnp.int32)
263
- elif labels.dtype != jnp.int32:
264
- raise ValueError(f'Expected labels.dtype==jnp.int32, got {labels.dtype}')
265
-
266
- super().update(values=(logits.argmax(axis=-1) == labels))
267
-
268
-
269
- class MultiMetric(Metric):
270
- """MultiMetric class to store multiple metrics and update them in a single call.
271
-
272
- Example usage::
273
-
274
- >>> from brainstate import nn
275
- >>> import jax, jax.numpy as jnp
276
-
277
- >>> metrics = nn.metrics.MultiMetric(
278
- ... accuracy=nn.metrics.Accuracy(),
279
- ... loss=nn.metrics.Average(),
280
- ... )
281
-
282
- >>> metrics
283
- MultiMetric(
284
- accuracy=Accuracy(
285
- argname='values',
286
- total=MetricState(
287
- value=Array(0., dtype=float32)
288
- ),
289
- count=MetricState(
290
- value=Array(0, dtype=int32)
291
- )
292
- ),
293
- loss=Average(
294
- argname='values',
295
- total=MetricState(
296
- value=Array(0., dtype=float32)
297
- ),
298
- count=MetricState(
299
- value=Array(0, dtype=int32)
300
- )
301
- )
302
- )
303
-
304
- >>> metrics.accuracy
305
- Accuracy(
306
- argname='values',
307
- total=MetricState(
308
- value=Array(0., dtype=float32)
309
- ),
310
- count=MetricState(
311
- value=Array(0, dtype=int32)
312
- )
313
- )
314
-
315
- >>> metrics.loss
316
- Average(
317
- argname='values',
318
- total=MetricState(
319
- value=Array(0., dtype=float32)
320
- ),
321
- count=MetricState(
322
- value=Array(0, dtype=int32)
323
- )
324
- )
325
-
326
- >>> logits = jax.random.normal(jax.random.key(0), (5, 2))
327
- >>> labels = jnp.array([1, 1, 0, 1, 0])
328
- >>> logits2 = jax.random.normal(jax.random.key(1), (5, 2))
329
- >>> labels2 = jnp.array([0, 1, 1, 1, 1])
330
-
331
- >>> batch_loss = jnp.array([1, 2, 3, 4])
332
- >>> batch_loss2 = jnp.array([3, 2, 1, 0])
333
-
334
- >>> metrics.compute()
335
- {'accuracy': Array(nan, dtype=float32), 'loss': Array(nan, dtype=float32)}
336
- >>> metrics.update(logits=logits, labels=labels, values=batch_loss)
337
- >>> metrics.compute()
338
- {'accuracy': Array(0.6, dtype=float32), 'loss': Array(2.5, dtype=float32)}
339
- >>> metrics.update(logits=logits2, labels=labels2, values=batch_loss2)
340
- >>> metrics.compute()
341
- {'accuracy': Array(0.7, dtype=float32), 'loss': Array(2., dtype=float32)}
342
- >>> metrics.reset()
343
- >>> metrics.compute()
344
- {'accuracy': Array(nan, dtype=float32), 'loss': Array(nan, dtype=float32)}
345
- """
346
-
347
- def __init__(self, **metrics):
348
- """Pass in key-word arguments to the constructor, e.g.
349
- ``MultiMetric(keyword1=Average(), keyword2=Accuracy(), ...)``.
350
-
351
- Args:
352
- **metrics: the key-word arguments that will be used to access
353
- the corresponding ``Metric``.
354
- """
355
- # TODO: raise error if a kwarg is passed that is in ('reset', 'update', 'compute'), since these names are reserved for methods
356
- self._metric_names = []
357
- for metric_name, metric in metrics.items():
358
- self._metric_names.append(metric_name)
359
- vars(self)[metric_name] = metric
360
-
361
- def reset(self) -> None:
362
- """Reset all underlying ``Metric``'s."""
363
- for metric_name in self._metric_names:
364
- getattr(self, metric_name).reset()
365
-
366
- def update(self, **updates) -> None:
367
- """In-place update all underlying ``Metric``'s in this ``MultiMetric``. All
368
- ``**updates`` will be passed to the ``update`` method of all underlying
369
- ``Metric``'s.
370
-
371
- Args:
372
- **updates: the key-word arguments that will be passed to the underlying ``Metric``'s
373
- ``update`` method.
374
- """
375
- # TODO: should we give the option of updating only some of the metrics and not all? e.g. if for some kwargs==None, don't do update
376
- # TODO: should we raise an error if a kwarg is passed into **updates that has no match with any underlying metric? e.g. user typo
377
- for metric_name in self._metric_names:
378
- getattr(self, metric_name).update(**updates)
379
-
380
- def compute(self) -> dict[str, Metric]:
381
- """Compute and return the value of all underlying ``Metric``'s. This method
382
- will return a dictionary, mapping strings (defined by the key-word arguments
383
- ``**metrics`` passed to the constructor) to the corresponding metric value.
384
- """
385
- return {
386
- f'{metric_name}': getattr(self, metric_name).compute()
387
- for metric_name in self._metric_names
388
- }
@@ -1,38 +0,0 @@
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
- from ._base import *
18
- from ._base import __all__ as base_all
19
- from ._lr_scheduler import *
20
- from ._lr_scheduler import __all__ as scheduler_all
21
- from ._optax_optimizer import *
22
- from ._optax_optimizer import __all__ as optax_all
23
- from ._sgd_optimizer import *
24
- from ._sgd_optimizer import __all__ as optimizer_all
25
-
26
- __all__ = (
27
- base_all
28
- + scheduler_all
29
- + optimizer_all
30
- + optax_all
31
- )
32
-
33
- del (
34
- base_all,
35
- optax_all,
36
- scheduler_all,
37
- optimizer_all,
38
- )
brainstate/optim/_base.py DELETED
@@ -1,64 +0,0 @@
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
- from typing import Dict, Hashable
18
-
19
- from brainstate._state import State, StateDictManager
20
- from brainstate.graph import Node
21
- from brainstate.typing import PyTree
22
-
23
- __all__ = [
24
- 'Optimizer',
25
- ]
26
-
27
-
28
- class Optimizer(Node):
29
- """
30
- Base Optimizer Class.
31
-
32
- Attributes:
33
- -----------
34
- param_states: StateDictManager
35
- The state dict manager for trainable weights.
36
-
37
- """
38
-
39
- param_states: StateDictManager
40
-
41
- def __init__(self):
42
- self.param_states = StateDictManager()
43
-
44
- def register_trainable_weights(self, param_states: Dict[Hashable, State]):
45
- """
46
- Register the trainable weights.
47
-
48
- Parameters:
49
- -----------
50
- param_states: Dict[Hashable, State]
51
- The trainable weights.
52
- """
53
- raise NotImplementedError
54
-
55
- def update(self, grads: Dict[Hashable, PyTree]):
56
- """
57
- Update the trainable weights according to weight gradients.
58
-
59
- Parameters:
60
- -----------
61
- grads: Dict[Hashable, PyTree]
62
- The weight gradients.
63
- """
64
- raise NotImplementedError