Skip to content

Commit 5abd7fc

Browse files
authored
Upgrade documentations(#310)
Upgrade documentations
2 parents d495004 + 4a4d138 commit 5abd7fc

File tree

121 files changed

+5534
-11041
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+5534
-11041
lines changed

brainpy/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88

99
# "base" module
1010
from . import base
11-
from .base.base import BrainPyObject, Base
12-
from .base.collector import Collector, TensorCollector
11+
from .base.base import (BrainPyObject, Base)
12+
from .base.collector import (
13+
Collector,
14+
ArrayCollector,
15+
TensorCollector
16+
)
1317

1418
# math foundation
1519
from . import math

brainpy/algorithms/offline.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import brainpy.math as bm
99
from brainpy.base import BrainPyObject
10-
from brainpy.types import Array
10+
from brainpy.types import ArrayType
1111
from .utils import (Sigmoid,
1212
Regularization, L1Regularization, L1L2Regularization, L2Regularization,
1313
polynomial_features, normalize)
@@ -46,43 +46,43 @@ def __call__(self, identifier, target, input, output):
4646
----------
4747
identifier: str
4848
The variable name.
49-
target: Array, ndarray
49+
target: ArrayType
5050
The 2d target data with the shape of `(num_batch, num_output)`.
51-
input: Array, ndarray
51+
input: ArrayType
5252
The 2d input data with the shape of `(num_batch, num_input)`.
53-
output: Array, ndarray
53+
output: ArrayType
5454
The 2d output data with the shape of `(num_batch, num_output)`.
5555
5656
Returns
5757
-------
58-
weight: Array
58+
weight: ArrayType
5959
The weights after fit.
6060
"""
6161
return self.call(identifier, target, input, output)
6262

63-
def call(self, identifier, targets, inputs, outputs) -> Array:
63+
def call(self, identifier, targets, inputs, outputs) -> ArrayType:
6464
"""The training procedure.
6565
6666
Parameters
6767
----------
6868
identifier: str
6969
The identifier.
7070
71-
inputs: Array, jax.numpy.ndarray, numpy.ndarray
71+
inputs: ArrayType
7272
The 3d input data with the shape of `(num_batch, num_time, num_input)`,
7373
or, the 2d input data with the shape of `(num_time, num_input)`.
7474
75-
targets: Array, jax.numpy.ndarray, numpy.ndarray
75+
targets: ArrayType
7676
The 3d target data with the shape of `(num_batch, num_time, num_output)`,
7777
or the 2d target data with the shape of `(num_time, num_output)`.
7878
79-
outputs: Array, jax.numpy.ndarray, numpy.ndarray
79+
outputs: ArrayType
8080
The 3d output data with the shape of `(num_batch, num_time, num_output)`,
8181
or the 2d output data with the shape of `(num_time, num_output)`.
8282
8383
Returns
8484
-------
85-
weight: Array
85+
weight: ArrayType
8686
The weights after fit.
8787
"""
8888
raise NotImplementedError('Must implement the __call__ function by the subclass itself.')
@@ -355,7 +355,7 @@ def __init__(
355355
self.gradient_descent = gradient_descent
356356
self.sigmoid = Sigmoid()
357357

358-
def call(self, identifier, targets, inputs, outputs=None) -> Array:
358+
def call(self, identifier, targets, inputs, outputs=None) -> ArrayType:
359359
# prepare data
360360
inputs = _check_data_2d_atls(bm.asarray(inputs))
361361
targets = _check_data_2d_atls(bm.asarray(targets))

brainpy/algorithms/online.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ def __call__(self, identifier, target, input, output):
3434
----------
3535
identifier: str
3636
The variable name.
37-
target: Array, ndarray
37+
target: ArrayType
3838
The 2d target data with the shape of `(num_batch, num_output)`.
39-
input: Array, ndarray
39+
input: ArrayType
4040
The 2d input data with the shape of `(num_batch, num_input)`.
41-
output: Array, ndarray
41+
output: ArrayType
4242
The 2d output data with the shape of `(num_batch, num_output)`.
4343
4444
Returns
4545
-------
46-
weight: Array
46+
weight: ArrayType
4747
The weights after fit.
4848
"""
4949
return self.call(identifier, target, input, output)
@@ -58,16 +58,16 @@ def call(self, identifier, target, input, output):
5858
----------
5959
identifier: str
6060
The variable name.
61-
target: Array, ndarray
61+
target: ArrayType
6262
The 2d target data with the shape of `(num_batch, num_output)`.
63-
input: Array, ndarray
63+
input: ArrayType
6464
The 2d input data with the shape of `(num_batch, num_input)`.
65-
output: Array, ndarray
65+
output: ArrayType
6666
The 2d output data with the shape of `(num_batch, num_output)`.
6767
6868
Returns
6969
-------
70-
weight: Array
70+
weight: ArrayType
7171
The weights after fit.
7272
"""
7373
raise NotImplementedError('Must implement the call() function by the subclass itself.')

brainpy/analysis/highdim/slow_points.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
import brainpy.math as bm
1414
from brainpy import optimizers as optim, losses
1515
from brainpy.analysis import utils, base, constants
16-
from brainpy.base import TensorCollector
16+
from brainpy.base import ArrayCollector
1717
from brainpy.dyn.base import DynamicalSystem
1818
from brainpy.dyn.runners import build_inputs, check_and_format_inputs
1919
from brainpy.errors import AnalyzerError, UnsupportedError
2020
from brainpy.tools.others.dicts import DotDict
21-
from brainpy.types import Array
21+
from brainpy.types import ArrayType
2222

2323
__all__ = [
2424
'SlowPointFinder',
@@ -136,11 +136,11 @@ def __init__(
136136

137137
# update function
138138
if target_vars is None:
139-
self.target_vars = TensorCollector()
139+
self.target_vars = ArrayCollector()
140140
else:
141141
if not isinstance(target_vars, dict):
142142
raise TypeError(f'"target_vars" must be a dict but we got {type(target_vars)}')
143-
self.target_vars = TensorCollector(target_vars)
143+
self.target_vars = ArrayCollector(target_vars)
144144
excluded_vars = () if excluded_vars is None else excluded_vars
145145
if isinstance(excluded_vars, dict):
146146
excluded_vars = tuple(excluded_vars.values())
@@ -295,7 +295,7 @@ def selected_ids(self, val):
295295

296296
def find_fps_with_gd_method(
297297
self,
298-
candidates: Union[Array, Dict[str, Array]],
298+
candidates: Union[ArrayType, Dict[str, ArrayType]],
299299
tolerance: Union[float, Dict[str, float]] = 1e-5,
300300
num_batch: int = 100,
301301
num_opt: int = 10000,
@@ -305,7 +305,7 @@ def find_fps_with_gd_method(
305305
306306
Parameters
307307
----------
308-
candidates : Array, dict
308+
candidates : ArrayType, dict
309309
The array with the shape of (batch size, state dim) of hidden states
310310
of RNN to start training for fixed points.
311311
@@ -335,7 +335,7 @@ def find_fps_with_gd_method(
335335
# set up optimization
336336
num_candidate = self._check_candidates(candidates)
337337
if not (isinstance(candidates, (bm.ndarray, jnp.ndarray, np.ndarray)) or isinstance(candidates, dict)):
338-
raise ValueError('Candidates must be instance of Array or dict of Array.')
338+
raise ValueError('Candidates must be instance of ArrayType or dict of ArrayType.')
339339
fixed_points = tree_map(lambda a: bm.TrainVar(a), candidates, is_leaf=lambda x: isinstance(x, bm.Array))
340340
f_eval_loss = self._get_f_eval_loss()
341341

@@ -401,14 +401,14 @@ def batch_train(start_i, n_batch):
401401

402402
def find_fps_with_opt_solver(
403403
self,
404-
candidates: Union[Array, Dict[str, Array]],
404+
candidates: Union[ArrayType, Dict[str, ArrayType]],
405405
opt_solver: str = 'BFGS'
406406
):
407407
"""Optimize fixed points with nonlinear optimization solvers.
408408
409409
Parameters
410410
----------
411-
candidates: Array, dict
411+
candidates: ArrayType, dict
412412
The candidate (initial) fixed points.
413413
opt_solver: str
414414
The solver of the optimization.
@@ -535,7 +535,7 @@ def exclude_outliers(self, tolerance: float = 1e0):
535535

536536
def compute_jacobians(
537537
self,
538-
points: Union[Array, Dict[str, Array]],
538+
points: Union[ArrayType, Dict[str, ArrayType]],
539539
stack_dict_var: bool = True,
540540
plot: bool = False,
541541
num_col: int = 4,
@@ -546,7 +546,7 @@ def compute_jacobians(
546546
547547
Parameters
548548
----------
549-
points: np.ndarray, bm.Array, jax.ndarray
549+
points: np.ndarray, bm.ArrayType, jax.ndarray
550550
The fixed points with the shape of (num_point, num_dim).
551551
stack_dict_var: bool
552552
Stack dictionary variables to calculate Jacobian matrix?
@@ -606,7 +606,7 @@ def decompose_eigenvalues(matrices, sort_by='magnitude', do_compute_lefts=False)
606606
607607
Parameters
608608
----------
609-
matrices: np.ndarray, bm.Array, jax.ndarray
609+
matrices: np.ndarray, bm.ArrayType, jax.ndarray
610610
A 3D array with the shape of (num_matrices, dim, dim).
611611
sort_by: str
612612
The method of sorting.

brainpy/analysis/utils/measurement.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def euclidean_distance(points: np.ndarray, num_point=None):
4949
5050
Parameters
5151
----------
52-
points: jnp.ndarray, bm.Array
52+
points: ArrayType
5353
The points.
5454
5555
Returns
@@ -96,13 +96,13 @@ def euclidean_distance_jax(points: Union[jnp.ndarray, bm.ndarray], num_point=Non
9696
9797
Parameters
9898
----------
99-
points: jnp.ndarray, bm.Array
99+
points: ArrayType
100100
The points.
101101
num_point: int
102102
103103
Returns
104104
-------
105-
dist_matrix: Array
105+
dist_matrix: ArrayType
106106
The distance matrix.
107107
"""
108108
if isinstance(points, dict):

brainpy/base/base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from brainpy import errors
77
from brainpy.base import io, naming
8-
from brainpy.base.collector import Collector, TensorCollector
8+
from brainpy.base.collector import Collector, ArrayCollector
99

1010
math = None
1111

@@ -40,7 +40,7 @@ def __init__(self, name=None):
4040

4141
# Used to wrap the implicit variables
4242
# which cannot be accessed by self.xxx
43-
self.implicit_vars = TensorCollector()
43+
self.implicit_vars = ArrayCollector()
4444

4545
# Used to wrap the implicit children nodes
4646
# which cannot be accessed by self.xxx
@@ -113,14 +113,14 @@ def vars(self, method='absolute', level=-1, include_self=True):
113113
114114
Returns
115115
-------
116-
gather : TensorCollector
116+
gather : ArrayCollector
117117
The collection contained (the path, the variable).
118118
"""
119119
global math
120120
if math is None: from brainpy import math
121121

122122
nodes = self.nodes(method=method, level=level, include_self=include_self)
123-
gather = TensorCollector()
123+
gather = ArrayCollector()
124124
for node_path, node in nodes.items():
125125
for k in dir(node):
126126
v = getattr(node, k)
@@ -144,7 +144,7 @@ def train_vars(self, method='absolute', level=-1, include_self=True):
144144
145145
Returns
146146
-------
147-
gather : TensorCollector
147+
gather : ArrayCollector
148148
The collection contained (the path, the trainable variable).
149149
"""
150150
global math
@@ -287,7 +287,7 @@ def save_states(self, filename, variables=None, **setting):
287287
----------
288288
filename : str
289289
The file name which to store the model states.
290-
variables: optional, dict, TensorCollector
290+
variables: optional, dict, ArrayCollector
291291
The variables to save. If not provided, all variables retrieved by ``~.vars()`` will be used.
292292
"""
293293
if variables is None:

brainpy/base/collector.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
__all__ = [
99
'Collector',
10+
'ArrayCollector',
1011
'TensorCollector',
1112
]
1213

@@ -108,7 +109,7 @@ def subset(self, var_type):
108109
>>> # get all trainable variables
109110
>>> some_collector.subset(bp.math.TrainVar)
110111
>>>
111-
>>> # get all Array
112+
>>> # get all Variable
112113
>>> some_collector.subset(bp.math.Variable)
113114
114115
or, it can be used to get a subset of integrators:
@@ -142,7 +143,7 @@ def unique(self):
142143
return gather
143144

144145

145-
class TensorCollector(Collector):
146+
class ArrayCollector(Collector):
146147
"""A ArrayCollector is a dictionary (name, var)
147148
with some additional methods to make manipulation
148149
of collections of variables easy. A Collection
@@ -195,3 +196,6 @@ def from_other(cls, other: Union[Sequence, Dict]):
195196
return cls(other)
196197
else:
197198
raise TypeError
199+
200+
201+
TensorCollector = ArrayCollector

brainpy/base/function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def __init__(self, f: Optional[Callable], child_objs=None, dyn_vars=None, name=N
7171
# variables
7272
# ---
7373
if dyn_vars is not None:
74-
self.implicit_vars = collector.TensorCollector()
74+
self.implicit_vars = collector.ArrayCollector()
7575
global math
7676
if math is None: from brainpy import math
7777
if isinstance(dyn_vars, math.ndarray):

brainpy/base/io.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88

99
from brainpy import errors
10-
from brainpy.base.collector import TensorCollector
10+
from brainpy.base.collector import ArrayCollector
1111

1212
logger = logging.getLogger('brainpy.base.io')
1313

@@ -124,7 +124,7 @@ def _load(
124124

125125

126126
def _unique_and_duplicate(collector: dict):
127-
gather = TensorCollector()
127+
gather = ArrayCollector()
128128
id2name = dict()
129129
duplicates = ([], [])
130130
for k, v in collector.items():

0 commit comments

Comments
 (0)