mxnet.ndarray.ndarray

NDArray API of MXNet.

Functions

add(lhs, rhs)

Returns element-wise sum of the input arrays with broadcasting.

arange(start[, stop, step, repeat, ...])

Returns evenly spaced values within a given interval.

array(source_array[, ctx, dtype])

Creates an array from any object exposing the array interface.

check_boolean_array_dimension(array_shape, ...)

Advanced boolean indexing is implemented through the use of nonzero.

concatenate(arrays[, axis, always_copy])

DEPRECATED, use concat instead

divide(lhs, rhs)

Returns element-wise division of the input arrays with broadcasting.

dtype_mx_to_np(dtype_idx)

dtype_np_to_mx(dtype)

empty(shape[, ctx, dtype])

Returns a new array of given shape and type, without initializing entries.

equal(lhs, rhs)

Returns the result of element-wise equal to (==) comparison operation with broadcasting.

eye(N[, M, k, ctx, dtype])

Return a 2-D array with ones on the diagonal and zeros elsewhere.

full(shape, val[, ctx, dtype, out])

Returns a new array of given shape and type, filled with the given value val.

get_dtype_name(dtype)

get_dtype_type(dtype)

get_indexing_dispatch_code(key)

Returns a dispatch code for calling basic or advanced indexing functions.

get_oshape_of_gather_nd_op(dshape, ishape)

Given data and index shapes, get the output NDArray shape.

greater(lhs, rhs)

Returns the result of element-wise greater than (>) comparison operation with broadcasting.

greater_equal(lhs, rhs)

Returns the result of element-wise greater than or equal to (>=) comparison operation with broadcasting.

histogram(a[, bins, range])

Compute the histogram of the input data.

imdecode(str_img[, clip_rect, out, index, ...])

DEPRECATED, use mx.img instead

indexing_key_expand_implicit_axes(key, shape)

Make implicit axes explicit by adding slice(None) and convert boolean array to integer array through nonzero.

is_mx_dtype(dtype)

lesser(lhs, rhs)

Returns the result of element-wise lesser than (<) comparison operation with broadcasting.

lesser_equal(lhs, rhs)

Returns the result of element-wise lesser than or equal to (<=) comparison operation with broadcasting.

linspace(start, stop, num[, endpoint, ctx, ...])

Return evenly spaced numbers within a specified interval.

logical_and(lhs, rhs)

Returns the result of element-wise logical and comparison operation with broadcasting.

logical_or(lhs, rhs)

Returns the result of element-wise logical or comparison operation with broadcasting.

logical_xor(lhs, rhs)

Returns the result of element-wise logical xor comparison operation with broadcasting.

maximum(lhs, rhs)

Returns element-wise maximum of the input arrays with broadcasting.

minimum(lhs, rhs)

Returns element-wise minimum of the input arrays with broadcasting.

modulo(lhs, rhs)

Returns element-wise modulo of the input arrays with broadcasting.

moveaxis(tensor, source, destination)

Moves the source axis into the destination position while leaving the other axes in their original order

multiply(lhs, rhs)

Returns element-wise product of the input arrays with broadcasting.

not_equal(lhs, rhs)

Returns the result of element-wise not equal to (!=) comparison operation with broadcasting.

onehot_encode(indices, out)

One-hot encoding indices into matrix out.

ones(shape[, ctx, dtype])

Returns a new array filled with all ones, with the given shape and type.

power(base, exp)

Returns result of first array elements raised to powers from second array, element-wise with broadcasting.

split_v2(ary, indices_or_sections[, axis, ...])

Split an array into multiple sub-arrays.

subtract(lhs, rhs)

Returns element-wise difference of the input arrays with broadcasting.

true_divide(lhs, rhs)

This function is similar to divide().

waitall()

Wait for all async operations to finish in MXNet.

zeros(shape[, ctx, dtype])

Returns a new array filled with all zeros, with the given shape and type.

Classes

NDArray

An array object representing a multidimensional, homogeneous array of fixed-size items.

class mxnet.ndarray.ndarray.NDArray[source]

Bases: NDArrayBase

An array object representing a multidimensional, homogeneous array of fixed-size items.

property T

Returns a copy of the array with axes transposed.

Equivalent to mx.nd.transpose(self) except that self is returned if self.ndim < 2.

Unlike numpy.ndarray.T, this function returns a copy rather than a view of the array unless self.ndim < 2.

Examples

>>> x = mx.nd.arange(0,6).reshape((2,3))
>>> x.asnumpy()
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.]], dtype=float32)
>>> x.T.asnumpy()
array([[ 0.,  3.],
       [ 1.,  4.],
       [ 2.,  5.]], dtype=float32)
abs(*args, **kwargs)[source]

Convenience fluent method for abs().

The arguments are the same as for abs(), with this array as data.

arccos(*args, **kwargs)[source]

Convenience fluent method for arccos().

The arguments are the same as for arccos(), with this array as data.

arccosh(*args, **kwargs)[source]

Convenience fluent method for arccosh().

The arguments are the same as for arccosh(), with this array as data.

arcsin(*args, **kwargs)[source]

Convenience fluent method for arcsin().

The arguments are the same as for arcsin(), with this array as data.

arcsinh(*args, **kwargs)[source]

Convenience fluent method for arcsinh().

The arguments are the same as for arcsinh(), with this array as data.

arctan(*args, **kwargs)[source]

Convenience fluent method for arctan().

The arguments are the same as for arctan(), with this array as data.

arctanh(*args, **kwargs)[source]

Convenience fluent method for arctanh().

The arguments are the same as for arctanh(), with this array as data.

argmax(*args, **kwargs)[source]

Convenience fluent method for argmax().

The arguments are the same as for argmax(), with this array as data.

argmax_channel(*args, **kwargs)[source]

Convenience fluent method for argmax_channel().

The arguments are the same as for argmax_channel(), with this array as data.

argmin(*args, **kwargs)[source]

Convenience fluent method for argmin().

The arguments are the same as for argmin(), with this array as data.

argsort(*args, **kwargs)[source]

Convenience fluent method for argsort().

The arguments are the same as for argsort(), with this array as data.

as_in_context(context)[source]

Returns an array on the target device with the same value as this array.

If the target context is the same as self.context, then self is returned. Otherwise, a copy is made.

Parameters:

context (Context) – The target context.

Returns:

The target array.

Return type:

NDArray, CSRNDArray or RowSparseNDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = x.as_in_context(mx.cpu())
>>> y is x
True
>>> z = x.as_in_context(mx.gpu(0))
>>> z is x
False
as_nd_ndarray()[source]

A convenience function for creating a classic ndarray from the current ndarray with zero copy. For this class, it just returns itself since it is already a classic ndarray.

as_np_ndarray()[source]

Convert mxnet.ndarray.NDArray to mxnet.numpy.ndarray.

asnumpy()[source]

Returns a numpy.ndarray object with value copied from this array.

Examples

>>> x = mx.nd.ones((2,3))
>>> y = x.asnumpy()
>>> type(y)
<type 'numpy.ndarray'>
>>> y
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> z = mx.nd.ones((2,3), dtype='int32')
>>> z.asnumpy()
array([[1, 1, 1],
       [1, 1, 1]], dtype=int32)
asscalar()[source]

Returns a scalar whose value is copied from this array.

This function is equivalent to self.asnumpy()[0]. This NDArray must have shape (1,).

Examples

>>> x = mx.nd.ones((1,), dtype='int32')
>>> x.asscalar()
1
>>> type(x.asscalar())
<type 'numpy.int32'>
astype(dtype, copy=True)[source]

Returns a copy of the array after casting to a specified type.

Parameters:
  • dtype (numpy.dtype or str) – The type of the returned array.

  • copy (bool) – Default True. By default, astype always returns a newly allocated ndarray on the same context. If this is set to False, and the dtype requested is the same as the ndarray’s dtype, the ndarray is returned instead of a copy.

Returns:

The copied array after casting to the specified type, or the same array if copy=False and dtype is the same as the input array.

Return type:

NDArray, CSRNDArray or RowSparseNDArray

Examples

>>> x = mx.nd.zeros((2,3), dtype='float32')
>>> y = x.astype('int32')
>>> y.dtype
<type 'numpy.int32'>
attach_grad(grad_req='write', stype=None)[source]

Attach a gradient buffer to this NDArray, so that backward can compute gradient with respect to it.

The gradient is initialized to zeros.

Parameters:
  • grad_req ({'write', 'add', 'null'}) – How gradient will be accumulated. - ‘write’: gradient will be overwritten on every backward. - ‘add’: gradient will be added to existing value on every backward. - ‘null’: do not compute gradient for this NDArray.

  • stype (str, optional) – The storage type of the gradient array. Defaults to the same stype of this NDArray.

backward(out_grad=None, retain_graph=False, train_mode=True)[source]

Compute the gradients of this NDArray w.r.t variables.

Parameters:
  • out_grad (NDArray, optional) – Gradient with respect to head.

  • retain_graph (bool, optional) – Whether to retain the computaion graph for another backward pass on the same graph. By default the computaion history is cleared.

  • train_mode (bool, optional) – Whether to compute gradient for training or inference.

broadcast_axes(*args, **kwargs)[source]

Convenience fluent method for broadcast_axes().

The arguments are the same as for broadcast_axes(), with this array as data.

broadcast_like(other)[source]

Broadcasts the input array to the shape of other.

Broadcasting is only allowed on axes with size 1. The new shape cannot change the number of dimensions. For example, you could broadcast from shape (2, 1) to (2, 3), but not from shape (2, 3) to (2, 3, 3).

Parameters:

other (NDArray) – Array with shape of the desired array.

Returns:

A NDArray with the desired shape that is not sharing data with this array, even if the new shape is the same as self.shape.

Return type:

NDArray

Examples

>>> x = mx.nd.arange(0,3).reshape((1,3,1))
>>> x.asnumpy()
array([[[ 0.],
        [ 1.],
        [ 2.]]], dtype=float32)
>>> y = x.broadcast_like(mx.nd.ones((2,3,3)))
>>> y.asnumpy()
array([[[ 0.,  0.,  0.],
        [ 1.,  1.,  1.],
        [ 2.,  2.,  2.]],

       [[ 0.,  0.,  0.],
        [ 1.,  1.,  1.],
        [ 2.,  2.,  2.]]], dtype=float32)
broadcast_to(shape)[source]

Broadcasts the input array to a new shape.

Broadcasting is only allowed on axes with size 1. The new shape cannot change the number of dimensions. For example, you could broadcast from shape (2, 1) to (2, 3), but not from shape (2, 3) to (2, 3, 3).

Parameters:

shape (tuple of int) – The shape of the desired array.

Returns:

A NDArray with the desired shape that is not sharing data with this array, even if the new shape is the same as self.shape.

Return type:

NDArray

Examples

>>> x = mx.nd.arange(0,3).reshape((1,3,1))
>>> x.asnumpy()
array([[[ 0.],
        [ 1.],
        [ 2.]]], dtype=float32)
>>> y = x.broadcast_to((2,3,3))
>>> y.asnumpy()
array([[[ 0.,  0.,  0.],
        [ 1.,  1.,  1.],
        [ 2.,  2.,  2.]],

       [[ 0.,  0.,  0.],
        [ 1.,  1.,  1.],
        [ 2.,  2.,  2.]]], dtype=float32)
cbrt(*args, **kwargs)[source]

Convenience fluent method for cbrt().

The arguments are the same as for cbrt(), with this array as data.

ceil(*args, **kwargs)[source]

Convenience fluent method for ceil().

The arguments are the same as for ceil(), with this array as data.

clip(*args, **kwargs)[source]

Convenience fluent method for clip().

The arguments are the same as for clip(), with this array as data.

property context

Device context of the array.

Examples

>>> x = mx.nd.array([1, 2, 3, 4])
>>> x.context
cpu(0)
>>> type(x.context)
<class 'mxnet.device.Device'>
>>> y = mx.nd.zeros((2,3), mx.gpu(0))
>>> y.context
gpu(0)
copy()[source]

Makes a copy of this NDArray, keeping the same context.

Returns:

The copied array

Return type:

NDArray, CSRNDArray or RowSparseNDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = x.copy()
>>> y.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
copyto(other)[source]

Copies the value of this array to another array.

If other is a NDArray object, then other.shape and self.shape should be the same. This function copies the value from self to other.

If other is a context, a new NDArray will be first created on the target context, and the value of self is copied.

Parameters:

other (NDArray or Context) – The destination array or context.

Returns:

The copied array. If other is an NDArray, then the return value and other will point to the same NDArray.

Return type:

NDArray, CSRNDArray or RowSparseNDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.zeros((2,3), mx.gpu(0))
>>> z = x.copyto(y)
>>> z is y
True
>>> y.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.copyto(mx.gpu(0))
<NDArray 2x3 @gpu(0)>
cos(*args, **kwargs)[source]

Convenience fluent method for cos().

The arguments are the same as for cos(), with this array as data.

cosh(*args, **kwargs)[source]

Convenience fluent method for cosh().

The arguments are the same as for cosh(), with this array as data.

property ctx

Device context of the array. Has the same meaning as context.

Examples

>>> x = mx.nd.array([1, 2, 3, 4])
>>> x.ctx
cpu(0)
>>> type(x.ctx)
<class 'mxnet.context.Context'>
>>> y = mx.nd.zeros((2,3), mx.gpu(0))
>>> y.ctx
gpu(0)
degrees(*args, **kwargs)[source]

Convenience fluent method for degrees().

The arguments are the same as for degrees(), with this array as data.

depth_to_space(*args, **kwargs)[source]

Convenience fluent method for depth_to_space().

The arguments are the same as for depth_to_space(), with this array as data.

detach()[source]

Returns a new NDArray, detached from the current graph.

property device

Device context of the array. Has the same meaning as context.

Examples

>>> x = mx.nd.array([1, 2, 3, 4])
>>> x.device
cpu(0)
>>> type(x.device)
<class 'mxnet.device.Device'>
>>> y = mx.nd.zeros((2,3), mx.gpu(0))
>>> y.device
gpu(0)
diag(k=0, **kwargs)[source]

Convenience fluent method for diag().

The arguments are the same as for diag(), with this array as data.

drop_grad()[source]

Free the memory of the marked ndarray.

property dtype

Data-type of the array’s elements.

Returns:

This NDArray’s data type.

Return type:

numpy.dtype

Examples

>>> x = mx.nd.zeros((2,3))
>>> x.dtype
<type 'numpy.float32'>
>>> y = mx.nd.zeros((2,3), dtype='int32')
>>> y.dtype
<type 'numpy.int32'>
exp(*args, **kwargs)[source]

Convenience fluent method for exp().

The arguments are the same as for exp(), with this array as data.

expand_dims(axis, inplace=False)[source]

Adds an additional dimension to the current array without altering any data.

Parameters:
  • axis (int) – Position where new axis is to be inserted. Suppose that the input NDArray’s dimension is ndim, the range of the inserted axis is [-ndim, ndim].

  • inplace (bool, default False) – If True, this method returns a view of this array that shares data with this array. Otherwise, a copy is returned.

Returns:

An array with expanded shape (d1, d2, …, 1, di, …, dk) that shares data with this array with shape (d1, d2, …, dk), given input axis i.

Return type:

NDArray

Examples

>>> x = mx.nd.arange(6).reshape(2,3)
>>> y = x.expand_dims(1, inplace=True)
>>> z = x.expand_dims(1)
>>> y.shape
(2, 1, 3)
>>> y[0].asnumpy()
array([[0., 1., 2.]], dtype=float32)
>>> y[:] = -1
>>> x.asnumpy()
array([[-1., -1., -1.],
       [-1., -1., -1.]], dtype=float32)
>>> z[0].asnumpy()
array([[0., 1., 2.]], dtype=float32)
expm1(*args, **kwargs)[source]

Convenience fluent method for expm1().

The arguments are the same as for expm1(), with this array as data.

fix(*args, **kwargs)[source]

Convenience fluent method for fix().

The arguments are the same as for fix(), with this array as data.

flatten(inplace=False)[source]

Flatten this array without altering any data.

Parameters:

inplace (bool, default False) – If True, this method returns a view of this array that shares data with this array. Otherwise, a copy is returned.

Returns:

An array with flattened shape (d1, d2*…*dk) that shares data with this array with shape (d1, d2, …, dk).

Return type:

NDArray

Examples

>>> x = mx.nd.arange(30).reshape(5,2,3)
>>> y = x.flatten(inplace=True)
>>> z = x.flatten()
>>> y.shape
(5, 6)
>>> y[0].asnumpy()
array([0., 1., 2., 3., 4., 5.], dtype=float32)
>>> y[:] = -1
>>> x[0].asnumpy()
array([[-1., -1., -1.],
       [-1., -1., -1.]], dtype=float32)
>>> z[0].asnumpy()
array([0., 1., 2., 3., 4., 5.], dtype=float32)
flip(*args, **kwargs)[source]

Convenience fluent method for flip().

The arguments are the same as for flip(), with this array as data.

floor(*args, **kwargs)[source]

Convenience fluent method for floor().

The arguments are the same as for floor(), with this array as data.

property grad

Returns gradient buffer attached to this NDArray.

log(*args, **kwargs)[source]

Convenience fluent method for log().

The arguments are the same as for log(), with this array as data.

log10(*args, **kwargs)[source]

Convenience fluent method for log10().

The arguments are the same as for log10(), with this array as data.

log1p(*args, **kwargs)[source]

Convenience fluent method for log1p().

The arguments are the same as for log1p(), with this array as data.

log2(*args, **kwargs)[source]

Convenience fluent method for log2().

The arguments are the same as for log2(), with this array as data.

log_sigmoid(*args, **kwargs)[source]

Convenience fluent method for log_sigmoid().

The arguments are the same as for log_sigmoid(), with this array as data.

log_softmax(*args, **kwargs)[source]

Convenience fluent method for log_softmax().

The arguments are the same as for log_softmax(), with this array as data.

max(*args, **kwargs)[source]

Convenience fluent method for max().

The arguments are the same as for max(), with this array as data.

mean(*args, **kwargs)[source]

Convenience fluent method for mean().

The arguments are the same as for mean(), with this array as data.

min(*args, **kwargs)[source]

Convenience fluent method for min().

The arguments are the same as for min(), with this array as data.

mish(*args, **kwargs)[source]

Convenience fluent method for mish().

The arguments are the same as for mish(), with this array as data.

nanprod(*args, **kwargs)[source]

Convenience fluent method for nanprod().

The arguments are the same as for nanprod(), with this array as data.

nansum(*args, **kwargs)[source]

Convenience fluent method for nansum().

The arguments are the same as for nansum(), with this array as data.

property ndim

Returns the number of dimensions of this array

Examples

>>> x = mx.nd.array([1, 2, 3, 4])
>>> x.ndim
1
>>> x = mx.nd.array([[1, 2], [3, 4]])
>>> x.ndim
2
norm(*args, **kwargs)[source]

Convenience fluent method for norm().

The arguments are the same as for norm(), with this array as data.

one_hot(*args, **kwargs)[source]

Convenience fluent method for one_hot().

The arguments are the same as for one_hot(), with this array as data.

ones_like(*args, **kwargs)[source]

Convenience fluent method for ones_like().

The arguments are the same as for ones_like(), with this array as data.

pad(*args, **kwargs)[source]

Convenience fluent method for pad().

The arguments are the same as for pad(), with this array as data.

pick(*args, **kwargs)[source]

Convenience fluent method for pick().

The arguments are the same as for pick(), with this array as data.

prod(*args, **kwargs)[source]

Convenience fluent method for prod().

The arguments are the same as for prod(), with this array as data.

radians(*args, **kwargs)[source]

Convenience fluent method for radians().

The arguments are the same as for radians(), with this array as data.

rcbrt(*args, **kwargs)[source]

Convenience fluent method for rcbrt().

The arguments are the same as for rcbrt(), with this array as data.

reciprocal(*args, **kwargs)[source]

Convenience fluent method for reciprocal().

The arguments are the same as for reciprocal(), with this array as data.

relu(*args, **kwargs)[source]

Convenience fluent method for relu().

The arguments are the same as for relu(), with this array as data.

repeat(*args, **kwargs)[source]

Convenience fluent method for repeat().

The arguments are the same as for repeat(), with this array as data.

reshape(*shape, **kwargs)[source]

Returns a view of this array with a new shape without altering any data.

Parameters:
  • shape (tuple of int, or n ints) –

    The new shape should not change the array size, namely np.prod(new_shape) should be equal to np.prod(self.shape). Some dimensions of the shape can take special values from the set {0, -1, -2, -3, -4}. The significance of each is explained below:

    • 0 copy this dimension from the input to the output shape.

      Example:

      - input shape = (2,3,4), shape = (4,0,2), output shape = (4,3,2)
      - input shape = (2,3,4), shape = (2,0,0), output shape = (2,3,4)
      
    • -1 infers the dimension of the output shape by using the remainder of the input dimensions keeping the size of the new array same as that of the input array. At most one dimension of shape can be -1.

      Example:

      - input shape = (2,3,4), shape = (6,1,-1), output shape = (6,1,4)
      - input shape = (2,3,4), shape = (3,-1,8), output shape = (3,1,8)
      - input shape = (2,3,4), shape=(-1,), output shape = (24,)
      
    • -2 copy all/remainder of the input dimensions to the output shape.

      Example:

      - input shape = (2,3,4), shape = (-2,), output shape = (2,3,4)
      - input shape = (2,3,4), shape = (2,-2), output shape = (2,3,4)
      - input shape = (2,3,4), shape = (-2,1,1), output shape = (2,3,4,1,1)
      
    • -3 use the product of two consecutive dimensions of the input shape as the output dimension.

      Example:

      - input shape = (2,3,4), shape = (-3,4), output shape = (6,4)
      - input shape = (2,3,4,5), shape = (-3,-3), output shape = (6,20)
      - input shape = (2,3,4), shape = (0,-3), output shape = (2,12)
      - input shape = (2,3,4), shape = (-3,-2), output shape = (6,4)
      
    • -4 split one dimension of the input into two dimensions passed subsequent to -4 in shape (can contain -1).

      Example:

      - input shape = (2,3,4), shape = (-4,1,2,-2), output shape =(1,2,3,4)
      - input shape = (2,3,4), shape = (2,-4,-1,3,-2), output shape = (2,1,3,4)
      
    • If the argument reverse is set to 1, then the special values are inferred from right to left.

      Example:

      - without reverse=1, for input shape = (10,5,4), shape = (-1,0), output shape would be                 (40,5).
      - with reverse=1, output shape will be (50,4).
      

  • reverse (bool, default False) – If true then the special values are inferred from right to left. Only supported as keyword argument.

Returns:

An array with desired shape that shares data with this array.

Return type:

NDArray

Examples

>>> x = mx.nd.arange(0,6).reshape(2,3)
>>> x.asnumpy()
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.]], dtype=float32)
>>> y = x.reshape(3,2)
>>> y.asnumpy()
array([[ 0.,  1.],
       [ 2.,  3.],
       [ 4.,  5.]], dtype=float32)
>>> y = x.reshape(3,-1)
>>> y.asnumpy()
array([[ 0.,  1.],
       [ 2.,  3.],
       [ 4.,  5.]], dtype=float32)
>>> y = x.reshape(3,2)
>>> y.asnumpy()
array([[ 0.,  1.],
       [ 2.,  3.],
       [ 4.,  5.]], dtype=float32)
>>> y = x.reshape(-3)
>>> y.asnumpy()
array([ 0.  1.  2.  3.  4.  5.], dtype=float32)
>>> y[:] = -1
>>> x.asnumpy()
array([[-1., -1., -1.],
       [-1., -1., -1.]], dtype=float32)
reshape_like(*args, **kwargs)[source]

Convenience fluent method for reshape_like().

The arguments are the same as for reshape_like(), with this array as data.

rint(*args, **kwargs)[source]

Convenience fluent method for rint().

The arguments are the same as for rint(), with this array as data.

round(*args, **kwargs)[source]

Convenience fluent method for round().

The arguments are the same as for round(), with this array as data.

rsqrt(*args, **kwargs)[source]

Convenience fluent method for rsqrt().

The arguments are the same as for rsqrt(), with this array as data.

property shape

Tuple of array dimensions.

Examples

>>> x = mx.nd.array([1, 2, 3, 4])
>>> x.shape
(4L,)
>>> y = mx.nd.zeros((2, 3, 4))
>>> y.shape
(2L, 3L, 4L)
shape_array(*args, **kwargs)[source]

Convenience fluent method for shape_array().

The arguments are the same as for shape_array(), with this array as data.

sigmoid(*args, **kwargs)[source]

Convenience fluent method for sigmoid().

The arguments are the same as for sigmoid(), with this array as data.

sign(*args, **kwargs)[source]

Convenience fluent method for sign().

The arguments are the same as for sign(), with this array as data.

sin(*args, **kwargs)[source]

Convenience fluent method for sin().

The arguments are the same as for sin(), with this array as data.

sinh(*args, **kwargs)[source]

Convenience fluent method for sinh().

The arguments are the same as for sinh(), with this array as data.

property size

Number of elements in the array.

Equivalent to the product of the array’s dimensions.

Examples

>>> import numpy as np
>>> x = mx.nd.zeros((3, 5, 2))
>>> x.size
30
>>> np.prod(x.shape)
30
size_array(*args, **kwargs)[source]

Convenience fluent method for size_array().

The arguments are the same as for size_array(), with this array as data.

slice(*args, **kwargs)[source]

Convenience fluent method for slice().

The arguments are the same as for slice(), with this array as data.

slice_assign(rhs, begin, end, step)[source]

Assign the rhs to a cropped subset of this NDarray in place. Returns the view of this NDArray.

Parameters:
  • rhs (NDArray.) – rhs and this NDArray should be of the same data type, and on the same device. The shape of rhs should be the same as the cropped shape of this NDArray.

  • begin (tuple of begin indices)

  • end (tuple of end indices)

  • step (tuple of step lenghths)

Return type:

This NDArray.

Examples

>>> x = nd.ones((2, 2, 2))
>>> assigned = nd.zeros((1, 1, 2))
>>> y = x.slice_assign(assigned, (0, 0, None), (1, 1, None), (None, None, None))
>>> y
[[[0. 0.]
[1. 1.]]

[[1. 1.] [1. 1.]]] <NDArray 2x2x2 @cpu(0)> >>> x [[[0. 0.] [1. 1.]]

[[1. 1.] [1. 1.]]] <NDArray 2x2x2 @cpu(0)>

slice_assign_scalar(value, begin, end, step)[source]

Assign the scalar to a cropped subset of this NDArray. Value will broadcast to the shape of the cropped shape and will be cast to the same dtype of the NDArray.

Parameters:
  • value (numeric value) – Value and this NDArray should be of the same data type. The shape of rhs should be the same as the cropped shape of this NDArray.

  • begin (tuple of begin indices)

  • end (tuple of end indices)

  • step (tuple of step lenghths)

Return type:

This NDArray.

Examples

>>> from mxnet import nd
>>> x = nd.ones((2, 2, 2))
>>> y = x.slice_assign_scalar(0, (0, 0, None), (1, 1, None), (None, None, None))
>>> y
[[[0. 0.]
[1. 1.]]

[[1. 1.] [1. 1.]]] <NDArray 2x2x2 @cpu(0)> >>> x [[[0. 0.] [1. 1.]]

[[1. 1.] [1. 1.]]] <NDArray 2x2x2 @cpu(0)>

slice_axis(*args, **kwargs)[source]

Convenience fluent method for slice_axis().

The arguments are the same as for slice_axis(), with this array as data.

slice_like(*args, **kwargs)[source]

Convenience fluent method for slice_like().

The arguments are the same as for slice_like(), with this array as data.

softmax(*args, **kwargs)[source]

Convenience fluent method for softmax().

The arguments are the same as for softmax(), with this array as data.

softmin(*args, **kwargs)[source]

Convenience fluent method for softmin().

The arguments are the same as for softmin(), with this array as data.

sort(*args, **kwargs)[source]

Convenience fluent method for sort().

The arguments are the same as for sort(), with this array as data.

space_to_depth(*args, **kwargs)[source]

Convenience fluent method for space_to_depth().

The arguments are the same as for space_to_depth(), with this array as data.

split(*args, **kwargs)[source]

Convenience fluent method for split().

The arguments are the same as for split(), with this array as data.

split_v2(*args, **kwargs)[source]

Convenience fluent method for split_v2().

The arguments are the same as for split_v2(), with this array as data.

sqrt(*args, **kwargs)[source]

Convenience fluent method for sqrt().

The arguments are the same as for sqrt(), with this array as data.

square(*args, **kwargs)[source]

Convenience fluent method for square().

The arguments are the same as for square(), with this array as data.

squeeze(axis=None, inplace=False)[source]

Remove dimensions with size 1 from this array without altering any data.

Parameters:
  • axis (int, tuple of int, or None) – Selects a subset of the single-dimensional entries in the shape. If an axis is selected with shape entry greater than one, an error is raised.

  • inplace (bool, default False) – If True, this method returns a view of this array that shares data with this array. Otherwise, a copy is returned.

property stype

Storage-type of the array.

sum(*args, **kwargs)[source]

Convenience fluent method for sum().

The arguments are the same as for sum(), with this array as data.

swapaxes(*args, **kwargs)[source]

Convenience fluent method for swapaxes().

The arguments are the same as for swapaxes(), with this array as data.

take(*args, **kwargs)[source]

Convenience fluent method for take().

The arguments are the same as for take(), with this array as data.

tan(*args, **kwargs)[source]

Convenience fluent method for tan().

The arguments are the same as for tan(), with this array as data.

tanh(*args, **kwargs)[source]

Convenience fluent method for tanh().

The arguments are the same as for tanh(), with this array as data.

tile(*args, **kwargs)[source]

Convenience fluent method for tile().

The arguments are the same as for tile(), with this array as data.

to_dlpack_for_read()[source]

Returns a reference view of NDArray that represents as DLManagedTensor until all previous write operations on the current array are finished.

Returns:

a reference view of NDArray that represents as DLManagedTensor.

Return type:

PyCapsule (the pointer of DLManagedTensor)

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.to_dlpack_for_read(x)
>>> type(y)
<class 'PyCapsule'>
>>> z = mx.nd.from_dlpack(y)
>>> z
[[1. 1. 1.]
 [1. 1. 1.]]
<NDArray 2x3 @cpu(0)>
to_dlpack_for_write()[source]

Returns a reference view of NDArray that represents as DLManagedTensor until all previous read/write operations on the current array are finished.

Returns:

a reference view of NDArray that represents as DLManagedTensor.

Return type:

PyCapsule (the pointer of DLManagedTensor)

Examples

>>> x = mx.nd.ones((2,3))
>>> w = mx.nd.to_dlpack_for_write(x)
>>> type(w)
<class 'PyCapsule'>
>>> u = mx.nd.from_dlpack(w)
>>> u += 1
>>> x
[[2. 2. 2.]
 [2. 2. 2.]]
<NDArray 2x3 @cpu(0)>
topk(*args, **kwargs)[source]

Convenience fluent method for topk().

The arguments are the same as for topk(), with this array as data.

tostype(stype)[source]

Return a copy of the array with chosen storage type.

Returns:

A copy of the array with the chosen storage stype

Return type:

NDArray, CSRNDArray or RowSparseNDArray

transpose(*args, **kwargs)[source]

Convenience fluent method for transpose().

The arguments are the same as for transpose(), with this array as data.

trunc(*args, **kwargs)[source]

Convenience fluent method for trunc().

The arguments are the same as for trunc(), with this array as data.

wait_to_read()[source]

Waits until all previous write operations on the current array are finished.

This method guarantees that all previous write operations that pushed into the backend engine for execution are actually finished.

Examples

>>> import time
>>> tic = time.time()
>>> a = mx.nd.ones((1000,1000))
>>> b = mx.nd.dot(a, a)
>>> print(time.time() - tic)
0.003854036331176758
>>> b.wait_to_read()
>>> print(time.time() - tic)
0.0893700122833252
zeros_like(*args, **kwargs)[source]

Convenience fluent method for zeros_like().

The arguments are the same as for zeros_like(), with this array as data.

mxnet.ndarray.ndarray.add(lhs, rhs)[source]

Returns element-wise sum of the input arrays with broadcasting.

Equivalent to lhs + rhs, mx.nd.broadcast_add(lhs, rhs) and mx.nd.broadcast_plus(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array to be added.

  • rhs (scalar or mxnet.ndarray.array) – Second array to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

The element-wise sum of the input arrays.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> (x+2).asnumpy()
array([[ 3.,  3.,  3.],
       [ 3.,  3.,  3.]], dtype=float32)
>>> (x+y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 2.,  2.,  2.]], dtype=float32)
>>> mx.nd.add(x,y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 2.,  2.,  2.]], dtype=float32)
>>> (z + y).asnumpy()
array([[ 0.,  1.],
       [ 1.,  2.]], dtype=float32)
mxnet.ndarray.ndarray.arange(start, stop=None, step=1.0, repeat=1, infer_range=None, ctx=None, dtype=<class 'numpy.float32'>)[source]

Returns evenly spaced values within a given interval.

Values are generated within the half-open interval [start, stop). In other words, the interval includes start but excludes stop. The function is similar to the built-in Python function range and to numpy.arange, but returns an NDArray.

Parameters:
  • start (number, optional) – Start of interval. The default start value is 0.

  • stop (number) – End of interval.

  • step (number, optional) – Spacing between values. The default step size is 1.

  • repeat (int, optional) – Number of times to repeat each element. The default repeat count is 1.

  • infer_range (boolean, optional) – Infer the stop position from the start, step, repeat, and output tensor size. Deprecated. Only False is supported.

  • ctx (Context, optional) – Device context. Default context is the current default context.

  • dtype (str or numpy.dtype, optional) – The data type of the NDArray. The default datatype is np.float32.

Returns:

NDArray of evenly spaced values in the specified range.

Return type:

NDArray

Examples

>>> mx.nd.arange(3).asnumpy()
array([ 0.,  1.,  2.], dtype=float32)
>>> mx.nd.arange(2, 6).asnumpy()
array([ 2.,  3.,  4.,  5.], dtype=float32)
>>> mx.nd.arange(2, 6, step=2).asnumpy()
array([ 2.,  4.], dtype=float32)
>>> mx.nd.arange(2, 6, step=1.5, repeat=2).asnumpy()
array([ 2. ,  2. ,  3.5,  3.5,  5. ,  5. ], dtype=float32)
>>> mx.nd.arange(2, 6, step=2, repeat=3, dtype='int32').asnumpy()
array([2, 2, 2, 4, 4, 4], dtype=int32)
mxnet.ndarray.ndarray.concatenate(arrays, axis=0, always_copy=True)[source]

DEPRECATED, use concat instead

Parameters:
  • arrays (list of NDArray) – Arrays to be concatenate. They must have identical shape except the first dimension. They also must have the same data type.

  • axis (int) – The axis along which to concatenate.

  • always_copy (bool) – Default True. When not True, if the arrays only contain one NDArray, that element will be returned directly, avoid copying.

Returns:

An NDArray that lives on the same context as arrays[0].context.

Return type:

NDArray

mxnet.ndarray.ndarray.divide(lhs, rhs)[source]

Returns element-wise division of the input arrays with broadcasting.

Equivalent to lhs / rhs and mx.nd.broadcast_div(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array in division.

  • rhs (scalar or mxnet.ndarray.array) – Second array in division. The arrays to be divided. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

The element-wise division of the input arrays.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))*6
>>> y = mx.nd.ones((2,1))*2
>>> x.asnumpy()
array([[ 6.,  6.,  6.],
       [ 6.,  6.,  6.]], dtype=float32)
>>> y.asnumpy()
array([[ 2.],
       [ 2.]], dtype=float32)
>>> x/2
<NDArray 2x3 @cpu(0)>
>>> (x/3).asnumpy()
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.]], dtype=float32)
>>> (x/y).asnumpy()
array([[ 3.,  3.,  3.],
       [ 3.,  3.,  3.]], dtype=float32)
>>> mx.nd.divide(x,y).asnumpy()
array([[ 3.,  3.,  3.],
       [ 3.,  3.,  3.]], dtype=float32)
mxnet.ndarray.ndarray.equal(lhs, rhs)[source]

Returns the result of element-wise equal to (==) comparison operation with broadcasting.

For each element in input arrays, return 1(true) if corresponding elements are same, otherwise return 0(false).

Equivalent to lhs == rhs and mx.nd.broadcast_equal(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array to be compared.

  • rhs (scalar or mxnet.ndarray.array) – Second array to be compared. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

Output array of boolean values.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> (x == 1).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (x == y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> mx.nd.equal(x,y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (z == y).asnumpy()
array([[ 1.,  0.],
       [ 0.,  1.]], dtype=float32)
mxnet.ndarray.ndarray.eye(N, M=0, k=0, ctx=None, dtype=None, **kwargs)[source]

Return a 2-D array with ones on the diagonal and zeros elsewhere.

Parameters:
  • N (int) – Number of rows in the output.

  • M (int, optional) – Number of columns in the output. If 0, defaults to N.

  • k (int, optional) – Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal.

  • ctx (Context, optional) – An optional device context (default is the current default context)

  • dtype (str or numpy.dtype, optional) – An optional value type (default is float32)

Returns:

A created array

Return type:

NDArray

Examples

>>> mx.nd.eye(2)
[[ 1.  0.]
 [ 0.  1.]]
<NDArray 2x2 @cpu(0)>
>>> mx.nd.eye(2, 3, 1)
[[ 0.  1.  0.]
 [ 0.  0.  1.]]
<NDArray 2x3 @cpu(0)>
mxnet.ndarray.ndarray.from_dlpack(dlpack)

Returns a NDArray backed by a dlpack tensor.

Parameters:

dlpack (PyCapsule (the pointer of DLManagedTensor)) – input data

Returns:

a NDArray backed by a dlpack tensor

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.to_dlpack_for_read(x)
>>> type(y)
<class 'PyCapsule'>
>>> z = mx.nd.from_dlpack(y)
>>> type(z)
<class 'mxnet.ndarray.ndarray.NDArray'>
>>> z
[[ 1.  1.  1.]
 [ 1.  1.  1.]]
<NDArray 2x3 @cpu(0)>
>>> w = mx.nd.to_dlpack_for_write(x)
>>> type(w)
<class 'PyCapsule'>
>>> u = mx.nd.from_dlpack(w)
>>> u += 1
>>> x
[[2. 2. 2.]
 [2. 2. 2.]]
<NDArray 2x3 @cpu(0)>
mxnet.ndarray.ndarray.from_numpy(ndarray, zero_copy=True)

Returns an MXNet’s NDArray backed by numpy’s ndarray. When zero_copy is set to be true, this API consumes numpy’s ndarray and produces MXNet’s ndarray without having to copy the content. In this case, we disallow users to modify the given numpy ndarray, and it is suggested not to read the numpy ndarray as well for internal correctness.

Parameters:
  • ndarray (NDArray) – input data

  • zero_copy (bool) – Whether we use DLPack’s zero-copy conversion to convert to MXNet’s NDArray. This is only available for c-contiguous arrays, i.e. array.flags[C_CONTIGUOUS] == True.

Returns:

a NDArray backed by a dlpack tensor

Return type:

NDArray

mxnet.ndarray.ndarray.full(shape, val, ctx=None, dtype=<class 'numpy.float32'>, out=None)[source]

Returns a new array of given shape and type, filled with the given value val.

Parameters:
  • shape (int or tuple of int) – The shape of the new array.

  • val (scalar) – Fill value.

  • ctx (Context, optional) – Device context (default is the current default context).

  • dtype (str or numpy.dtype, optional) – The data type of the returned NDArray. The default datatype is float32.

  • out (NDArray, optional) – The output NDArray (default is None).

Returns:

NDArray filled with val, with the given shape, ctx, and dtype.

Return type:

NDArray

Examples

>>> mx.nd.full(1, 2.0).asnumpy()
array([ 2.], dtype=float32)
>>> mx.nd.full((1, 2), 2.0, mx.gpu(0))
<NDArray 1x2 @gpu(0)>
>>> mx.nd.full((1, 2), 2.0, dtype='float16').asnumpy()
array([[ 2.,  2.]], dtype=float16)
mxnet.ndarray.ndarray.get_indexing_dispatch_code(key)[source]

Returns a dispatch code for calling basic or advanced indexing functions.

mxnet.ndarray.ndarray.get_oshape_of_gather_nd_op(dshape, ishape)[source]

Given data and index shapes, get the output NDArray shape. This basically implements the infer shape logic of op gather_nd.

mxnet.ndarray.ndarray.greater(lhs, rhs)[source]

Returns the result of element-wise greater than (>) comparison operation with broadcasting.

For each element in input arrays, return 1(true) if lhs elements are greater than rhs, otherwise return 0(false).

Equivalent to lhs > rhs and mx.nd.broadcast_greater(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array to be compared.

  • rhs (scalar or mxnet.ndarray.array) – Second array to be compared. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

Output array of boolean values.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> (x > 1).asnumpy()
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> (x > y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> mx.nd.greater(x, y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> (z > y).asnumpy()
array([[ 0.,  1.],
       [ 0.,  0.]], dtype=float32)
mxnet.ndarray.ndarray.greater_equal(lhs, rhs)[source]

Returns the result of element-wise greater than or equal to (>=) comparison operation with broadcasting.

For each element in input arrays, return 1(true) if lhs elements are greater than equal to rhs, otherwise return 0(false).

Equivalent to lhs >= rhs and mx.nd.broadcast_greater_equal(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array to be compared.

  • rhs (scalar or mxnet.ndarray.array) – Second array to be compared. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

Output array of boolean values.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> (x >= 1).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (x >= y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> mx.nd.greater_equal(x, y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (z >= y).asnumpy()
array([[ 1.,  1.],
       [ 0.,  1.]], dtype=float32)
mxnet.ndarray.ndarray.histogram(a, bins=10, range=None)[source]

Compute the histogram of the input data.

Parameters:
  • a (NDArray) – Input data. The histogram is computed over the flattened array.

  • bins (int or sequence of scalars) – If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths.

  • range ((float, float), optional) – The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored. The first element of the range must be less than or equal to the second. range affects the automatic bin computation as well, the range will be equally divided by the number of bins.

Returns:

A created array.

Return type:

NDArray

mxnet.ndarray.ndarray.imdecode(str_img, clip_rect=(0, 0, 0, 0), out=None, index=0, channels=3, mean=None)[source]

DEPRECATED, use mx.img instead

Parameters:
  • str_img (str) – Binary image data

  • clip_rect (iterable of 4 int) – Clip decoded image to rectangle (x0, y0, x1, y1).

  • out (NDArray) – Output buffer. Can be 3 dimensional (c, h, w) or 4 dimensional (n, c, h, w).

  • index (int) – Output decoded image to i-th slice of 4 dimensional buffer.

  • channels (int) – Number of channels to output. Decode to grey scale when channels = 1.

  • mean (NDArray) – Subtract mean from decode image before outputing.

mxnet.ndarray.ndarray.indexing_key_expand_implicit_axes(key, shape)[source]

Make implicit axes explicit by adding slice(None) and convert boolean array to integer array through nonzero.

Examples

>>> shape = (3, 4, 5)
>>> indexing_key_expand_implicit_axes(np.s_[2, 1, 1], shape)
(2, 1, 1)
>>> indexing_key_expand_implicit_axes(np.s_[0], shape)
(0, slice(None, None, None), slice(None, None, None))
>>> indexing_key_expand_implicit_axes(np.s_[0, ...], shape)  # equivalent
(0, slice(None, None, None), slice(None, None, None))
>>> indexing_key_expand_implicit_axes(np.s_[:2, None, 0, ...], shape)
(slice(None, 2, None), None, 0, slice(None, None, None))
>>> bool_array = np.array([[True, False, True, False],
                           [False, True, False, True],
                           [True, False, True, False]], dtype=np.bool_)
>>> indexing_key_expand_implicit_axes(np.s_[bool_array, None, 0:2], shape)
(array([0, 0, 1, 1, 2, 2], dtype=int64), array([0, 2, 1, 3, 0, 2], dtype=int64), None, slice(None, 2, None))
mxnet.ndarray.ndarray.lesser(lhs, rhs)[source]

Returns the result of element-wise lesser than (<) comparison operation with broadcasting.

For each element in input arrays, return 1(true) if lhs elements are less than rhs, otherwise return 0(false).

Equivalent to lhs < rhs and mx.nd.broadcast_lesser(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array to be compared.

  • rhs (scalar or mxnet.ndarray.array) – Second array to be compared. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

Output array of boolean values.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> (x < 1).asnumpy()
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> (x < y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> mx.nd.lesser(x, y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> (z < y).asnumpy()
array([[ 0.,  0.],
       [ 1.,  0.]], dtype=float32)
mxnet.ndarray.ndarray.lesser_equal(lhs, rhs)[source]

Returns the result of element-wise lesser than or equal to (<=) comparison operation with broadcasting.

For each element in input arrays, return 1(true) if lhs elements are lesser than equal to rhs, otherwise return 0(false).

Equivalent to lhs <= rhs and mx.nd.broadcast_lesser_equal(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array to be compared.

  • rhs (scalar or mxnet.ndarray.array) – Second array to be compared. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

Output array of boolean values.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> (x <= 1).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (x <= y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> mx.nd.lesser_equal(x, y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (z <= y).asnumpy()
array([[ 1.,  0.],
       [ 1.,  1.]], dtype=float32)
mxnet.ndarray.ndarray.linspace(start, stop, num, endpoint=True, ctx=None, dtype=<class 'numpy.float32'>)[source]

Return evenly spaced numbers within a specified interval.

Values are generated within the half-open interval [start, stop) or closed interval [start, stop] depending on whether endpoint is True or False. The function is similar to numpy.linspace, but returns an NDArray.

Parameters:
  • start (number) – Start of interval.

  • stop (number) – End of interval, unless endpoint is set to False. In that case, the sequence consists of all but the last of num + 1 evenly spaced samples, so that stop is excluded. Note that the step size changes when endpoint is False.

  • num (number) – Number of samples to generate. Must be non-negative.

  • endpoint (bool) – If True, stop is the last sample. Otherwise, it is not included. The default is True.

  • ctx (Context, optional) – Device context. Default context is the current default context.

  • dtype (str or numpy.dtype, optional) – The data type of the NDArray. The default datatype is np.float32.

Returns:

NDArray of evenly spaced values in the specified range.

Return type:

NDArray

Examples

>>> mx.nd.linspace(2.0, 3.0, 5).asnumpy()
array([ 2.,  2.25.,  2.5,  2.75,  3.], dtype=float32)
>>> mx.nd.linspace(2.0, 3.0, 5, endpoint=False).asnumpy()
array([ 2.,  2.2.,  2.4,  2.6,  2.8], dtype=float32)
mxnet.ndarray.ndarray.logical_and(lhs, rhs)[source]

Returns the result of element-wise logical and comparison operation with broadcasting.

For each element in input arrays, return 1(true) if lhs elements and rhs elements are true, otherwise return 0(false).

Equivalent to lhs and rhs and mx.nd.broadcast_logical_and(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First input of the function.

  • rhs (scalar or mxnet.ndarray.array) – Second input of the function. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

Output array of boolean values.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> mx.nd.logical_and(x, 1).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> mx.nd.logical_and(x, y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> mx.nd.logical_and(z, y).asnumpy()
array([[ 0.,  0.],
       [ 0.,  1.]], dtype=float32)
mxnet.ndarray.ndarray.logical_or(lhs, rhs)[source]

Returns the result of element-wise logical or comparison operation with broadcasting.

For each element in input arrays, return 1(true) if lhs elements or rhs elements are true, otherwise return 0(false).

Equivalent to lhs or rhs and mx.nd.broadcast_logical_or(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First input of the function.

  • rhs (scalar or mxnet.ndarray.array) – Second input of the function. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

Output array of boolean values.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> mx.nd.logical_or(x, 1).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> mx.nd.logical_or(x, y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> mx.nd.logical_or(z, y).asnumpy()
array([[ 0.,  1.],
       [ 1.,  1.]], dtype=float32)
mxnet.ndarray.ndarray.logical_xor(lhs, rhs)[source]

Returns the result of element-wise logical xor comparison operation with broadcasting.

For each element in input arrays, return 1(true) if lhs elements or rhs elements are true, otherwise return 0(false).

Equivalent to bool(lhs) ^ bool(rhs) and mx.nd.broadcast_logical_xor(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First input of the function.

  • rhs (scalar or mxnet.ndarray.array) – Second input of the function. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

Output array of boolean values.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> mx.nd.logical_xor(x, y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 0.,  0.,  0.]], dtype=float32)
mxnet.ndarray.ndarray.maximum(lhs, rhs)[source]

Returns element-wise maximum of the input arrays with broadcasting.

Equivalent to mx.nd.broadcast_maximum(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array to be compared.

  • rhs (scalar or mxnet.ndarray.array) – Second array to be compared. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

The element-wise maximum of the input arrays.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> mx.nd.maximum(x, 2).asnumpy()
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.]], dtype=float32)
>>> mx.nd.maximum(x, y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> mx.nd.maximum(y, z).asnumpy()
array([[ 0.,  1.],
       [ 1.,  1.]], dtype=float32)
mxnet.ndarray.ndarray.minimum(lhs, rhs)[source]

Returns element-wise minimum of the input arrays with broadcasting.

Equivalent to mx.nd.broadcast_minimum(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array to be compared.

  • rhs (scalar or mxnet.ndarray.array) – Second array to be compared. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

The element-wise minimum of the input arrays.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> mx.nd.minimum(x, 2).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> mx.nd.minimum(x, y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> mx.nd.minimum(z, y).asnumpy()
array([[ 0.,  0.],
       [ 0.,  1.]], dtype=float32)
mxnet.ndarray.ndarray.modulo(lhs, rhs)[source]

Returns element-wise modulo of the input arrays with broadcasting.

Equivalent to lhs % rhs and mx.nd.broadcast_mod(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array in modulo.

  • rhs (scalar or mxnet.ndarray.array) – Second array in modulo. The arrays to be taken modulo. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

The element-wise modulo of the input arrays.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))*6
>>> y = mx.nd.ones((2,1))*4
>>> x.asnumpy()
array([[ 6.,  6.,  6.],
       [ 6.,  6.,  6.]], dtype=float32)
>>> y.asnumpy()
array([[ 4.],
       [ 4.]], dtype=float32)
>>> x%5
<NDArray 2x3 @cpu(0)>
>>> (x%5).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (x%y).asnumpy()
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.]], dtype=float32)
>>> mx.nd.modulo(x,y).asnumpy()
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.]], dtype=float32)
mxnet.ndarray.ndarray.moveaxis(tensor, source, destination)[source]

Moves the source axis into the destination position while leaving the other axes in their original order

Parameters:
  • tensor (mx.nd.array) – The array which axes should be reordered

  • source (int or sequence of int) – Original position of the axes to move. Can be negative but must be unique.

  • destination (int or sequence of int) – Destination position for each of the original axes. Can be negative but must be unique.

Returns:

result – Array with moved axes.

Return type:

mx.nd.array

Examples

>>> X = mx.nd.array([[1, 2, 3], [4, 5, 6]])
>>> mx.nd.moveaxis(X, 0, 1).shape
(3L, 2L)
>>> X = mx.nd.zeros((3, 4, 5))
>>> mx.nd.moveaxis(X, [0, 1], [-1, -2]).shape
(5, 4, 3)
mxnet.ndarray.ndarray.multiply(lhs, rhs)[source]

Returns element-wise product of the input arrays with broadcasting.

Equivalent to lhs * rhs and mx.nd.broadcast_mul(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array to be multiplied.

  • rhs (scalar or mxnet.ndarray.array) – Second array to be multiplied. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

The element-wise multiplication of the input arrays.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> (x*2).asnumpy()
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.]], dtype=float32)
>>> (x*y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> mx.nd.multiply(x, y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (z*y).asnumpy()
array([[ 0.,  0.],
       [ 0.,  1.]], dtype=float32)
mxnet.ndarray.ndarray.not_equal(lhs, rhs)[source]

Returns the result of element-wise not equal to (!=) comparison operation with broadcasting.

For each element in input arrays, return 1(true) if corresponding elements are different, otherwise return 0(false).

Equivalent to lhs != rhs and mx.nd.broadcast_not_equal(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array to be compared.

  • rhs (scalar or mxnet.ndarray.array) – Second array to be compared. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

Output array of boolean values.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> (z == y).asnumpy()
array([[ 1.,  0.],
       [ 0.,  1.]], dtype=float32)
>>> (x != 1).asnumpy()
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> (x != y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> mx.nd.not_equal(x, y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> (z != y).asnumpy()
array([[ 0.,  1.],
       [ 1.,  0.]], dtype=float32)
mxnet.ndarray.ndarray.onehot_encode(indices, out)[source]

One-hot encoding indices into matrix out.

Note

onehot_encode is deprecated. Use one_hot instead.

mxnet.ndarray.ndarray.ones(shape, ctx=None, dtype=None, **kwargs)[source]

Returns a new array filled with all ones, with the given shape and type.

Parameters:
  • shape (int or tuple of int or list of int) – The shape of the empty array.

  • ctx (Context, optional) – An optional device context. Defaults to the current default context (mxnet.context.current_context()).

  • dtype (str or numpy.dtype, optional) – An optional value type (default is float32).

  • out (NDArray, optional) – The output NDArray (default is None).

Returns:

A new array of the specified shape filled with all ones.

Return type:

NDArray

Examples

>>> mx.nd.ones(1).asnumpy()
array([ 1.], dtype=float32)
>>> mx.nd.ones((1,2), mx.gpu(0))
<NDArray 1x2 @gpu(0)>
>>> mx.nd.ones((1,2), dtype='float16').asnumpy()
array([[ 1.,  1.]], dtype=float16)
mxnet.ndarray.ndarray.power(base, exp)[source]

Returns result of first array elements raised to powers from second array, element-wise with broadcasting.

Equivalent to base ** exp and mx.nd.broadcast_power(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • base (scalar or NDArray) – The base array

  • exp (scalar or NDArray) – The exponent array. If base.shape != exp.shape, they must be broadcastable to a common shape.

Returns:

The bases in x raised to the exponents in y.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))*2
>>> y = mx.nd.arange(1,3).reshape((2,1))
>>> z = mx.nd.arange(1,3).reshape((2,1))
>>> x.asnumpy()
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.]], dtype=float32)
>>> y.asnumpy()
array([[ 1.],
       [ 2.]], dtype=float32)
>>> z.asnumpy()
array([[ 1.],
       [ 2.]], dtype=float32)
>>> (x**2).asnumpy()
array([[ 4.,  4.,  4.],
       [ 4.,  4.,  4.]], dtype=float32)
>>> (x**y).asnumpy()
array([[ 2.,  2.,  2.],
       [ 4.,  4.,  4.]], dtype=float32)
>>> mx.nd.power(x,y).asnumpy()
array([[ 2.,  2.,  2.],
       [ 4.,  4.,  4.]], dtype=float32)
>>> (z**y).asnumpy()
array([[ 1.],
       [ 4.]], dtype=float32)
mxnet.ndarray.ndarray.split_v2(ary, indices_or_sections, axis=0, squeeze_axis=False)[source]

Split an array into multiple sub-arrays.

Parameters:
  • ary (NDArray) – Array to be divided into sub-arrays.

  • indices_or_sections (int or tuple of ints) – If indices_or_sections is an integer, N, the array will be divided into N equal arrays along axis. If such a split is not possible, an error is raised. If indices_or_sections is a 1-D array of sorted integers, the entries indicate where along axis the array is split. For example, [2, 3] would, for axis=0, result in - ary[:2] - ary[2:3] - ary[3:] If an index exceeds the dimension of the array along axis, an empty sub-array is returned correspondingly.

  • axis (int, optional) – The axis along which to split, default is 0.

  • squeeze_axis (boolean, optional) – Whether to squeeze the axis of sub-arrays or not, only useful when size of the sub-arrays are 1 on the axis. Default is False.

Returns:

A created array.

Return type:

NDArray

mxnet.ndarray.ndarray.subtract(lhs, rhs)[source]

Returns element-wise difference of the input arrays with broadcasting.

Equivalent to lhs - rhs, mx.nd.broadcast_sub(lhs, rhs) and mx.nd.broadcast_minus(lhs, rhs).

Note

If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape.

Parameters:
  • lhs (scalar or mxnet.ndarray.array) – First array to be subtracted.

  • rhs (scalar or mxnet.ndarray.array) – Second array to be subtracted. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.

Returns:

The element-wise difference of the input arrays.

Return type:

NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.arange(2).reshape((2,1))
>>> z = mx.nd.arange(2).reshape((1,2))
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> y.asnumpy()
array([[ 0.],
       [ 1.]], dtype=float32)
>>> z.asnumpy()
array([[ 0.,  1.]], dtype=float32)
>>> (x-2).asnumpy()
array([[-1., -1., -1.],
       [-1., -1., -1.]], dtype=float32)
>>> (x-y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> mx.nd.subtract(x,y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> (z-y).asnumpy()
array([[ 0.,  1.],
       [-1.,  0.]], dtype=float32)
mxnet.ndarray.ndarray.to_dlpack_for_read(data)

Returns a reference view of NDArray that represents as DLManagedTensor until all previous write operations on the current array are finished.

Parameters:

data (NDArray) – input data.

Returns:

a reference view of NDArray that represents as DLManagedTensor.

Return type:

PyCapsule (the pointer of DLManagedTensor)

Examples

>>> x = mx.nd.ones((2,3))
>>> y = mx.nd.to_dlpack_for_read(x)
>>> type(y)
<class 'PyCapsule'>
>>> z = mx.nd.from_dlpack(y)
>>> z
[[1. 1. 1.]
 [1. 1. 1.]]
<NDArray 2x3 @cpu(0)>
mxnet.ndarray.ndarray.to_dlpack_for_write(data)

Returns a reference view of NDArray that represents as DLManagedTensor until all previous read/write operations on the current array are finished.

Parameters:

data (NDArray) – input data.

Returns:

PyCapsule – a reference view of NDArray that represents as DLManagedTensor.

Return type:

the pointer of DLManagedTensor

Examples

>>> x = mx.nd.ones((2,3))
>>> w = mx.nd.to_dlpack_for_write(x)
>>> type(w)
<class 'PyCapsule'>
>>> u = mx.nd.from_dlpack(w)
>>> u += 1
>>> x
[[2. 2. 2.]
 [2. 2. 2.]]
<NDArray 2x3 @cpu(0)>
mxnet.ndarray.ndarray.true_divide(lhs, rhs)[source]

This function is similar to divide().

mxnet.ndarray.ndarray.waitall()[source]

Wait for all async operations to finish in MXNet.

This function is used for benchmarking only.

Note

If your mxnet code throws an exception, then waitall can cause performance impact.

mxnet.ndarray.ndarray.zeros(shape, ctx=None, dtype=None, **kwargs)[source]

Returns a new array filled with all zeros, with the given shape and type.

Parameters:
  • shape (int or tuple of int) – The shape of the empty array.

  • ctx (Context, optional) – An optional device context (default is the current default context).

  • dtype (str or numpy.dtype, optional) – An optional value type (default is float32).

  • out (NDArray, optional) – The output NDArray (default is None).

Returns:

A created array

Return type:

NDArray

Examples

>>> mx.nd.zeros(1).asnumpy()
array([ 0.], dtype=float32)
>>> mx.nd.zeros((1,2), mx.gpu(0))
<NDArray 1x2 @gpu(0)>
>>> mx.nd.zeros((1,2), mx.gpu(0), 'float16').asnumpy()
array([[ 0.,  0.]], dtype=float16)