Variable¶

class
nnabla.
Variable
[source]¶ Bases:
nnabla._variable.Variable
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.
See also
Parameters:  shape (Iterable of int) – Shape of variable.
 need_grad (bool) – Flag for backprop or not.

__add__
(other)[source]¶ Elementwise addition. Implements the addition operator expression
A + B
, together with__radd__()
. When a scalar is specified forother
, this function performs an elementwise operation for all elements inself
.Parameters: other (float or Variable) – Internally calling add2()
oradd_scalar()
according to the type.Returns:
nnabla.Variable

__radd__
(other)[source]¶ Elementwise addition. Part of the implementation of the addition operator expression.
Parameters: other (float or Variable) – Replace with __add__()
.Returns:
nnabla.Variable

__sub__
(other)[source]¶ Elementwise subtraction. Implements the subtraction operator expression
A  B
, together with__rsub__()
. When a scalar is specified forother
, this function performs an elementwise operation for all elements inself
.Parameters: other (float or Variable) – Internally calling sub2()
oradd_scalar()
according to the type.Returns:
nnabla.Variable

__rsub__
(other)[source]¶ Elementwise subtraction. Part of the implementation of the subtraction operator.
Parameters: other (float or Variable) – Internally calling sub2()
orr_sub_scalar()
according to the type.Returns:
nnabla.Variable

__mul__
(other)[source]¶ Elementwise multiplication. Implements the multiplication operator expression
A * B
, together with__rmul__()
. When a scalar is specified forother
, this function performs an elementwise operation for all elements inself
.Parameters: other (float or Variable) – Internally calling mul2()
ormul_scalar()
according to the type.Returns:
nnabla.Variable

__rmul__
(other)[source]¶ Elementwise division. Part of the implementation of the division operator expression.
Parameters: other (float or Variable) – Replace with __mul__()
.Returns:
nnabla.Variable

__div__
(other)[source]¶ Elementwise division. Implements the division operator expression
A / B
, together with__rdiv__()
. When a scalar is specified forother
, this function performs an elementwise operation for all elements inself
.Parameters: other (float or Variable) – Internally calling div2()
ormul_scalar()
according to the type.Returns:
nnabla.Variable

__rdiv__
(other)[source]¶ Elementwise division. Part of the implementation of the division operator.
Parameters: other (float or Variable) – Internally calling sub2()
oradd_scalar()
according to the type.Returns:
nnabla.Variable

__pow__
(other)[source]¶ Elementwise power function. Implements the power operator expression
A ** B
, together with__rpow__()
. When a scalar is specified forother
, this function performs an elementwise operation for all elements inself
.Parameters: other (float or Variable) – Internally calling pow2()
orpow_scalar()
according to the type.Returns:
nnabla.Variable

__rpow__
(other)[source]¶ Elementwise power function. Part of the implementation of the power operator expression.
Parameters: other (float or Variable) – Internally calling pow2()
orr_pow_scalar()
according to the type.Returns:
nnabla.Variable

__neg__
()[source]¶ Elementwise negation. Implements the unary negation expression
A
.Returns:
nnabla.Variable

__pos__
()[source]¶ This function simply returns itself. Implements the unary plus operator,
+A
.Returns:
nnabla.Variable

backward
(self, grad=1, 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, clear_buffer=False, 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.

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 ( nblaeran.function.Function
) –Returns: nblaeran.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.