Functions

All NNabla functions are derived from the nnabla.function.Function class.

Function

class nnabla.function.Function

Function interface class.

Instances of nnabla.function.Function are not directly created by users. It is indirectly created by the functions available in nnabla.functions. These functions return nnabla.Variable (s) holding the created function instance as the parent property.

backward(self, inputs, outputs, accum=None)
forward(self, inputs, outputs)
grad_depends_output_data(self, int i, int o)
info

info – object

inplace_data(self, int i)
inplace_data_with(self, int i)
inplace_grad(self, int i)
inplace_grad_with(self, int i)
min_outputs(self)
setup(self, inputs, outputs)

List of Functions

The nnabla.functions module provides various types of functions listed below. These functions takes input nnabla.Variable (s) as its leading argument(s), followed by options specific to each function.

Note:
The functions can also take NdArray (s) as output(s) holding output values of the operation. We call this “Imperative Mode” (NdArray + Functions).

Neural Network Layers

nnabla.functions.affine(x, weight, bias=None, base_axis=1, n_outputs=-1, outputs=None)[source]

Affine layer, also called as the fully connected layer. It calculates:

\[{\mathbf y} = {\mathbf A} {\mathbf x} + {\mathbf b}.\]

where \({\mathbf x}\) is the input and \({\mathbf y}\) is the output.

Parameters:
  • x (Variable) – Input N-D array with shape (\(M_0 \times ... \times M_{B-1} \times D_B \times ... \times D_N\)). Dimensions before and after base_axis are flattened as if it is a matrix.
  • weight (Variable) – [Parameter] Weight matrix with shape (\((D_B \times ... \times D_N) \times L\))
  • bias (Variable) – [Optional Parameter] Bias vector (\(L\))
  • base_axis (int) – Base axis of Affine operation. Dimensions up to base_axis is treated as sample dimension.
Returns:

\((B + 1)\)-D array. (\(M_0 \times ... \times M_{B-1} \times L\))

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.convolution(x, weight, bias=None, base_axis=1, pad=None, stride=None, dilation=None, group=1, n_outputs=-1, outputs=None)[source]

N-D Convolution with bias.

See references for dilated convolution (a.k.a. atrous convolution).

References

Parameters:
  • x (Variable) – \((B + 1 + N)\)-D array (\(M_1 \times ... \times M_B \times C \times L_1 \times ... \times L_N\)).
  • weight (Variable) – [Parameter] \((2 + N)\)-D array (\(C' \times C \times K_1 \times ... \times K_N\)).
  • bias (Variable) – [Optional Parameter] Bias vector (\(C'\)).
  • base_axis (int) – base axis \(B\).
  • pad (tuple of int) – Padding sizes for dimensions.
  • stride (tuple of int) – Stride sizes for dimensions.
  • dilation (tuple of int) – Dilation sizes for dimensions.
  • group (int) – Number of groups of channels. This makes the connection across channels sparser, by grouping connections along the mapping direction.
Returns:

\((B + 1 + N)\)-D array (\(M_1 \times ... \times M_B \times C' \times L'_1 \times ... \times L'_N\)).

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.deconvolution(x, weight, bias=None, base_axis=1, pad=None, stride=None, dilation=None, group=1, n_outputs=-1, outputs=None)[source]

N-D deconvolution, also known as transposed convolution, with bias operates backward convolution (derivative of the output w.r.t. the input) plus channel-wise learned bias.

The weights are specified in the same manner as convolution() , as if it was an ordinary convolution function. The forward operation of deconvolution() will then be operationally equivalent to the backward pass of convolution() . Therefore, the number of input channels (can be seen as output channels of forward convolution) is specified in the first dimension, and the number of the output channels divided by the number of groups is specified in the second dimension.

Parameters:
  • x (Variable) – \((B + 1 + N)\)-D array (\(M_1 \times ... \times M_B \times C \times L_1 \times ... \times L_N\)).
  • weight (Variable) – [Parameter] \((2 + N)\)-D array (\(C' \times C \times K_1 \times ... \times K_N\)).
  • bias (Variable) – [Optional Parameter] Bias vector (\(C'\)).
  • base_axis (int) – base axis \(B\).
  • pad (tuple of int) – Padding sizes for dimensions.
  • stride (tuple of int) – Stride sizes for dimensions.
  • dilation (tuple of int) – Dilation sizes for dimensions.
  • group (int) – Number of groups of channels. This makes the connection across channels sparser, by grouping connections along the mapping direction.
Returns:

\((B + 1 + N)\)-D array (\(M_1 \times ... \times M_B \times C' \times L'_1 \times ... \times L'_N\)).

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.max_pooling(x, kernel, stride=None, ignore_border=True, pad=None, n_outputs=-1, outputs=None)[source]

Max pooling. It pools the maximum values inside the scanning kernel:

\[y_{i_1, i_2} = \max_{k_1, k_2 \in K} (x_{i_1 + k_1, i_2 + k_2})\]

where \(x_{i_1 + k_1, i_2 + k_2}\) is the input and \(y_{i_1, i_2}\) is the output.

Parameters:
  • x (Variable) – Input variable.
  • kernel (tuple of int) – Kernel sizes for each spatial axis.
  • stride (tuple of int) – Subsampling factors for each spatial axis.
  • ignore_border (bool) – If false, kernels covering borders are also considered for the output.
  • pad (tuple of int) – Border padding values for each spatial axis. Padding will be added both sides of the dimension.
Returns:

Maximum values variable

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.average_pooling(x, kernel, stride=None, ignore_border=True, pad=None, including_pad=True, n_outputs=-1, outputs=None)[source]

Average pooling. It pools the averaged values inside the scanning kernel:

\[y_{i_1, i_2} = \frac{1}{K_1 K_2} \sum_{k1} \sum_{k2} x_{i_1 + k_1, i_2 + k_2}\]

where \(x_{i_1 + k_1, i_2 + k_2}\) is the input and \(y_{i_1, i_2}\) is the output.

Parameters:
  • x (Variable) – Input variable.
  • kernel (tuple of int) – Kernel sizes for each spatial axis.
  • stride (tuple of int) – Subsampling factors for each spatial axis.
  • ignore_border (bool) – If false, kernels covering borders are also considered for the output.
  • pad (tuple of int) – Border padding values for each spatial axis. Padding will be added both sides of the dimension.
  • including_pad (bool) – If true, border padding values are considered for the output.
Returns:

Average values variable

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.sum_pooling(x, kernel, stride=None, ignore_border=True, pad=None, n_outputs=-1, outputs=None)[source]

Sum pooling. It pools the summed values inside the scanning kernel:

\[y_{i_1, i_2} = \sum_{k1} \sum_{k2} x_{i_1 + k_1, i_2 + k_2}\]

where \(x_{i_1 + k_1, i_2 + k_2}\) is the input and \(y_{i_1, i_2}\) is the output.

Parameters:
  • x (Variable) – Input variable.
  • kernel (tuple of int) – Kernel sizes for each spatial axis.
  • stride (tuple of int) – Subsampling factors for each spatial axis.
  • ignore_border (bool) – If false, kernels covering borders are also considered for the output.
  • pad (tuple of int) – Border padding values for each spatial axis. Padding will be added both sides of the dimension.
Returns:

Summed values variable

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.unpooling(x, kernel, n_outputs=-1, outputs=None)[source]

Inverse operation of pooling. It spreads the input values:

\[y_{k_1 i_1 + j_1, k_2 i_2 + j_2} = x_{i_1, i_2}\]

where \(_{i_1, i_2}\) is the input and \(y_{k_1 i_1 + j_1, k_2 i_2 + j_2}\) is the output.

Parameters:
  • x (Variable) – Input variable.
  • kernel (tuple of int) – Kernel sizes for each spatial axis.
Returns:

Spread values variable

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.embed(x0, x1, n_outputs=-1, outputs=None)[source]

Embed slices of a matrix/tensor with indexing array/tensor.

Parameters:
  • x0 (Variable) – [Integer] Indices with shape \((I_0, ..., I_N)\)
  • x1 (Variable) – Weights with shape \((W_0, ..., W_M)\)
Returns:

Output with shape \((I_0, ..., I_N, W_1, ..., W_M)\)

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

Neural Network Activation

nnabla.functions.sigmoid(x, n_outputs=-1, outputs=None)[source]

Element-wise sigmoid function.

\[f(x) = \frac{1}{1 + \exp(-x)},\]
Parameters:x (Variable) – Input
Returns:Output
Return type:Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.tanh(x, n_outputs=-1, outputs=None)[source]

Element-wise hyperbolic tangent (tanh) function.

\[y_i = \tanh (x_i)\]
Parameters:x (Variable) – N-D array
Returns:N-D array with the same shape as x
Return type:Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.relu(x, inplace=False, n_outputs=-1, outputs=None)[source]

Element-wise Rectified Linear Unit (ReLU) function.

\[y_i = \max (0, x_i)\]
Parameters:
  • x (Variable) – N-D array
  • inplace (bool) – The output array is shared with the input array if True.
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.softmax(x, axis=None, n_outputs=-1, outputs=None)[source]

Softmax normalization. Calculates

\[y_i = \frac{\exp(x_i)}{\sum_j exp(x_j)}\]

along the dimension specified by axis, where \(y_i\) is the input and \(x_i\) is the output.

Parameters:
  • x (Variable) – N-D array. Typically indicates a score.
  • axis (int) – Axis normalization is taken.
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.elu(x, alpha=1.0, n_outputs=-1, outputs=None)[source]

Element-wise Exponential Linear Unit (ELU) function.

\[\begin{split}y_i= \left\{ \begin{array}{ll} x_i & (x > 0)\\ \alpha (\exp(x_i) - 1) & (x \leq 0) \end{array} \right..\end{split}\]

References

Parameters:
  • x (Variable) – N-D array
  • alpha (float) – Coefficient for negative outputs. \(\alpha\) in definition
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.crelu(x, axis=1, n_outputs=-1, outputs=None)[source]

Element-wise Concatenated Rectified Linear Unit (CReLU) function. This function calculates the ReLU of \(x\) and \(-x\) , then concatenates the results together at a specified axis, and returns the resulting array.

References

Parameters:
  • x (Variable) – N-D array.
  • axis (int) – The ReLU activations of positive inputs and negative inputs are concatenated at axis.
Returns:

N-D array where axis dimension is doubled by concatenating.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.celu(x, alpha=1.0, axis=1, n_outputs=-1, outputs=None)[source]

Element-wise Concatenated Exponential Linear Unit (CELU) function. Concatenates ELU outputs of positive and negative inputs together at specified axis.

Parameters:
  • x (Variable) – N-D array.
  • alpha (float) – Coefficient for negative outputs. \(\alpha\) in definition.
  • axis (int) – The ELU activations of positive inputs and negative inputs are concatenated at axis.
Returns:

N-D array where axis dimension is doubled by concatenating.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.prelu(x0, x1, base_axis=1, n_outputs=-1, outputs=None)[source]

Element-wise Parametrized Rectified Linear Unit function. Calculates:

\[y_i = \max(0, x_i) + w_i \min(0, -x_i)\]

where negative slope \(w\) is learned and can vary across channels (an axis specified with base_axis).

Parameters:
  • x0 (Variable) – (N-D array) Input
  • x1 (Variable) – (N-D array) Weights
  • base_axis (int) – Dimensions up to base_axis is treated as sample dimension.
Returns:

N-D array.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

Normalization

nnabla.functions.batch_normalization(x, beta, gamma, mean, variance, axes=[1], decay_rate=0.9, eps=1e-05, batch_stat=True, output_stat=False, n_outputs=None)[source]

Batch normalization.

\[\begin{split}\begin{eqnarray} \mu &=& \frac{1}{M} \sum x_i \\ \sigma^2 &=& \frac{1}{M} \left(\sum x_i - \mu\right)^2 \\ \hat{x}_i &=& \frac{x_i - \mu}{\sqrt{\sigma^2 + \epsilon}} \\ y_i &=& \hat{x}_i \gamma + \beta. \end{eqnarray}\end{split}\]

At testing time, the mean and variance values used are those that were computed during training by moving average.

References

Parameters:
  • x (Variable) – N-D array of input.
  • beta (Variable) – N-D array of beta which is learned.
  • gamma (Variable) – N-D array of gamma which is learned.
  • mean (Variable) – N-D array of running mean (modified during forward execution).
  • variance (Variable) – N-D array of running variance (modified during forward execution).
  • axes (repeated int64) – Axes mean and variance are taken.
  • decay_rate (float) – Decay rate of running mean and variance.
  • eps (float) – Tiny value to avoid zero division by std.
  • batch_stat (bool) – Use mini-batch statistics rather than running ones.
  • output_stat (bool) – It true, the batch statistics of mean and variance, will be returned as Variables. They are also differentiable.
Returns:

Retruns batch normalization output as Variable. If output_stat=True, it also returns the mean and variance of the mini-batch

See also

nnabla.function_bases.batch_normalization.

nnabla.functions.mean_subtraction(x, rmean, t, base_axis=1, update_running_mean=True, n_outputs=-1, outputs=None)[source]

It subtracts the mean of the elements of the input array, and normalizes it to \(0\). Preprocessing arrays with this function has the effect of improving accuracy in various tasks such as image classification.

At training time, this function is defined as

\[\begin{split}\begin{eqnarray} \mu &=& \frac{1}{M} \sum x_i \\ rm &=& ({\rm decay\_rate}) rm + (1 - {\rm decay\_rate}) \mu \\ y_i &=& x_i - rm \end{eqnarray}\end{split}\]

At validation time, it is defined as

\[y_i = x_i - rm\]

Note

The backward performs an approximated differentiation that takes into account only the latest mini-batch.

Parameters:
  • x (Variable) – N-D array of input.
  • rmean (Variable) – N-D array of running mean (modified during forward execution).
  • t (Variable) – Scalar of num of iteration of running mean (modified during forward execution).
  • base_axis (int) – Base axis of Mean Subtraction operation. Dimensions up to base_axis is treated as sample dimension.
  • update_running_mean (bool) – Update running mean during forward execution.
Returns:

N-D array.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

Reduction

nnabla.functions.sum(x, axis=None, keepdims=False)[source]

Reduction along axes with sum operation.

Parameters:
  • x (Variable) – An input variable.
  • axis (None, int or tuple of ints) – Axis or axes along which the sum is calculated. Passing the default value None will reduce all dimensions.
  • keepdims (bool) – Flag whether the reduced axes are kept as a dimension with 1 element.
Returns:

N-D array.

Return type:

Variable

nnabla.functions.mean(x, axis=None, keepdims=False)[source]

Reduction along axes with mean operation.

Parameters:
  • x (Variable) – An input variable.
  • axis (None, int or tuple of ints) – Axis or axes along which mean is calculated. Passing the default value None will reduce all dimensions.
  • keepdims (bool) – Flag whether the reduced axes are kept as a dimension with 1 element.
Returns:

N-D array.

Return type:

Variable

nnabla.functions.max(x, axis=None, keepdims=False)[source]

Reduction along axes with max operation.

Parameters:
  • x (Variable) – An input variable.
  • axis (None, int or tuple of ints) – Axis or axes along which max is calculated. Passing the default value None will reduce all dimensions.
  • keepdims (bool) – Flag whether the reduced axes are kept as a dimension with 1 element.
Returns:

N-D array.

Return type:

Variable

nnabla.functions.min(x, axis=None, keepdims=False)[source]

Reduction along axes with min operation.

Parameters:
  • x (Variable) – An input variable.
  • axis (None, int or tuple of ints) – Axis or axes along which min is calculated. Passing the default value None will reduce all dimensions.
  • keepdims (bool) – Flag whether the reduced axes are kept as a dimension with 1 element.
Returns:

N-D array.

Return type:

Variable

nnabla.functions.prod(x, axis=None, keepdims=False)[source]

Reduction along axes with product operation.

Parameters:
  • x (Variable) – An input variable.
  • axis (None, int or tuple of ints) – Axis or axes along which product is calculated. Passing the default value None will reduce all dimensions.
  • keepdims (bool) – Flag whether the reduced axes are kept as a dimension with 1 element.
Returns:

N-D array.

Return type:

Variable

Note

Backward computation is not accurate in a zero value input.

nnabla.functions.reduce_sum(x, n_outputs=-1, outputs=None)[source]

Reduction along an axis with sum operation.

Note

This is deprecated. Use sum instead.

Parameters:x (Variable) – N-D array.
Returns:N-D array
Return type:Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.reduce_mean(x, n_outputs=-1, outputs=None)[source]

Reduction by mean along an axis.

Note

This is deprecated. Use mean instead.

Parameters:x (Variable) – N-D array
Returns:N-D array
Return type:Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

Arithmetic

nnabla.functions.add2(x0, x1, inplace=False, n_outputs=-1, outputs=None)[source]

Element-wise addition.

\[y_i = x^{(0)}_i + x^{(1)}_i\]
Parameters:
  • x0 (Variable) – N-D array
  • x1 (Variable) – N-D array
  • inplace (bool) – The output array is shared with the 1st input array if True.
Returns:

N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.sub2(x0, x1, n_outputs=-1, outputs=None)[source]

Element-wise subtraction.

\[y_i = x^{(0)}_i - x^{(1)}_i\]
Parameters:
Returns:

N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.mul2(x0, x1, n_outputs=-1, outputs=None)[source]

Element-wise multiplication.

\[y_i = x^{(0)}_i x^{(1)}_i\]
Parameters:
Returns:

N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.div2(x0, x1, n_outputs=-1, outputs=None)[source]

Element-wise division.

\[y_i = \frac{x^{(0)}_i} {x^{(1)}_i}\]
Parameters:
Returns:

N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.pow2(x0, x1, n_outputs=-1, outputs=None)[source]

Element-wise power funtion.

\[y_i = {(x^{(0)}_i)} ^ {x^{(1)}_i}\]
Parameters:
Returns:

N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.add_scalar(x, val=1, n_outputs=-1, outputs=None)[source]

Element-wise scalar addition.

\[y_i = x_i + v\]
Parameters:
  • x (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.mul_scalar(x, val=1, n_outputs=-1, outputs=None)[source]

Element-wise scalar multiplication.

\[y_i = v x_i\]
Parameters:
  • x (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.pow_scalar(x, val=1, n_outputs=-1, outputs=None)[source]

Element-wise scalar power function.

\[y_i = (x_i) ^ v\]
Parameters:
  • x (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.r_sub_scalar(x, val=1, n_outputs=-1, outputs=None)[source]

Element-wise scalar subtraction.

\[y_i = v - x_i\]
Parameters:
  • x (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.r_div_scalar(x, val=1, n_outputs=-1, outputs=None)[source]

Element-wise scalar division.

\[y_i = \frac{v}{x_i}\]
Parameters:
  • x (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.r_pow_scalar(x, val=1, n_outputs=-1, outputs=None)[source]

Element-wise scalar power function.

\[y_i = v ^ {x_i}\]
Parameters:
  • x (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

Logical

nnabla.functions.equal(x0, x1, n_outputs=-1, outputs=None)[source]

Element wise ‘equal’

\[\begin{split}f(x^{(0)}_i,x^{(1)}_i) = \begin{cases} 1 & (x^{(0)}_i = x^{(1)}_i) \\ 0 & otherwise \end{cases}.\end{split}\]
Parameters:
Returns:

No Description

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.equal_scalar(x0, val=1, n_outputs=-1, outputs=None)[source]

Element wise ‘equal’ with a scalar

\[\begin{split}f(x_i,v) = \begin{cases} 1 & (x_i = v) \\ 0 & otherwise \end{cases}.\end{split}\]
Parameters:
  • x0 (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.greater(x0, x1, n_outputs=-1, outputs=None)[source]

Element wise comparison. The \(i^{th}\) element of the output is:

\[\begin{split}f(x^{(0)}_i,x^{(1)}_i) = \begin{cases} 1 & (x^{(0)}_i > x^{(1)}_i) \\ 0 & (x^{(0)}_i \leq x^{(1)}_i) \end{cases}.\end{split}\]
Parameters:
Returns:

No Description

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.greater_equal(x0, x1, n_outputs=-1, outputs=None)[source]

Element wise comparison. The \(i^{th}\) element of the output is:

\[\begin{split}f(x^{(0)}_i,x^{(1)}_i) = \begin{cases} 1 & (x^{(0)}_i \geq x^{(1)}_i) \\ 0 & (x^{(0)}_i < x^{(1)}_i) \end{cases}.\end{split}\]
Parameters:
Returns:

No Description

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.greater_equal_scalar(x0, val=1, n_outputs=-1, outputs=None)[source]

Element wise comparison with a scalar. The \(i^{th}\) element of the output is:

\[\begin{split}f(x^{(0)}_i,v) = \begin{cases} 1 & (x^{(0)}_i \geq v \\ 0 & (x^{(0)}_i < v \end{cases}.\end{split}\]
Parameters:
  • x0 (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.greater_scalar(x0, val=1, n_outputs=-1, outputs=None)[source]

Element wise comparison with a scalar. The \(i^{th}\) element of the output is:

\[\begin{split}f(x^{(0)}_i,v) = \begin{cases} 1 & (x^{(0)}_i > v \\ 0 & (x^{(0)}_i \leq v \end{cases}.\end{split}\]
Parameters:
  • x0 (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.less(x0, x1, n_outputs=-1, outputs=None)[source]

Element wise comparison. The \(i^{th}\) element of the output is:

\[\begin{split}f(x^{(0)}_i,x^{(1)}_i) = \begin{cases} 1 & (x^{(0)}_i < x^{(1)}_i) \\ 0 & (x^{(0)}_i \geq x^{(1)}_i) \end{cases}.\end{split}\]
Parameters:
Returns:

No Description

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.less_equal(x0, x1, n_outputs=-1, outputs=None)[source]

Element wise comparison. The \(i^{th}\) element of the output is:

\[\begin{split}f(x^{(0)}_i,x^{(1)}_i) = \begin{cases} 1 & (x^{(0)}_i \leq x^{(1)}_i) \\ 0 & (x^{(0)}_i > x^{(1)}_i) \end{cases}.\end{split}\]
Parameters:
Returns:

No Description

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.less_equal_scalar(x0, val=1, n_outputs=-1, outputs=None)[source]

Element wise comparison with a scalar. The \(i^{th}\) element of the output is:

\[\begin{split}f(x^{(0)}_i,v) = \begin{cases} 1 & (x^{(0)}_i \leq v) \\ 0 & (x^{(0)}_i > v) \end{cases}.\end{split}\]
Parameters:
  • x0 (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.less_scalar(x0, val=1, n_outputs=-1, outputs=None)[source]

Element wise comparison with a scalar. The \(i^{th}\) element of the output is:

\[\begin{split}f(x^{(0)}_i,v) = \begin{cases} 1 & (x^{(0)}_i < v) \\ 0 & (x^{(0)}_i \geq v) \end{cases}.\end{split}\]
Parameters:
  • x0 (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.logical_and(x0, x1, n_outputs=-1, outputs=None)[source]

Elementwise logical AND.

\[\begin{split}f(x^{(0)}_i,x^{(1)}_i) = \begin{cases} 1 & (x^{(0)}_i \neq 0 \;\&\; x^{(1)}_i \neq 0) \\ 0 & otherwise \end{cases}.\end{split}\]
Parameters:
Returns:

No Description

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.logical_and_scalar(x0, val, n_outputs=-1, outputs=None)[source]

Elementwise logical AND with scalar.

\[\begin{split}f(x_i,v) = \begin{cases} 1 & (x_i \neq 0 \;\&\; v \neq 0) \\ 0 & otherwise \end{cases}.\end{split}\]
Parameters:
  • x0 (Variable) – Input variable
  • val (bool) – No Description
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.logical_not(x0, n_outputs=-1, outputs=None)[source]

Element-wise logical NOT operation

\[\begin{split}f(x_i) = \begin{cases} 1 & (x_i = 0) \\ 0 & otherwise \end{cases}.\end{split}\]
Parameters:x0 (Variable) – Input variable
Returns:N-D array with the same shape as x
Return type:Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.logical_or(x0, x1, n_outputs=-1, outputs=None)[source]

Elementwise logical OR.

\[\begin{split}f(x^{(0)}_i,x^{(1)}_i) = \begin{cases} 0 & (x^{(0)}_i = 0 \;\&\; x^{(1)}_i = 0) \\ 1 & otherwise \end{cases}.\end{split}\]
Parameters:
Returns:

No Description

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.logical_or_scalar(x0, val, n_outputs=-1, outputs=None)[source]

Elementwise logical OR with scalar.

\[\begin{split}f(x_i,v) = \begin{cases} 0 & (x_i = 0 \;\&\; v = 0) \\ 1 & otherwise \end{cases}.\end{split}\]
Parameters:
  • x0 (Variable) – Input variable
  • val (bool) – No Description
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.logical_xor(x0, x1, n_outputs=-1, outputs=None)[source]

Elementwise logical XOR.

\[\begin{split}f(x^{(0)}_i,x^{(1)}_i) = \begin{cases} 1 & (x^{(0)}_i = 0 \;\&\; x^{(1)}_i = 0) \\ 1 & (x^{(0)}_i \neq 0 \;\&\; x^{(1)}_i \neq 0) \\ 0 & otherwise \end{cases}.\end{split}\]
Parameters:
Returns:

No Description

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.logical_xor_scalar(x0, val, n_outputs=-1, outputs=None)[source]

Elementwise logical XOR with scalar.

\[\begin{split}f(x_i,v) = \begin{cases} 1 & (x_i = 0 \;\&\; v = 0) \\ 1 & (x_i \neq 0 \;\&\; v \neq 0) \\ 0 & otherwise \end{cases}.\end{split}\]
Parameters:
  • x0 (Variable) – Input variable
  • val (bool) – No Description
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.not_equal(x0, x1, n_outputs=-1, outputs=None)[source]

Element wise ‘not equal’

\[\begin{split}f(x^{(0)}_i,x^{(1)}_i) = \begin{cases} 0 & (x^{(0)}_i = x^{(1)}_i) \\ 1 & otherwise \end{cases}.\end{split}\]
Parameters:
Returns:

No Description

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.not_equal_scalar(x0, val=1, n_outputs=-1, outputs=None)[source]

Element wise ‘not equal’ with a scalar

\[\begin{split}f(x_i,v) = \begin{cases} 0 & (x_i = v) \\ 1 & otherwise \end{cases}.\end{split}\]
Parameters:
  • x0 (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.sign(x, alpha=0.0, n_outputs=-1, outputs=None)[source]

Element-wise sign function.

In the forward pass, it is defined as

\[\begin{split}f(x) = \begin{cases} 1 & (x > 0) \\ -1 & (x < 0) \\ \alpha & (x = 0) \end{cases}.\end{split}\]

In the backward pass, it is defined as

\[\frac{\partial f(x)}{\partial x} = 1,\]

or in other words, it behaves as the identity function for the gradient in the backward pass.

Parameters:
  • x (Variable) – Input
  • alpha (float) – Value in case of \(x = 0\).
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.minimum2(x0, x1, n_outputs=-1, outputs=None)[source]

Element-wise minimum.

\[y_i = \min(x^{(0)}_i, x^{(1)}_i)\]
Parameters:
Returns:

N-D array of min value

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.maximum2(x0, x1, n_outputs=-1, outputs=None)[source]

Element-wise maximum.

\[y_i = \max(x^{(0)}_i, x^{(1)}_i)\]
Parameters:
Returns:

N-D array of max value

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.minimum_scalar(x, val=1.0, n_outputs=-1, outputs=None)[source]

Element-wise scalar minimum.

\[y_i = \min(x_i, v)\]
Parameters:
  • x (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.maximum_scalar(x, val=1.0, n_outputs=-1, outputs=None)[source]

Element-wise scalar maximum.

\[y_i = \max (x_i, v)\]
Parameters:
  • x (Variable) – Input variable
  • val (float) – Value of the scalar
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

Math

nnabla.functions.constant(val=0, shape=[], n_outputs=-1, outputs=None)[source]

Generate a constant-valued array.

Parameters:
  • val (float) – Constant value.
  • shape (tuple of int) – Shape of the output array.
Returns:

N-D array where all values are the specified constant.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.abs(x, n_outputs=-1, outputs=None)[source]

Element-wise absolute value function.

\[y_i = |x_i|\]
Parameters:x (Variable) – Input variable
Returns:Element-wise absolute variable
Return type:Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.exp(x, n_outputs=-1, outputs=None)[source]

Element-wise natural exponential function.

\[y_i = \exp(x_i).\]
Parameters:x (Variable) – Input variable
Returns:Element-wise exp variable
Return type:Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.log(x, n_outputs=-1, outputs=None)[source]

Element-wise natural logarithm function.

\[y_i = \ln(x_i).\]
Parameters:x (Variable) – Input variable
Returns:Element-wise log variable
Return type:Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.identity(x, n_outputs=-1, outputs=None)[source]

Identity function.

\[y = x\]
Parameters:x (Variable) – N-D array.
Returns:N-D array
Return type:Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

Array Manipulation

nnabla.functions.concatenate(*inputs, **args)[source]

Concatenate two arrays along the specified axis.

Parameters:
  • *inputs (Variable) – [Variadic Parameter] N-D arrays.
  • **param (int) – [name=axis] Axis
Returns:

Concatenate variable

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.split(x, axis=0)[source]

Split arrays at the specified axis.

It returns a number corresponding the size of the given axis (i.e x.shape[axis]) of Variable s.

Parameters:

Returns: A tuple of Variable s

See also

nnabla.function_bases.split().

nnabla.functions.stack(*inputs, **args)[source]

Joins two or more arrays on a new axis.

Note

Unlike nnabla.functions.concatenate() , which joins arrays on an existing axis, Stack joins arrays on a new axis.

Parameters:
  • *inputs (Variable) – [Variadic Parameter] N-D arrays. The sizes of all the arrays to be stacked must be the same.
  • **param (int) – [name=axis] The axis on which to concatenate arrays. Axis indices take on values 0, 1, 2, and so on from the left. For example, to stack four (3,28,28) inputs on the second axis, specify 1. In this case, the output size will be (3,4,28,28).
Returns:

Output

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.slice(x, start=None, stop=None, step=None, n_outputs=-1, outputs=None)[source]

Slice arrays along specified axis.

Parameters:
  • x (Variable) – N-D array
  • start (repeated int64) – Start indices for each axis
  • stop (repeated int64) – Stop indices for each axis
  • step (repeated int64) – Step indices for each axis
Returns:

Sliced N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.transpose(x, axes, n_outputs=-1, outputs=None)[source]

Transposes tensor dimensions.

Parameters:
  • x (Variable) – N-D array
  • axes (repeated int64) – Source axis indices for each axis.
Returns:

Transposed N-D array.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.broadcast(x, shape, n_outputs=-1, outputs=None)[source]

Broadcasting ND-array to the specified shape.

Parameters:
  • x (Variable) – N-D array
  • shape (tuple of int) – Shape broadcasted to. The size must be the same in axis where x‘s shape is not 1.
Returns:

Broadcasted N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.flip(x, axes=None, n_outputs=-1, outputs=None)[source]

Reverses the order of elements of the specified dimension of an array.

Parameters:
  • x (Variable) – N-D array
  • axes (repeated int64) – The index of the dimension to reverse the order of the elements. Axis indices take on values 0, 1, 2, and so on from the left. For example, to flip a 32 (W) by 24 (H) 100 RGB image (100,3,24,32) vertically and horizontally, specify (2,3).
Returns:

N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.shift(x, shifts=None, border_mode='nearest', n_outputs=-1, outputs=None)[source]

Shifts the array elements by the specified amount.

Parameters:
  • x (Variable) – N-D array.
  • shifts (repeated int64) – The amount to shift elements. For example, to shift image data to the right by 2 pixels and up 3 pixels, specify (-3,2).
  • border_mode (string) – Specify how to process the ends of arrays whose values will be undetermined as a result of shifting. nearest: The data at the ends of the original array is copied and used. reflect: Original data reflected at the ends of the original array is used.
Returns:

N-D array.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.reshape(x, shape, n_outputs=-1, outputs=None)[source]

Returns a copy of the reshaped input variable.

Note

If you do not need a copy, you should use the nnabla.Variable.reshape() method instead.

Parameters:
Returns:

Reshaped N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.one_hot(x, shape, n_outputs=-1, outputs=None)[source]

OneHot creates one-hot vector based on input indices.

Parameters:
Returns:

N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

Stochasticity

nnabla.functions.dropout(x, p=0.5, seed=-1, n_outputs=-1, outputs=None)[source]

Dropout. Samples a number \(u\) from a uniform distribution in \([0, 1]\) , and ignores the input if \(u > p\).

\[\begin{split}y = \left\{ \begin{array}{ll} \frac{x}{1 - p} & (u > p) \\ 0 & ({\rm otherwise}) \end{array} \right.\end{split}\]

Note

Usually dropout only applied during training as below (except Bayesian dropout).

h = PF.affine(x, num_hidden)
if train:
    h = F.dropout(h, 0.5)
Parameters:
  • x (Variable) – N-D array
  • p (float) – \(p\) in definition.
  • seed (int) – Random seed. When -1, seed is sampled from global random number generator.
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.rand(low=0, high=1, shape=[], seed=-1, n_outputs=-1, outputs=None)[source]

Samples numbers from a uniform distribution \(x \sim U(low, high)\) given lowest value \(low\), upper bound \(high\), and shape of the returned Variable.

Parameters:
  • low (float) – \(low\) in definition.
  • high (float) – \(high\) in definition.
  • shape (tuple of int) – Shape of returned variable.
  • seed (int) – Random seed. When -1, seed is sampled from global random number generator.
Returns:

Variable with the shape specified in the argument.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.randint(low=0, high=1, shape=[], seed=-1, n_outputs=-1, outputs=None)[source]

Samples integer numbers from a uniform distribution \(x \sim U(low, high)\) given lowest value \(low\), upper bound \(high\), and shape of the returned Variable.

Parameters:
  • low (int) – \(low\) in definition.
  • high (int) – \(high\) in definition.
  • shape (tuple of int) – Shape of returned variable.
  • seed (int) – Random seed. When -1, seed is sampled from global random number generator.
Returns:

Variable with the shape specified in the argument. The dtype is int32.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.randn(mu=0, sigma=1, shape=[], seed=-1, n_outputs=-1, outputs=None)[source]

Samples numbers from a normal distribution \(x \sim N(\mu, \sigma)\) given mean \(\mu\), standard deviation \(\sigma\), and shape of the returned Variable.

Parameters:
  • mu (float) – \(\mu\) in definition.
  • sigma (float) – \(\sigm\) in definition.
  • shape (tuple of int) – Shape of returned variable.
  • seed (int) – Random seed. When -1, seed is sampled from global random number generator.
Returns:

Variable with the shape specified in the argument.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.dropout(x, p=0.5, seed=-1, n_outputs=-1, outputs=None)[source]

Dropout. Samples a number \(u\) from a uniform distribution in \([0, 1]\) , and ignores the input if \(u > p\).

\[\begin{split}y = \left\{ \begin{array}{ll} \frac{x}{1 - p} & (u > p) \\ 0 & ({\rm otherwise}) \end{array} \right.\end{split}\]

Note

Usually dropout only applied during training as below (except Bayesian dropout).

h = PF.affine(x, num_hidden)
if train:
    h = F.dropout(h, 0.5)
Parameters:
  • x (Variable) – N-D array
  • p (float) – \(p\) in definition.
  • seed (int) – Random seed. When -1, seed is sampled from global random number generator.
Returns:

N-D array with the same shape as x

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.random_crop(x, shape=None, base_axis=1, seed=-1, n_outputs=-1, outputs=None)[source]

RandomCrop randomly extracts a portion of an array.

Parameters:
  • x (Variable) – N-D array
  • shape (tuple of int) – The data size to extract. For example, to randomly extract a portion of the image (3,48,48) from a 3,64,64 image, specify (3,48,48).
  • base_axis (int) – No Description
  • seed (int) – Random seed. When -1, seed is sampled from global random number generator.
Returns:

N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.random_flip(x, axes=None, base_axis=1, seed=-1, n_outputs=-1, outputs=None)[source]

Reverses the order of elements of the specified dimension of an array at 50% probability.

Parameters:
  • x (Variable) – N-D array
  • axes (repeated int64) – The index of the axis to reverse the order of the elements. Axis indices take on values 0, 1, 2, and so on from the left. For example, to flip a 32 (W) by 24 (H) 100 RGB images (100, 3,24,32) vertically and horizontally at random, specify (2,3).
  • base_axis (int) – No Description
  • seed (int) – Random seed. When -1, seed is sampled from global random number generator.
Returns:

N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.random_shift(x, shifts=None, border_mode='nearest', base_axis=1, seed=-1, n_outputs=-1, outputs=None)[source]

Randomly shifts the array elements within the specified range.

Parameters:
  • x (Variable) – N-D array.
  • shifts (repeated int64) – Max absolute amount to shift elements. For example, to shift image data horizontally by \(\pm 2\) pixels and vertically by \(\pm 3\) pixels, specify (3,2).
  • border_mode (string) – Specify how to process the ends of arrays whose values will be undetermined as a result of shifting. nearest: The data at the ends of the original array is copied and used. reflect: Original data reflected at the ends of the original array is used.
  • base_axis (int) – No Description
  • seed (int) – Random seed. When -1, seed is sampled from global random number generator.
Returns:

N-D array.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.image_augmentation(x, shape=None, pad=(0, 0), min_scale=1.0, max_scale=1.0, angle=0.0, aspect_ratio=1.0, distortion=0.0, flip_lr=False, flip_ud=False, brightness=0.0, brightness_each=False, contrast=1.0, contrast_center=0.0, contrast_each=False, noise=0.0, seed=-1, n_outputs=-1, outputs=None)[source]

ImageAugmentation randomly alters the input image.

Parameters:
  • x (Variable) – N-D array.
  • shape (tuple of int) – The output image data size.
  • pad (tuple of int) – Border padding values for each spatial axis. Padding will be added both sides of the dimension.
  • min_scale (float) – The minimum scale ratio when randomly scaling the image. For example, to scale down to 0.8 times the size of the original image, specify “0.8”. To not apply random scaling, set both min_scale and max_scale to “1.0”.
  • max_scale (float) – The maximum scale ratio when randomly scaling the image. For example, to scale down to 2 times the size of the original image, specify “2.0”.
  • angle (float) – The rotation angle range in radians when randomly rotating the image. The image is randomly rotated in the -Angle to +Angle range. For example, to rotate in a +-15 degree range, specify “0.26” (15 degrees/360 degrees * 2PI). To not apply random rotation, specify “0.0”.
  • aspect_ratio (float) – The aspect ratio range when randomly deforming the image. For example, to deform aspect ratio of image from 1:1.3 to 1.3:1, specify “1.3”. To not apply random deforming, specify “1.0”.
  • distortion (float) – The distortion range when randomly distorting the image. To not apply distortion, specify “0.0”.
  • flip_lr (bool) – Whether to randomly flip the image horizontally at 50% probability.
  • flip_ud (bool) – Whether to randomly flip the image vertically at 50% probability.
  • brightness (float) – The absolute range of values to randomly add to the brightness. A random value in the -Brightness to +Brightness range is added to the brightness. For example, to vary the brightness in the -0.05 to +0.05 range, specify “0.05”. To not apply random addition to brightness, specify “0.0”.
  • brightness_each (bool) – Whether to apply the random addition to brightness (as specified by brightness) to each color channel. True: brightness is added based on a different random number for each channel. False: brightness is added based on a random number common to all channels.
  • contrast (float) – The range in which to randomly vary the image contrast. The contrast is varied in the 1/Contrast times to Contrast times range. The output brightness is equal to (input - contrast_center) * contrast + contrast_center. For example, to vary the contrast in the 0.91 times to 1.1 times range, specify “1.1”. To not apply random contrast variation, specify “1.0”.
  • contrast_center (float) – Intensity center used for applying contrast.
  • contrast_each (bool) – Whether to apply the random contrast variation (as specified by contrast) to each color channel. True: contrast is varied based on a different random number for each channel. False: contrast is varied based on a random number common to all channels.
  • noise (float) – Sigma of normal random number to be added.
  • seed (int) – Random seed. When -1, seed is sampled from global random number generator.
Returns:

N-D array.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

Loss Functions

nnabla.functions.sigmoid_cross_entropy(x, target, n_outputs=-1, outputs=None)[source]

Element-wise cross entropy between x and the target variables, passed to a sigmoid function.

\[y_i = - \left(x^{(1)}_i \ln \left(\sigma \left(x^{(0)}_i \right)\right) + \ \left(1 - x^{(1)}_i\right) \ln \left(1 - \sigma \left(x^{(0)}_i \ \right)\right)\right)\]

where \(\sigma(s)=\frac{1}{1+\exp(-s)}\).

Note

SigmoidCrossEntropy is equivalent to Sigmoid+BinaryCrossEntropy, but computing them at once has the effect of reducing computational error.

Parameters:
  • x (Variable) – [Parameter] N-D array. Typically indicates a score. The value lies in \([-\infty, \infty]\)
  • target (Variable) – [Integer Parameter] N-D array of labels. Only 0 or 1 value is allowed.
Returns:

N-D array of element-wise losses.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.binary_cross_entropy(x, target, n_outputs=-1, outputs=None)[source]

Element-wise cross entropy between x and the target variables.

\[y_i = - \left(x^{(1)}_i * \ln \left(x^{(0)}_i\right) + \left(1 - \ x^{(1)}_i\right) * \ln \left(1 - x^{(0)}_i\right)\right).\]
Parameters:
  • x (Variable) – Probabilities N-D array. \(-\infty\) to \(\infty\).
  • target (Variable) – N-D array of labels. Usually set as 0 or 1, but, unlike SigmoidCrossEntropy, it allows propbability (0 to 1) as inputs and backpropagation can be done.
Returns:

N-D array of element-wise losses.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.softmax_cross_entropy(x, target, axis=None, n_outputs=-1, outputs=None)[source]

Element-wise cross entropy between the variables and the variables of a label given by a category index with Softmax normalization.

\[y_{j} = -\ln \left(\frac{\exp(x_{t_j,j})}{\sum_{i'} exp(x_{i'j})}\right)\]

along dimension specified by axis (\(i\) is the axis where normalization is performed on).

Note

SoftmaxCrossEntropy is equivalent to Softmax+CategoricalCrossEntropy, but computing them at once has the effect of reducing computational error.

Parameters:
  • x (Variable) – [Parameter] N-D array. Typically indicates a score. \((D_1 \times ... \times D_i \times ... \times D_N)\)
  • target (Variable) – [Integer Parameter] N-D array of labels. \((D_1 \times ... \times 1 \times ... \times D_N)\)
  • axis (int) – Axis normalization is taken.
Returns:

N-D array of element-wise losses. \((D_1 \times ... \times 1 \times ... \times D_N)\)

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.categorical_cross_entropy(x, target, axis=None, n_outputs=-1, outputs=None)[source]

Element-wise cross entropy between x and the target, given by a category index.

\[y_{j} = -\ln \left(\frac{\exp(x_{t_j,j})}{\sum_{i'} exp(x_{i'j})}\right)\]

along dimension specified by axis (\(i\) is the axis where normalization is performed on).

Parameters:
  • x (Variable) – [Parameter] N-D array. Typically indicates a score. \((D_1 \times ... \times D_i \times ... \times D_N)\)
  • target (Variable) – [Integer Parameter] N-D array of labels. \((D_1 \times ... \times 1 \times ... \times D_N)\)
  • axis (int) – Axis normalization is taken.
Returns:

N-D array of element-wise losses. \((D_1 \times ... \times 1 \times ... \times D_N)\)

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.squared_error(x0, x1, n_outputs=-1, outputs=None)[source]

Element-wise squared error

\[y_i = \left(x^{(0)}_i - x^{(1)}_i\right)^2.\]
Parameters:
Returns:

N-D array.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.absolute_error(x0, x1, n_outputs=-1, outputs=None)[source]

Element-wise absolute error

\[y_i = | x^{(0)}_i - x^{(1)}_i |.\]
Parameters:
Returns:

N-D array.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.huber_loss(x0, x1, delta=1.0, n_outputs=-1, outputs=None)[source]

Element-wise Huber loss

\[\begin{split}y_i= \left\{ \begin{array}{ll} d^2 & (|d| < \delta)\\ \delta (2 |d| - \delta) & ({\rm otherwise}) \end{array} \right.\end{split}\]

where \(d = x^{(0)}_i - x^{(1)}_i\)

Parameters:
Returns:

N-D array of element-wise losses.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.epsilon_insensitive_loss(x0, x1, epsilon, n_outputs=-1, outputs=None)[source]

Element-wise Eplision Insensitive Loss

\[\begin{split}y_i= \left\{ \begin{array}{ll} | x^{(0)}_i - x^{(1)}_i | - \epsilon & if \ \ | x^{(0)}_i - x^{(1)}_i | > \epsilon \\ 0 & otherwise \end{array} \right.\end{split}\]
Parameters:
Returns:

N-D array of element-wise losses.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.kl_multinomial(p, q, base_axis=1, n_outputs=-1, outputs=None)[source]

The Kullback Leibler Divergence for multinomial distributions.

\[D = \sum_i p_i \log \left( \frac{p_i}{q_i} \right)\]
Parameters:
  • p (Variable) – N-D array of the source categorical probabilities
  • q (Variable) – N-D array of the target categorical probabilities
  • base_axis (int) – Dimensions up to base_axis is treated as sample dimension.
Returns:

Kullback Leibler divergence \(KL(p \parallel q)\).

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

Quantization Neural Network Layers

nnabla.functions.binary_sigmoid(x, n_outputs=-1, outputs=None)[source]

Element-wise binary sigmoid function. In the forward pass, it computes

\[\begin{split}f(x) = \begin{cases} 1 & (x > 0) \\ 0 & ({\rm otherwise})\end{cases},\end{split}\]

but in the backward pass, a straight-through approximation of the gradient is used, i.e.,

\[\begin{split}\frac{\partial f(x)}{\partial x} = \begin{cases} 0 & (|x| \geq 1) \\ \frac{1}{2} & ({\rm otherwise}) \end{cases}.\end{split}\]

References

Parameters:x (Variable) – Input .
Returns:Output.
Return type:Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.binary_tanh(x, n_outputs=-1, outputs=None)[source]

Element-wise binary tanh function. In the forward pass, it computes

\[\begin{split}f(x) = \begin{cases} 1 & (x > 0) \\ -1 & ({\rm otherwise}) \end{cases},\end{split}\]

but in the backward pass, a straight-through approximation of the gradient is used, i.e.,

\[\begin{split}\frac{\partial f(x)}{\partial x} = \begin{cases} 0 & (|x| \geq 1) \\ 1 & ({\rm otherwise}) \end{cases}.\end{split}\]

References

Parameters:x (Variable) – Input .
Returns:Output.
Return type:Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.binary_connect_affine(x, weight, binary_weight, bias=None, base_axis=1, n_outputs=-1, outputs=None)[source]

This function provides a BinaryConnect affine layer. It computes in the forward pass

\[y_j = \sum_{i} sign(w_{j,i}) x_i,\]

i.e., the weights \(w_{j,i}\) are binarized to \(sign(w_{j,i})\) and, hence, each weight is in \(\{-1,\,1\}\). By this weight binarization, the inner product computations do not require any multiplications anymore as they turn into additions/subtractions.

This function should be used together with batch_normalization().

Note

1) If you would like to share the binary weights between other layers, please use the standard, floating value weights (weight) and not the binary weights (binary_weight).

2) The weights and the binary weights become in sync only after a call to forward(), and not after a call to backward(). If you wish to store the parameters of the network, remember to call forward(), once before doing so, otherwise the weights and the binary weights will not be in sync.

3) CPU and GPU implementations now use floating values for binary_weight, since this function is for simulation purposes.

References

Parameters:
  • x (Variable) – Input .
  • weight (Variable) – [Parameter] Weight .
  • binary_weight (Variable) – [Parameter] Binarized weight .
  • bias (Variable) – [Optional Parameter] Bias.
  • base_axis (int) – Dimensions up to base_axis is treated as sample dimension.
Returns:

Output.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.binary_connect_convolution(x, weight, binary_weight, bias=None, base_axis=1, pad=None, stride=None, dilation=None, group=1, n_outputs=-1, outputs=None)[source]

This function provides a BinaryConnect convolution layer. It computes in the forward pass

\[y_{n, a, b} = \sum_{m} \sum_{i} \sum_{j} sign(w_{n, m, i, j}) x_{m, a + i, b + j},\]

i.e., the weights \(w_{n, m, i, j}\) are binarized to \(sign(w_{n, m, i, j})\) and, hence, each weight is in \(\{-1,\,1\}\). By this weight binarization, the inner product computations do not require any multiplications anymore as they turn into additions/subtractions.

This function should be used together with batch_normalization().

Reference

Note

1) If you would like to share the binary weights between other layers, please use the standard, floating value weights (weight) and not the binary weights (binary_weight).

2) The weights and the binary weights become in sync only after a call to forward(), and not after a call to backward(). If you wish to store the parameters of the network, remember to call forward(), once before doing so, otherwise the weights and the binary weights will not be in sync.

3) CPU and GPU implementations now use floating values for binary_weight, since this function is for simulation purposes.

Parameters:
  • x (Variable) – Input.
  • weight (Variable) – [Parameter] Weight.
  • binary_weight (Variable) – [Parameter] Binarized weight.
  • bias (Variable) – [Optional Parameter] Bias.
  • base_axis (int) – Dimensions up to base_axis is treated as sample dimension.
  • pad (tuple of int) – Padding sizes for dimensions.
  • stride (tuple of int) – Stride sizes for dimensions.
  • dilation (tuple of int) – Dilation sizes for dimensions.
  • group (int) – Number of groups of channels. This makes the connection across channels sparser, by grouping connections along the mapping direction.
Returns:

Output

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.binary_weight_affine(x, weight, binary_weight, alpha, bias=None, base_axis=1, n_outputs=-1, outputs=None)[source]

This function provides a Binary Weight Network affine layer. It computes in the forward pass

\[y_j = \frac{1}{\|\mathbf{w}_j\|_{\ell_1}} \sum_{i} sign(w_{j,i}) x_i\]

i.e., the weights \(w_{j,i}\) are binarized to \(sign(w_{j,i})\) and, hence, each weight is in \(\{-1,\,1\}\). By this weight binarization, the inner product computations turn into additions/subtractions which are followed by multiplication with the scaling factor \(\alpha_j = \frac{1}{\|\mathbf{w}_j\|_{\ell_1}}\).

Reference

Note

1) If you would like to share the binary weights with other layers, please use the standard, floating value weights (weight) and not the binary weights (binary_weight).

2) The weights and the binary weights become in sync only after a call to forward(), and not after a call to backward(). If you wish to store the parameters of the network, remember to call forward(), once before doing so, otherwise the weights and the binary weights will not be in sync.

3) CPU and GPU implementations now use floating values for binary_weight, since this function is for simulation purposes.

Parameters:
  • x (Variable) – Input .
  • weight (Variable) – [Parameter] Weight.
  • binary_weight (Variable) – [Parameter] Binarized weight.
  • alpha (Variable) – [Parameter] Alpha.
  • bias (Variable) – [Optional Parameter] Bias.
  • base_axis (int) – Dimensions up to base_axis is treated as sample dimension.
Returns:

Output.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.binary_weight_convolution(x, weight, binary_weight, alpha, bias=None, base_axis=1, pad=None, stride=None, dilation=None, group=1, n_outputs=-1, outputs=None)[source]

This function provides a Binary Weight Network convolution layer. It computes in the forward pass

\[y_{n, a, b} = \frac{1}{\|\mathbf{w}_n\|_{\ell_1}} \sum_{m} \sum_{i} \sum_{j} sign(w_{n, m, i, j}) x_{m, a + i, b + j}.\]

i.e., the weights \(w_{n, m, i, j}\) are binarized to \(sign(w_{n, m, i, j})\) and, hence, each weight is in \(\{-1,\,1\}\). By this weight binarization, the inner product computations turn into additions/subtractions which are followed by multiplication with the scaling factor \(\alpha_n = \frac{1}{\|\mathbf{w}_n\|_{\ell_1}}\).

Reference

Note

1) If you would like to share the binary weights between other standard layers, please use the standard, floating value weights (weight) and not the binary weights (binary_weight).

2) The weights and the binary weights become in sync only after a call to forward(), and not after a call to backward(). If you wish to store the parameters of the network, remember to call forward(), once before doing so, otherwise the weights and the binary weights will not be in sync.

3) CPU and GPU implementations now use floating values for binary_weight, since this function is for simulation purposes.

Parameters:
  • x (Variable) – Input.
  • weight (Variable) – [Parameter] Weight.
  • binary_weight (Variable) – [Parameter] Binarized weight.
  • alpha (Variable) – [Parameter] Alpha.
  • bias (Variable) – [Optional Parameter] Bias.
  • base_axis (int) – Dimensions up to base_axis is treated as sample dimension.
  • pad (tuple of int) – Padding sizes for dimensions.
  • stride (tuple of int) – Stride sizes for dimensions.
  • dilation (tuple of int) – Dilation sizes for dimensions.
  • group (int) – Number of groups of channels. This makes the connection across channels sparser, by grouping connections along the mapping direction.
Returns:

Output

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

Unsupported, Special Use

nnabla.functions.vat_noise(x, w, base_axis=1, eps=1.0, n_outputs=-1, outputs=None)[source]

Noise for virtual adversarial training.

This layer is a special layer for GUI network designing, specialized for getting the noise of virtual adversarial training.

In the backward process, the weight parameter will be replaced with the gradient.

Forward

\[y_i = \frac{\epsilon x_i}{\sqrt{\sum_k x_k^2 + c}}\]

Backward

\[\delta x_i = 0\]
\[w_i = \epsilon \delta y_i\]

Note

This layer is a special layer for GUI network designing.

References

Parameters:
  • x (Variable) – N-D array of noise input. Noise is standard Gaussian noise initially, but the next step, fed back gradient variable.
  • w (Variable) – N-D array for keep gradient values.
  • base_axis (int) – Dimensions up to base_axis is treated as sample dimension.
  • eps (float) – Noise norm (l2) factor.
Returns:

N-D array

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

This function behaves as an identity function on the forward pass, and deletes the gradient for the background pass.

This layer is a special layer for GUI network designing, used for getting zero backward operation by adding this layer.

Forward

\[y_i = x_i\]

Backward

\[\delta x_i = 0\]

Note

This layer is a special layer for GUI network designing.

Parameters:x (Variable) – N-D array.
Returns:N-D array.
Return type:Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

nnabla.functions.sink(*inputs, **args)[source]

Creates a dummy variable used to call forward or backward function of multiple variables at one place.

This takes any numbers of input variables with any shape, and creates a single 0-shape outputs. The forward pass does nothing. The backward pass set ones to the input grads if one_input_grad is set as true.

Parameters:
  • *inputs (Variable) – [Variadic] Any number of inputs with any shape.
  • **param (bool) – [name=one_input_grad] Set grads of inputs as one during backward. It is useful to set false if you want to set external gradients to the input variables.
Returns:

Dummy variable.

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.

Validation

nnabla.functions.top_n_error(x, target, axis=None, n=1, n_outputs=-1, outputs=None)[source]

Top N error along the dimension specified by the axis, the elment of outputs is

\[\begin{split}y_i = \left \{ \begin{array}{l} 1 \ (x_i \ is \ not \ within \ N-th \ place) \\ 0 \ (x_i \ is \ within \ N-th \ place) \end{array} \right.\end{split}\]
Parameters:
  • x (Variable) – Probabilities N-D array. \(D_1 \times ... \times D_i \times ... \times D_N\)
  • target (Variable) – [Integer] N-D array of labels. \(D_1 \times ... \times 1 \times ... \times D_N\)
  • axis (int) – Axis on which the top N error is calculated.
  • n (int) – top N
Returns:

Element-wise error N-D array. (\(D_1 \times ... \times 1 \times ... \times D_N\))

Return type:

Variable

Note

All nnabla functions in nnabla.functions are decorated with the nnabla.function_bases.function_api decorator, which queries the current context and passes it into the first argument of the original function. The original function always takes a context as the first argument.