Variable¶

class
nnabla.
Variable
¶ Bases:
object
nnabla.Variable
is used to construct computation graphs (neural networks) together with functions in List of Functions and List of Parametric Functions . It also provides a method to execute forward and backward propagation of the network. Thennabla.Variable
class holds: Reference to the parent function in a computation graph. This provides traceability of all connections in the computation graph.
 Both data and error
signal (gradient) containers as
nnabla._nd_array.NdArray
s.  Some additional information of the computation graph.
Variable
overrides some arithmetic operators (+
,
,*
,/
,**
). Operands can be either a scalar number,NdArray
orVariable
. IfNdArray
is given as either of left or right operand, the arithmetic operation returns anNdArray
which stores the output of the computation immediately invoked. Otherwise, it returnsVariable
holds the graph connection. The computation is invoked immediately when :function:`nnabla.auto_forward` or :function:`nnabla.set_auto_forward(True)` is used.See also
Parameters:  shape (Iterable of int) – Shape of variable.
 need_grad (bool) – Flag for backprop or not.

backward
(self, grad=1, bool clear_buffer=False)¶ Performs a backward propagation starting from this variable until the root variable(s) is/are reached in the function graph. The propagation will stop at a variable with need_grad=False.
Parameters:  grad (scalar,
numpy.ndarray
, ornnabla._nd_array.NdArray
) – The gradient signal value(s) of this variable. The default value 1 is used in an usual neural network training. This option is useful if you have a gradient computation module outside NNabla, and want to use it as a gradient signal of the neural network built in NNabla. Note that this doesn’t modifies the grad values of this variable.  clear_buffer (bool) – Clears the no longer referenced variables during backpropagation to save memory.
 grad (scalar,

d
¶ Returns the values held by this variable, as a
numpy.ndarray
. Note that the values are referenced (not copied). Therefore, the modification of the returned ndarray will affet the data of the NNabla array. This method can be called as a setter to set the value held by this variable.Parameters: value ( numpy.ndarray
) (optional) –Returns: numpy.ndarray

data
¶ Returns the data held by this variable, as a
NdArray
. This can also be used as a setter.Parameters: ndarray (NdArray) – NdArray object. Size must be the same as this Variable. Returns: NdArray

forward
(self, bool clear_buffer=False, bool clear_no_need_grad=False)¶ Performs a forward propagation from the root node to this variable. The forward propagation is performed on a subset of variables determined by the dependency of this variable. The subset is recursively constructed by tracking variables that the variables in the subset depend on, starting from this variable, until it reaches the root variable(s) in the function graph.
Parameters:  clear_buffer (bool) – Clear the no longer referenced variables during forward propagation to save memory. This is usually set as True in an inference or a validation phase. Default is False.
 clear_no_need_grad (bool) – Clear the unreferenced variables with need_grad=False during forward propagation. True is usually used when calling this during training. This is ignored when clear_buffer=True.

static
from_numpy_array
(data, grad=None, need_grad=False)¶ Create a Variable object from Numpy array(s).
The
data
is initialized with the given Numpy array, as well asgrad
if given.The shape is also determined by the given array.
Parameters: Returns: ~nnabla.Variable

g
¶ Returns the gradient values held by this variable, as a
numpy.ndarray
. Note that the values are referenced (not copied). Therefore, the modification of the returned ndarray will affet the data of the NNabla array. This method can be called as a setter to set the gradient held by this variable.Parameters: value ( numpy.ndarray
) –Returns: numpy.ndarray

grad
¶ Returns the gradient held by this variable, as a
NdArray
. This can also be used as a setter.Parameters: ndarray (NdArray) – NdArray object. Size must be the same as this Variable. Returns: NdArray

info
¶ info – object
Information of the variable.

ndim
¶ Gets the number of dimensions of this variable.
Returns: int

need_grad
¶ Gets or sets a boolean indicating whether backpropagation is performed at this variable.
Parameters: b (bool) – Whether backpropagation is performed at this variable. Returns: Whether this variable requires gradient or not. Return type: bool

parent
¶ Returns the parent function of this variable. This method can also be called as a setter.
Parameters: func ( nnabla.function.Function
) –Returns: nnabla.function.Function

persistent
¶ Returns the persistent flag of this variable. If True, the variable is not cleared even if clear options in
nnabla._variable.Variable.forward()
andnnabla._variable.Variable.backward()
are enabled. This is useful when you debug the variable values, or log them. This method can also be called as a setter.Parameters: b (bool) – Returns: bool

reset_shape
(self, shape, force=False)¶ Resizes the shape of the variable to a specified shape.
Parameters:  shape (Iterable of int) – Target shape.
 force (bool) – Flag to force reshape.
Note
This method destructively changes the shape of the target variable. For safety,
reshape()
should be used instead.Returns: None

reshape
(self, shape, unlink=False)¶ Returns a new variable, where this variable is reshaped to a specified shape.
Parameters:  shape (Iterable of int) – Target shape.
 unlink (bool) – Unlink graph connection. Or, keep graph connection, i.e. the gradient will be backproped to the original variable.
Returns:

size_from_axis
(self, axis=1)¶ Gets the size followed by the provided axis.
Example
a = nnabla.Variable([10,9]) a.size_from_axis() # ==> 90 a.size_from_axis(0) # ==> 90 a.size_from_axis(1) # ==> 9 a.size_from_axis(2) # ==> 1
Parameters: axis ( int
, optional) – 1 as defaultReturns: int

unlinked
(self)¶ Gets unlinked (forgetting parent) variable that shares a Variable buffer instance.

visit
(self, f)¶ Visit functions recursively in forward order.
Parameters: f (function) – Function object which takes nnabla._function.Function
object as an argument.Returns: None

visit_check
(self, f)¶ Visit functions recursively in forward order.
Note
If any of evaluation of the function object returns True, the visit propagation will stop immediately, and will return True.
Parameters: f (function) – Function object which takes nnabla._function.Function
object as an argument. Returns: bool
 Returns True if any of the function object call returns True.