BrainPyObject#
- class brainpy.math.BrainPyObject(name=None)[source]#
The BrainPyObject class for the whole BrainPy ecosystem.
The subclass of BrainPyObject includes but not limited to:
DynamicalSystemin brainpy.dyn.base.pyIntegratorin brainpy.integrators.base.pyOptimizerin brainpy.optimizers.pySchedulerin brainpy.optimizers.py
Note
Note a variable created in the
BrainPyObjectwill never be replaced.For example, if here we create an object which has an attribute
a:>>> import brainpy as bp >>> import brainpy.math as bm >>> >>> class MyObj(bp.BrainPyObject): >>> def __init__(self): >>> super().__init__() >>> self.a = bm.Variable(bm.ones(1)) >>> >>> def reset1(self): >>> self.a = bm.asarray([10.]) >>> >>> def reset2(self): >>> self.a = 1. >>> >>> ob = MyObj() >>> id(ob.a) 2643434845056
After we call
ob.reset1()function,ob.ais still the original Variable. what’s change is its value.>>> ob.reset1() >>> id(ob.a) 2643434845056
What’s really happend when we call
self.a = bm.asarray([10.])isself.a.value = bm.asarray([10.]). Therefore we when callob.reset2(), there will be an error.>>> ob.reset2() brainpy.errors.MathError: The shape of the original data is (1,), while we got () with batch_axis=None.
- load_state_dict(state_dict, warn=True, compatible='v2', **kwargs)[source]#
Copy parameters and buffers from
state_dictinto this module and its descendants.- Parameters:
- Returns:
out –
NamedTuplewithmissing_keysandunexpected_keysfields:missing_keys is a list of str containing the missing keys
unexpected_keys is a list of str containing the unexpected keys
- Return type:
StateLoadResult
- property name#
Name of the model.
- state_dict(**kwargs)[source]#
Returns a dictionary containing a whole state of the module.
- Returns:
out – A dictionary containing a whole state of the module.
- Return type:
- tracing_variable(name, init, shape, batch_or_mode=None, batch_axis=0, axis_names=None, batch_axis_name='batch')[source]#
Initialize a variable that can be traced during computations and transformations.
Deprecated since version 3.0.0: This feature is no longer supported. Since BrainPy 3.0.0 the library has been rewritten on top of
brainstateand variable tracing is handled bybrainstatedirectly. Calling this method always raisesNotImplementedError.- Parameters:
name (
str) – The variable name.init (
Union[Callable,Array,Array]) – The data to be initialized as aVariable.shape (
Union[int,Sequence[int]]) – The shape of the variable.batch_or_mode (
Union[int,bool,Mode]) – The batch size of this variable.batch_axis (
int) – The batch axis, if batch size is given.axis_names (
Optional[Sequence[str]]) – The name for each axis.batch_axis_name (
Optional[str]) – The name for the batch axis.
- Raises:
NotImplementedError – Always, because this feature is unsupported since 3.0.0.
- Return type:
- train_vars(method='absolute', level=-1, include_self=True)[source]#
The shortcut for retrieving all trainable variables.
- Parameters:
- Returns:
gather – The collection contained (the path, the trainable variable).
- Return type:
ArrayCollector
- tree_flatten()[source]#
Flattens the object as a PyTree.
The flattening order is determined by attributes added order.
Added in version 2.3.1.
- Returns:
res – A tuple of dynamical values and static values.
- Return type:
- classmethod tree_unflatten(aux, dynamic_values)[source]#
Unflatten the data to construct an object of this class.
Added in version 2.3.1.
- unique_name(name=None, type_=None)[source]#
Get the unique name for this object.
- Parameters:
name (
str, optional) – The expected name. If None, the default unique name will be returned. Otherwise, the provided name will be checked to guarantee its uniqueness.type_ – The name of this class, used for object naming.
- Returns:
name – The unique name for this object.
- Return type: