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:
DynamicalSystem
in brainpy.dyn.base.pyIntegrator
in brainpy.integrators.base.pyOptimizer
in brainpy.optimizers.pyScheduler
in brainpy.optimizers.py
Note
Note a variable created in the
BrainPyObject
will 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.a
is 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_dict
into this module and its descendants.- Parameters:
- Returns:
out –
NamedTuple
withmissing_keys
andunexpected_keys
fields: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 the variable which can be traced during computations and transformations.
Although this function is designed to initialize tracing variables during computation or compilation, it can also be used for the initialization of variables before computation and compilation.
If the variable has not been instantiated, a
Variable
will be instantiated.If the variable has been created, the further call of this function will return the created variable.
Here is the usage example:
class Example(bm.BrainPyObject): def fun(self): # The first time of calling `.fun()`, this line will create a Variable instance. # If users repeatedly call `.fun()` function, this line will not initialize variables again. # Instead, it will return the variable has been created. self.tracing_variable('a', bm.zeros, (10,)) # The created variable can be accessed with self.xxx self.a.value = bm.ones(10) # Calling this function again will not reinitialize the # variable again, Instead, it will return the variable # that has been created. a = self.tracing_variable('a', bm.zeros, (10,))
New in version 2.4.5.
- Parameters:
name (
str
) – str. The variable name.init (
Union
[Callable
,Array
,Array
]) – callable, Array. The data to be initialized as aVariable
.batch_or_mode (
Union
[int
,bool
,Mode
,None
]) – int, bool, Mode. This is used to specify the batch size of this variable. If it is a boolean or an instance ofMode
, the batch size will be 1. If it is None, the variable has no batch axis.shape (
Union
[int
,Sequence
[int
]]) – int, sequence of int. The shape of the variable.batch_axis (
int
) – int. The batch axis, if batch size is given.axis_names (
Optional
[Sequence
[str
]]) – sequence of str. The name for each axis. These names should match the givenaxes
.batch_axis_name (
Optional
[str
]) – str. The name for the batch axis. The name will be used ifbatch_or_mode
is given. Default isbrainpy.math.sharding.BATCH_AXIS
.
- Return type:
- Returns:
The instance of
Variable
.
- 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.
New 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.
New in version 2.3.1.
- unique_name(name=None, type_=None)[source]#
Get the unique name for this object.
- Parameters:
- Returns:
name – The unique name for this object.
- Return type: