VariableStack#
- class brainpy.math.VariableStack(*args, **kwargs)[source]#
Variable stack, for collecting all
Variable
used in the program.VariableStack
supports all features of python dict.- assign(data, check=True)[source]#
Assign the value for each \(~.Variable\) according to the given
data
.
- call_on_subset(cond, call)[source]#
Call a function on the subset of this
VariableStack
.>>> import brainpy.math as bm >>> stack = VariableStack(a=bm.Variable(1), b=bm.random.RandomState(1)) >>> stack.call_on_subset(lambda a: isinstance(a, bm.random.RandomState), >>> lambda a: a.split_key()) {'b': Array([3819641963, 2025898573], dtype=uint32)}
- Parameters:
- Return type:
- Returns:
A dict containing the results of
call
function for each element in thecond
constrained subset.
- dict_data()[source]#
Get all data in the collected variables with a python dict structure.
- Return type:
- dict_data_of_subset(subset_cond)[source]#
Get data of the given subset constrained by function
subset_cond
.
- instance_of(cls)#
Collect all variables which are instances of the given class type.
- Return type:
- list_data()[source]#
Get all data in the collected variables with a python list structure.
- Return type:
- not_instance_of(cls)#
Collect all variables which are not instance of the given class type.
- Return type:
- remove_by_id(*ids, error_when_absent=False)[source]#
Remove or pop variables in the stack by the given ids.
- remove_var_by_id(*ids, error_when_absent=False)#
Remove or pop variables in the stack by the given ids.
- separate_by_instance(cls)[source]#
Separate all variables into two groups: (variables that are instances of the given
cls
, variables that are not instances of the givencls
).>>> import brainpy.math as bm >>> stack = VariableStack(a=bm.Variable(1), b=bm.random.RandomState(1)) >>> stack.separate_by_instance(bm.random.RandomState) ({'b': RandomState(key=([0, 1], dtype=uint32))}, {'a': Variable(value=Array([0.]), dtype=float32)}) >>> stack.separate_by_instance(bm.Variable) ({'a': Variable(value=Array([0.]), dtype=float32), 'b': RandomState(key=([0, 1], dtype=uint32))}, {})
- Parameters:
cls (
type
) – The class type.- Returns:
VariableStack of variables that are instances of the given
cls
VariableStack of variables that are not instances of the given
cls
- Return type:
A tuple with two elements
- subset_by_instance(cls)[source]#
Collect all variables which are instances of the given class type.
- Return type: