NDArray API

Overview

This document lists the routines of the n-dimensional array package

mxnet.ndarray NDArray API of mxnet.

A NDArray is a multidimensional container of items of the same type and size. Various methods for data manipulation and computation are provided.

>>> x = mx.nd.array([[1, 2, 3], [4, 5, 6]])
>>> type(x)
<class 'mxnet.ndarray.NDArray'>
>>> x.shape
(2, 3)
>>> y = x + mx.nd.ones(x.shape)*3
>>> print(y.asnumpy())
[[ 4.  5.  6.]
 [ 7.  8.  9.]]
>>> z = y.as_in_context(mx.gpu(0))
>>> print(z)
<NDArray 2x3 @gpu(0)>

A detailed tutorial is available at http://mxnet.io/tutorials/python/ndarray.html..

Note

mxnet.ndarray is similar to numpy.ndarray in some aspects. But the difference is not negligible. For example

  • NDArray.T does real data transpose to return new a copied array, instead of returning a view of the input array.
  • ndarray.dot performs dot between the last axis of the first input array and the first axis of the second input, while numpy.dot uses the second last axis of the input array.

In additional, NDArray supports GPU computation and various neural network layers.

Note

ndarray also provides almost same routines to symbol. Most routines between these two packages share the same C++ operator source codes. But ndarray differs to symbol in several aspects:

  • ndarray adopts imperative programming, namely sentences are executed step-by-step so that the results can be obtained immediately.
  • Most binary operators such as + and > are enabled broadcasting in default.

In the rest of this document, we first overview the methods provided by the ndarray.NDArray class, and then list other routines provided by the ndarray package.

The NDArray class

Array attributes

NDArray.shape Tuple of array dimensions.
NDArray.size Number of elements in the array.
NDArray.context Device context of the array.
NDArray.dtype Data-type of the array’s elements.

Array conversion

NDArray.copy Makes a copy of this NDArray, keeping the same context.
NDArray.copyto Copies the value of this array to another array.
NDArray.as_in_context Returns an array on the target device with the same value as this array.
NDArray.asnumpy Returns a numpy.ndarray object with value copied from this array.
NDArray.asscalar Returns a scalar whose value is copied from this array.
NDArray.astype Returns a copy of the array after casting to a specified type.

Array change shape

NDArray.T Returns a copy of the array with axes transposed.
NDArray.reshape Returns a view of this array with a new shape without altering any data.
NDArray.broadcast_to Broadcasts an array to a new shape.

Arithmetic operations

NDArray.__add__ x.__add__(y) <=> x+y <=> mx.nd.add(x, y)
NDArray.__sub__ x.__sub__(y) <=> x-y <=> mx.nd.subtract(x, y)
NDArray.__rsub__ x.__rsub__(y) <=> y-x <=> mx.nd.subtract(y, x)
NDArray.__neg__ x.__neg__(y) <=> -x
NDArray.__mul__ x.__mul__(y) <=> x*y <=> mx.nd.multiply(x, y)
NDArray.__div__ x.__div__(y) <=> x/y <=> mx.nd.divide(x, y)
NDArray.__rdiv__ x.__rdiv__(y) <=> y/x <=> mx.nd.divide(y, x)
NDArray.__pow__ x.__pow__(y) <=> x**y <=> mx.nd.power(x,y)

In-place arithmetic operations

NDArray.__iadd__ x.__iadd__(y) <=> x+=y
NDArray.__isub__ x.__isub__(y) <=> x-=y
NDArray.__imul__ x.__imul__(y) <=> x*=y
NDArray.__idiv__ x.__rdiv__(y) <=> x/=y

Comparison operators

NDArray.__lt__ x.__lt__(y) <=> x<y <=> mx.nd.lesser(x, y)
NDArray.__le__ x.__le__(y) <=> x<=y <=> mx.nd.less_equal(x, y)
NDArray.__gt__ x.__gt__(y) <=> x>y <=> mx.nd.greater(x, y)
NDArray.__ge__ x.__ge__(y) <=> x>=y <=> mx.nd.greater_equal(x, y)
NDArray.__eq__ x.__eq__(y) <=> x==y <=> mx.nd.equal(x, y)
NDArray.__ne__ x.__ne__(y) <=> x!=y <=> mx.nd.not_equal(x, y)

Indexing

NDArray.__getitem__ x.__getitem__(i) <=> x[i]
NDArray.__setitem__ x.__setitem__(i, y) <=> x[i]=y

Lazy evaluation

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

Array creation routines

array Creates a new array from any object exposing the array interface.
empty Returns a new array of given shape and type, without initializing entries.
zeros Returns a new array filled with all zeros, with the given shape and type.
ones Returns a new array filled with all ones, with the given shape and type.
full Returns a new array of given shape and type, filled with the given value val.
arange Returns evenly spaced values within a given interval.
load Load array from file.
save Save a list of arrays of a str->array dict into file.

Array manipulation routines

Changing array shape and type

cast Cast to a specified type, element-wise.
reshape Reshape array into a new shape.
flatten Flatten input into a 2-D array by collapsing the higher dimensions.
expand_dims Insert a new axis with size 1 into the array shape

Expanding array elements

broadcast_to Broadcast an array to a new shape.
broadcast_axes Broadcast an array over particular axes.
repeat Repeat elements of an array.
tile Repeat the whole array by multiple times.
pad Pad an array.

Rearranging elements

transpose Permute the dimensions of an array.
swapaxes Interchange two axes of an array.
flip Reverse elements of an array with axis

Joining and splitting arrays

concat Concate a list of array along a given axis.
split Split an array along a particular axis into multiple sub-arrays.

Indexing routines

slice Crop a continuous region from the array.
slice_axis Slice along a given axis.
take Take elements from an array along an axis.
batch_take Take elements from a data batch.
one_hot Returns a one-hot array.

Mathematical functions

Arithmetic operations

add Add arguments, element-wise with broadcasting.
subtract Subtracts arguments element-wise with broadcasting.
negative Numerical negative, element-wise.
multiply Multiplies arguments element-wise with broadcasting.
divide Divides arguments element-wise with broadcasting.
dot Dot product of two arrays.
batch_dot Batchwise dot product.
add_n Add all input arguments element-wise.

Trigonometric functions

sin Trigonometric sine, element-wise.
cos Cosine, element-wise.
tan Tangent, element-wise.
arcsin Inverse sine, element-wise.
arccos Inverse cosine, element-wise.
arctan Inverse tangent, element-wise.
degrees Convert angles from radians to degrees.
radians Convert angles from degrees to radians.

Hyperbolic functions

sinh Hyperbolic sine, element-wise.
cosh Hyperbolic cosine, element-wise.
tanh Hyperbolic tangent element-wise.
arcsinh Inverse hyperbolic sine, element-wise.
arccosh Inverse hyperbolic cosine, element-wise.
arctanh Inverse hyperbolic tangent, element-wise.

Reduce functions

sum Compute the sum of array elements over given axes.
nansum Compute the sum of array elements over given axes with NaN ignored
prod Compute the product of array elements over given axes.
nanprod Compute the product of array elements over given axes with NaN ignored
mean Compute the mean of array elements over given axes.
max Compute the max of array elements over given axes.
min Compute the min of array elements over given axes.
norm Compute the L2 norm.

Rounding

round Round elements of the array to the nearest integer, element-wise.
rint Round elements of the array to the nearest integer, element-wise.
fix Round elements of the array to the nearest integer towards zero, element-wise.
floor Return the floor of the input, element-wise.
ceil Return the ceiling of the input, element-wise.

Exponents and logarithms

exp Calculate the exponential of the array, element-wise
expm1 Calculate exp(x) - 1
log Natural logarithm, element-wise.
log10 Calculate the base 10 logarithm of the array, element-wise.
log2 Calculate the base 2 logarithm of the array, element-wise.
log1p Calculate log(1 + x)

Powers

power First array elements raised to powers from second array, element-wise with broadcasting.
sqrt Calculate the square-root of an array, element-wise.
rsqrt Calculate the inverse square-root of an array, element-wise.
square Calculate the square of an array, element-wise.

Logic functions

equal Returns (lhs == rhs), element-wise with broadcasting.
not_equal Returns (lhs != rhs), element-wise with broadcasting.
greater Returns (lhs > rhs), element-wise with broadcasting.
greater_equal Returns (lhs >= rhs), element-wise with broadcasting.
lesser Returns (lhs < rhs), element-wise with broadcasting.
lesser_equal Returns (lhs <= rhs), element-wise with broadcasting.

Random sampling

uniform Draw samples from a uniform distribution.
normal Draw random samples from a normal (Gaussian) distribution.
mxnet.random.seed Seed the random number generators in MXNet.

Sorting and searching

sort Return a sorted copy of an array.
topk Return the top k elements in an array.
argsort Returns the indices that can sort an array.
argmax Returns the indices of the maximum values along an axis.
argmin Returns the indices of the minimum values along an axis.

Miscellaneous

maximum Element-wise maximum of array elements with broadcasting.
minimum Element-wise minimum of array elements with broadcasting.
clip Clip (limit) the values in an array, elementwise
abs Returns the absolute value of array elements, element-wise.
sign Returns the indication sign of array elements, element-wise.
gamma The gamma function (extension of the factorial function), element-wise
gammaln Log of the absolute value of the gamma function, element-wise

Neural network

Basic

FullyConnected Apply a linear transformation: \(Y = XW^T + b\).
Convolution Compute N-D convolution on (N+2)-D input.
Activation Elementwise activation function.
BatchNorm Batch normalization.
Pooling Perform pooling on the input.
SoftmaxOutput Softmax with logit loss.
softmax
param data:The input
log_softmax
param data:The input

More

Correlation Apply correlation to inputs
Deconvolution Apply deconvolution to input then add a bias.
RNN Apply a recurrent layer to input.
Embedding Map integer index to vector representations (embeddings).
LeakyReLU Leaky ReLu activation
InstanceNorm An operator taking in a n-dimensional input tensor (n > 2), and normalizing the input by subtracting the mean and variance calculated over the spatial dimensions.
L2Normalization Set the l2 norm of each instance to a constant.
LRN Apply convolution to input then add a bias.
ROIPooling Performs region-of-interest pooling on inputs.
SoftmaxActivation Apply softmax activation to input.
Dropout Apply dropout to input.
BilinearSampler Apply bilinear sampling to input feature map, which is the key of “[NIPS2015] Spatial Transformer Networks” output[batch, channel, y_dst, x_dst] = G(data[batch, channel, y_src, x_src) x_dst, y_dst enumerate all spatial locations in output x_src = grid[batch, 0, y_dst, x_dst] y_src = grid[batch, 1, y_dst, x_dst] G() denotes the bilinear interpolation kernel The out-boundary points will be padded as zeros.
GridGenerator generate sampling grid for bilinear sampling.
UpSampling Perform nearest neighboor/bilinear up sampling to inputs
SpatialTransformer Apply spatial transformer to input feature map.
LinearRegressionOutput Use linear regression for final output, this is used on final output of a net.
LogisticRegressionOutput Use Logistic regression for final output, this is used on final output of a net.
MAERegressionOutput Use mean absolute error regression for final output, this is used on final output of a net.
SVMOutput Support Vector Machine based transformation on input, backprop L2-SVM
softmax_cross_entropy Calculate cross_entropy(data, one_hot(label))
smooth_l1 Calculate Smooth L1 Loss(lhs, scalar)
IdentityAttachKLSparseReg Apply a sparse regularization to the output a sigmoid activation function.
MakeLoss Get output from a symbol and pass 1 gradient back.
BlockGrad Get output from a symbol and pass 0 gradient back
Custom Custom operator implemented in frontend.

API Reference

NDArray API of mxnet.

mxnet.ndarray.waitall()

Wait for all async operations to finish in MXNet.

This function is used for benchmarking only.

class mxnet.ndarray.NDArray(handle, writable=True)

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

reshape(shape)

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

Parameters:shape (tuple of int) – The new shape should not change the array size, namely np.prod(new_shape) should be equal to np.prod(self.shape). One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
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[:] = -1
>>> x.asnumpy()
array([[-1., -1., -1.],
       [-1., -1., -1.]], dtype=float32)
broadcast_to(shape)

Broadcasts an array to a new shape.

Broadcast only allows on axes with size 1. The new shape cannot change the number of dimensions such as from 2D to 3D.

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

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

Device context of the array.

Examples

>>> x = mx.nd.array([1, 2, 3, 4])
>>> x.context
cpu(0)
>>> type(x.context)
<class 'mxnet.context.Context'>
>>> y = mx.nd.zeros((2,3), mx.gpu(0))
>>> y.context
gpu(0)
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'>
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)
asnumpy()

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

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)

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

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

Examples

>>> x = mx.nd.zeros((2,3), dtype='float32')
>>> y = x.astype('int32')
>>> y.dtype
<type 'numpy.int32'>
copyto(other)

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

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

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

Returns:The copied array
Return type:NDArray

Examples

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

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

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
mxnet.ndarray.onehot_encode(indices, out)

One-hot encoding indices into matrix out.

Deprecated, use one_hot instead.

mxnet.ndarray.empty(shape, ctx=None, dtype=<Mock name='mock.float32' id='47721193158288'>)

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

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).
Returns:

A created array.

Return type:

NDArray

Examples

>>> mx.nd.empty(1)
<NDArray 1 @cpu(0)>
>>> mx.nd.empty((1,2), mx.gpu(0))
<NDArray 1x2 @gpu(0)>
>>> mx.nd.empty((1,2), mx.gpu(0), 'float16')
<NDArray 1x2 @gpu(0)>
mxnet.ndarray.zeros(shape, ctx=None, dtype=<Mock name='mock.float32' id='47721193158288'>)

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).
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)
mxnet.ndarray.ones(shape, ctx=None, dtype=<Mock name='mock.float32' id='47721193158288'>)

Returns a new array filled with all ones, 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. Defaults to the current default context (mxnet.Context.default_ctx).
  • dtype (str or numpy.dtype, optional) – An optional value type (default is float32).
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.full(shape, val, ctx=None, dtype=<Mock name='mock.float32' id='47721193158288'>)

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 empty array.
  • val (scalar) – Fill value
  • 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.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.array(source_array, ctx=None, dtype=None)

Creates a new array from any object exposing the array interface.

Parameters:
  • source_array (array_like) – Any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence.
  • ctx (Context, optional) – An optional device context (default is the current default context).
  • dtype (str or numpy.dtype, optional) – An optional value type. If the source_array is an NDArray, then defaults to source_array.dtype, otherwise default to float32.
Returns:

An NDArray array with the same contets as the source_array.

Return type:

NDArray

Examples

>>> import numpy as np
>>> mx.nd.array([1, 2, 3])
<NDArray 3 @cpu(0)>
>>> mx.nd.array([[1, 2], [3, 4]])
<NDArray 2x2 @cpu(0)>
>>> mx.nd.array(np.zeros((3,2)))
<NDArray 3x2 @cpu(0)>
>>> mx.nd.array(np.zeros((3,2)), mx.gpu(0))
<NDArray 3x2 @gpu(0)>
mxnet.ndarray.arange(start, stop=None, step=1.0, repeat=1, ctx=None, dtype=<Mock name='mock.float32' id='47721193158288'>)

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. For integer arguments, the function is equivalent to the built-in Python function range and to numpy.arange, but returns an NDArray.

Parameters:
  • start (int, optional) – An optional start of interval, the default value is 0.
  • stop (int) – The end of interval.
  • step (int, optional) – A optional spacing between values, the default value is 1.
  • repeat (int, optional) – The repeating time of all elements.
  • 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).
  • dtype – The value type of the NDArray, default to np.float32.
Returns:

The created NDArray

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,2).asnumpy()
array([ 2.,  4.], dtype=float32)
>>> mx.nd.arange(2,6,step=2).asnumpy()
array([ 2.,  4.], dtype=float32)
>>> mx.nd.arange(2,6,step=2,repeat=2).asnumpy()
array([ 2.,  2.,  4.,  4.], dtype=float32)
>>> mx.nd.arange(2,6,step=2,repeat=3).asnumpy()
array([ 2.,  2.,  2.,  4.,  4.,  4.], 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.add(lhs, rhs)

Add arguments, element-wise with broadcasting.

Equivalent to lhs + rhs

Parameters:
  • lhs (scalar or array) –
  • rhs (scalar or array) – The arrays to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape
Returns:

The sum of lhs and rhs, element-wise.

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+2).asnumpy()
array([[ 3.,  3.,  3.],
       [ 3.,  3.,  3.]], dtype=float32)
>>> (x+y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 2.,  2.,  2.]], dtype=float32)
>>> (z + y).asnumpy()
array([[ 0.,  1.],
       [ 1.,  2.]], dtype=float32)
mxnet.ndarray.subtract(lhs, rhs)

Subtracts arguments element-wise with broadcasting.

Equivalent to lhs - rhs.

Parameters:
  • lhs (scalar or array) –
  • rhs (scalar or array) – The arrays to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.
Returns:

The difference of lhs and rhs, element-wise.

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-2).asnumpy()
array([[-1., -1., -1.],
       [-1., -1., -1.]], dtype=float32)
>>> (x-y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> (z-y).asnumpy()
array([[ 0.,  1.],
       [-1.,  0.]], dtype=float32)
mxnet.ndarray.multiply(lhs, rhs)

Multiplies arguments element-wise with broadcasting.

Equivalent to lhs * rhs.

Parameters:
  • lhs (scalar or array) –
  • rhs (scalar or array) – The arrays to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.
Returns:

The multiplication of lhs and rhs, element-wise.

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*2).asnumpy()
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.]], dtype=float32)
>>> (x*y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (z*y).asnumpy()
array([[ 0.,  0.],
       [ 0.,  1.]], dtype=float32)
mxnet.ndarray.divide(lhs, rhs)

Divides arguments element-wise with broadcasting.

Equivalent to lhs / rhs.

Parameters:
  • lhs (scalar or array) –
  • rhs (scalar or array) – The arrays to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.
Returns:

The quotient of lhs/rhs, element-wise.

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/2
<NDArray 2x3 @cpu(0)>
>>> (x/2).asnumpy()
array([[ 0.5,  0.5,  0.5],
       [ 0.5,  0.5,  0.5]], dtype=float32)
>>> (x/y).asnumpy()
array([[ inf,  inf,  inf],
       [  1.,   1.,   1.]], dtype=float32)
>>> (y/z).asnumpy()
array([[ nan,   0.],
       [ inf,   1.]], dtype=float32)
mxnet.ndarray.power(base, exp)

First array elements raised to powers from second array, element-wise with broadcasting.

Equivalent to base ** exp.

Parameters:
  • base (scalar or NDArray) –
  • exp (scalar or NDArray) – The arrays to be added. 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**2).asnumpy()
array([[ 4.,  4.,  4.],
       [ 4.,  4.,  4.]], dtype=float32)
>>> (x**y).asnumpy()
array([[ 2.,  2.,  2.],
       [ 4.,  4.,  4.]], dtype=float32)
>>> (z**y).asnumpy()
array([[ 1.],
       [ 4.]], dtype=float32)
mxnet.ndarray.maximum(lhs, rhs)

Element-wise maximum of array elements with broadcasting.

Parameters:
  • lhs (scalar or array) –
  • rhs (scalar or array) – The arrays to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.
Returns:

The maximum of lhs and rhs, element-wise.

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))
>>> 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.minimum(lhs, rhs)

Element-wise minimum of array elements with broadcasting.

Parameters:
  • lhs (scalar or array) –
  • rhs (scalar or array) – The arrays to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.
Returns:

The minimum of lhs and rhs, element-wise.

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))
>>> 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.equal(lhs, rhs)

Returns (lhs == rhs), element-wise with broadcasting.

Equivalent to lhs == rhs

Parameters:
  • lhs (scalar or array) –
  • rhs (scalar or array) – The arrays to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.
Returns:

For each element in lhs, rhs, return True if lhs is equal to rhs and False otherwise.

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 == 1).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (x == y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (z == y).asnumpy()
array([[ 1.,  0.],
       [ 0.,  1.]], dtype=float32)
mxnet.ndarray.not_equal(lhs, rhs)

Returns (lhs != rhs), element-wise with broadcasting.

Equivalent to lhs != rhs.

Parameters:
  • lhs (scalar or array) –
  • rhs (scalar or array) – The arrays to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape,
Returns:

For each element in lhs, rhs, return True if lhs is not equal to rhs and False otherwise.

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))
>>> (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)
>>> (z != y).asnumpy()
array([[ 0.,  1.],
       [ 1.,  0.]], dtype=float32)
mxnet.ndarray.greater(lhs, rhs)

Returns (lhs > rhs), element-wise with broadcasting.

Equivalent to lhs > rhs.

Parameters:
  • lhs (scalar or array) –
  • rhs (scalar or array) – The arrays to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.
Returns:

For each element in lhs, rhs, return True if lhs is greater than rhs and False otherwise.

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 > 1).asnumpy()
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> (x > y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> (z > y).asnumpy()
array([[ 0.,  1.],
       [ 0.,  0.]], dtype=float32)
mxnet.ndarray.greater_equal(lhs, rhs)

Returns (lhs >= rhs), element-wise with broadcasting.

Equivalent to lhs >= rhs.

Parameters:
  • lhs (scalar or array) –
  • rhs (scalar or array) – The arrays to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.
Returns:

For each element in lhs, rhs, return True if lhs is greater equal than rhs and False otherwise.

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 >= 1).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (x >= y).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (z >= y).asnumpy()
array([[ 1.,  1.],
       [ 0.,  1.]], dtype=float32)
mxnet.ndarray.lesser(lhs, rhs)

Returns (lhs < rhs), element-wise with broadcasting.

Equivalent to lhs < rhs.

Parameters:
  • lhs (scalar or array) –
  • rhs (scalar or array) – The arrays to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.
Returns:

For each element in lhs, rhs, return True if lhs is lesser than rhs and False otherwise.

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 < 1).asnumpy()
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> (x < y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> (z < y).asnumpy()
array([[ 0.,  0.],
       [ 1.,  0.]], dtype=float32)
mxnet.ndarray.lesser_equal(lhs, rhs)

Returns (lhs <= rhs), element-wise with broadcasting.

Equivalent to lhs <= rhs.

Parameters:
  • lhs (scalar or array) –
  • rhs (scalar or array) – The arrays to be added. If lhs.shape != rhs.shape, they must be broadcastable to a common shape.
Returns:

For each element in lhs, rhs, return True if lhs is lesser equal than rhs and False otherwise.

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 <= 1).asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (x <= y).asnumpy()
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> (z <= y).asnumpy()
array([[ 1.,  0.],
       [ 1.,  1.]], dtype=float32)
mxnet.ndarray.true_divide(lhs, rhs)

Same as divide.

mxnet.ndarray.negative(arr)

Numerical negative, element-wise.

Equals -arr

Parameters:arr (NDArray) – The input array
Returns:-arr
Return type:NDArray

Examples

>>> x = mx.nd.ones((2,3))
>>> (-x).asnumpy()
array([[-1., -1., -1.],
       [-1., -1., -1.]], dtype=float32)
mxnet.ndarray.load(fname)

Load array from file.

See more details in save.

Parameters:fname (str) – The filename.
Returns:Loaded data.
Return type:list of NDArray or dict of str to NDArray
mxnet.ndarray.save(fname, data)

Save a list of arrays of a str->array dict into file.

Examples of filenames:

  • /path/to/file
  • s3://my-bucket/path/to/file (if compiled with AWS S3 supports)
  • hdfs://path/to/file (if compiled with HDFS supports)
Parameters:
  • fname (str) – The filename.
  • data (list of NDArray` or dict of str to ``NDArray) – The data for saving.

Examples

>>> x = mx.nd.zeros((2,3))
>>> y = mx.nd.ones((1,4))
>>> mx.nd.save('my_list', [x,y])
>>> mx.nd.save('my_dict', {'x':x, 'y':y})
>>> mx.nd.load('my_list')
[<NDArray 2x3 @cpu(0)>, <NDArray 1x4 @cpu(0)>]
>>> mx.nd.load('my_dict')
{'y': <NDArray 1x4 @cpu(0)>, 'x': <NDArray 2x3 @cpu(0)>}
mxnet.ndarray.concatenate(arrays, axis=0, always_copy=True)

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.imdecode(str_img, clip_rect=(0, 0, 0, 0), out=None, index=0, channels=3, mean=None)

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.Activation(*args, **kwargs)

Elementwise activation function. The activation operations are applied elementwisely to each array elements. The following types are supported:

  • relu: Rectified Linear Unit, y = max(x, 0)
  • sigmoid: y = 1 / (1 + exp(-x))
  • tanh: Hyperbolic tangent, y = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
  • softrelu: Soft ReLU, or SoftPlus, y = log(1 + exp(x))

Defined in src/operator/activation.cc:L76

Parameters:
  • data (NDArray) – Input data to activation function.
  • act_type ({'relu', 'sigmoid', 'softrelu', 'tanh'}, required) – Activation function to be applied.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.BatchNorm(*args, **kwargs)

Batch normalization.

Normalizes a data batch by mean and variance, and applies a scale gamma as well as offset beta.

Assume the input has more than one dimension and we normalize along axis 1. We first compute the mean and variance along this axis:

\[\begin{split}data\_mean[i] = mean(data[:,i,:,...]) \\ data\_var[i] = var(data[:,i,:,...])\end{split}\]

Then compute the normalized output, which has the same shape as input, as following:

\[out[:,i,:,...] = \frac{data[:,i,:,...] - data\_mean[i]}{\sqrt{data\_var[i]+\epsilon}} * gamma[i] + beta[i]\]

Both mean and var returns a scalar by treating the input as a vector.

Assume the input has size k on axis 1, then both gamma and beta have shape (k,). If output_mean_var is set to be true, then outputs both data_mean and data_var as well, which are needed for the backward pass.

Besides the inputs and the outputs, this operator accepts two auxiliary states, moving_mean and moving_var, which are k-length vectors. They are global statistics for the whole dataset, which are updated by:

moving_mean = moving_mean * momentum + data_mean * (1 - momentum)
moving_var = moving_var * momentum + data_var * (1 - momentum)

If use_global_stats is set to be true, then moving_mean and moving_var are used instead of data_mean and data_var to compute the output. It is often used during inference.

Both gamma and beta are learnable parameters. But if fix_gamma is true, then set gamma to 1 and its gradient to 0.

Defined in src/operator/batch_norm.cc:L79

Parameters:
  • data (NDArray) – Input data to batch normalization
  • gamma (NDArray) – gamma array
  • beta (NDArray) – beta array
  • eps (float, optional, default=0.001) – Epsilon to prevent div 0
  • momentum (float, optional, default=0.9) – Momentum for moving average
  • fix_gamma (boolean, optional, default=True) – Fix gamma while training
  • use_global_stats (boolean, optional, default=False) – Whether use global moving statistics instead of local batch-norm. This will force change batch-norm into a scale shift operator.
  • output_mean_var (boolean, optional, default=False) – Output All,normal mean and var
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.BilinearSampler(*args, **kwargs)
Apply bilinear sampling to input feature map, which is the key of “[NIPS2015] Spatial Transformer Networks”
output[batch, channel, y_dst, x_dst] = G(data[batch, channel, y_src, x_src) x_dst, y_dst enumerate all spatial locations in output x_src = grid[batch, 0, y_dst, x_dst] y_src = grid[batch, 1, y_dst, x_dst] G() denotes the bilinear interpolation kernel

The out-boundary points will be padded as zeros. (The boundary is defined to be [-1, 1]) The shape of output will be (data.shape[0], data.shape[1], grid.shape[2], grid.shape[3]) The operator assumes that grid has been nomalized. If you want to design a CustomOp to manipulate grid, please refer to GridGeneratorOp.

Parameters:
  • data (Symbol) – Input data to the BilinearsamplerOp.
  • grid (Symbol) – Input grid to the BilinearsamplerOp.grid has two channels: x_src, y_src
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.BlockGrad(*args, **kwargs)

Get output from a symbol and pass 0 gradient back

From:src/operator/tensor/elemwise_unary_op.cc:32

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Cast(*args, **kwargs)

Cast to a specified type, element-wise.

For example:

cast([1e20, 11.1], dtype='float16') = [inf, 11.09375]
cast([300, 11.1, 10.9, -1, -3], dtype='uint8') = [44, 11, 10, 255, 253]

Defined in src/operator/tensor/elemwise_unary_op.cc:L86

Parameters:
  • data (NDArray) – Source input
  • dtype ({'float16', 'float32', 'float64', 'int32', 'uint8'}, required) – Output data type.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Concat(*args, **kwargs)

Concate a list of array along a given axis.

The dimension sizes of the input arrays on the given axis should be the same.

For example:

x = [[1,1],[1,1]]
y = [[2,2],[2,2]]
z = [[3,3],[3,3],[3,3]]

Concat(x,y,z,dim=0) = [[ 1.,  1.],
                       [ 1.,  1.],
                       [ 2.,  2.],
                       [ 2.,  2.],
                       [ 3.,  3.],
                       [ 3.,  3.],
                       [ 3.,  3.]]

Concat(x,y,z,dim=1) = [[ 1.,  1.,  2.,  2.],
                       [ 1.,  1.,  2.,  2.]]

Defined in src/operator/concat.cc:L70

Parameters:
  • data (NDArray[]) – List of tensors to concatenate
  • dim (int, optional, default='1') – the dimension to be concated.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Convolution(*args, **kwargs)

Compute N-D convolution on (N+2)-D input.

In the simplest 2-D convolution, given input data with shape (batch_size, channel, height, weight), the output is computed by

\[out[n,i,:,:] = bias[i] + \sum_{j=0}^{num\_filter} data[n,j,:,:] \star weight[i,j,:,:]\]

where \(\star\) is the 2-D cross-correlation operator.

For general 2-D convolution, the shapes are

  • data: (batch_size, channel, height, weight)
  • weight: (num_filter, channel, kernel[0], kernel[1])
  • bias: (num_filter,)
  • out: (batch_size, num_filter, out_height, out_weight).

Define:

f(x,k,p,s,d) = floor((x+2*p-d*(k-1)-1)/s)+1

then we have:

out_height=f(height, kernel[0], pad[0], stride[0], dilate[0])
out_weight=f(weight, kernel[1], pad[1], stride[1], dilate[1])

If no_bias is set to be true, then the bias term is ignored.

The default data layout is NCHW, namely (batch_size, channle, height, weight). We can choose other layouts such as NHWC.

If num_group is larger than 1, denoted by g, then split the input data evenly into g parts along the channel axis, and also evenly split weight along the first dimension. Next compute the convolution on the i-th part of the data with the i-th weight part. The output is obtained by concating all the g results.

To perform 1-D convolution, simply use 2-D convolution but set the last axis size to be 1 for both data and weight.

3-D convolution adds an additional depth dimension besides height and weight. The shapes are

  • data: (batch_size, channel, depth, height, weight)
  • weight: (num_filter, channel, kernel[0], kernel[1], kernel[2])
  • bias: (num_filter,)
  • out: (batch_size, num_filter, out_depth, out_height, out_weight).

Both weight and bias are learnable parameters.

There are other options to tune the performance.

  • cudnn_tune: enable this option leads to higher startup time but may give faster speed. Options are
    • off: no tuning
    • limited_workspace:run test and pick the fastest algorithm that doesn’t exceed workspace limit.
    • fastest: pick the fastest algorithm and ignore workspace limit.
    • None (default): the behavior is determined by environment variable MXNET_CUDNN_AUTOTUNE_DEFAULT. 0 for off, 1 for limited workspace (default), 2 for fastest.
  • workspace: A large number leads to more (GPU) memory usage but may improve the performance.

Defined in src/operator/convolution.cc:L150

Parameters:
  • data (NDArray) – Input data to the ConvolutionOp.
  • weight (NDArray) – Weight matrix.
  • bias (NDArray) – Bias parameter.
  • kernel (Shape(tuple), required) – convolution kernel size: (h, w) or (d, h, w)
  • stride (Shape(tuple), optional, default=()) – convolution stride: (h, w) or (d, h, w)
  • dilate (Shape(tuple), optional, default=()) – convolution dilate: (h, w) or (d, h, w)
  • pad (Shape(tuple), optional, default=()) – pad for convolution: (h, w) or (d, h, w)
  • num_filter (int (non-negative), required) – convolution filter(channel) number
  • num_group (int (non-negative), optional, default=1) – Number of group partitions.
  • workspace (long (non-negative), optional, default=1024) – Maximum temperal workspace allowed for convolution (MB).
  • no_bias (boolean, optional, default=False) – Whether to disable bias parameter.
  • cudnn_tune ({None, 'fastest', 'limited_workspace', 'off'},optional, default='None') – Whether to pick convolution algo by running performance test.
  • cudnn_off (boolean, optional, default=False) – Turn off cudnn for this layer.
  • layout ({None, 'NCDHW', 'NCHW', 'NCW', 'NDHWC', 'NHWC'},optional, default='None') – Set layout for input, output and weight. Empty for default layout: NCW for 1d, NCHW for 2d and NCDHW for 3d.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Convolution_v1(*args, **kwargs)

Apply convolution to input then add a bias.

Parameters:
  • data (Symbol) – Input data to the ConvolutionV1Op.
  • weight (Symbol) – Weight matrix.
  • bias (Symbol) – Bias parameter.
  • kernel (Shape(tuple), required) – convolution kernel size: (h, w) or (d, h, w)
  • stride (Shape(tuple), optional, default=()) – convolution stride: (h, w) or (d, h, w)
  • dilate (Shape(tuple), optional, default=()) – convolution dilate: (h, w) or (d, h, w)
  • pad (Shape(tuple), optional, default=()) – pad for convolution: (h, w) or (d, h, w)
  • num_filter (int (non-negative), required) – convolution filter(channel) number
  • num_group (int (non-negative), optional, default=1) – Number of group partitions. Equivalent to slicing input into num_group partitions, apply convolution on each, then concatenate the results
  • workspace (long (non-negative), optional, default=1024) – Maximum tmp workspace allowed for convolution (MB).
  • no_bias (boolean, optional, default=False) – Whether to disable bias parameter.
  • cudnn_tune ({None, 'fastest', 'limited_workspace', 'off'},optional, default='None') – Whether to pick convolution algo by running performance test. Leads to higher startup time but may give faster speed. Options are: ‘off’: no tuning ‘limited_workspace’: run test and pick the fastest algorithm that doesn’t exceed workspace limit. ‘fastest’: pick the fastest algorithm and ignore workspace limit. If set to None (default), behavior is determined by environment variable MXNET_CUDNN_AUTOTUNE_DEFAULT: 0 for off, 1 for limited workspace (default), 2 for fastest.
  • cudnn_off (boolean, optional, default=False) – Turn off cudnn for this layer.
  • layout ({None, 'NCDHW', 'NCHW', 'NDHWC', 'NHWC'},optional, default='None') – Set layout for input, output and weight. Empty for default layout: NCHW for 2d and NCDHW for 3d.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Correlation(*args, **kwargs)

Apply correlation to inputs

Parameters:
  • data1 (Symbol) – Input data1 to the correlation.
  • data2 (Symbol) – Input data2 to the correlation.
  • kernel_size (int (non-negative), optional, default=1) – kernel size for Correlation must be an odd number
  • max_displacement (int (non-negative), optional, default=1) – Max displacement of Correlation
  • stride1 (int (non-negative), optional, default=1) – stride1 quantize data1 globally
  • stride2 (int (non-negative), optional, default=1) – stride2 quantize data2 within the neighborhood centered around data1
  • pad_size (int (non-negative), optional, default=0) – pad for Correlation
  • is_multiply (boolean, optional, default=True) – operation type is either multiplication or subduction
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Crop(*args, **kwargs)

Crop the 2nd and 3rd dim of input data, with the corresponding size of h_w or with width and height of the second input symbol, i.e., with one input, we need h_w to specify the crop height and width, otherwise the second input symbol’s size will be used

Parameters:
  • data (Symbol or Symbol[]) – Tensor or List of Tensors, the second input will be used as crop_like shape reference
  • offset (Shape(tuple), optional, default=(0,0)) – crop offset coordinate: (y, x)
  • h_w (Shape(tuple), optional, default=(0,0)) – crop height and weight: (h, w)
  • center_crop (boolean, optional, default=False) – If set to true, then it will use be the center_crop,or it will crop using the shape of crop_like
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Custom(*args, **kwargs)

Custom operator implemented in frontend.

Parameters:
  • op_type (string) – Type of custom operator. Must be registered first.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Deconvolution(*args, **kwargs)

Apply deconvolution to input then add a bias.

Parameters:
  • data (Symbol) – Input data to the DeconvolutionOp.
  • weight (Symbol) – Weight matrix.
  • bias (Symbol) – Bias parameter.
  • kernel (Shape(tuple), required) – deconvolution kernel size: (y, x)
  • stride (Shape(tuple), optional, default=(1,1)) – deconvolution stride: (y, x)
  • pad (Shape(tuple), optional, default=(0,0)) – pad for deconvolution: (y, x), a good number is : (kernel-1)/2, if target_shape set, pad will be ignored and will be computed automatically
  • adj (Shape(tuple), optional, default=(0,0)) – adjustment for output shape: (y, x), if target_shape set, adj will be ignored and will be computed automatically
  • target_shape (Shape(tuple), optional, default=(0,0)) – output shape with targe shape : (y, x)
  • num_filter (int (non-negative), required) – deconvolution filter(channel) number
  • num_group (int (non-negative), optional, default=1) – number of groups partition
  • workspace (long (non-negative), optional, default=512) – Tmp workspace for deconvolution (MB)
  • no_bias (boolean, optional, default=True) – Whether to disable bias parameter.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Dropout(*args, **kwargs)

Apply dropout to input. During training, each element of the input is randomly set to zero with probability p. And then the whole tensor is rescaled by 1/(1-p) to keep the expectation the same as before applying dropout. During the test time, this behaves as an identity map.

Parameters:
  • data (Symbol) – Input data to dropout.
  • p (float, optional, default=0.5) – Fraction of the input that gets dropped out at training time
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.ElementWiseSum(*args, **kwargs)

Add all input arguments element-wise.

\[add\_n(a_1, a_2, ..., a_n) = a_1 + a_2 + ... + a_n\]

add_n is potentially more efficient than calling add by n times.

Defined in src/operator/tensor/elemwise_sum.cc:L63

Parameters:
  • args (NDArray[]) – Positional input arguments
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Embedding(*args, **kwargs)

Map integer index to vector representations (embeddings). Those embeddings are learnable parameters. For a input of shape (d1, ..., dK), the output shape is (d1, ..., dK, output_dim). All the input values should be integers in the range [0, input_dim).

From:src/operator/tensor/indexing_op.cc:19

Parameters:
  • data (Symbol) – Input data to the EmbeddingOp.
  • weight (Symbol) – Embedding weight matrix.
  • input_dim (int, required) – vocabulary size of the input indices.
  • output_dim (int, required) – dimension of the embedding vectors.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Flatten(*args, **kwargs)

Flatten input into a 2-D array by collapsing the higher dimensions.

Assume the input array has shape (d1, d2, ..., dk), then flatten reshapes the input array into shape (d1, d2*...*dk).

Defined in src/operator/tensor/matrix_op.cc:L101

Parameters:
  • data (NDArray) – Input data to reshape.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.FullyConnected(*args, **kwargs)

Apply a linear transformation: \(Y = XW^T + b\).

Shapes:

  • data: (batch_size, input_dim)
  • weight: (num_hidden, input_dim)
  • bias: (num_hidden,)
  • out: (batch_size, num_hidden)

The learnable parameters include both weight and bias.

If no_bias is set to be true, then the bias term is ignored.

Defined in src/operator/fully_connected.cc:L90

Parameters:
  • data (NDArray) – Input data.
  • weight (NDArray) – Weight matrix.
  • bias (NDArray) – Bias parameter.
  • num_hidden (int, required) – Number of hidden nodes of the output.
  • no_bias (boolean, optional, default=False) – Whether to disable bias parameter.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.GridGenerator(*args, **kwargs)

generate sampling grid for bilinear sampling.

Parameters:
  • data (Symbol) – Input data to the GridGeneratorOp.
  • transform_type ({'affine', 'warp'}, required) – transformation type if transformation type is affine, data is affine matrix : (batch, 6) if transformation type is warp, data is optical flow : (batch, 2, h, w)
  • target_shape (Shape(tuple), optional, default=(0,0)) – if transformation type is affine, the operator need a target_shape : (H, W) if transofrmation type is warp, the operator will ignore target_shape
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.IdentityAttachKLSparseReg(*args, **kwargs)

Apply a sparse regularization to the output a sigmoid activation function.

Parameters:
  • data (Symbol) – Input data.
  • sparseness_target (float, optional, default=0.1) – The sparseness target
  • penalty (float, optional, default=0.001) – The tradeoff parameter for the sparseness penalty
  • momentum (float, optional, default=0.9) – The momentum for running average
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.InstanceNorm(*args, **kwargs)

An operator taking in a n-dimensional input tensor (n > 2), and normalizing the input by subtracting the mean and variance calculated over the spatial dimensions. This is an implemention of the operator described in “Instance Normalization: The Missing Ingredient for Fast Stylization”, D. Ulyanov, A. Vedaldi, V. Lempitsky, 2016 (arXiv:1607.08022v2). This layer is similar to batch normalization, with two differences: first, the normalization is carried out per example (‘instance’), not over a batch. Second, the same normalization is applied both at test and train time. This operation is also known as ‘contrast normalization’.

Parameters:
  • data (Symbol) – A n-dimensional tensor (n > 2) of the form [batch, channel, spatial_dim1, spatial_dim2, ...].
  • gamma (Symbol) – A vector of length ‘channel’, which multiplies the normalized input.
  • beta (Symbol) – A vector of length ‘channel’, which is added to the product of the normalized input and the weight.
  • eps (float, optional, default=0.001) – Epsilon to prevent division by 0.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.L2Normalization(*args, **kwargs)

Set the l2 norm of each instance to a constant.

Parameters:
  • data (Symbol) – Input data to the L2NormalizationOp.
  • eps (float, optional, default=1e-10) – Epsilon to prevent div 0
  • mode ({'channel', 'instance', 'spatial'},optional, default='instance') – Normalization Mode. If set to instance, this operator will compute a norm for each instance in the batch; this is the default mode. If set to channel, this operator will compute a cross channel norm at each position of each instance. If set to spatial, this operator will compute a norm for each channel.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.LRN(*args, **kwargs)

Apply convolution to input then add a bias.

Parameters:
  • data (Symbol) – Input data to the ConvolutionOp.
  • alpha (float, optional, default=0.0001) – value of the alpha variance scaling parameter in the normalization formula
  • beta (float, optional, default=0.75) – value of the beta power parameter in the normalization formula
  • knorm (float, optional, default=2) – value of the k parameter in normalization formula
  • nsize (int (non-negative), required) – normalization window width in elements.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.LeakyReLU(*args, **kwargs)

Leaky ReLu activation

The following types are supported:

  • elu: y = x > 0 ? x : slop * (exp(x)-1)
  • leaky: y = x > 0 ? x : slope * x
  • prelu: same as leaky but the slope is learnable.
  • rrelu: same as leaky but the slope is uniformly randomly chosen from [lower_bound, upper_bound) for training, while fixed to be (lower_bound+upper_bound)/2 for inference.

Defined in src/operator/leaky_relu.cc:L36

Parameters:
  • data (NDArray) – Input data to activation function.
  • act_type ({'elu', 'leaky', 'prelu', 'rrelu'},optional, default='leaky') – Activation function to be applied.
  • slope (float, optional, default=0.25) – Init slope for the activation. (For leaky and elu only)
  • lower_bound (float, optional, default=0.125) – Lower bound of random slope. (For rrelu only)
  • upper_bound (float, optional, default=0.334) – Upper bound of random slope. (For rrelu only)
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.LinearRegressionOutput(*args, **kwargs)

Use linear regression for final output, this is used on final output of a net.

Parameters:
  • data (Symbol) – Input data to function.
  • label (Symbol) – Input label to function.
  • grad_scale (float, optional, default=1) – Scale the gradient by a float factor
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.LogisticRegressionOutput(*args, **kwargs)

Use Logistic regression for final output, this is used on final output of a net. Logistic regression is suitable for binary classification or probability prediction tasks.

Parameters:
  • data (Symbol) – Input data to function.
  • label (Symbol) – Input label to function.
  • grad_scale (float, optional, default=1) – Scale the gradient by a float factor
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.MAERegressionOutput(*args, **kwargs)

Use mean absolute error regression for final output, this is used on final output of a net.

Parameters:
  • data (Symbol) – Input data to function.
  • label (Symbol) – Input label to function.
  • grad_scale (float, optional, default=1) – Scale the gradient by a float factor
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.MakeLoss(*args, **kwargs)

Get output from a symbol and pass 1 gradient back. This is used as a terminal loss if unary and binary operator are used to composite a loss with no declaration of backward dependency

Parameters:
  • data (Symbol) – Input data.
  • grad_scale (float, optional, default=1) – gradient scale as a supplement to unary and binary operators
  • valid_thresh (float, optional, default=0) – regard element valid when x > valid_thresh, this is used only in valid normalization mode.
  • normalization ({'batch', 'null', 'valid'},optional, default='null') – If set to null, op will not normalize on output gradient.If set to batch, op will normalize gradient by divide batch size.If set to valid, op will normalize gradient by divide # sample marked as valid
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Pad(*args, **kwargs)

Pad an array.

Only supports 4-D and 5-D input array.

Defined in src/operator/pad.cc:L407

Parameters:
  • data (NDArray) – An n-dimensional input tensor.
  • mode ({'constant', 'edge'}, required) – Padding type to use. “constant” pads all values with a constant value, the value of which can be specified with the constant_value option. “edge” uses the boundary values of the array as padding.
  • pad_width (Shape(tuple), required) – A tuple of padding widths of length 2*r, where r is the rank of the input tensor, specifying number of values padded to the edges of each axis. (before_1, after_1, ... , before_N, after_N) unique pad widths for each axis. Equivalent to pad_width in numpy.pad, but flattened.
  • constant_value (double, optional, default=0) – This option is only used when mode is “constant”. This value will be used as the padding value. Defaults to 0 if not specified.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Pooling(*args, **kwargs)

Perform pooling on the input.

The shapes for 1-D pooling are - data: (batch_size, channel, width), - out: (batch_size, num_filter, out_width).

The shapes for 2-D pooling is

  • data: (batch_size, channel, height, width)

  • out: (batch_size, num_filter, out_height, out_width), with:

    out_height = f(height, kernel[0], pad[0], stride[0])
    out_width = f(width, kernel[1], pad[1], stride[1])
    

The defintion of f depends on pooling_convention, which has two options:

  • valid (default):

    f(x, k, p, s) = floor(x+2*p-k)/s+1
    
  • full, which is compatible with Caffe:

    f(x, k, p, s) = ceil(x+2*p-k)/s+1
    

But global_pool is set to be true, then do a global pooling, namely reset kernel=(height, width).

Three pooling options are supported by pool_type:

  • avg: average pooling
  • max: max pooling
  • sum: sum pooling

1-D pooling is special case of 2-D pooling with width=1 and kernel[1]=1.

For 3-D pooling, an additional depth dimension is added before height. Namely the input data will have shape (batch_size, channel, depth, height, width).

Defined in src/operator/pooling.cc:L126

Parameters:
  • data (NDArray) – Input data to the pooling operator.
  • global_pool (boolean, optional, default=False) – Ignore kernel size, do global pooling based on current input feature map.
  • cudnn_off (boolean, optional, default=False) – Turn off cudnn pooling and use MXNet pooling operator.
  • kernel (Shape(tuple), required) – pooling kernel size: (y, x) or (d, y, x)
  • pool_type ({'avg', 'max', 'sum'}, required) – Pooling type to be applied.
  • pooling_convention ({'full', 'valid'},optional, default='valid') – Pooling convention to be applied.
  • stride (Shape(tuple), optional, default=()) – stride: for pooling (y, x) or (d, y, x)
  • pad (Shape(tuple), optional, default=()) – pad for pooling: (y, x) or (d, y, x)
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Pooling_v1(*args, **kwargs)

Perform pooling on the input.

The shapes for 2-D pooling is

  • data: (batch_size, channel, height, width)

  • out: (batch_size, num_filter, out_height, out_width), with:

    out_height = f(height, kernel[0], pad[0], stride[0])
    out_width = f(width, kernel[1], pad[1], stride[1])
    

The defintion of f depends on pooling_convention, which has two options:

  • valid (default):

    f(x, k, p, s) = floor(x+2*p-k)/s+1
    
  • full, which is compatible with Caffe:

    f(x, k, p, s) = ceil(x+2*p-k)/s+1
    

But global_pool is set to be true, then do a global pooling, namely reset kernel=(height, width).

Three pooling options are supported by pool_type:

  • avg: average pooling
  • max: max pooling
  • sum: sum pooling

1-D pooling is special case of 2-D pooling with weight=1 and kernel[1]=1.

For 3-D pooling, an additional depth dimension is added before height. Namely the input data will have shape (batch_size, channel, depth, height, width).

Defined in src/operator/pooling_v1.cc:L84

Parameters:
  • data (NDArray) – Input data to the pooling operator.
  • global_pool (boolean, optional, default=False) – Ignore kernel size, do global pooling based on current input feature map.
  • kernel (Shape(tuple), required) – pooling kernel size: (y, x) or (d, y, x)
  • pool_type ({'avg', 'max', 'sum'}, required) – Pooling type to be applied.
  • pooling_convention ({'full', 'valid'},optional, default='valid') – Pooling convention to be applied.
  • stride (Shape(tuple), optional, default=()) – stride: for pooling (y, x) or (d, y, x)
  • pad (Shape(tuple), optional, default=()) – pad for pooling: (y, x) or (d, y, x)
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.RNN(*args, **kwargs)

Apply a recurrent layer to input.

Parameters:
  • data (Symbol) – Input data to RNN
  • parameters (Symbol) – Vector of all RNN trainable parameters concatenated
  • state (Symbol) – initial hidden state of the RNN
  • state_cell (Symbol) – initial cell state for LSTM networks (only for LSTM)
  • state_size (int (non-negative), required) – size of the state for each layer
  • num_layers (int (non-negative), required) – number of stacked layers
  • bidirectional (boolean, optional, default=False) – whether to use bidirectional recurrent layers
  • mode ({'gru', 'lstm', 'rnn_relu', 'rnn_tanh'}, required) – the type of RNN to compute
  • p (float, optional, default=0) – Dropout probability, fraction of the input that gets dropped out at training time
  • state_outputs (boolean, optional, default=False) – Whether to have the states as symbol outputs.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.ROIPooling(*args, **kwargs)

Performs region-of-interest pooling on inputs. Resize bounding box coordinates by spatial_scale and crop input feature maps accordingly. The cropped feature maps are pooled by max pooling to a fixed size output indicated by pooled_size. batch_size will change to the number of region bounding boxes after ROIPooling

Parameters:
  • data (Symbol) – Input data to the pooling operator, a 4D Feature maps
  • rois (Symbol) – Bounding box coordinates, a 2D array of [[batch_index, x1, y1, x2, y2]]. (x1, y1) and (x2, y2) are top left and down right corners of designated region of interest. batch_index indicates the index of corresponding image in the input data
  • pooled_size (Shape(tuple), required) – fix pooled size: (h, w)
  • spatial_scale (float, required) – Ratio of input feature map height (or w) to raw image height (or w). Equals the reciprocal of total stride in convolutional layers
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Reshape(*args, **kwargs)

Reshape array into a new shape.

The shape is a tuple of int such as (2,3,4). The new shape should not change the array size. For example:

reshape([1,2,3,4], shape=(2,2)) = [[1,2], [3,4]]

In addition, we can use special codes, which are integers less than 1, on some shape dimensions. To inference the output shape, we set it to an empty tuple at beginning. When continuously pop dimensions from the original shape starting from the beginning, and then push translated results into the output shape.

Each special code presents a way of translation.

  • 0 for copying one. Pop one input dimension and push into the output. For example:

    - input=(2,3,4), shape=(4,0,2), output=(4,3,2)
    - input=(2,3,4), shape=(2,0,0), output=(2,3,4)
    
  • -1 for inference. Push a placeholder into the output whose value will be inferred later:

    - input=(2,3,4), shape=(6,1,-1), output=(6,1,4)
    - input=(2,3,4), shape=(3,-1,8), output=(3,1,8)
    - input=(2,3,4), shape=(-1,), output=(24,)
    
  • -2 for copying all. Pop all remaining input dimensions and push them into the output:

    - input=(2,3,4), shape=(-2), output=(9,8,7)
    - input=(2,3,4), shape=(2,-2), output=(2,3,4)
    - input=(2,3,4), shape=(-2,1,1), output=(2,3,4,1,1)
    
  • -3 for merging two dimensions. Pop two input dimensions, compute the product and then push into the output:

    - input=(2,3,4), shape=(-3,4), output=(6,4)
    - input=(2,3,4), shape=(0,-3), output=(2,12)
    - input=(2,3,4), shape=(-3,-2), output=(6,4)
    
  • -4 for splitting two dimensions. Pop one input dimensions, next split it according to the next two dimensions (can contain one -1) specified after this code, then push into the output:

    - input=(2,3,4), shape=(-4,1,2,-2), output=(1,2,3,4)
    - input=(2,3,4), shape=(2,-4,-1,3,-2), output=(2,1,3,4)
    

If the argument reverse is set to be true, then translating the input shape from right to left. For example, with input shape (10, 5, 4) target shape (-1, 0), then the output shape will be (50,4) if reverse=1, otherwise it will be (40,5).

Defined in src/operator/tensor/matrix_op.cc:L78

Parameters:
  • data (NDArray) – Input data to reshape.
  • shape (Shape(tuple), optional, default=()) – The target shape
  • keep_highest (boolean, optional, default=False) – (Deprecated! Use shape instead.) Whether keep the highest dim unchanged.If set to true, then the first dim in target_shape is ignored,and always fixed as input
  • reverse (boolean, optional, default=False) – If true then translating the input shape from right to left
  • target_shape (Shape(tuple), optional, default=(0,0)) – (Deprecated! Use shape instead.) Target new shape. One and only one dim can be 0, in which case it will be inferred from the rest of dims
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.SVMOutput(*args, **kwargs)

Support Vector Machine based transformation on input, backprop L2-SVM

Parameters:
  • data (Symbol) – Input data to svm.
  • label (Symbol) – Label data.
  • margin (float, optional, default=1) – Scale the DType(param_.margin) for activation size
  • regularization_coefficient (float, optional, default=1) – Scale the coefficient responsible for balacing coefficient size and error tradeoff
  • use_linear (boolean, optional, default=False) – If set true, uses L1-SVM objective function. Default uses L2-SVM objective
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.SequenceLast(*args, **kwargs)

Takes the last element of a sequence. Takes an n-dimensional tensor of the form [max sequence length, batchsize, other dims] and returns a (n-1)-dimensional tensor of the form [batchsize, other dims]. This operator takes an optional input tensor sequence_length of positive ints of dimension [batchsize] when the sequence_length option is set to true. This allows the operator to handle variable-length sequences. If sequence_length is false, then each example in the batch is assumed to have the max sequence length.

Parameters:
  • data (Symbol) – n-dimensional input tensor of the form [max sequence length, batchsize, other dims]
  • sequence_length (Symbol) – vector of sequence lengths of size batchsize
  • use_sequence_length (boolean, optional, default=False) – If set to true, this layer takes in extra input sequence_length to specify variable length sequence
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.SequenceMask(*args, **kwargs)

Sets all elements outside the sequence to a constant value. Takes an n-dimensional tensor of the form [max sequence length, batchsize, other dims] and returns a tensor of the same shape. This operator takes an optional input tensor sequence_length of positive ints of dimension [batchsize] when the sequence_length option is set to true. This allows the operator to handle variable-length sequences. If sequence_length is false, then each example in the batch is assumed to have the max sequence length, and this operator becomes the identity operator.

Parameters:
  • data (Symbol) – n-dimensional input tensor of the form [max sequence length, batchsize, other dims]
  • sequence_length (Symbol) – vector of sequence lengths of size batchsize
  • use_sequence_length (boolean, optional, default=False) – If set to true, this layer takes in extra input sequence_length to specify variable length sequence
  • value (float, optional, default=0) – The value to be used as a mask.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.SequenceReverse(*args, **kwargs)

Reverses the elements of each sequence. Takes an n-dimensional tensor of the form [max sequence length, batchsize, other dims] and returns a tensor of the same shape. This operator takes an optional input tensor sequence_length of positive ints of dimension [batchsize] when the sequence_length option is set to true. This allows the operator to handle variable-length sequences. If sequence_length is false, then each example in the batch is assumed to have the max sequence length.

Parameters:
  • data (Symbol) – n-dimensional input tensor of the form [max sequence length, batchsize, other dims]
  • sequence_length (Symbol) – vector of sequence lengths of size batchsize
  • use_sequence_length (boolean, optional, default=False) – If set to true, this layer takes in extra input sequence_length to specify variable length sequence
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.SliceChannel(*args, **kwargs)

Split an array along a particular axis into multiple sub-arrays.

Assume the input array has shape (d_0, ..., d_n) and we slice it into m (num_outputs=m) subarrays along axis k, then we will obtain a list of m arrays with each of which has shape (d_0, ..., d_k/m, ..., d_n).

For example:

x = [[1, 2],
     [3, 4],
     [5, 6],
     [7, 8]]  // 4x2 array

y = split(x, axis=0, num_outputs=4) // a list of 4 arrays
y[0] = [[ 1.,  2.]]  // 1x2 array

z = split(x, axis=0, num_outputs=2) // a list of 2 arrays
z[0] = [[ 1.,  2.],
        [ 3.,  4.]]

When setting optional argument squeeze_axis=1, then the k-dimension will be removed from the shape if it becomes 1:

y = split(x, axis=0, num_outputs=4, squeeze_axis=1)
y[0] = [ 1.,  2.]  // (2,) vector

Defined in src/operator/slice_channel.cc:L56

Parameters:
  • num_outputs (int, required) – Number of outputs to be sliced.
  • axis (int, optional, default='1') – Dimension along which to slice.
  • squeeze_axis (boolean, optional, default=False) – If true, the dimension will be squeezed. Also, input.shape[axis] must be the same as num_outputs when squeeze_axis is turned on.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.Softmax(*args, **kwargs)

DEPRECATED: Perform a softmax transformation on input. Please use SoftmaxOutput

Parameters:
  • data (NDArray) – Input data to softmax.
  • grad_scale (float, optional, default=1) – Scale the gradient by a float factor
  • ignore_label (float, optional, default=-1) – the labels with value equals to ignore_label will be ignored during backward (only works if use_ignore is set to be true).
  • multi_output (boolean, optional, default=False) – If set to true, softmax will applied on axis 1
  • use_ignore (boolean, optional, default=False) – If set to true, the ignore_label value will not contribute to the backward gradient
  • preserve_shape (boolean, optional, default=False) – If true, softmax will applied on the last axis
  • normalization ({'batch', 'null', 'valid'},optional, default='null') – Normalize the gradient
  • out_grad (boolean, optional, default=False) – Apply weighting from output gradient
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.SoftmaxActivation(*args, **kwargs)

Apply softmax activation to input. This is intended for internal layers. For output (loss layer) please use SoftmaxOutput. If mode=instance, this operator will compute a softmax for each instance in the batch; this is the default mode. If mode=channel, this operator will compute a num_channel-class softmax at each position of each instance; this can be used for fully convolutional network, image segmentation, etc.

Parameters:
  • data (Symbol) – Input data to activation function.
  • mode ({'channel', 'instance'},optional, default='instance') – Softmax Mode. If set to instance, this operator will compute a softmax for each instance in the batch; this is the default mode. If set to channel, this operator will compute a num_channel-class softmax at each position of each instance; this can be used for fully convolutional network, image segmentation, etc.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.SoftmaxOutput(*args, **kwargs)

Softmax with logit loss.

In the forward pass, the softmax output is returned. Assume the input data has shape (n,k), then the output will have the same shape as the input, which is computed by

\[out[i,:] = softmax(data[i,:])\]

for \(i=0,...,n-1\), where

\[softmax(x) = \left[..., \frac{exp(x[j])}{exp(x[0])+...+exp(x[k-1])}, ...\right]\]

For general N-D input array with shape \((d_1, ..., d_n)\). Denoted by the size \(s=d_1d_2...d_n\). The way to compute softmax various:

  • preserve_shape is false (default). Reshape input into a 2-D array with shape \((d_1, s/d_1)\) beforing computing the softmax, and then reshaped back to the original shape.

  • preserve_shape is true. For all \(i_1, ..., i_{n-1}\), compute

    \[out[i_1, ..., i_{n-1}, :] = softmax(data[i_1, ..., i_{n-1},:])\]
  • multi_output is true. For all \(i_1, ..., i_{n-1}\), compute

    \[out[i_1, :, ..., i_{n-1}] = softmax(data[i_1, :, ..., i_{n-1}])\]

In the backward pass, the logit loss, also called cross-entroy loss, is added. The provided label can be a (N-1)-D label index array or a N-D label probability array.

Examples with a particular label can be ignored during backward by specifying ignore_label (also need use_ignore to be true).

A scale can be applied to the gradient by grad_scale, which is often used in mutli-loss object function in which we can given each loss different weight. It also supports various ways to normalize the gradient by normalization:

  • null: do nothing
  • batch: divide by batch size (number of examples)
  • valid: divide by the number of examples which are not ignored.

Defined in src/operator/softmax_output.cc:L77

Parameters:
  • data (NDArray) – Input data.
  • label (NDArray) – Ground truth label.
  • grad_scale (float, optional, default=1) – Scale the gradient by a float factor
  • ignore_label (float, optional, default=-1) – the labels with value equals to ignore_label will be ignored during backward (only works if use_ignore is set to be true).
  • multi_output (boolean, optional, default=False) – If set to true, softmax will applied on axis 1
  • use_ignore (boolean, optional, default=False) – If set to true, the ignore_label value will not contribute to the backward gradient
  • preserve_shape (boolean, optional, default=False) – If true, softmax will applied on the last axis
  • normalization ({'batch', 'null', 'valid'},optional, default='null') – Normalize the gradient
  • out_grad (boolean, optional, default=False) – Apply weighting from output gradient
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.SpatialTransformer(*args, **kwargs)

Apply spatial transformer to input feature map.

Parameters:
  • data (Symbol) – Input data to the SpatialTransformerOp.
  • loc (Symbol) – localisation net, the output dim should be 6 when transform_type is affine. You shold initialize the weight and bias with identity tranform.
  • target_shape (Shape(tuple), optional, default=(0,0)) – output shape(h, w) of spatial transformer: (y, x)
  • transform_type ({'affine'}, required) – transformation type
  • sampler_type ({'bilinear'}, required) – sampling type
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.SwapAxis(*args, **kwargs)

Interchange two axes of an array.

Examples:

 x = [[1, 2, 3]])
 swapaxes(x, 0, 1) = [[ 1],
                      [ 2],
                      [ 3]]

 x = [[[ 0, 1],
       [ 2, 3]],
      [[ 4, 5],
       [ 6, 7]]]  // (2,2,2) array

swapaxes(x, 0, 2) = [[[ 0, 4],
                      [ 2, 6]],
                     [[ 1, 5],
                      [ 3, 7]]]

Defined in src/operator/swapaxis.cc:L55

Parameters:
  • data (NDArray) – Input array.
  • dim1 (int (non-negative), optional, default=0) – the first axis to be swapped.
  • dim2 (int (non-negative), optional, default=0) – the second axis to be swapped.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.UpSampling(*args, **kwargs)

Perform nearest neighboor/bilinear up sampling to inputs

Parameters:
  • data (Symbol[]) – Array of tensors to upsample
  • scale (int (non-negative), required) – Up sampling scale
  • num_filter (int (non-negative), optional, default=0) – Input filter. Only used by bilinear sample_type.
  • sample_type ({'bilinear', 'nearest'}, required) – upsampling method
  • multi_input_mode ({'concat', 'sum'},optional, default='concat') – How to handle multiple input. concat means concatenate upsampled images along the channel dimension. sum means add all images together, only available for nearest neighbor upsampling.
  • workspace (long (non-negative), optional, default=512) – Tmp workspace for deconvolution (MB)
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.abs(*args, **kwargs)

Returns the absolute value of array elements, element-wise.

For example:
abs([-2, 0, 3]) = [2, 0, 3]

Defined in src/operator/tensor/elemwise_unary_op.cc:L116

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.adam_update(*args, **kwargs)

Updater function for adam optimizer

Parameters:
  • lr (float, required) – learning_rate
  • beta1 (float, optional, default=0.9) – beta1
  • beta2 (float, optional, default=0.999) – beta2
  • epsilon (float, optional, default=1e-08) – epsilon
  • wd (float, optional, default=0) – weight decay
  • rescale_grad (float, optional, default=1) – rescale gradient as grad = rescale_grad*grad.
  • clip_gradient (float, optional, default=-1) – If greater than 0, clip gradient to grad = max(min(grad, -clip_gradient), clip_gradient). Otherwise turned off.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.add_n(*args, **kwargs)

Add all input arguments element-wise.

\[add\_n(a_1, a_2, ..., a_n) = a_1 + a_2 + ... + a_n\]

add_n is potentially more efficient than calling add by n times.

Defined in src/operator/tensor/elemwise_sum.cc:L63

Parameters:
  • args (NDArray[]) – Positional input arguments
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.arccos(*args, **kwargs)

Inverse cosine, element-wise.

The input should be in range \([-1, 1]\). The output is in the closed interval \([0, \pi]\)

\[arccos([-1, -.707, 0, .707, 1]) = [\pi, 3\pi/4, \pi/2, \pi/4, 0]\]

Defined in src/operator/tensor/elemwise_unary_op.cc:L375

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.arccosh(*args, **kwargs)

Inverse hyperbolic cosine, element-wise.

Defined in src/operator/tensor/elemwise_unary_op.cc:L481

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.arcsin(*args, **kwargs)

Inverse sine, element-wise.

The input should be in range \([-1, 1]\). The output is in the closed interval \([-\pi/2, \pi/2]\)

\[arcsin([-1, -.707, 0, .707, 1]) = [-\pi/2, -\pi/4, 0, \pi/4, \pi/2]\]

Defined in src/operator/tensor/elemwise_unary_op.cc:L358

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.arcsinh(*args, **kwargs)

Inverse hyperbolic sine, element-wise.

Defined in src/operator/tensor/elemwise_unary_op.cc:L471

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.arctan(*args, **kwargs)

Inverse tangent, element-wise.

The output is in the closed interval \([-\pi/2, \pi/2]\)

\[arccos([-1, 0, 1]) = [-\pi/4, 0, \pi/4]\]

Defined in src/operator/tensor/elemwise_unary_op.cc:L391

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.arctanh(*args, **kwargs)

Inverse hyperbolic tangent, element-wise.

Defined in src/operator/tensor/elemwise_unary_op.cc:L491

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.argmax(*args, **kwargs)

Returns the indices of the maximum values along an axis.

From:src/operator/tensor/broadcast_reduce_op_index.cc:11

Parameters:
  • data (NDArray) – The input
  • axis (int or None, optional, default='None') – int or None. The axis to perform the reduction. Negative values means indexing from right to left. If is None, a global reduction will be performed.
  • keepdims (boolean, optional, default=False) – If true, the axis which is reduced is left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.argmax_channel(*args, **kwargs)
Parameters:
  • src (NDArray) – Source input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.argmin(*args, **kwargs)

Returns the indices of the minimum values along an axis.

From:src/operator/tensor/broadcast_reduce_op_index.cc:16

Parameters:
  • data (NDArray) – The input
  • axis (int or None, optional, default='None') – int or None. The axis to perform the reduction. Negative values means indexing from right to left. If is None, a global reduction will be performed.
  • keepdims (boolean, optional, default=False) – If true, the axis which is reduced is left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.argsort(*args, **kwargs)

Returns the indices that can sort an array.

Examples:

x = [[ 0.3,  0.2,  0.4],
     [ 0.1,  0.3,  0.2]]

// sort along axis -1
argsort(x) = [[ 1.,  0.,  2.],
              [ 0.,  2.,  1.]]

// sort along axis 0
argsort(x, axis=0) = [[ 1.,  0.,  1.]
                      [ 0.,  1.,  0.]]

// flatten and then sort
argsort(x, axis=None) = [ 3.,  1.,  5.,  0.,  4.,  2.]

Defined in src/operator/tensor/ordering_op.cc:L146

Parameters:
  • src (NDArray) – Source input
  • axis (int or None, optional, default='-1') – Axis along which to sort the input tensor. If not given, the flattened array is used. Default is -1.
  • is_ascend (boolean, optional, default=True) – Whether sort in ascending or descending order.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.batch_dot(*args, **kwargs)

Batchwise dot product.

batch_dot is used to compute dot product of x and y when x and y are data in batch, namely 3D arrays in shape of (batch_size, :, :).

For example, given x with shape (batch_size, n, m) and y with shape (batch_size, m, k), the result array will have shape (batch_size, n, k), which is computed by:

batch_dot(x,y)[i,:,:] = dot(x[i,:,:], y[i,:,:])

Defined in src/operator/tensor/matrix_op.cc:L354

Parameters:
  • lhs (NDArray) – The first input
  • rhs (NDArray) – The second input
  • transpose_a (boolean, optional, default=False) – If true then transpose the first input before dot.
  • transpose_b (boolean, optional, default=False) – If true then transpose the second input before dot.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.batch_take(*args, **kwargs)

Take elements from a data batch.

Given an (d0, d1) input array, and (d0,) indices, the output will be a (d0,) computed by:

output[i] = input[i, indices[i]]

Examples:

x = [[ 1.,  2.],
     [ 3.,  4.],
     [ 5.,  6.]]

batch_take(x, [0,1,0]) = [ 1.  4.  5.]

Defined in src/operator/tensor/indexing_op.cc:L131

Parameters:
  • a (NDArray) – Input data array
  • indices (NDArray) – index array
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_add(*args, **kwargs)

Add arguments, element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_basic.cc:L16

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_axes(*args, **kwargs)

Broadcast an array over particular axes.

Broadcasting is allowed on axes which size 1, such as from (2,1,3,1) to (2,8,3,9). Elemenets will be duplicated on the broadcasted axes.

For example:

// given (1,2,1) shape x
x = [[[ 1.],
      [ 2.]]]

// broadcast on axis 2
broadcast_axis(x, axis=2, size=3) = [[[ 1.,  1.,  1.],
                                      [ 2.,  2.,  2.]]]
// broadcast on axes 0 and 2
broadcast_axis(x, axis=(0,2), size=(2,3)) = [[[ 1.,  1.,  1.],
                                              [ 2.,  2.,  2.]],
                                             [[ 1.,  1.,  1.],
                                              [ 2.,  2.,  2.]]]

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L137

Parameters:
  • data (NDArray) – The input
  • axis (Shape(tuple), optional, default=()) – The axes to perform the broadcasting.
  • size (Shape(tuple), optional, default=()) – Target sizes of the broadcasting axes.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_axis(*args, **kwargs)

Broadcast an array over particular axes.

Broadcasting is allowed on axes which size 1, such as from (2,1,3,1) to (2,8,3,9). Elemenets will be duplicated on the broadcasted axes.

For example:

// given (1,2,1) shape x
x = [[[ 1.],
      [ 2.]]]

// broadcast on axis 2
broadcast_axis(x, axis=2, size=3) = [[[ 1.,  1.,  1.],
                                      [ 2.,  2.,  2.]]]
// broadcast on axes 0 and 2
broadcast_axis(x, axis=(0,2), size=(2,3)) = [[[ 1.,  1.,  1.],
                                              [ 2.,  2.,  2.]],
                                             [[ 1.,  1.,  1.],
                                              [ 2.,  2.,  2.]]]

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L137

Parameters:
  • data (NDArray) – The input
  • axis (Shape(tuple), optional, default=()) – The axes to perform the broadcasting.
  • size (Shape(tuple), optional, default=()) – Target sizes of the broadcasting axes.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_div(*args, **kwargs)

Divide arguments, element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_basic.cc:L79

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_equal(*args, **kwargs)

Return (lhs == rhs), element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_logic.cc:L16

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_greater(*args, **kwargs)

Return (lhs > rhs), element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_logic.cc:L30

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_greater_equal(*args, **kwargs)

Return (lhs >= rhs), element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_logic.cc:L37

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_hypot(*args, **kwargs)

Given the “legs” of a right triangle, return its hypotenuse with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_extended.cc:L71

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_lesser(*args, **kwargs)

Return (lhs < rhs), element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_logic.cc:L44

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_lesser_equal(*args, **kwargs)

Return (lhs <= rhs), element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_logic.cc:L51

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_maximum(*args, **kwargs)

Element-wise maximum of array elements with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_extended.cc:L34

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_minimum(*args, **kwargs)

Element-wise minimum of array elements with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_extended.cc:L52

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_minus(*args, **kwargs)

Substract arguments, element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_basic.cc:L39

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_mul(*args, **kwargs)

Multiply arguments, element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_basic.cc:L61

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_not_equal(*args, **kwargs)

Return (lhs != rhs), element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_logic.cc:L23

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_plus(*args, **kwargs)

Add arguments, element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_basic.cc:L16

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_power(*args, **kwargs)

First array elements raised to powers from second array, element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_extended.cc:L16

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_sub(*args, **kwargs)

Substract arguments, element-wise with broadcasting.

Defined in src/operator/tensor/elemwise_binary_broadcast_op_basic.cc:L39

Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.broadcast_to(*args, **kwargs)

Broadcast an array to a new shape.

Broadcasting is allowed on axes which size 1, such as from (2,1,3,1) to (2,8,3,9). Elemenets will be duplicated on the broadcasted axes.

For example:

broadcast_to([[1,2,3]], shape=(2,3)) = [[ 1.,  2.,  3.],
                                        [ 1.,  2.,  3.]])

The dimensions that will not be changed can also use the special code 0 that means copy the original value. So with shape=(2,0) we will obtain the same results in the above example.

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L158

Parameters:
  • data (NDArray) – The input
  • shape (Shape(tuple), optional, default=()) – The shape of the desired array. We can set the dim to zero if it’s same as the original. E.g A = broadcast_to(B, shape=(10, 0, 0)) has the same meaning as A = broadcast_axis(B, axis=0, size=10).
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.cast(*args, **kwargs)

Cast to a specified type, element-wise.

For example:

cast([1e20, 11.1], dtype='float16') = [inf, 11.09375]
cast([300, 11.1, 10.9, -1, -3], dtype='uint8') = [44, 11, 10, 255, 253]

Defined in src/operator/tensor/elemwise_unary_op.cc:L86

Parameters:
  • data (NDArray) – Source input
  • dtype ({'float16', 'float32', 'float64', 'int32', 'uint8'}, required) – Output data type.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.ceil(*args, **kwargs)

Return the ceiling of the input, element-wise.

For example::
ceil([-2.1, -1.9, 1.5, 1.9, 2.1]) = [-2., -1., 2., 2., 3.]

Defined in src/operator/tensor/elemwise_unary_op.cc:L153

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.choose_element_0index(*args, **kwargs)

Choose one element from each line(row for python, column for R/Julia) in lhs according to index indicated by rhs. This function assume rhs uses 0-based index.

Parameters:
  • lhs (NDArray) – Left operand to the function.
  • rhs (NDArray) – Right operand to the function.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.clip(*args, **kwargs)

Clip (limit) the values in an array, elementwise

Given an interval, values outside the interval are clipped to the interval edges. That is:

clip(x) = max(min(x, a_max)), a_min)

Defined in src/operator/tensor/matrix_op.cc:L393

Parameters:
  • data (NDArray) – Source input
  • a_min (float, required) – Minimum value
  • a_max (float, required) – Maximum value
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.concat(*args, **kwargs)

Concate a list of array along a given axis.

The dimension sizes of the input arrays on the given axis should be the same.

For example:

x = [[1,1],[1,1]]
y = [[2,2],[2,2]]
z = [[3,3],[3,3],[3,3]]

Concat(x,y,z,dim=0) = [[ 1.,  1.],
                       [ 1.,  1.],
                       [ 2.,  2.],
                       [ 2.,  2.],
                       [ 3.,  3.],
                       [ 3.,  3.],
                       [ 3.,  3.]]

Concat(x,y,z,dim=1) = [[ 1.,  1.,  2.,  2.],
                       [ 1.,  1.,  2.,  2.]]

Defined in src/operator/concat.cc:L70

Parameters:
  • data (NDArray[]) – List of tensors to concatenate
  • dim (int, optional, default='1') – the dimension to be concated.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.cos(*args, **kwargs)

Cosine, element-wise.

Then input is in radians (\(2\pi\) rad equals 360 degress).

\[cos([0, \pi/4, \pi/2]) = [1, 0.707, 0]\]

Defined in src/operator/tensor/elemwise_unary_op.cc:L325

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.cosh(*args, **kwargs)

Hyperbolic cosine, element-wise.

For example::
cosh(x) = 0.5times(exp(x) + exp(-x))

Defined in src/operator/tensor/elemwise_unary_op.cc:L447

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.crop(*args, **kwargs)

Crop a continuous region from the array.

Assume the input array has n dimensions, given begin=(b_1, ..., b_n) and end=(e_1, ..., e_n), then crop will return a region with shape (e_1-b_1, ..., e_n-b_n). The result’s k-th dimension contains elements from the k-th dimension of the input array with the open range [b_k, e_k).

For example:

x = [[  1.,   2.,   3.,   4.],
     [  5.,   6.,   7.,   8.],
     [  9.,  10.,  11.,  12.]]

crop(x, begin=(0,1), end=(2,4)) = [[ 2.,  3.,  4.],
                                   [ 6.,  7.,  8.]]

Defined in src/operator/tensor/matrix_op.cc:L207

Parameters:
  • data (NDArray) – Source input
  • begin (Shape(tuple), required) – starting coordinates
  • end (Shape(tuple), required) – ending coordinates
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.degrees(*args, **kwargs)

Convert angles from radians to degrees.

\[degrees([0, \pi/2, \pi, 3\pi/2, 2\pi]) = [0, 90, 180, 270, 360]\]

Defined in src/operator/tensor/elemwise_unary_op.cc:L405

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.dot(*args, **kwargs)

Dot product of two arrays.

dot‘s behavior depends on the input array dimensions:

  • 1-D arrays: inner product of vectors

  • 2-D arrays: matrix multiplication

  • N-D arrays: a sum product over the last axis of the first input and the first axis of the second input

    For example, given 3-D x with shape (n,m,k) and y with shape (k,r,s), the result array will have shape (n,m,r,s). It is computed by:

    dot(x,y)[i,j,a,b] = sum(x[i,j,:]*y[:,a,b])
    

Defined in src/operator/tensor/matrix_op.cc:L318

Parameters:
  • lhs (NDArray) – The first input
  • rhs (NDArray) – The second input
  • transpose_a (boolean, optional, default=False) – If true then transpose the first input before dot.
  • transpose_b (boolean, optional, default=False) – If true then transpose the second input before dot.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.elemwise_add(*args, **kwargs)
Parameters:
  • lhs (NDArray) – first input
  • rhs (NDArray) – second input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.exp(*args, **kwargs)

Calculate the exponential of the array, element-wise

For example::
exp(x) = e^x approx 2.718^x

Defined in src/operator/tensor/elemwise_unary_op.cc:L236

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.expand_dims(*args, **kwargs)

Insert a new axis with size 1 into the array shape

For example, given x with shape (2,3,4), then expand_dims(x, axis=1) will return a new array with shape (2,1,3,4).

Defined in src/operator/tensor/matrix_op.cc:L175

Parameters:
  • data (NDArray) – Source input
  • axis (int (non-negative), required) – Position (amongst axes) where new axis is to be inserted.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.expm1(*args, **kwargs)

Calculate exp(x) - 1

This function provides greater precision than exp(x) - 1 for small values of x.

Defined in src/operator/tensor/elemwise_unary_op.cc:L309

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.fill_element_0index(*args, **kwargs)

Fill one element of each line(row for python, column for R/Julia) in lhs according to index indicated by rhs and values indicated by mhs. This function assume rhs uses 0-based index.

Parameters:
  • lhs (NDArray) – Left operand to the function.
  • mhs (NDArray) – Middle operand to the function.
  • rhs (NDArray) – Right operand to the function.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.fix(*args, **kwargs)

Round elements of the array to the nearest integer towards zero, element-wise.

For example::
fix([-2.1, -1.9, 1.9, 2.1]) = [-2., -1., 1., 2.]

Defined in src/operator/tensor/elemwise_unary_op.cc:L185

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.flatten(*args, **kwargs)

Flatten input into a 2-D array by collapsing the higher dimensions.

Assume the input array has shape (d1, d2, ..., dk), then flatten reshapes the input array into shape (d1, d2*...*dk).

Defined in src/operator/tensor/matrix_op.cc:L101

Parameters:
  • data (NDArray) – Input data to reshape.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.flip(*args, **kwargs)

Reverse elements of an array with axis

From:src/operator/tensor/matrix_op.cc:512

Parameters:
  • data (NDArray) – Input data array
  • axis (Shape(tuple), required) – The axis which to reverse elements.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.floor(*args, **kwargs)

Return the floor of the input, element-wise.

For example::
floor([-2.1, -1.9, 1.5, 1.9, 2.1]) = [-3., -2., 1., 1., 2.]

Defined in src/operator/tensor/elemwise_unary_op.cc:L162

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.gamma(*args, **kwargs)

The gamma function (extension of the factorial function), element-wise

From:src/operator/tensor/elemwise_unary_op.cc:500

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.gammaln(*args, **kwargs)

Log of the absolute value of the gamma function, element-wise

From:src/operator/tensor/elemwise_unary_op.cc:509

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.identity(*args, **kwargs)

Identity mapping, copy src to output

From:src/operator/tensor/elemwise_unary_op.cc:15

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.log(*args, **kwargs)

Natural logarithm, element-wise.

The natural logarithm is logarithm in base e, so that log(exp(x)) = x

Defined in src/operator/tensor/elemwise_unary_op.cc:L246

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.log10(*args, **kwargs)

Calculate the base 10 logarithm of the array, element-wise.

10**log10(x) = x

Defined in src/operator/tensor/elemwise_unary_op.cc:L256

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.log1p(*args, **kwargs)

Calculate log(1 + x)

This function is more accurate than log(1 + x) for small x so that \(1+x\approx 1\)

Defined in src/operator/tensor/elemwise_unary_op.cc:L296

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.log2(*args, **kwargs)

Calculate the base 2 logarithm of the array, element-wise.

2**log2(x) = x

Defined in src/operator/tensor/elemwise_unary_op.cc:L266

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.log_softmax(*args, **kwargs)
Parameters:
  • data (NDArray) – The input
  • axis (int, optional, default='-1') – The axis along which to compute softmax. By default use the last axis
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.make_loss(*args, **kwargs)

Get output from a symbol and pass 1 gradient back. This is used as a terminal loss if unary and binary operator are used to composite a loss with no declaration of backward dependency

From:src/operator/tensor/elemwise_unary_op.cc:40

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.max(*args, **kwargs)

Compute the max of array elements over given axes.

The argument axis specifies the axes to compute over:

  • (): compute over all elements into a scalar array with shape (1,). This is the default option.
  • int: compute over along a particular axis. If input has shape (n, m, k), use axis=0 will result in an array with shape (m, k).
  • tuple of int: compute over multiple axes. Again assume input shape (n, m, k), with axis=(0,2) we obtain a (m,) shape array.

If keepdims = 1, then the result array will has the same number of dimensions as the input, while the reduced axes will have size 1.

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L98

Parameters:
  • data (NDArray) – The input
  • axis (Shape(tuple), optional, default=()) – The axes to perform the reduction.
  • keepdims (boolean, optional, default=False) – If true, the axes which are reduced are left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.max_axis(*args, **kwargs)

Compute the max of array elements over given axes.

The argument axis specifies the axes to compute over:

  • (): compute over all elements into a scalar array with shape (1,). This is the default option.
  • int: compute over along a particular axis. If input has shape (n, m, k), use axis=0 will result in an array with shape (m, k).
  • tuple of int: compute over multiple axes. Again assume input shape (n, m, k), with axis=(0,2) we obtain a (m,) shape array.

If keepdims = 1, then the result array will has the same number of dimensions as the input, while the reduced axes will have size 1.

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L98

Parameters:
  • data (NDArray) – The input
  • axis (Shape(tuple), optional, default=()) – The axes to perform the reduction.
  • keepdims (boolean, optional, default=False) – If true, the axes which are reduced are left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.mean(*args, **kwargs)

Compute the mean of array elements over given axes.

The argument axis specifies the axes to compute over:

  • (): compute over all elements into a scalar array with shape (1,). This is the default option.
  • int: compute over along a particular axis. If input has shape (n, m, k), use axis=0 will result in an array with shape (m, k).
  • tuple of int: compute over multiple axes. Again assume input shape (n, m, k), with axis=(0,2) we obtain a (m,) shape array.

If keepdims = 1, then the result array will has the same number of dimensions as the input, while the reduced axes will have size 1.

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L53

Parameters:
  • data (NDArray) – The input
  • axis (Shape(tuple), optional, default=()) – The axes to perform the reduction.
  • keepdims (boolean, optional, default=False) – If true, the axes which are reduced are left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.min(*args, **kwargs)

Compute the min of array elements over given axes.

The argument axis specifies the axes to compute over:

  • (): compute over all elements into a scalar array with shape (1,). This is the default option.
  • int: compute over along a particular axis. If input has shape (n, m, k), use axis=0 will result in an array with shape (m, k).
  • tuple of int: compute over multiple axes. Again assume input shape (n, m, k), with axis=(0,2) we obtain a (m,) shape array.

If keepdims = 1, then the result array will has the same number of dimensions as the input, while the reduced axes will have size 1.

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L108

Parameters:
  • data (NDArray) – The input
  • axis (Shape(tuple), optional, default=()) – The axes to perform the reduction.
  • keepdims (boolean, optional, default=False) – If true, the axes which are reduced are left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.min_axis(*args, **kwargs)

Compute the min of array elements over given axes.

The argument axis specifies the axes to compute over:

  • (): compute over all elements into a scalar array with shape (1,). This is the default option.
  • int: compute over along a particular axis. If input has shape (n, m, k), use axis=0 will result in an array with shape (m, k).
  • tuple of int: compute over multiple axes. Again assume input shape (n, m, k), with axis=(0,2) we obtain a (m,) shape array.

If keepdims = 1, then the result array will has the same number of dimensions as the input, while the reduced axes will have size 1.

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L108

Parameters:
  • data (NDArray) – The input
  • axis (Shape(tuple), optional, default=()) – The axes to perform the reduction.
  • keepdims (boolean, optional, default=False) – If true, the axes which are reduced are left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.nanprod(*args, **kwargs)

Compute the product of array elements over given axes with NaN ignored

Refer to prod for more details.

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L88

Parameters:
  • data (NDArray) – The input
  • axis (Shape(tuple), optional, default=()) – The axes to perform the reduction.
  • keepdims (boolean, optional, default=False) – If true, the axes which are reduced are left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.nansum(*args, **kwargs)

Compute the sum of array elements over given axes with NaN ignored

Refer to sum for more details.

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L75

Parameters:
  • data (NDArray) – The input
  • axis (Shape(tuple), optional, default=()) – The axes to perform the reduction.
  • keepdims (boolean, optional, default=False) – If true, the axes which are reduced are left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.norm(*args, **kwargs)

Compute the L2 norm.

Flatten then input array and then compute the l2 norm.

Examples:

x = [[1, 2],
     [3, 4]]

norm(x) = [5.47722578]

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L182

Parameters:
  • src (NDArray) – Source input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.normal(*args, **kwargs)

Draw random samples from a normal (Gaussian) distribution.

Examples:

normal(loc=0, scale=1, shape=(2,2)) = [[ 1.89171135, -1.16881478],
                                       [-1.23474145,  1.55807114]]

Defined in src/operator/tensor/sample_op.cc:L35

Parameters:
  • loc (float, optional, default=0) – Mean of the distribution.
  • scale (float, optional, default=1) – Standard deviation of the distribution.
  • shape (Shape(tuple), optional, default=()) – The shape of the output
  • ctx (string, optional, default='') – Context of output, in format [cpu|gpu|cpu_pinned](n).Only used for imperative calls.
  • dtype ({'None', 'float16', 'float32', 'float64'},optional, default='None') – DType of the output. If output given, set to type of output.If output not given and type not defined (dtype=None), set to float32.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.one_hot(*args, **kwargs)

Returns a one-hot array.

The locations represented by indices take value on_value, while all other locations take value off_value.

Assume indices has shape (i0, i1), then the output will have shape (i0, i1, depth) and:

output[i,j,:] = off_value
output[i,j,indices[i,j]] = on_value

Examples:

one_hot([1,0,2,0], 3) = [[ 0.  1.  0.]
                         [ 1.  0.  0.]
                         [ 0.  0.  1.]
                         [ 1.  0.  0.]]

one_hot([1,0,2,0], 3, on_value=8, off_value=1,
        dtype='int32') = [[1 8 1]
                          [8 1 1]
                          [1 1 8]
                          [8 1 1]]

one_hot([[1,0],[1,0],[2,0]], 3) = [[[ 0.  1.  0.]
                                    [ 1.  0.  0.]]

                                   [[ 0.  1.  0.]
                                    [ 1.  0.  0.]]

                                   [[ 0.  0.  1.]
                                    [ 1.  0.  0.]]]

Defined in src/operator/tensor/indexing_op.cc:L177

Parameters:
  • indices (NDArray) – array of locations where to set on_value
  • depth (int, required) – The dimension size at dim = axis.
  • on_value (double, optional, default=1) – The value assigned to the locations represented by indices.
  • off_value (double, optional, default=0) – The value assigned to the locations not represented by indices.
  • dtype ({'float16', 'float32', 'float64', 'int32', 'uint8'},optional, default='float32') – DType of the output
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.pad(*args, **kwargs)

Pad an array.

Only supports 4-D and 5-D input array.

Defined in src/operator/pad.cc:L407

Parameters:
  • data (NDArray) – An n-dimensional input tensor.
  • mode ({'constant', 'edge'}, required) – Padding type to use. “constant” pads all values with a constant value, the value of which can be specified with the constant_value option. “edge” uses the boundary values of the array as padding.
  • pad_width (Shape(tuple), required) – A tuple of padding widths of length 2*r, where r is the rank of the input tensor, specifying number of values padded to the edges of each axis. (before_1, after_1, ... , before_N, after_N) unique pad widths for each axis. Equivalent to pad_width in numpy.pad, but flattened.
  • constant_value (double, optional, default=0) – This option is only used when mode is “constant”. This value will be used as the padding value. Defaults to 0 if not specified.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.pick(*args, **kwargs)
Parameters:
  • data (NDArray) – Source input
  • index (NDArray) – Index array
  • axis (int or None, optional, default='None') – int or None. The axis to perform the reduction. Negative values means indexing from right to left. If is None, a global reduction will be performed.
  • keepdims (boolean, optional, default=False) – If true, the axis which is reduced is left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.prod(*args, **kwargs)

Compute the product of array elements over given axes.

The argument axis specifies the axes to compute over:

  • (): compute over all elements into a scalar array with shape (1,). This is the default option.
  • int: compute over along a particular axis. If input has shape (n, m, k), use axis=0 will result in an array with shape (m, k).
  • tuple of int: compute over multiple axes. Again assume input shape (n, m, k), with axis=(0,2) we obtain a (m,) shape array.

If keepdims = 1, then the result array will has the same number of dimensions as the input, while the reduced axes will have size 1.

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L62

Parameters:
  • data (NDArray) – The input
  • axis (Shape(tuple), optional, default=()) – The axes to perform the reduction.
  • keepdims (boolean, optional, default=False) – If true, the axes which are reduced are left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.radians(*args, **kwargs)

Convert angles from degrees to radians.

\[radians([0, 90, 180, 270, 360]) = [0, \pi/2, \pi, 3\pi/2, 2\pi]\]

Defined in src/operator/tensor/elemwise_unary_op.cc:L419

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.repeat(*args, **kwargs)

Repeat elements of an array.

In default, repeat flatten the input array into 1-D and then repeat the elements:

x = [[ 1, 2],
     [ 3, 4]]

repeat(x, repeats=2) = [ 1.,  1.,  2.,  2.,  3.,  3.,  4.,  4.]

We can also choose a particular axis to repeat, in which a negative axis is interpreted counting from the backward:

repeat(x, repeats=2, axis=1) = [[ 1.,  1.,  2.,  2.],
                                [ 3.,  3.,  4.,  4.]]

repeat(x, repeats=2, axis=-1) = [[ 1.,  2.],
                                 [ 1.,  2.],
                                 [ 3.,  4.],
                                 [ 3.,  4.]]

Defined in src/operator/tensor/matrix_op.cc:L432

Parameters:
  • data (NDArray) – Input data array
  • repeats (int, required) – The number of repetitions for each element.
  • axis (int or None, optional, default='None') – The axis along which to repeat values. The negative numbers are interpreted counting from the backward. By default, use the flattened input array, and return a flat output array.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.reshape(*args, **kwargs)

Reshape array into a new shape.

The shape is a tuple of int such as (2,3,4). The new shape should not change the array size. For example:

reshape([1,2,3,4], shape=(2,2)) = [[1,2], [3,4]]

In addition, we can use special codes, which are integers less than 1, on some shape dimensions. To inference the output shape, we set it to an empty tuple at beginning. When continuously pop dimensions from the original shape starting from the beginning, and then push translated results into the output shape.

Each special code presents a way of translation.

  • 0 for copying one. Pop one input dimension and push into the output. For example:

    - input=(2,3,4), shape=(4,0,2), output=(4,3,2)
    - input=(2,3,4), shape=(2,0,0), output=(2,3,4)
    
  • -1 for inference. Push a placeholder into the output whose value will be inferred later:

    - input=(2,3,4), shape=(6,1,-1), output=(6,1,4)
    - input=(2,3,4), shape=(3,-1,8), output=(3,1,8)
    - input=(2,3,4), shape=(-1,), output=(24,)
    
  • -2 for copying all. Pop all remaining input dimensions and push them into the output:

    - input=(2,3,4), shape=(-2), output=(9,8,7)
    - input=(2,3,4), shape=(2,-2), output=(2,3,4)
    - input=(2,3,4), shape=(-2,1,1), output=(2,3,4,1,1)
    
  • -3 for merging two dimensions. Pop two input dimensions, compute the product and then push into the output:

    - input=(2,3,4), shape=(-3,4), output=(6,4)
    - input=(2,3,4), shape=(0,-3), output=(2,12)
    - input=(2,3,4), shape=(-3,-2), output=(6,4)
    
  • -4 for splitting two dimensions. Pop one input dimensions, next split it according to the next two dimensions (can contain one -1) specified after this code, then push into the output:

    - input=(2,3,4), shape=(-4,1,2,-2), output=(1,2,3,4)
    - input=(2,3,4), shape=(2,-4,-1,3,-2), output=(2,1,3,4)
    

If the argument reverse is set to be true, then translating the input shape from right to left. For example, with input shape (10, 5, 4) target shape (-1, 0), then the output shape will be (50,4) if reverse=1, otherwise it will be (40,5).

Defined in src/operator/tensor/matrix_op.cc:L78

Parameters:
  • data (NDArray) – Input data to reshape.
  • shape (Shape(tuple), optional, default=()) – The target shape
  • keep_highest (boolean, optional, default=False) – (Deprecated! Use shape instead.) Whether keep the highest dim unchanged.If set to true, then the first dim in target_shape is ignored,and always fixed as input
  • reverse (boolean, optional, default=False) – If true then translating the input shape from right to left
  • target_shape (Shape(tuple), optional, default=(0,0)) – (Deprecated! Use shape instead.) Target new shape. One and only one dim can be 0, in which case it will be inferred from the rest of dims
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.reverse(*args, **kwargs)

Reverse elements of an array with axis

From:src/operator/tensor/matrix_op.cc:512

Parameters:
  • data (NDArray) – Input data array
  • axis (Shape(tuple), required) – The axis which to reverse elements.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.rint(*args, **kwargs)

Round elements of the array to the nearest integer, element-wise.

For example::
rint([-2.1, -1.9, 1.5, 1.9, 2.1]) = [-2., -2., 1., 2., 2.]

The difference to round is that rint returns n for input n.5 while round returns n+1 for n>=0.

Defined in src/operator/tensor/elemwise_unary_op.cc:L175

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.rmsprop_update(*args, **kwargs)

Updater function for RMSProp optimizer. The RMSProp code follows the version in http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf Tieleman & Hinton, 2012.

Parameters:
  • lr (float, required) – learning_rate
  • gamma1 (float, optional, default=0.95) – gamma1
  • epsilon (float, optional, default=1e-08) – epsilon
  • wd (float, optional, default=0) – weight decay
  • rescale_grad (float, optional, default=1) – rescale gradient as grad = rescale_grad*grad.
  • clip_gradient (float, optional, default=-1) – If greater than 0, clip gradient to grad = max(min(grad, -clip_gradient), clip_gradient). Otherwise turned off.
  • clip_weights (float, optional, default=-1) – If greater than 0, clip weights to weights = max(min(weights, -clip_weights), clip_weights). Otherwise turned off.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.rmspropalex_update(*args, **kwargs)

Updater function for RMSPropAlex optimizer. The RMSPropAlex code follows the version in http://arxiv.org/pdf/1308.0850v5.pdf Eq(38) - Eq(45) by Alex Graves, 2013.

Parameters:
  • lr (float, required) – learning_rate
  • gamma1 (float, optional, default=0.95) – gamma1
  • gamma2 (float, optional, default=0.9) – gamma2
  • epsilon (float, optional, default=1e-08) – epsilon
  • wd (float, optional, default=0) – weight decay
  • rescale_grad (float, optional, default=1) – rescale gradient as grad = rescale_grad*grad.
  • clip_gradient (float, optional, default=-1) – If greater than 0, clip gradient to grad = max(min(grad, -clip_gradient), clip_gradient). Otherwise turned off.
  • clip_weights (float, optional, default=-1) – If greater than 0, clip weights to weights = max(min(weights, -clip_weights), clip_weights). Otherwise turned off.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.round(*args, **kwargs)

Round elements of the array to the nearest integer, element-wise.

For example::
round([-2.1, -1.9, 1.5, 1.9, 2.1]) = [-2., -2., 2., 2., 2.]

Defined in src/operator/tensor/elemwise_unary_op.cc:L143

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.rsqrt(*args, **kwargs)

Calculate the inverse square-root of an array, element-wise.

For example::
rsqrt(x) = 1/sqrt{x}

Defined in src/operator/tensor/elemwise_unary_op.cc:L221

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.sgd_mom_update(*args, **kwargs)

Updater function for sgd optimizer

Parameters:
  • lr (float, required) – learning_rate
  • momentum (float, optional, default=0) – momentum
  • wd (float, optional, default=0) – weight decay
  • rescale_grad (float, optional, default=1) – rescale gradient as grad = rescale_grad*grad.
  • clip_gradient (float, optional, default=-1) – If greater than 0, clip gradient to grad = max(min(grad, -clip_gradient), clip_gradient). Otherwise turned off.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.sgd_update(*args, **kwargs)

Updater function for sgd optimizer

Parameters:
  • lr (float, required) – learning_rate
  • wd (float, optional, default=0) – weight decay
  • rescale_grad (float, optional, default=1) – rescale gradient as grad = rescale_grad*grad.
  • clip_gradient (float, optional, default=-1) – If greater than 0, clip gradient to grad = max(min(grad, -clip_gradient), clip_gradient). Otherwise turned off.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.sign(*args, **kwargs)

Returns the indication sign of array elements, element-wise.

For example::
sign([-2, 0, 3]) = [-1, 0, 1]

Defined in src/operator/tensor/elemwise_unary_op.cc:L130

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.sin(*args, **kwargs)

Trigonometric sine, element-wise.

Then input is in radians (\(2\pi\) rad equals 360 degress).

\[sin([0, \pi/4, \pi/2]) = [0, 0.707, 1]\]

Defined in src/operator/tensor/elemwise_unary_op.cc:L282

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.sinh(*args, **kwargs)

Hyperbolic sine, element-wise.

For example::
sinh(x) = 0.5times(exp(x) - exp(-x))

Defined in src/operator/tensor/elemwise_unary_op.cc:L433

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.slice(*args, **kwargs)

Crop a continuous region from the array.

Assume the input array has n dimensions, given begin=(b_1, ..., b_n) and end=(e_1, ..., e_n), then crop will return a region with shape (e_1-b_1, ..., e_n-b_n). The result’s k-th dimension contains elements from the k-th dimension of the input array with the open range [b_k, e_k).

For example:

x = [[  1.,   2.,   3.,   4.],
     [  5.,   6.,   7.,   8.],
     [  9.,  10.,  11.,  12.]]

crop(x, begin=(0,1), end=(2,4)) = [[ 2.,  3.,  4.],
                                   [ 6.,  7.,  8.]]

Defined in src/operator/tensor/matrix_op.cc:L207

Parameters:
  • data (NDArray) – Source input
  • begin (Shape(tuple), required) – starting coordinates
  • end (Shape(tuple), required) – ending coordinates
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.slice_axis(*args, **kwargs)

Slice along a given axis.

Examples

x = [[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.], [ 9., 10., 11., 12.]]
slice_axis(x, axis=0, begin=1, end=3) = [[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.]]
slice_axis(x, axis=1, begin=0, end=2) = [[ 1., 2.],
[ 5., 6.], [ 9., 10.]]
slice_axis(x, axis=1, begin=-3, end=-1) = [[ 2., 3.],
[ 6., 7.], [ 10., 11.]]

Defined in src/operator/tensor/matrix_op.cc:L285

Parameters:
  • data (NDArray) – Source input
  • axis (int, required) – The axis to be sliced. Negative axis means to count from the last to the first axis.
  • begin (int, required) – The beginning index to be sliced. Negative values are interpreted as counting from the backward.
  • end (int or None, required) – The end index to be sliced. The end can be None, in which case all the rest elements are used. Also, negative values are interpreted as counting from the backward.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.smooth_l1(*args, **kwargs)

Calculate Smooth L1 Loss(lhs, scalar)

From:src/operator/tensor/elemwise_binary_scalar_op_extended.cc:63

Parameters:
  • data (NDArray) – source input
  • scalar (float) – scalar input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.softmax(*args, **kwargs)
Parameters:
  • data (NDArray) – The input
  • axis (int, optional, default='-1') – The axis along which to compute softmax. By default use the last axis
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.softmax_cross_entropy(*args, **kwargs)

Calculate cross_entropy(data, one_hot(label))

From:src/operator/loss_binary_op.cc:12

Parameters:
  • data (NDArray) – Input data
  • label (NDArray) – Input label
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.sort(*args, **kwargs)

Return a sorted copy of an array.

Examples:

x = [[ 1, 4],
     [ 3, 1]]

// sort along the last axis
sort(x) = [[ 1.,  4.],
           [ 1.,  3.]]

// flatten and then sort
sort(x, axis=None) = [ 1.,  1.,  3.,  4.]

// sort long the first axis
sort(x, axis=0) = [[ 1.,  1.],
                   [ 3.,  4.]]

// in a descend order
sort(x, is_ascend=0) = [[ 4.,  1.],
                        [ 3.,  1.]]

Defined in src/operator/tensor/ordering_op.cc:L99

Parameters:
  • src (NDArray) – Source input
  • axis (int or None, optional, default='-1') – Axis along which to choose sort the input tensor. If not given, the flattened array is used. Default is -1.
  • is_ascend (boolean, optional, default=True) – Whether sort in ascending or descending order.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.split(*args, **kwargs)

Split an array along a particular axis into multiple sub-arrays.

Assume the input array has shape (d_0, ..., d_n) and we slice it into m (num_outputs=m) subarrays along axis k, then we will obtain a list of m arrays with each of which has shape (d_0, ..., d_k/m, ..., d_n).

For example:

x = [[1, 2],
     [3, 4],
     [5, 6],
     [7, 8]]  // 4x2 array

y = split(x, axis=0, num_outputs=4) // a list of 4 arrays
y[0] = [[ 1.,  2.]]  // 1x2 array

z = split(x, axis=0, num_outputs=2) // a list of 2 arrays
z[0] = [[ 1.,  2.],
        [ 3.,  4.]]

When setting optional argument squeeze_axis=1, then the k-dimension will be removed from the shape if it becomes 1:

y = split(x, axis=0, num_outputs=4, squeeze_axis=1)
y[0] = [ 1.,  2.]  // (2,) vector

Defined in src/operator/slice_channel.cc:L56

Parameters:
  • num_outputs (int, required) – Number of outputs to be sliced.
  • axis (int, optional, default='1') – Dimension along which to slice.
  • squeeze_axis (boolean, optional, default=False) – If true, the dimension will be squeezed. Also, input.shape[axis] must be the same as num_outputs when squeeze_axis is turned on.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.sqrt(*args, **kwargs)

Calculate the square-root of an array, element-wise.

For example::
sqrt(x) = sqrt{x}

Defined in src/operator/tensor/elemwise_unary_op.cc:L208

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.square(*args, **kwargs)

Calculate the square of an array, element-wise.

For example::
square(x) = x^2

Defined in src/operator/tensor/elemwise_unary_op.cc:L195

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.stop_gradient(*args, **kwargs)

Get output from a symbol and pass 0 gradient back

From:src/operator/tensor/elemwise_unary_op.cc:32

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.sum(*args, **kwargs)

Compute the sum of array elements over given axes.

The argument axis specifies the axes to compute over:

  • (): compute over all elements into a scalar array with shape (1,). This is the default option.
  • int: compute over along a particular axis. If input has shape (n, m, k), use axis=0 will result in an array with shape (m, k).
  • tuple of int: compute over multiple axes. Again assume input shape (n, m, k), with axis=(0,2) we obtain a (m,) shape array.

If keepdims = 1, then the result array will has the same number of dimensions as the input, while the reduced axes will have size 1.

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L44

Parameters:
  • data (NDArray) – The input
  • axis (Shape(tuple), optional, default=()) – The axes to perform the reduction.
  • keepdims (boolean, optional, default=False) – If true, the axes which are reduced are left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.sum_axis(*args, **kwargs)

Compute the sum of array elements over given axes.

The argument axis specifies the axes to compute over:

  • (): compute over all elements into a scalar array with shape (1,). This is the default option.
  • int: compute over along a particular axis. If input has shape (n, m, k), use axis=0 will result in an array with shape (m, k).
  • tuple of int: compute over multiple axes. Again assume input shape (n, m, k), with axis=(0,2) we obtain a (m,) shape array.

If keepdims = 1, then the result array will has the same number of dimensions as the input, while the reduced axes will have size 1.

Defined in src/operator/tensor/broadcast_reduce_op_value.cc:L44

Parameters:
  • data (NDArray) – The input
  • axis (Shape(tuple), optional, default=()) – The axes to perform the reduction.
  • keepdims (boolean, optional, default=False) – If true, the axes which are reduced are left in the result as dimension with size one.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.swapaxes(*args, **kwargs)

Interchange two axes of an array.

Examples:

 x = [[1, 2, 3]])
 swapaxes(x, 0, 1) = [[ 1],
                      [ 2],
                      [ 3]]

 x = [[[ 0, 1],
       [ 2, 3]],
      [[ 4, 5],
       [ 6, 7]]]  // (2,2,2) array

swapaxes(x, 0, 2) = [[[ 0, 4],
                      [ 2, 6]],
                     [[ 1, 5],
                      [ 3, 7]]]

Defined in src/operator/swapaxis.cc:L55

Parameters:
  • data (NDArray) – Input array.
  • dim1 (int (non-negative), optional, default=0) – the first axis to be swapped.
  • dim2 (int (non-negative), optional, default=0) – the second axis to be swapped.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.take(*args, **kwargs)

Take elements from an array along an axis.

Slice along a particular axis with the provided indices. E.g., given an input array with shape (d0, d1, d2) and indices with shape (i0, i1), then the output will have shape (i0, i1, d1, d2), with:

output[i,j,:,:] = input[indices[i,j],:,:]

Examples:

 x = [[ 1.,  2.],
      [ 3.,  4.],
      [ 5.,  6.]]

take(x, [[0,1],[1,2]]) = [[[ 1.,  2.],
                           [ 3.,  4.]],

                          [[ 3.,  4.],
                           [ 5.,  6.]]]

Note

Only slicing axis 0 is supported now.

Defined in src/operator/tensor/indexing_op.cc:L79

Parameters:
  • a (NDArray) – The source array.
  • indices (NDArray) – The indices of the values to extract.
  • axis (int, optional, default='0') – the axis of data tensor to be taken.
  • mode ({'clip', 'raise', 'wrap'},optional, default='raise') – specify how out-of-bound indices bahave.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.tan(*args, **kwargs)

Tangent, element-wise.

Then input is in radians (\(2\pi\) rad equals 360 degress).

\[tan([0, \pi/4, \pi/2]) = [0, 1, -inf]\]

Defined in src/operator/tensor/elemwise_unary_op.cc:L341

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.tanh(*args, **kwargs)

Hyperbolic tangent element-wise.

For example::
tanh(x) = sinh(x) / cosh(x)

Defined in src/operator/tensor/elemwise_unary_op.cc:L461

Parameters:
  • data (NDArray) – The input
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.tile(*args, **kwargs)

Repeat the whole array by multiple times.

If reps has length d, and input array has dimension of n. There are there cases:

  • n=d. Repeat i-th dimension of the input by reps[i] times:

    x = [[1, 2],
         [3, 4]]
    
    tile(x, reps=(2,3)) = [[ 1.,  2.,  1.,  2.,  1.,  2.],
                           [ 3.,  4.,  3.,  4.,  3.,  4.],
                           [ 1.,  2.,  1.,  2.,  1.,  2.],
                           [ 3.,  4.,  3.,  4.,  3.,  4.]]
    
  • n>d. reps is promoted to length n by pre-pending 1’s to it. Thus for an input shape (2,3), repos=(2,) is treated as (1,2):

    tile(x, reps=(2,)) = [[ 1.,  2.,  1.,  2.],
                          [ 3.,  4.,  3.,  4.]]
    
  • n<d. The input is promoted to be d-dimensional by prepending new axes. So a shape (2,2) array is promoted to (1,2,2) for 3-D replication:

    tile(x, reps=(2,2,3)) = [[[ 1.,  2.,  1.,  2.,  1.,  2.],
                              [ 3.,  4.,  3.,  4.,  3.,  4.],
                              [ 1.,  2.,  1.,  2.,  1.,  2.],
                              [ 3.,  4.,  3.,  4.,  3.,  4.]],
    
                             [[ 1.,  2.,  1.,  2.,  1.,  2.],
                              [ 3.,  4.,  3.,  4.,  3.,  4.],
                              [ 1.,  2.,  1.,  2.,  1.,  2.],
                              [ 3.,  4.,  3.,  4.,  3.,  4.]]]
    

Defined in src/operator/tensor/matrix_op.cc:L489

Parameters:
  • data (NDArray) – Input data array
  • reps (Shape(tuple), required) – The number of times for repeating the tensor a. If reps has length d, the result will have dimension of max(d, a.ndim); If a.ndim < d, a is promoted to be d-dimensional by prepending new axes. If a.ndim > d, reps is promoted to a.ndim by pre-pending 1’s to it.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.topk(*args, **kwargs)

Return the top k elements in an array.

Examples:

x = [[ 0.3,  0.2,  0.4],
     [ 0.1,  0.3,  0.2]]

// return the index of the largest element on last axis
topk(x) = [[ 2.],
           [ 1.]]

// return the value of the top-2 elements on last axis
topk(x, ret_typ='value', k=2) = [[ 0.4,  0.3],
                                 [ 0.3,  0.2]]

// flatten and then return both index and value
topk(x, ret_typ='both', k=2, axis=None) = [ 0.4,  0.3], [ 2.,  0.]

Defined in src/operator/tensor/ordering_op.cc:L36

Parameters:
  • src (NDArray) – Source input
  • axis (int or None, optional, default='-1') – Axis along which to choose the top k indices. If not given, the flattened array is used. Default is -1.
  • k (int, optional, default='1') – Number of top elements to select, should be always smaller than or equal to the element number in the given axis. A global sort is performed if set k < 1.
  • ret_typ ({'both', 'indices', 'mask', 'value'},optional, default='indices') – The return type. “value” means returning the top k values, “indices” means returning the indices of the top k values, “mask” means to return a mask array containing 0 and 1. 1 means the top k values. “both” means to return both value and indices.
  • is_ascend (boolean, optional, default=False) – Whether to choose k largest or k smallest. Top K largest elements will be chosen if set to false.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.transpose(*args, **kwargs)

Permute the dimensions of an array.

Examples:

x = [[ 1, 2],
     [ 3, 4]]

transpose(x) = [[ 1.,  3.],
                [ 2.,  4.]]

x = [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]]]

transpose(x) = [[[ 1.,  5.],
                 [ 3.,  7.]],

                [[ 2.,  6.],
                 [ 4.,  8.]]]

transpose(x, axes=(1,0,2)) = [[[ 1.,  2.],
                               [ 5.,  6.]],

                              [[ 3.,  4.],
                               [ 7.,  8.]]]

Defined in src/operator/tensor/matrix_op.cc:L142

Parameters:
  • data (NDArray) – Source input
  • axes (Shape(tuple), optional, default=()) – Target axis order. By default the axes will be inverted.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.uniform(*args, **kwargs)

Draw samples from a uniform distribution.

Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high):

nd.uniform(low=0, high=1, shape=(2,2)) = [[ 0.60276335,  0.85794562],
                                          [ 0.54488319,  0.84725171]]

Defined in src/operator/tensor/sample_op.cc:L24

Parameters:
  • low (float, optional, default=0) – The lower bound of distribution
  • high (float, optional, default=1) – The upper bound of distribution
  • shape (Shape(tuple), optional, default=()) – The shape of the output
  • ctx (string, optional, default='') – Context of output, in format [cpu|gpu|cpu_pinned](n).Only used for imperative calls.
  • dtype ({'None', 'float16', 'float32', 'float64'},optional, default='None') – DType of the output. If output given, set to type of output.If output not given and type not defined (dtype=None), set to float32.
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

mxnet.ndarray.where(*args, **kwargs)

Given three ndarrays, condition, x, and y, return an ndarray with the elements from x or y, depending on the elements from condition are true or false. x and y must have the same shape. If condition has the same shape as x, each element in the output array is from x if the corresponding element in the condition is true, and from y if false. If condtion does not have the same shape as x, it must be a 1D array whose size is the same as x’s first dimension size. Each row of the output array is from x’s row if the corresponding element from condition is true, and from y’s row if false.

From:src/operator/tensor/control_flow_op.cc:21

Parameters:
  • condition (NDArray) – condition array
  • x (NDArray) –
  • y (NDArray) –
  • out (NDArray, optional) – The output NDArray to hold the result.
Returns:

out – The output of this function.

Return type:

NDArray or list of NDArray

Random number interface of mxnet.

mxnet.random.seed(seed_state)

Seed the random number generators in MXNet.

This seed will affect behavior of functions in this module. It also affects the results from executors that contain random numbers such as dropout operators.

Parameters:seed_state (int) – The random number seed to set to all devices.

Notes

The random number generator of MXNet is, by default, device-specific. This means that if you set the same seed, the random number sequence generated from GPU0 can be different from CPU.