VariableView

Contents

VariableView#

class brainpy.math.VariableView(value, index)[source]#

A view of a Variable instance.

This class is used to create a subset view of brainpy.math.Variable.

>>> import brainpy.math as bm
>>> bm.random.seed(123)
>>> origin = bm.Variable(bm.random.random(5))
>>> view = bm.VariableView(origin, slice(None, 2, None))  # origin[:2]
VariableView([0.02920651, 0.19066381], dtype=float32)

VariableView can be used to update the subset of the original Variable instance, and make operations on this subset of the Variable.

>>> view[:] = 1.
>>> view
VariableView([1., 1.], dtype=float32)
>>> origin
Variable([1.       , 1.       , 0.5482849, 0.6564884, 0.8446237], dtype=float32)
>>> view + 10
Array([11., 11.], dtype=float32)
>>> view *= 10
VariableView([10., 10.], dtype=float32)

The above example demonstrates that the updating of an VariableView instance is actually made in the original Variable instance.

Moreover, it’s worthy to note that VariableView is not a PyTree.