chainerx.ndarray¶

class
chainerx.
ndarray
(shape, dtype, device=None)¶ Multidimensional 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 tonumpy.ndarray
, except for some differences including the following noticeable points:chainerx.ndarray
has adevice
attribute. It indicates on which device the array is allocated.chainerx.ndarray
supports DefinebyRun backpropagation. Once you callrequire_grad()
, the array starts recording the operations applied to it recursively. Gradient of the result with respect to the original array can be computed then with thebackward()
method or thechainerx.backward()
function.
 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
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.

all
()¶

any
()¶

argmax
(axis=None)¶ Returns the indices of the maximum elements along a given axis.
See
chainerx.argmax()
for the full documentation.

argmin
(axis=None)¶ Returns the indices of the minimum elements along a given axis.
See
chainerx.argmin()
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()
andcopy()
, 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).

astype
(dtype, copy=True)¶ Casts each element to the specified data type.

backward
(backprop_id=None, enable_double_backprop=False)¶ Performs backpropagation starting from this array.
This method is equivalent to
chainerx.backward([self], *args)
. Seechainerx.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

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.

flatten
()¶

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.

item
()¶ Copies an element of an array to a standard Python scalar and returns it.
 Returns
A copy of the specified element of the array as a suitable Python scalar.
 Return type
z
See also
numpy.item()

max
(axis=None, keepdims=False)¶ Returns the maximum along a given axis.
See
chainerx.amax()
for the full documentation.

mean
()¶

min
(axis=None, keepdims=False)¶ Returns the minimum along a given axis.
See
chainerx.amin()
for the full documentation.

ravel
()¶

repeat
(repeats, axis=None)¶ Constructs an array by repeating a given array.
See
chainerx.repeats()
for the full documentation.

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 callrequire_grad()
on the array even if the array is an intermediate result of differentiable computations. Returns
self
 Return type

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 sizeone 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.

swapaxes
(axis1, axis2)¶ Interchange two axes of an array..
See
chainerx.swapaxes()
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
 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

tolist
()¶

transpose
(axes=None)¶ Creates a view of an array with permutated axes.
See
chainerx.transpose()
for the full documentation.

var
()¶

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.

__neg__
()¶ Computes
x
elementwise.

__abs__
()¶

__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.

__rtruediv__
()¶

__floordiv__
()¶

__rfloordiv__
()¶

__pow__
()¶

__rpow__
()¶
Attributes

T
¶ Shapereversed view of the array.
New array is created at every access to this property.
x.T
is just a shorthand ofx.transpose()
. Type

data_ptr
¶ Address of the underlying memory allocation.
The meaning of the address is devicedependent.
 Type

dtype
¶ Data type of the array.

grad
¶ Gradient held by the array.
It is
None
if the gradient is not available. Setter of this property overwrites the gradient. Type

shape
¶ Lengths of axes.
Note
Currently, this property does not support setter.
 Type
tuple of int

strides
¶ Strides of axes in bytes.
 Type
tuple of int