class chainerx.ndarray(shape, dtype, device=None)

Multi-dimensional array, the central data structure of ChainerX.

This class, along with other APIs in the chainerx module, provides a subset of NumPy APIs. This class works similar to numpy.ndarray, except for some differences including the following noticeable points:

  • shape (tuple of ints) – Shape of the new array.
  • dtype – Data type.
  • device (Device) – Device on which the array is allocated. If omitted, the default device is chosen.

See also




___getitem__(self, key) Returns self[key].


Currently, only basic indexing is supported not advanced indexing.

__setitem__(key, value)

Returns the length of the first axis.


Returns the indices of the maximum elements along a given axis.

See chainerx.argmax() for the full documentation.


Creates a view or a copy of the array that stops gradient propagation.

This method behaves similar to view() and copy(), except that the gradient is not propagated through this operation (internally, this method creates a copy or view of the array without connecting the computational graph for backprop).

Parameters:copy (bool) – If True, it copies the array. Otherwise, it returns a view of the original array.
Returns:A view or a copy of the array without propagating the gradient on backprop.
Return type:ndarray
astype(dtype, copy=True)

Casts each element to the specified data type.

  • dtype – Data type of the new array.
  • copy (bool) – If True, this method always copies the data. Otherwise, it creates a view of the array if possible.

An array with the specified dtype.

Return type:


backward(backprop_id=None, enable_double_backprop=False)

Performs backpropagation starting from this array.

This method is equivalent to chainerx.backward([self], *args). See chainerx.backward() for the full documentation.


Clears the gradient held by this array.

clip(a_min, a_max)

Returns an array with values limited to [a_min, a_max].

See also

chainerx.clip() for full documentation, numpy.ndarray.clip()


Creates an array and copies all the elements to it.

The copied array is allocated on the same device as self.

See also



Returns the dot product with a given array.

See for the full documentation.


Fills the array with a scalar value in place.

Parameters:value – Scalar value with which the array will be filled.

Returns the gradient held by the array.

If the gradient is not available, it returns None.


Returns True if gradient propagates through this array on backprop.

See the note on require_grad() for details.


Returns True if the gradient will be set after backprop.

See the note on require_grad() for details.

max(axis=None, keepdims=False)

Returns the maximum along a given axis.

See chainerx.amax() for the full documentation.


Returns an array flattened into one dimension.

See also

chainerx.ravel() for full documentation, numpy.ndarray.ravel()


Declares that a gradient for this array will be made available after backprop.

Once calling this method, any operations applied to this array are recorded for later backprop. After backprop, the grad attribute holds the gradient array.


ChainerX distinguishes gradient requirements and backprop requirements strictly. They are strongly related, but different concepts as follows.

  • Gradient requirement indicates that the gradient array should be made available after backprop. This attribute is not propagated through any operations. It implicates the backprop requirement.
  • Backprop requirement indicates that the gradient should be propagated through the array during backprop. This attribute is propagated through differentiable operations.

require_grad() sets the gradient requirement flag. If you need to extract the gradient after backprop, you have to call require_grad() on the array even if the array is an intermediate result of differentiable computations.

Return type:ndarray

Creates an array with a new shape and the same data.

See chainerx.reshape() for the full documentation.


Sets a gradient to the array.

This method overwrites the gradient with a given array.

Parameters:grad (ndarray) – New gradient array.

Removes size-one axes from an array.

See chainerx.squeeze() for the full documentation.

sum(axis=None, keepdims=False)

Returns the sum of an array along given axes.

See chainerx.sum() for the full documentation.

take(indices, axis)

Takes elements from the array along an axis.

See chainerx.take() for the full documentation.

to_device(device, index=None)

Transfers the array to the specified device.

  • device (Device or str) – Device to which the array is transferred, or a backend name. If it is a backend name, index should also be specified.
  • index (int) – Index of the device for the backend specified by device.

An array on the target device. If the original array is already on the device, it is a view of that. Otherwise, it is a copy of the array on the target device.

Return type:



Creates a view of an array with permutated axes.

See chainerx.transpose() for the full documentation.


Returns a view of the array.

The returned array shares the underlying buffer, though it has a different identity as a Python object.


Computes x == y elementwise.


Computes x != y elementwise.


Computes x < y elementwise.


Computes x <= y elementwise.


Computes x > y elementwise.


Computes x >= y elementwise.


Casts a size-one array into a bool value.


Computes -x elementwise.


Computes x + y elementwise.


Computes y + x elementwise.


Computes x - y elementwise.


Computes y - x elementwise.


Computes x * y elementwise.


Computes y * x elementwise.


Computes x / y elementwise.



~chainerx.ndarray – Shape-reversed view of the array.

New array is created at every access to this property. x.T is just a shorthand of x.transpose().


int – Address of the underlying memory allocation.

The meaning of the address is device-dependent.


int – Total size of the underlying memory allocation.


~chainerx.Device – Device on which the data exists.


Data type of the array.


~chainerx.ndarray – Gradient held by the array.

It is None if the gradient is not available. Setter of this property overwrites the gradient.


boolTrue iff the array is stored in the C-contiguous order.


int – Size of each element in bytes.


int – Total size of all elements in bytes.

It does not count skips between elements.


int – Number of dimensions.


int – Offset of the first element from the memory allocation in bytes.


tuple of int – Lengths of axes.


Currently, this property does not support setter.


int – Number of elements in the array.


tuple of int – Strides of axes in bytes.