class nnabla._nd_array.NdArray(shape=tuple())

nnabla._nd_array.NdArray is a device-agnostic data container for multi-dimensional arrays (tensors). nnabla._nd_array.NdArray can also implicitly handle data transfers across different devices (e.g. CPU to CUDA GPU, CUDA GPU to CPU). See Python API Tutorial for more details.

NdArray overrides some arithmetic operators (+, -, *, /, **). Operands can be either a scalar number, NdArray or Variable. An arithmetic operation containing NdArray returns NdArray which stores the output of the computation immediately invoked. Also, inplace arithmetic operations (+=, -=, *=, /=, **=) are implemented. Note that = doesn’t perform inplace substitution but just replaces the object reference. Instead, you can use copy_from() for inplace substitution.

Parameters:shape (tuple or int) – Shape of tuple.
cast(self, dtype, ctx=None)

In-place cast of data type of the NdArray. It returns the reference values as a numpy.ndarray only if optional parameter ctx is not given, None otherwise.


numpy.array if ctx is None, otherwise nothing.

copy_from(self, NdArray arr)

Copy values from another NdArray object.

It returns the caller object itself. nnabla.functions.identity() is called internally to copy values.

Parameters:arr (NdArray) – Values will be copied to the caller object. The shape of arr` must be same as the caller object.

Returns the values held by this array as a numpy.ndarray. Note that only the references are returned, and the values are not copied. Therefore, modifying the returned nnabla._nd_array.NdArray will affect the data contained inside the NNabla array. This method can also be called as a setter. Note that this may implicitly invoke a data transfer from device arrays to the CPU.

Parameters:value (numpy.ndarray) –

Returns: numpy.ndarray

data_ptr(self, dtype, ctx=None)

Get array’s pointer.

The behavior is similar to cast method but returns the data pointer based on the ctx. If the ctx is not specified, the default context obtained by nn.get_current_context is used.


The data pointer.

Return type:



Get dtype.

Returns: numpy.dtype

fill(self, value)

Fill all of the elements with the provided scalar value.

Note: This method is lazily evaluated. It is evaluated during the forward or backward propagation.

Parameters:value (int, float) – The value filled with.
static from_numpy_array(nparr)

Create a NdArray object from Numpy array data.

The data is initialized with the given Numpy array.

Parameters:nparr (ndarray) – Numpy multi-dimensional array.

Returns: ~nnabla._nd_array.NdArray

get_data(self, str mode='rw')

Returns the values held by this array as a numpy.ndarray with a specified mode.

Parameters:mode (str) – Computation becomes more efficient if right one is chosen. * ‘r’: Read-only access. * ‘w’: Write-only access. * ‘rw’: You can both read and write.

See :function:`nnabla._nd_array.NdArray.data for more details.


Number of dimensions.

Returns: int


Shape of the N-d array.

Returns: tuple of int


Total size of the N-d array.

Returns: int

size_from_axis(self, axis=-1)

Gets the size followed by the provided axis.


a = nnabla.NdArray([10,9])
# ==> 90
# ==> 90
# ==> 9
# ==> 1
Parameters:axis (int, optional) – -1 as default


Returns: tuple of int


Fill all of the elements with 0.

Note: This method is lazily evaluated. It is evaluated during the forward or backward propagation.