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. The nnabla.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.
Parameters:
  • shape (Iterable of int) – Shape of variable.
  • need_grad (bool) – Flag for backprop or not.
__add__(other)[source]

Element-wise addition. Implements the addition operator expression A + B, together with __radd__() . When a scalar is specified for other, this function performs an element-wise operation for all elements in self.

Parameters:other (float or Variable) – Internally calling add2() or add_scalar() according to the type.

Returns: nnabla.Variable

__radd__(other)[source]

Element-wise addition. Part of the implementation of the addition operator expression.

Parameters:other (float or Variable) – Replace with __add__().

Returns: nnabla.Variable

__sub__(other)[source]

Element-wise subtraction. Implements the subtraction operator expression A - B, together with __rsub__() . When a scalar is specified for other, this function performs an element-wise operation for all elements in self.

Parameters:other (float or Variable) – Internally calling sub2() or add_scalar() according to the type.

Returns: nnabla.Variable

__rsub__(other)[source]

Element-wise subtraction. Part of the implementation of the subtraction operator.

Parameters:other (float or Variable) – Internally calling sub2() or r_sub_scalar() according to the type.

Returns: nnabla.Variable

__mul__(other)[source]

Element-wise multiplication. Implements the multiplication operator expression A * B, together with __rmul__() . When a scalar is specified for other, this function performs an element-wise operation for all elements in self.

Parameters:other (float or Variable) – Internally calling mul2() or mul_scalar() according to the type.

Returns: nnabla.Variable

__rmul__(other)[source]

Element-wise division. Part of the implementation of the division operator expression.

Parameters:other (float or Variable) – Replace with __mul__().

Returns: nnabla.Variable

__div__(other)[source]

Element-wise division. Implements the division operator expression A / B, together with __rdiv__() . When a scalar is specified for other, this function performs an element-wise operation for all elements in self.

Parameters:other (float or Variable) – Internally calling div2() or mul_scalar() according to the type.

Returns: nnabla.Variable

__rdiv__(other)[source]

Element-wise division. Part of the implementation of the division operator.

Parameters:other (float or Variable) – Internally calling sub2() or add_scalar() according to the type.

Returns: nnabla.Variable

__pow__(other)[source]

Element-wise power function. Implements the power operator expression A ** B, together with __rpow__() . When a scalar is specified for other, this function performs an element-wise operation for all elements in self.

Parameters:other (float or Variable) – Internally calling pow2() or pow_scalar() according to the type.

Returns: nnabla.Variable

__rpow__(other)[source]

Element-wise power function. Part of the implementation of the power operator expression.

Parameters:other (float or Variable) – Internally calling pow2() or r_pow_scalar() according to the type.

Returns: nnabla.Variable

__neg__()[source]

Element-wise 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, or nnabla._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.
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 as grad if given.

The shape is also determined by the given array.

Parameters:
  • data (ndarray) – Values copied to the data of the created Variable.
  • grad (ndarray) – Values copied to the grad of the created Variable.
  • need_grad (bool) – Flag for backprop or not.

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() and nnabla._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 backprop-ed to the original variable.
Returns:

Variable

shape

Gets the shape of the variable.

Returns: tuple of int

size

Gets the size of the variable.

Returns: int

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 default
Returns:int
unlinked(self)

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