NdArray
- class nnabla.NdArray(*args, **kwargs)
nnabla.NdArray
is a device-agnostic data container for multi-dimensional arrays (tensors).nnabla.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
orVariable
. An arithmetic operation containingNdArray
returnsNdArray
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 usecopy_from()
for inplace substitution.- bool_fill(self, mask, value)
Return a new but inplaced
nnabla.NdArray
filled with value where mask is non-zero.- Parameters:
mask (
nnabla.NdArray
) – Mask with which to fill. Non-zero/zero elements are supposed to be a binary mask as 1/0. No gradients are computed with respect to mask.value (float) – The value to fill.
- 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.
- Parameters:
dtype (
numpy.dtype
) – Numpy Data type.ctx (
nnabla.Context
, optional) – Context descriptor.
- Returns:
numpy.array
ifctx
is None, otherwise nothing.
- clear(self)
Clear memories which this NdArray has and return them to allocator.
- clear_called
Checking if the array is not modified after cleared. This returns False until clear is called at the first time.
- copy_from(self, NdArray arr, use_current_context=True)
Copy values from another NdArray object.
It returns the caller object itself.
- Parameters:
arr (NdArray) – Values will be copied to the caller object. The shape of
arr`
must be same as the caller object.use_current_context (bool) – If True, a copy is happening in a device and dtype specified in the current context (equivalent to call
F.identity(src, output=[self])
). Otherwise, a device and dtype in the source array is used. The default is True.
- Returns:
- data
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 returnednnabla.NdArray
will affect the data contained inside the NNabla array. This method can also be called as a setter where an array is created as the same type as rhs. There is an exception wherezero()
orfill(rhs)
is invoked if a scalar with a float or an integer <= 2^53 (as filling value is maintained as float64) is given.Note that this may implicitly invoke a data transfer from device arrays to the CPU.
- Parameters:
value (
numpy.ndarray
) –- Returns:
- data_ptr(self, dtype, ctx=None, bool write_only=False)
Get array’s pointer.
The behavior is similar to
cast
method but returns the data pointer based on thectx
. If thectx
is not specified, the default context obtained bynn.get_current_context
is used.- Parameters:
dtype (
numpy.dtype
) – Numpy Data type.ctx (
nnabla.Context
, optional) – Context descriptor.write_only (
bool
, optional) – No synchronization happens.
- Returns:
The data pointer.
- Return type:
- dtype
Get dtype.
- Returns:
- fill(self, value)
Fill all of the elements with the provided scalar value.
Note
This doesn’t not fill values in an internal array with 0 immediately. An array is created as a requested data type when this array is used (in forward or backward computation for exampe), and is filled with the value.
- Parameters:
value (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.NdArray
- get_data(self, str mode='rw', dtype=None)
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.
dtype (
numpy.dtype
, optional) – Force dtype of a returned array.
See :function:`nnabla.NdArray.data` for more details.
- masked_fill(mask, value)
NdArray.bool_fill(self, mask, value) Return a new but inplaced
nnabla.NdArray
filled with value where mask is non-zero.- Args:
mask (
nnabla.NdArray
): Mask with which to fill. Non-zero/zero elements are supposed to be a binary mask as 1/0. No gradients are computed with respect to mask. value (float): The value to fill.
- modification_count
Returns how many times modified after memory allocation or clearing buffer.
- narrow(self, dim, start, length)
Returns a new array that is a narrowed part of this array. The narrowed part is specified by the slice of this array from
start
tostart
+length
along the dimensiondim
. The returned array and this array share the same underlying allocated memory.- Parameters:
See :function:`nnabla.NdArray.narrow` for more details.
- ndim
Number of dimensions.
- Returns:
int
- shape
Shape of the N-d array.
- Returns:
tuple of int
- size
Total size of the N-d array.
- Returns:
int
- size_from_axis(self, axis=-1)
Gets the size followed by the provided axis.
Example
a = nnabla.NdArray([10,9]) a.size_from_axis() # ==> 90 a.size_from_axis(0) # ==> 90 a.size_from_axis(1) # ==> 9 a.size_from_axis(2) # ==> 1
- strides
Strides.
- Returns:
tuple of int
- view(self, shape)
Create viewd NdArray. Create a new NdArray of sharing same data with specified shape. :param shape: Shape of tuple. :type shape: tuple
- Returns:
nnabla.NdArray
- zero(self)
Fill all of the elements with 0.
Note
This doesn’t not fill values in an internal array with 0 immediately. An array is created as a requested data type when this array is used (in forward or backward computation for exampe), and is filled with 0.