chainerx.ndarray

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:

Parameters:
  • 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

numpy.ndarray

Methods

__getitem__(key)

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

Note

Currently, only basic indexing is supported not advanced indexing.

__setitem__(key, value)
__len__()

Returns the length of the first axis.

argmax(axis=None)

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

See chainerx.argmax() for the full documentation.

as_grad_stopped(copy=False)

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.

Parameters:
  • 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.
Returns:

An array with the specified dtype.

Return type:

ndarray

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.

cleargrad()

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()

copy()

Creates an array and copies all the elements to it.

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

See also

chainerx.copy()

dot(b)

Returns the dot product with a given array.

See chainerx.dot() for the full documentation.

fill(value)

Fills the array with a scalar value in place.

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

Returns the gradient held by the array.

If the gradient is not available, it returns None.

is_backprop_required()

Returns True if gradient propagates through this array on backprop.

See the note on require_grad() for details.

is_grad_required()

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.

ravel()

Returns an array flattened into one dimension.

See also

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

require_grad()

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.

Note

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.

Returns:self
Return type:ndarray
reshape(newshape)

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

See chainerx.reshape() for the full documentation.

set_grad(grad)

Sets a gradient to the array.

This method overwrites the gradient with a given array.

Parameters:grad (ndarray) – New gradient array.
squeeze(axis=None)

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.

Parameters:
  • 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.
Returns:

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:

ndarray

transpose(axes=None)

Creates a view of an array with permutated axes.

See chainerx.transpose() for the full documentation.

view()

Returns a view of the array.

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

__eq__(other)

Computes x == y elementwise.

__ne__(other)

Computes x != y elementwise.

__lt__(other)

Computes x < y elementwise.

__le__(other)

Computes x <= y elementwise.

__gt__(other)

Computes x > y elementwise.

__ge__(other)

Computes x >= y elementwise.

__bool__()

Casts a size-one array into a bool value.

__neg__()

Computes -x elementwise.

__add__(other)

Computes x + y elementwise.

__radd__(other)

Computes y + x elementwise.

__sub__(other)

Computes x - y elementwise.

__rsub__(other)

Computes y - x elementwise.

__mul__(other)

Computes x * y elementwise.

__rmul__(other)

Computes y * x elementwise.

__truediv__(other)

Computes x / y elementwise.

Attributes

T

~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().

data_ptr

int – Address of the underlying memory allocation.

The meaning of the address is device-dependent.

data_size

int – Total size of the underlying memory allocation.

device

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

dtype

Data type of the array.

grad

~chainerx.ndarray – Gradient held by the array.

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

is_contiguous

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

itemsize

int – Size of each element in bytes.

nbytes

int – Total size of all elements in bytes.

It does not count skips between elements.

ndim

int – Number of dimensions.

offset

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

shape

tuple of int – Lengths of axes.

Note

Currently, this property does not support setter.

size

int – Number of elements in the array.

strides

tuple of int – Strides of axes in bytes.