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 は、算術演算子 (+, -, *, /, **) をオーバーライドします。オペランドは、スカラー数、 NdArray 、または Variable のいずれかになります。 NdArray を含む算術演算は、すぐに呼び出される計算の出力を保持する NdArray を返します。また、インプレース算術演算 (+=, -=, *=, /=, **=) が実装されます。 = ではインプレース置換は行われずに、単にオブジェクト参照が置換されることに注意してください。代わりに、 copy_from() を使ってインプレース置換を行うことができます。

パラメータ:

shape (tuple or int) -- タプルの形状。

bool_fill(self, mask, value)

Return a new but inplaced nnabla.NdArray filled with value where mask is non-zero.

パラメータ:
  • 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)

NdArray のデータ型のインプレースキャスト。オプションパラメータの ctx が指定されない場合のみ、 numpy.ndarray として参照値を返します。それ以外の場合は、 None を返します。

パラメータ:
戻り値:

ctx が None の場合は、 numpy.array 。それ以外の場合はなし。

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)

他の NdArray オブジェクトから値をコピーします。

呼び出し元のオブジェクト自身を返します。

パラメータ:
  • arr (NdArray) -- 呼び出し元のオブジェクトに値をコピーします。 arr の形状は呼び出し元のオブジェクトと同じでないといけません。

  • use_current_context (bool) -- True の場合、デバイスと現在のコンテキスト ( call F.identity(src, output=[self]) と同等 ) において指定されている dtype でコピーが行われています。そうでない場合は、ソース配列のデバイスと dtype が使われます。デフォルトは True です。

戻り値:

nnabla.NdArray

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 returned nnabla.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 where zero() or fill(rhs) is invoked if a scalar with a float or an integer <= 2^53 (as filling value is maintained as float64) is given.

これはデバイス配列から CPU へデータ転送が暗黙的に呼び出される可能性があるということですので注意してください。

パラメータ:

value (numpy.ndarray) --

戻り値:

numpy.ndarray

data_ptr(self, dtype, ctx=None, bool write_only=False)

配列のポインターを取得します。

この動作は、 cast メソッド と同じですが、 ctx に基づいたデータポインターを返します。 ctx が指定されない場合は、 nn.get_current_context によって得られるデフォルトのコンテキストが使われます。

パラメータ:
  • dtype (numpy.dtype) -- Numpy データ型。

  • ctx (nnabla.Context, optional) -- コンテキスト記述子。

  • write_only (bool, optional) -- No synchronization happens.

戻り値:

データポインター。

戻り値の型:

int

dtype

dtype を取得します。

戻り値:

numpy.dtype

fill(self, value)

すべての要素に指定されたスカラー値を代入します。

注釈

ここでは、内部配列にはすぐに 0 が入りません。この配列を使うとき ( 例えば、順方向または逆方向の計算の際 ) に、要求されたデータ型で配列が生成され、その値が入ります。

パラメータ:

value (float) -- 代入する値。

static from_numpy_array(nparr)

Numpy 配列データから NdArray オブジェクトを作成します。

このデータは所定の Numpy 配列で初期化されます。

パラメータ:

nparr (ndarray) -- Numpy 多次元配列。

戻り値:

nnabla.NdArray

get_data(self, str mode='rw', dtype=None)

指定されたモードで numpy.ndarray としてこの配列が保持する値を返します。

パラメータ:
  • mode (str) -- 正しく選択されると、より効率の良い計算が行われます。 * ‘r’ : 読み取り専用アクセス。 * ‘w’ : 書き込み専用アクセス。 * ‘rw’ : 読み書き可。

  • dtype (numpy.dtype, optional) -- 強制的に戻り値の配列の dtype にします。

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

masked_fill()

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 to start + length along the dimension dim. The returned array and this array share the same underlying allocated memory.

パラメータ:
  • dim (int) -- Dimension along which to narrow. Currently, only 0 can be specified.

  • start (int) -- Starting index in specified dimension.

  • length (int) -- Distance to the ending index from start.

See :function:`nnabla.NdArray.narrow` for more details.

ndim

次元数。

戻り値:

int

shape

N-d 配列の形状。

戻り値:

tuple of int

size

N-d 配列の合計サイズ。

戻り値:

int

size_from_axis(self, axis=-1)

指定された軸に続いてサイズを返します。

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
パラメータ:

axis (int, optional) -- デフォルトで -1

戻り値:

int

strides

ストライドします。

戻り値:

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

戻り値:

nnabla.NdArray

zero(self)

すべての要素に 0 を入れます。

注釈

ここでは、内部配列にすぐには 0 が入りません。この配列を使うとき ( 例えば、順方向または逆方向の計算の際 ) に、要求されたデータ型で配列が生成され、 0 が入ります。

zeroing

zero() を呼び出した後に、配列に変更がないかどうかを確かめます。