class MXNet::NDArray

Overview

The NDArray API provides imperative tensor operations on CPU/GPU. An NDArray represents a multi-dimensional, fixed-size homogeneous array.

x = MXNet::NDArray.array([[1, 2, 3], [4, 5, 6]], dtype: :float32)
x.shape      # [2, 3]
y = x + MXNet::NDArray.ones(x.shape) * 3
puts y       # [[4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]
             # <NDArray 2x3 float32 cpu(0)>
z = y.as_in_context(MXNet.gpu(0))
puts z       # [[4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]
             # <NDArray 2x3 float32 gpu(0)>

A detailed (albeit in Python) tutorial is available at NDArray - Imperative tensor operations on CPU/GPU.

Note: NDArray provides almost the same routines as Symbol. Most routines between these two packages share source code. But NDArray differs from Symbol in few aspects:

Included Modules

Extended Modules

Defined in:

mxnet/ndarray.cr

Class Method Summary

Instance Method Summary

Class Method Detail

def self.abs(data, **kwargs) #

Returns the element-wise absolute value of the input.

Assume x is an array with the following elements:

[-2, 0, 3]

Then:

abs(x) # => [2, 0, 3]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.activation(data : self, act_type, **kwargs) #

Applies an activation function element-wise to the input.

The following activation functions are supported:

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

Parameters

  • data (NDArray, required) The input array.
  • act_type (::Symbol, :relu, :softrelu, :tanh, :sigmoid, or :softsign, required) Activation function to be applied.
  • out (NDArray, optional) The output array.

def self.add(lhs : self | Number, rhs : self | Number) #

Returns element-wise sum of the input arrays.

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

Equivalent to lhs + rhs. Equivalent to .broadcast_add and .broadcast_plus for NDArray arguments.

Parameters


[View source]
def self.add_n(data : Array(self), **kwargs) #

Adds all input arguments element-wise.

add_n(a1,a2,...,an)=a1+a2+...+an

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

Parameters

  • data (Array(NDArray), required) List of arrays to add.
  • out (NDArray, optional) The output array.

def self.arange(start : Number, stop : Number? = nil, ctx = Context.current, **kwargs) #

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.

Examples:

arange(3)                                       # => [0.0, 1.0, 2.0]
arange(2, 6)                                    # => [2.0, 3.0, 4.0, 5.0]
arange(2, 6, step: 2)                           # => [2.0, 4.0]
arange(2, 6, step: 1.5, repeat: 2)              # => [2.0, 2.0, 3.5, 3.5, 5.0 , 5.0]
arange(2, 6, step: 2, repeat: 3, dtype: :int32) # => [2, 2, 2, 4, 4, 4]

Parameters

  • data (NDArray, required) Input data.
  • start (Number, optional, default = 0.0) Start of interval.
  • stop (Number, required) End of interval.
  • step (Number, optional, default = 1.0) Spacing between values.
  • repeat (Int, optional, default = 1) Number of times to repeat each value.
  • dtype (::Symbol, default = :float32) The data type of the output array.
  • ctx (Context, optional) Device context (default is the current context). Only used for imperative calls.
  • out (NDArray, optional) The output array.

def self.arccos(data, **kwargs) #

Returns element-wise inverse cosine of the input array.

The input should be in range [-1, 1]. The output is in the closed interval [0, 𝜋]

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

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.arccosh(data, **kwargs) #

Returns the inverse hyperbolic cosine of the input array, computed element-wise.

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.arcsin(data, **kwargs) #

Returns element-wise inverse sine of the input array.

The input should be in the range [-1, 1]. The output is in the closed interval [-𝜋/2, 𝜋/2].

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

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.arcsinh(data, **kwargs) #

Returns the inverse hyperbolic sine of the input array, computed element-wise.

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.arctan(data, **kwargs) #

Returns element-wise inverse tangent of the input array.

The output is in the closed interval [-𝜋/2, 𝜋/2]

arctan([-1, 0, 1]) = [-𝜋/4, 0, 𝜋/4]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.arctanh(data, **kwargs) #

Returns the inverse hyperbolic tangent of the input array, computed element-wise.

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.argmax(data, **kwargs) #

Returns indices of the maximum values along an axis.

In the case of multiple occurrences of maximum values, the indices corresponding to the first occurrence are returned.

Assume x is an array with the following elements:

[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]]

Then:

argmax(x, axis: 0) = [1.0, 1.0, 1.0]
argmax(x, axis: 1) = [2.0, 2.0]
argmax(x, axis: 1, keepdims: true) = [[2.0], [2.0]]

Parameters

  • data (NDArray, required) Input data.
  • axis (Int, optional, default = -1) The axis along which to perform the reduction. If omitted, the last axis is used.
  • keepdims (Bool, optional, default = false) If true, the reduced axis is left in the result as a dimension with size one.
  • out (NDArray, optional) The output array.

def self.argmin(data, **kwargs) #

Returns indices of the minimum values along an axis.

In the case of multiple occurrences of minimum values, the indices corresponding to the first occurrence are returned.

Assume x is an array with the following elements:

[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]]

Then:

argmin(x, axis: 0) = [0.0, 0.0, 0.0]
argmin(x, axis: 1) = [0.0, 0.0]
argmin(x, axis: 1, keepdims: true) = [[0.0], [0.0]]

Parameters

  • data (NDArray, required) Input data.
  • axis (Int, optional, default = -1) The axis along which to perform the reduction. If omitted, the last axis is used.
  • keepdims (Bool, optional, default = false) If true, the reduced axis is left in the result as a dimension with size one.
  • out (NDArray, optional) The output array.

def self.argsort(data, **kwargs) #

Returns the indices that would sort an input array along the given axis.

This function performs sorting along the given axis and returns an array of indices having the same shape as an input array that index data in the sorted order.

Assume x is an array with the following elements:

[[0.3, 0.2, 0.4], [0.1, 0.3, 0.2]]

Then:

argsort(x) = [[1.0, 0.0, 2.0], [0.0, 2.0, 1.0]]
argsort(x, axis: 0) = [[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]
argsort(x, axis: None) = [3.0, 1.0, 5.0, 0.0, 4.0, 2.0]
argsort(x, is_ascend: false) = [[2.0, 0.0, 1.0], [1.0, 2.0, 0.0]]

Parameters

  • data (NDArray, required) Input data.
  • axis (Int or None, optional, default = -1) The axis along which to choose sort the input tensor. If omitted, the last axis is used. If None, the flattened array is used.
  • is_ascend (Bool, optional, default = false) Whether to sort in ascending or descending order.
  • dtype (::Symbol, optional, default = :float32) The data type of the output indices.
  • out (NDArray, optional) The output array.

def self.array(source : Enumerable(T), dtype = nil, ctx = nil) forall T #

Creates an MXNet array from any enumerable object.

Parameters

  • source (Enumerable(T)) Any enumerable object, or nested enumerable object, whose elements can be converted to numbers.
  • dtype (::Symbol, optional) The type of the output array. If unspecified, the type is inferred from the source type.
  • ctx (Context, optional) Device context (default is the current context).

[View source]
def self.broadcast_add(lhs, rhs, **kwargs) #

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

.broadcast_add is an alias for .broadcast_plus.

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_add(x, y) # => [[1, 1, 1], [2, 2, 2]]

Parameters

  • lhs (NDArray, required) The first input.
  • rhs (NDArray, required) The second input.
  • out (NDArray, optional) The output array.

def self.broadcast_axis(data, **kwargs) #

Broadcasts the input array over particular axis.

Broadcasting is allowed on axes with size 1, such as from [2, 1, 3, 1] to [2, 8, 3, 9]. Elements will be duplicated on the broadcasted axis.

Assume x is an array with the following elements:

[[[1], [2]]]

Then:

broadcast_axis(x, axis: 2, size: 3) = [[[1, 1, 1], [2, 2, 2]]]
broadcast_axis(x, axis: [0, 2], size: [2, 3]) = [[[1, 1, 1], [2, 2, 2]], [[1, 1, 1], [2, 2, 2]]]

Parameters

  • out (NDArray, optional) The output array.
  • axis (Int or Array(Int), optional) The axis on which to perform the broadcasting.
  • size (Int or Array(Int), optional) Target sizes of the broadcasting axis.
  • out (NDArray, optional) The output array.

def self.broadcast_div(lhs, rhs, **kwargs) #

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

Assume x and y are arrays with the following elements:

[[6, 6, 6], [6, 6, 6]] # x
[[2], [3]]             # y

Then:

broadcast_div(x, y) # => [[3, 3, 3], [2, 2, 2]]

Parameters

  • lhs (NDArray, required) The first input.
  • rhs (NDArray, required) The second input.
  • out (NDArray, optional) The output array.

def self.broadcast_equal(lhs, rhs, **kwargs) #

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

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_equal(x, y) # => [[0, 0, 0], [1, 1, 1]]

Parameters

  • lhs (NDArray, required) The first input to be compared.
  • rhs (NDArray, required) The second input to be compared.
  • out (NDArray, optional) The output array.

def self.broadcast_greater(lhs, rhs, **kwargs) #

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

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_greater(x, y) # => [[1, 1, 1], [0, 0, 0]]

Parameters

  • lhs (NDArray, required) The first input to be compared.
  • rhs (NDArray, required) The second input to be compared.
  • out (NDArray, optional) The output array.

def self.broadcast_greater_equal(lhs, rhs, **kwargs) #

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

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_greater_equal(x, y) # => [[1, 1, 1], [1, 1, 1]]

Parameters

  • lhs (NDArray, required) The first input to be compared.
  • rhs (NDArray, required) The second input to be compared.
  • out (NDArray, optional) The output array.

def self.broadcast_lesser(lhs, rhs, **kwargs) #

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

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_lesser(x, y) # => [[0, 0, 0], [0, 0, 0]]

Parameters

  • lhs (NDArray, required) The first input to be compared.
  • rhs (NDArray, required) The second input to be compared.
  • out (NDArray, optional) The output array.

def self.broadcast_lesser_equal(lhs, rhs, **kwargs) #

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

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_lesser_equal(x, y) # => [[0, 0, 0], [1, 1, 1]]

Parameters

  • lhs (NDArray, required) The first input to be compared.
  • rhs (NDArray, required) The second input to be compared.
  • out (NDArray, optional) The output array.

def self.broadcast_like(lhs, rhs, **kwargs) #

Broadcasts the left hand side to have the same shape as right hand side.

Broadcasting is a mechanism that allows NDArray to perform arithmetic operations with other arrays of different shapes efficiently without creating multiple copies of arrays. See: Broadcasting for explanation.

Broadcasting is allowed on axes with size 1, such as from [2, 1, 3, 1] to [2, 8, 3, 9]. Elements will be duplicated on the broadcasted axes.

Assume x and y are arrays with the following elements:

[[1, 2, 3]]            # x
[[5, 6, 7], [7, 8, 9]] # y

Then:

broadcast_like(x, y) = [[1, 2, 3], [1, 2, 3]])

Parameters

  • lhs (NDArray, required) The first input.
  • rhs (NDArray, required) The second input.
  • lhs_axes (Array(Int), optional) Axes to perform broadcast on in the first input array.
  • rhs_axes (Array(Int), optional) Axes to copy from the second input array.
  • out (NDArray, optional) The output array.

def self.broadcast_logical_and(lhs, rhs, **kwargs) #

Returns element-wise logical and of the input arrays with broadcasting.

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_logical_and(x, y) # => [[0, 0, 0], [1, 1, 1]]

Parameters

  • lhs (NDArray, required) The first input.
  • rhs (NDArray, required) The second input.
  • out (NDArray, optional) The output array.

def self.broadcast_logical_or(lhs, rhs, **kwargs) #

Returns element-wise logical or of the input arrays with broadcasting.

Assume x and y are arrays with the following elements:

[[1, 1, 0], [1, 1, 0]] # x
[[1], [0]]             # y

Then:

broadcast_logical_or(x, y) # => [[1, 1, 1], [1, 1, 0]]

Parameters

  • lhs (NDArray, required) The first input.
  • rhs (NDArray, required) The second input.
  • out (NDArray, optional) The output array.

def self.broadcast_logical_xor(lhs, rhs, **kwargs) #

Returns element-wise logical xor of the input arrays with broadcasting.

Assume x and y are arrays with the following elements:

[[1, 1, 0], [1, 1, 0]] # x
[[1], [0]]             # y

Then:

broadcast_logical_or(x, y) # => [[0, 0, 1], [1, 1, 0]]

Parameters

  • lhs (NDArray, required) The first input.
  • rhs (NDArray, required) The second input.
  • out (NDArray, optional) The output array.

def self.broadcast_maximum(lhs, rhs, **kwargs) #

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

This function compares two input arrays and returns a new array having the element-wise maxima.

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_maximum(x, y) # => [[1, 1, 1], [1, 1, 1]]

Parameters

  • lhs (NDArray, required) The first input to be compared.
  • rhs (NDArray, required) The second input to be compared.
  • out (NDArray, optional) The output array.

def self.broadcast_minimum(lhs, rhs, **kwargs) #

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

This function compares two input arrays and returns a new array having the element-wise minima.

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_minimum(x, y) # => [[0, 0, 0], [1, 1, 1]]

Parameters

  • lhs (NDArray, required) The first input to be compared.
  • rhs (NDArray, required) The second input to be compared.
  • out (NDArray, optional) The output array.

def self.broadcast_minus(lhs, rhs, **kwargs) #

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

.broadcast_minus is an alias to the function .broadcast_sub.

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_minus(x, y) # => [[1, 1, 1], [0, 0, 0]]

Parameters

  • lhs (NDArray, required) The first input.
  • rhs (NDArray, required) The second input.
  • out (NDArray, optional) The output array.

def self.broadcast_mul(lhs, rhs, **kwargs) #

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

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_mul(x, y) # => [[0, 0, 0], [1, 1, 1]]

Parameters

  • lhs (NDArray, required) The first input.
  • rhs (NDArray, required) The second input.
  • out (NDArray, optional) The output array.

def self.broadcast_not_equal(lhs, rhs, **kwargs) #

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

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_not_equal(x, y) # => [[1, 1, 1], [0, 0, 0]]

Parameters

  • lhs (NDArray, required) The first input to be compared.
  • rhs (NDArray, required) The second input to be compared.
  • out (NDArray, optional) The output array.

def self.broadcast_plus(lhs, rhs, **kwargs) #

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

.broadcast_plus is an alias for .broadcast_add.

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_plus(x, y) # => [[1, 1, 1], [2, 2, 2]]

Parameters

  • lhs (NDArray, required) The first input.
  • rhs (NDArray, required) The second input.
  • out (NDArray, optional) The output array.

def self.broadcast_power(lhs, rhs, **kwargs) #

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

Assume x and y are arrays with the following elements:

[[2, 2, 2], [2, 2, 2]] # x
[[1], [2]]             # y

Then:

broadcast_power(x, y) # => [[2, 2, 2], [4, 4, 4]]

Parameters

  • lhs (NDArray, required) The base input.
  • rhs (NDArray, required) The exponent input.
  • out (NDArray, optional) The output array.

def self.broadcast_sub(lhs, rhs, **kwargs) #

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

.broadcast_sub is an alias to the function .broadcast_minus.

Assume x and y are arrays with the following elements:

[[1, 1, 1], [1, 1, 1]] # x
[[0], [1]]             # y

Then:

broadcast_sub(x, y) # => [[1, 1, 1], [0, 0, 0]]

Parameters

  • lhs (NDArray, required) The first input.
  • rhs (NDArray, required) The second input.
  • out (NDArray, optional) The output array.

def self.broadcast_to(data, **kwargs) #

Broadcasts the input array to a new shape.

Broadcasting is a mechanism that allows NDArray to perform arithmetic operations with other arrays of different shapes efficiently without creating multiple copies of arrays. See: Broadcasting for explanation.

Broadcasting is allowed on axes with size 1, such as from [2, 1, 3, 1] to [2, 8, 3, 9]. Elements will be duplicated on the broadcasted axes.

Assume x is an array with the following elements:

[[1, 2, 3]]

Then:

broadcast_to(x, shape: [2, 3]) = [[1, 2, 3], [1, 2, 3]])

The dimension which you do not want to change can also be specified as 0. So with shape: [2, 0], we will obtain the same result as in the above example.

Parameters

  • data (NDArray, required) Input data.
  • shape (Int or Array(Int), required) The shape of the desired array.
  • out (NDArray, optional) The output array.

def self.cbrt(data, **kwargs) #

Returns element-wise cube-root value of the input.

Assume x is an array with the following elements:

[1, 8, -125]

Then:

cbrt(x) = [1, 2, -5]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.ceil(data, **kwargs) #

Returns element-wise ceiling of the input.

The ceiling x is the smallest integer i, such that i >= x.

Assume x is an array with the following elements:

[-2.1, -1.9, 1.5, 1.9, 2.1]

Then:

ceil(x) = [-2.0, -1.9, 2.0, 2.0, 3.0]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.clip(data, a_min, a_max, **kwargs) #

Clips (limits) the values in an array.

Given an interval, values outside the interval are clipped to the interval edges. Clipping x between a_min and a_x would be:

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

Assume x is an array with the following elements:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Then:

clip(x, 1, 8) # => [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]

Parameters

  • data (NDArray, required) Input data.
  • a_min (Float, required) Minimum value.
  • a_max (Float, required) Maximum value.
  • out (NDArray, optional) The output array.

def self.concat(data : Array(self), **kwargs) #

Joins input arrays along a given axis.

The dimensions of the input arrays should be the same except for the axis along which they will be concatenated. The dimension of the output array along the concatenated axis will be equal to the sum of the corresponding dimensions of the input arrays.

Assume x and y are arrays with the following elements:

[[1, 2], [3, 4]] # x
[[1, 4], [1, 1]] # y

Then:

concat(x, y) # => [[1, 2, 1, 4], [3, 4, 1, 1]]

Parameters

  • data (Array(NDArray), required) List of arrays to concatenate.
  • dim (Int, default = 1) The dimension to be concated.
  • out (NDArray, optional) The output array.

def self.convolution(data : self, weight : self?, bias : self?, kernel, num_filter, **kwargs) #

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

For general 2-D convolution, the shapes are:

  • data: [batch_size, channel, height, width]
  • weight: [num_filter, channel, kernel[0], kernel[1]]
  • bias: [num_filter]
  • out: [batch_size, num_filter, out_height, out_width]

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

The default data layout is NCHW, namely (batch_size, channel, height, width). We can choose other layouts such as NWC.

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 concatenating all the g results.

1-D convolution does not have height dimension but only width in space. The shapes are:

  • data: [batch_size, channel, width]
  • weight: [num_filter, channel, kernel[0]]
  • bias: [num_filter]
  • out: [batch_size, num_filter, out_width]

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

  • data: [batch_size, channel, depth, height, width]
  • weight: [num_filter, channel, kernel[0], kernel[1], kernel[2]]
  • bias: [num_filter]
  • out: [batch_size, num_filter, out_depth, out_height, out_width]

Both weight and bias are learnable parameters.

There are other options to tune the performance:

  • cudnn_tune: enabling 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, nil (default) - the behavior is determined by the environment variable "MXNET_CUDNN_AUTOTUNE_DEFAULT" -- 0 for off, 1 for limited workspace (default), 2 for fastest.
  • workspace: a larger number leads to more (GPU) memory usage but may improve the performance.

Parameters

  • data (NDArray, required) Input data.
  • weight (NDArray, required) Weight matrix.
  • bias (NDArray, required) Bias parameter.
  • kernel (Array(Int), shape, required) Convolution kernel size: [w], [h, w] or [d, h, w].
  • stride (Array(Int), shape, optional, default = []) Convolution stride: [w], [h, w] or [d, h, w]. Defaults to 1 for each dimension.
  • dilate (Array(Int), shape, optional, default = []) Convolution dilation: [w], [h, w] or [d, h, w]. Defaults to 1 for each dimension.
  • pad (Array(Int), shape, optional, default = []) Zero pad for convolution: [w], [h, w] or [d, h, w]. Defaults to no padding.
  • num_filter (Int::Unsigned, required) Convolution filter (channel) number.
  • num_group (Int::Unsigned, optional, default = 1) Number of group partitions.
  • workspace (Int::Unsigned, optional, default = 1024) Maximum temporary workspace allowed (MB) for convolution. This parameter has two usages. When CUDNN is not used, it determines the effective batch size of the convolution kernel. When CUDNN is used, it controls the maximum temporary storage used for tuning the best CUDNN kernel when "limited_workspace" strategy is used.
  • no_bias (Bool, optional, default = false) Whether to disable bias parameter.
  • cudnn_tune (::Symbol, :fastest, :limited_workspace, :off or nil, optional) Whether to pick the convolution algorithm by running a performance test.
  • cudnn_off (Bool, optional, default = false) Turn off cudnn for this layer.
  • layout (String, "NCDHW", "NCHW", "NCW", "NDHWC", "NHWC", "NWC" or nil, optional) Set layout for input, output and weight. Empty for default layout: "NCW" for 1D, "NCHW" for 2D and "NCDHW" for 3D. "NHWC" and "NDHWC" are only supported on GPU.
  • out (NDArray, optional) The output array.

def self.cos(data, **kwargs) #

Computes the element-wise cosine of the input array.

The input should be in radians (2\𝜋 radians equals 360 degrees).

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

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.cosh(data, **kwargs) #

Returns the hyperbolic cosine of the input array, computed element-wise.

cosh(x) = (exp(x) + exp(-x)) / 2

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.degrees(data, **kwargs) #

Converts each element of the input array from radians to degrees.

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

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.diag(data, **kwargs) #

Extracts a diagonal or constructs a diagonal array.

.diag‘s behavior depends on the input array dimensions:

  • 1-D arrays: constructs a 2-D array with the input as its diagonal, all other elements are zero.
  • N-D arrays: extracts the diagonals of the sub-arrays with axes specified by axis1 and axis2. The output shape is decided by removing the axes numbered axis1 and axis2 from the input shape and appending to the result a new axis with the size of the diagonals in question.

For example, when the input shape is [2, 3, 4, 5], axis1 and axis2 are 0 and 2 respectively and k is 0, the resulting shape is [3, 5, 2].

Assume x and y are arrays with the following elements:

[[1, 2, 3], [4, 5, 6]]               # x
[[[1, 2], [3, 4]], [[5, 6], [7, 8]]] # y

Then:

diag(x) = [1, 5]
diag(x, k: 1) = [2, 6]
diag(x, k: -1) = [4]

diag(y) = [[1, 7], [2, 8]]
diag(y, k: 1) = [[3], [4]]
diag(y, axis1: -2, axis2: -1) = [[1, 4], [5, 8]]

Parameters

  • data (NDArray, required) Input data.
  • k (Int, optional, default = 0) The diagonal in question. The default is 0. Use k > 0 for diagonals above the main diagonal, and k < 0 for diagonals below the main diagonal.
  • axis1 (Int, optional, default = 0) The first axis of the sub-arrays of interest. Ignored when the input is a 1-D array.
  • axis2 (Int, optional, default = 1) The second axis of the sub-arrays of interest. Ignored when the input is a 1-D array.
  • out (NDArray, optional) The output array.

def self.divide(lhs : self | Number, rhs : self | Number) #

Returns element-wise division of the input arrays.

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

Equivalent to lhs / rhs. Equivalent to .broadcast_div for NDArray arguments.

Parameters


[View source]
def self.dot(lhs, rhs, **kwargs) #

Computes the 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

Assume x and y are arrays with the following elements:

[[1, 2], [3, 4]] # x
[[4, 3], [1, 1]] # y

Then:

dot(x, y) # => [[8, 5], [20, 13]]

Parameters

  • lhs (NDArray, required) The first input.
  • rhs (NDArray, required) The second input.
  • transpose_a (Bool, default = false) If true then transpose the first input before dot.
  • transpose_b (Bool, default = false) If true then transpose the second input before dot.
  • out (NDArray, optional) The output array.

def self.empty(shape : Int | Array(Int), dtype = :float32, ctx = nil) #

Returns an MXNet array of given shape and type, without initializing entries.

Parameters

  • shape (Int or Array(Int)) The shape of the empty array.
  • dtype (::Symbol, default = :float32) The data type of the output array.
  • ctx (Context, optional) Device context (default is the current context).

[View source]
def self.equal(lhs : self | Number, rhs : self | Number) #

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

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

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

Equivalent to lhs == rhs. Equivalent to .broadcast_equal for NDArray arguments.

Parameters


[View source]
def self.exp(data, **kwargs) #

Returns element-wise exponential value of the input.

Assume x is an array with the following elements:

[0.0, 1.0, 2.0]

Then:

exp(x) = [1.0, 2.71828175, 7.38905621]

The storage type of .exp output is always dense.

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.expand_dims(data, axis, **kwargs) #

Inserts a new axis of 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].

Parameters

  • data (NDArray, required) Input data.
  • axis (Int, required) Position where new axis is to be inserted. Suppose that the input array‘s dimension is ndim, the range of the inserted axis is [-ndim, ndim].
  • out (NDArray, optional) The output array.

def self.expm1(data, **kwargs) #

Returns exp(x) - 1 computed element-wise on the input.

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

Assume x is an array with the following elements:

[0.0, 1.0, 2.0]

Then:

expm1(x) = [0.0, 1.71828182, 6.38905609]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.fix(data, **kwargs) #

Returns element-wise rounded value to the nearest integer towards zero.

Assume x is an array with the following elements:

[-2.1, -1.9, 1.5, 1.9, 2.1]

Then:

fix(x) = [-2.0, -1.0, 1.0, 1.0, 2.0]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.flatten(data, **kwargs) #

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

For an input array with shape (d1, d2, ..., dk), .flatten reshapes the input array into an output array of shape (d1, d2 * ... * dk).

Note that the bahavior of this function is different from Array#flatten, which behaves similar to .reshape(shape: [-1]).

Assume x is an array with the following elements:

[[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]

Then:

flatten(x).shape # => [2, 6]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.flip(data, axis, **kwargs) #

Reverses the order of elements along given axis while preserving array shape.

Assume x is an array with the following elements:

[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]

Then:

flip(x, axis: 0) # => [[5, 6, 7, 8, 9], [0, 1, 2, 3, 4]]
flip(x, axis: 1) # => [[4, 3, 2, 1, 0], [9, 8, 7, 6, 5]]

Parameters

  • data (NDArray, required) Input data.
  • axis (Int, required) The axis on which to reverse elements.
  • out (NDArray, optional) The output array.

def self.floor(data, **kwargs) #

Returns the element-wise floor of the input.

The floor of x is the largest integer i, such that i <= x.

Assume x is an array with the following elements:

[-2.1, -1.9, 1.5, 1.9, 2.1]

Then:

floor(x) = [-3.0, -2.0, 1.0, 1.0, 2.0]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.full(shape : Int | Array(Int), value : T, dtype = nil, ctx = nil) forall T #

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

Parameters

  • shape (Int or Array(Int)) The shape of the new array.
  • value (T) A fill value of any supported numeric type.
  • dtype (::Symbol, optional) The data type of the output array. If unspecified, the type is inferred from the value.
  • ctx (Context, optional) Device context (default is the current context).

[View source]
def self.fully_connected(data : self, weight : self?, bias : self?, num_hidden : Int, **kwargs) #

Applies a linear transformation: Y = XWᵀ + b.

If flatten is true, then the shapes are:

  • data: [batch_size, x1, x2, ..., xn]
  • weight: *[num_hidden, x1 * x2 * ... xn]
  • bias: [num_hidden]
  • out: [batch_size, num_hidden]

If flatten is false, then the shapes are:

  • data: [x1, x2, ..., xn, input_dim]
  • weight: [num_hidden, input_dim]
  • bias: [num_hidden]
  • out: [x1, x2, ..., xn, num_hidden]

The learnable parameters include both weight and bias.

If no_bias is true, then the bias term is ignored.

Parameters

  • data (NDArray, required) Input data.
  • weight (NDArray, required) Weight matrix.
  • bias (NDArray, required) Bias parameter.
  • num_hidden (Int, required) Number of hidden nodes of the output.
  • no_bias (Bool, optional, default = false) Whether to disable bias parameter.
  • flatten (Bool, optional, default = true) Whether to collapse all but the first axis of the input data tensor.
  • out (NDArray, optional) The output array.

def self.gamma(data, **kwargs) #

Returns the gamma function (extension of the factorial function to the reals), computed element-wise on the input array.

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.gammaln(data, **kwargs) #

Returns the log of the absolute value of the gamma function, computed element-wise on the input array.

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.greater(lhs : self | Number, rhs : self | Number) #

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

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

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

Equivalent to lhs > rhs. Equivalent to .broadcast_greater for NDArray arguments.

Parameters


[View source]
def self.greater_equal(lhs : self | Number, rhs : self | Number) #

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

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

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

Equivalent to lhs >= rhs. Equivalent to .broadcast_greater_equal for NDArray arguments.

Parameters


[View source]
def self.hypot(lhs : self, rhs : self, **kwargs) #

Given the legs of a right triangle, return its hypotenuse.

Parameters

  • lhs (NDArray, required) Input data.
  • rhs (NDArray, required) Input data.

def self.imperative_invoke(op, *ndargs, out _out : self? = nil, **kwargs) #

TODO cache op handles


[View source]
def self.lesser(lhs : self | Number, rhs : self | Number) #

Returns the result of element-wise less than (#<) comparison operation.

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

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

Equivalent to lhs < rhs. Equivalent to .broadcast_lesser for NDArray arguments.

Parameters


[View source]
def self.lesser_equal(lhs : self | Number, rhs : self | Number) #

Returns the result of element-wise less than or equal to (#<=) comparison operation.

For each element in input arrays, return 1 (true) if lhs element is less than or equal to rhs element, otherwise return 0 (false).

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

Equivalent to lhs <= rhs. Equivalent to .broadcast_lesser_equal for NDArray arguments.

Parameters


[View source]
def self.load(fname) #

Loads arrays from a file.

Returns Array(NDArray) or Hash(String, NDArray). See .save for more detail on format.

Examples of filenames:

  • /path/to/file
  • s3://my-bucket/path/to/file (if MXNet is compiled with AWS S3 supports)
  • hdfs://path/to/file (if MXNet is compiled with HDFS supports)

Parameters

  • fname (String) The filename.

[View source]
def self.log(data, **kwargs) #

Returns element-wise natural logarithmic value of the input.

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

The storage type of .log output is always dense.

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.log10(data, **kwargs) #

Returns element-wise base-10 logarithmic value of the input.

10**log10(x) = x

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.log1p(data, **kwargs) #

Returns .log(1 + x) computed element-wise on the input.

This function is more accurate than explicitly calculating .log(1 + x) for small x.

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.log2(data, **kwargs) #

Returns element-wise base-2 logarithmic value of the input.

2**log2(x) = x

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.log_softmax(data, **kwargs) #

Computes the log softmax of the input.

This is equivalent to computing .softmax followed by .log.

Assume x is an array with the following elements:

[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]

Then:

softmax(x, axis: 0) # => [[-0.6931, -0.6931, -0.6931], [-0.6931, -0.6931, -0.6931]]
softmax(x, axis: 1) # => [[-1.0986, -1.0986, -1.0986], [-1.0986, -1.0986, -1.0986]]

Parameters

  • data (NDArray, required) Input data.
  • axis (Int, optional, default = -1) The axis along which to compute softmax.
  • temperature (Float, optional, default = 1.0) Temperature parameter in softmax.
  • dtype (::Symbol, :float16, :float32 or :float64, optional) Type of the output in case this can't be inferred. Defaults to the same type as the input if not defined.
  • out (NDArray, optional) The output array.

def self.logical_and(lhs : self | Number, rhs : self | Number) #

Returns the result of element-wise logical and (#&) comparison operation.

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

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

Equivalent to lhs & rhs. Equivalent to .broadcast_logical_and for NDArray arguments.

Parameters


[View source]
def self.logical_not(data, **kwargs) #

Performs element-wise logical not of the input array.

logical_not([-2, 0, 1]) = [0, 1, 0]
  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.logical_or(lhs : self | Number, rhs : self | Number) #

Returns the result of element-wise logical or (#|) comparison operation.

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

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

Equivalent to lhs | rhs. Equivalent to .broadcast_logical_or for NDArray arguments.

Parameters


[View source]
def self.logical_xor(lhs : self | Number, rhs : self | Number) #

Returns the result of element-wise logical xor (#^) comparison operation.

For each element in input arrays, return 1 (true) if either lhs element or rhs element is true (not zero) but not both, otherwise return 0 (false).

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

Equivalent to lhs ^ rhs. Equivalent to .broadcast_logical_xor for NDArray arguments.

Parameters


[View source]
def self.max(data, **kwargs) #

Computes the max of array elements over given axes.

Parameters

  • data (NDArray, required) Input data.
  • axis (Int or Array(Int), optional) The axis or axes along which to perform the reduction. By default it computes over all elements into a scalar array with shape [1]. If axis is Int, a reduction is performed on a particular axis. If axis is Array(Int), a reduction is performed on all the axes specified in the list. If exclude is true, reduction will be performed on the axes that are not in axis instead. Negative values means indexing from right to left.
  • keepdims (Bool, optional, default = false) If true, the reduced axes are left in the result as a dimension with size one.
  • exclude (Bool, optional, default = false) Whether to perform reduction on axes that are not in axis instead.
  • out (NDArray, optional) The output array.

def self.maximum(lhs : self | Number, rhs : self | Number) #

Returns element-wise maximum of the input arrays.

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

Equivalent to .broadcast_maximum for NDArray arguments.

Parameters


[View source]
def self.mean(data, **kwargs) #

Computes the mean of array elements over given axes.

Parameters

  • data (NDArray, required) Input data.
  • axis (Int or Array(Int), optional) The axis or axes along which to perform the reduction. By default it computes over all elements into a scalar array with shape [1]. If axis is Int, a reduction is performed on a particular axis. If axis is Array(Int), a reduction is performed on all the axes specified in the list. If exclude is true, reduction will be performed on the axes that are not in axis instead. Negative values means indexing from right to left.
  • keepdims (Bool, optional, default = false) If true, the reduced axes are left in the result as a dimension with size one.
  • exclude (Bool, optional, default = false) Whether to perform reduction on axes that are not in axis instead.
  • out (NDArray, optional) The output array.

def self.min(data, **kwargs) #

Computes the min of array elements over given axes.

Parameters

  • data (NDArray, required) Input data.
  • axis (Int or Array(Int), optional) The axis or axes along which to perform the reduction. By default it computes over all elements into a scalar array with shape [1]. If axis is Int, a reduction is performed on a particular axis. If axis is Array(Int), a reduction is performed on all the axes specified in the list. If exclude is true, reduction will be performed on the axes that are not in axis instead. Negative values means indexing from right to left.
  • keepdims (Bool, optional, default = false) If true, the reduced axes are left in the result as a dimension with size one.
  • exclude (Bool, optional, default = false) Whether to perform reduction on axes that are not in axis instead.
  • out (NDArray, optional) The output array.

def self.minimum(lhs : self | Number, rhs : self | Number) #

Returns element-wise minimum of the input arrays.

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

Equivalent to .broadcast_minimum for NDArray arguments.

Parameters


[View source]
def self.modulo(lhs : self | Number, rhs : self | Number) #

Returns element-wise modulo of the input arrays.

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

Equivalent to lhs % rhs. Equivalent to .broadcast_mod for NDArray arguments.

Parameters


[View source]
def self.multiply(lhs : self | Number, rhs : self | Number) #

Returns element-wise product of the input arrays.

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

Equivalent to lhs * rhs. Equivalent to .broadcast_mul for NDArray arguments.

Parameters


[View source]
def self.nanprod(data, **kwargs) #

Computes the product of array elements over given axes treating not-a-number values (NaN) as one.

See .prod.

Parameters

  • data (NDArray, required) Input data.
  • axis (Int or Array(Int), optional) The axis or axes along which to perform the reduction. axis: [] or axis: nil will compute over all elements into a scalar array with shape [1]. If axis is an Int, a reduction is performed on a particular axis. If axis is an array of Int, a reduction is performed on all the axes specified in the array. If exclude is true, reduction will be performed on the axes that are not in axis instead. Negative values means indexing from right to left.
  • keepdims (Bool, optional, default = false) If this is set to true, the reduced axes are left in the result as dimension with size one.
  • exclude (Bool, optional, default = false) Whether to perform reduction on axis that are not in axis instead.
  • out (NDArray, optional) The output array.

def self.nansum(data, **kwargs) #

Computes the sum of array elements over given axes treating not-a-number values (NaN) as zero.

See .sum.

Parameters

  • data (NDArray, required) Input data.
  • axis (Int or Array(Int), optional) The axis or axes along which to perform the reduction. axis: [] or axis: nil will compute over all elements into a scalar array with shape [1]. If axis is an Int, a reduction is performed on a particular axis. If axis is an array of Int, a reduction is performed on all the axes specified in the array. If exclude is true, reduction will be performed on the axes that are not in axis instead. Negative values means indexing from right to left.
  • keepdims (Bool, optional, default = false) If this is set to true, the reduced axes are left in the result as dimension with size one.
  • exclude (Bool, optional, default = false) Whether to perform reduction on axis that are not in axis instead.
  • out (NDArray, optional) The output array.

def self.norm(data, **kwargs) #

Computes the norm.

This operator computes the norm on an array with the specified axis, depending on the value of the ord parameter. By default, it computes the L2 norm on the entire array. Currently only ord: 2 supports sparse arrays.

Assume x is an array with the following elements:

[[[1.0, 2.0], [3.0, 4.0]], [[2.0, 2.0], [5.0, 6.0]]]

Then:

norm(x, ord: 2, axis: 1) # => [[3.1622, 4.4721], [5.3851, 6.3245]]
norm(x, ord: 1, axis: 1) # => [[40., 6.0], [7.0, 8.0]]

Parameters

  • data (NDArray, required) Input data.
  • ord (Int, optional, default = 2) Order of the norm. Currently ord: 1 and ord: 2 are supported.
  • axis (Int or Array(Int), optional) The axis or axes along which to perform the reduction. By default it computes over all elements into a scalar array with shape [1]. If axis is Int, a reduction is performed on a particular axis. If axis is Array(Int), it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed.
  • out_dtype (::Symbol, :float16, :float32, :float64, :int32, :int64 or :int8, optional) The data type of the output.
  • keepdims (Bool, optional, default = false) If true, the reduced axes are left in the result as a dimension with size one.
  • out (NDArray, optional) The output array.

def self.not_equal(lhs : self | Number, rhs : self | Number) #

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

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

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

Equivalent to lhs != rhs. Equivalent to .broadcast_not_equal for NDArray arguments.

Parameters


[View source]
def self.one_hot(indices, depth, **kwargs) #

Returns a one-hot array.

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

.one_hot with indices of shape [i0, i1] and depth of d would result in an output array of shape [i0, i1, d] with:

output[i, j, 0..-1] = off_value
output[i, j, indices[i, j]] = on_value

Assume x is an array with the following elements:

[1, 0, 2, 0]

Then:

one_hot(x, 3) # => [[0, 1, 0], [1, 0, 0], [0, 0, 1], [1, 0, 0]]

Parameters

  • indices (NDArray, required) Array of locations where to set on_value.
  • depth (Int, required) Depth of the one hot dimension.
  • on_value (Float, optional, default = 1.0) The value assigned to the locations represented by indices.
  • off_value (Float, optional, default = 0.0) The value assigned to the locations not represented by indices.
  • dtype (::Symbol, optional, default = :float32) Type of the output.
  • out (NDArray, optional) The output array.

def self.ones(shape : Int | Array(Int), ctx = Context.current, **kwargs) #

Returns an array filled with all ones, with the given shape.

Parameters

  • data (NDArray, required) Input data.
  • shape (Int or Array(Int)) The shape of the array.
  • dtype (::Symbol, default = :float32) The data type of the output array.
  • ctx (Context, optional) Device context (default is the current context). Only used for imperative calls.
  • out (NDArray, optional) The output array.

def self.ones_like(data, **kwargs) #

Returns an array of ones with the same shape, data type and storage type as the input array.

Assume x is an array with the following elements:

[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]

Then:

ones_like(x) # => [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.pick(data, index, **kwargs) #

Picks elements from an input array according to the indices along the given axis.

Given an input array of shape [d0, d1] and indices of shape [i0], the result will be an output array of shape [i0] with:

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

By default, if any index mentioned is too large, it is replaced by the index that addresses the last element along an axis (clip mode).

This function supports n-dimensional input and (n-1)-dimensional indices arrays.

Assume x, i, j, and k are arrays with the following elements:

[[1, 2], [3, 4], [5, 6]] # x
[0, 1]                   # i
[0, 1, 0]                # j
[1, 0, 2]                # k

Then:

# pick elements with specified indices along axis 0
pick(x, index: i, 0) # => [1, 4]
# pick elements with specified indices along axis 1
pick(x, index: j, 1) # => [1, 4, 5]
# pick elements with specified indices along axis 1 --
# dims are maintained
pick(x, index: k, 1, keepdims: true) # => [[2], [3], [6]]

Parameters

  • data (NDArray, required) The input array.
  • index (NDArray, required) The index array.
  • axis (Int or nil, optional, default = -1) The axis to pick the elements. Negative values mean indexing from right to left. If nil, elements in the index with respect to the flattened input will be picked.
  • keepdims (Bool, optional, default = false) If true, the axis where we pick the elements is left in the result as a dimension with size one.
  • out (NDArray, optional) The output array.

def self.pooling(data : self, **kwargs) #

Performs pooling on the input.

The shapes for 1-D pooling are:

  • data and out: [batch_size, channel, width] ("NCW" layout) or [batch_size, width, channel] ("NWC" layout)

The shapes for 2-D pooling are:

  • data and out: [batch_size, channel, height, width] ("NCHW" layout) or [batch_size, height, width, channel] ("NHWC" layout)

Three pooling options are supported by pool_type:

  • avg: average pooling
  • max: max pooling
  • sum: sum pooling
  • lp: Lp pooling

For 3-D pooling, an additional depth dimension is added before height. Namely the input data and output will have shape: [batch_size, channel, depth, height, width] ("NCDHW" layout) or [batch_size, depth, height, width, channel] ("NDHWC" layout).

Notes on Lp pooling:

Lp pooling was first introduced by this paper: https://arxiv.org/pdf/1204.3968.pdf. L-1 pooling is simply sum pooling, while L-inf pooling is simply max pooling. We can see that Lp pooling stands between those two, in practice the most common value for p is 2.

Parameters

  • data (NDArray, required) Input data.
  • kernel (Array(Int), shape, optional, default = []) Pooling kernel size: [y, x] or [d, y, x].
  • pool_type (::Symbol, :avg, :lp, :max or :sum, optional, default = :max) Pooling type to be applied.
  • global_pool (Bool, optional, default = false) Ignore kernel size; do global pooling based on current input feature map.
  • cudnn_off (Bool, optional, default = false) Turn off cudnn pooling and use MXNet pooling operator.
  • pooling_convention (::Symbol, :full, :same, or :valid, optional, default = :valid) Pooling convention to be applied.
  • stride (Array(Int), shape, optional, default = []) Stride for pooling: [y, x] or [d, y, x]. Defaults to 1 for each dimension.
  • pad (Array(Int), shape, optional, default = []) Pad for pooling: [y, x] or [d, y, x]. Defaults to no padding.
  • p_value (Int, optional) Value of p for Lp pooling, can be 1 or 2, required for Lp pooling.
  • count_include_pad (Bool, optional) Only used for average pooling. Specify whether to count padding elements for average calculation. For example, with a 55 kernel on a 33 corner of a image, the sum of the 9 valid elements will be divided by 25 if this is set to true, or it will be divided by 9 if this is set to false. Defaults to true.
  • layout (String, "NCDHW", "NCHW", "NCW", "NDHWC", "NHWC", "NWC" or nil, optional) Set layout for input, output and weight. Empty for default layout: "NCW" for 1D, "NCHW" for 2D and "NCDHW" for 3D. "NHWC" and "NDHWC" are only supported on GPU.
  • out (NDArray, optional) The output array.

def self.power(base : self | Number, exp : self | Number) #

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

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

Equivalent to base ** exp. Equivalent to .broadcast_power for NDArray arguments.

Parameters


[View source]
def self.prod(data, **kwargs) #

Computes the product of array elements over given axes.

Assume x is an array with the following elements:

[[[1, 2], [2, 3], [1, 3]],
 [[1, 4], [4, 3], [5, 2]],
 [[7, 1], [7, 2], [7, 3]]]

Then:

prod(x, axis: 1) # => [[2, 18], [20, 24], [343, 6]]
prod(x, axis: [1, 2]) # => [36, 480, 2058]

Parameters

  • data (NDArray, required) Input data.
  • axis (Int or Array(Int), optional) The axis or axes along which to perform the reduction. axis: [] or axis: nil will compute over all elements into a scalar array with shape [1]. If axis is an Int, a reduction is performed on a particular axis. If axis is an array of Int, a reduction is performed on all the axes specified in the array. If exclude is true, reduction will be performed on the axes that are not in axis instead. Negative values means indexing from right to left.
  • keepdims (Bool, optional, default = false) If this is set to true, the reduced axes are left in the result as dimension with size one.
  • exclude (Bool, optional, default = false) Whether to perform reduction on axis that are not in axis instead.
  • out (NDArray, optional) The output array.

def self.radians(data, **kwargs) #

Converts each element of the input array from degrees to radians.

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

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.random_exponential(lam : Number = 1.0, ctx : Context = Context.current, **kwargs) #

Draws random samples from an exponential distribution.

Samples are distributed according to an exponential distribution parametrized by lam (rate).

random_exponential(4.0, shape: [2, 2]) # => [[0.0097189 , 0.08999364], [0.04146638, 0.31715935]]

Parameters

  • lam (Float, default = 1.0) Lambda parameter (rate) of the exponential distribution.
  • shape (Int or Array(Int)) The shape of the output.
  • dtype (::Symbol, default = :float32) The data type of the output in case this can’t be inferred.
  • ctx (Context, optional) Device context (default is the current context). Only used for imperative calls.
  • out (NDArray, optional) The output array.

def self.random_gamma(alpha : Number = 1.0, beta : Number = 1.0, ctx : Context = Context.current, **kwargs) #

Draws random samples from a gamma distribution.

Samples are distributed according to a gamma distribution parametrized by alpha (shape) and beta (scale).

random_gamma(9.0, 0.5, shape: [2, 2]) # => [[6.2806954, 6.1658335], [4.5625057, 6.479337]]

Parameters

  • alpha (Float, default = 1.0) Alpha parameter (shape) of the gamma distribution.
  • beta (Float, default = 1.0) Beta parameter (scale) of the gamma distribution.
  • shape (Int or Array(Int)) The shape of the output.
  • dtype (::Symbol, default = :float32) The data type of the output in case this can’t be inferred.
  • ctx (Context, optional) Device context (default is the current context). Only used for imperative calls.
  • out (NDArray, optional) The output array.

def self.random_normal(loc : Number = 0.0, scale : Number = 1.0, ctx : Context = Context.current, **kwargs) #

Draws random samples from a normal (Gaussian) distribution.

Samples are distributed according to a normal distribution parametrized by loc (mean) and scale (standard deviation).

random_normal(0.0, 1.0, shape: [2, 2]) # => [[1.89171135, -1.16881478], [-1.23474145, 1.55807114]]

Parameters

  • loc (Float, default = 0.0) Mean of the distribution.
  • scale (Float, default = 1.0) Standard deviation of the distribution.
  • shape (Int or Array(Int)) The shape of the output.
  • dtype (::Symbol, default = :float32) The data type of the output in case this can’t be inferred.
  • ctx (Context, optional) Device context (default is the current context). Only used for imperative calls.
  • out (NDArray, optional) The output array.

def self.random_poisson(lam : Number = 1.0, ctx : Context = Context.current, **kwargs) #

Draws random samples from a Poisson distribution.

Samples are distributed according to a Poisson distribution parametrized by lam (rate). Samples will always be returned as a floating point data type.

random_poisson(4.0, shape: [2, 2]) # => [[5.0, 2.0], [4.0, 6.0]]

Parameters

  • lam (Float, default = 1.0) Lambda parameter (rate) of the Poisson distribution.
  • shape (Int or Array(Int)) The shape of the output.
  • dtype (::Symbol, default = :float32) The data type of the output in case this can’t be inferred.
  • ctx (Context, optional) Device context (default is the current context). Only used for imperative calls.
  • out (NDArray, optional) The output array.

def self.random_randint(low : Int, high : Int, ctx : Context = Context.current, **kwargs) #

Draws random samples from a discrete uniform distribution.

Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high).

random_randint(0, 5, shape: [2, 2]) # => [[0, 2], [3, 1]]

Parameters

  • low (Int, required) Lower boundary of the output interval.
  • high (Int, required) Upper boundary of the output interval.
  • shape (Int or Array(Int)) The shape of the output.
  • dtype (::Symbol, default = :int32) The data type of the output.
  • ctx (Context, optional) Device context (default is the current context). Only used for imperative calls.
  • out (NDArray, optional) The output array.

def self.random_uniform(low : Number = 0.0, high : Number = 1.0, ctx : Context = Context.current, **kwargs) #

Draws random samples from a uniform distribution.

Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high).

random_uniform(0.0, 1.0, shape: [2, 2]) # => [[0.60276335, 0.85794562], [0.54488319, 0.84725171]]

Parameters

  • low (Float, default = 0.0) Lower bound of the distribution.
  • high (Float, default = 1.0) Upper bound of the distribution.
  • shape (Int or Array(Int)) The shape of the output.
  • dtype (::Symbol, default = :float32) The data type of the output in case this can’t be inferred.
  • ctx (Context, optional) Device context (default is the current context). Only used for imperative calls.
  • out (NDArray, optional) The output array.

def self.rcbrt(data, **kwargs) #

Returns element-wise inverse cube-root value of the input.

rcbrt(x) = 1/cbrt(x)

Assume x is an array with the following elements:

[1, 8, -125]

Then:

rcbrt(x) = [1.0, 0.5, -0.2]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.reciprocal(data, **kwargs) #

Returns the reciprocal of the argument, element-wise.

reciprocal(x) = 1/x

Assume x is an array with the following elements:

[-2, 1, 3, 1.6, 0.2]

Then:

reciprocal(x) = [-0.5, 1.0, 0.33333334, 0.625, 5.0]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.relu(data, **kwargs) #

Computes the rectified linear activation.

y=max(input,0)

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.reshape(data, shape, **kwargs) #

Reshapes the input array.

Returns a copy of the array with a new shape without altering any data.

Assume x is an array with the following elements:

[1, 2, 3, 4]

Then:

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

Some dimensions of the shape can take special values from the set {0, -1, -2, -3, -4}. The significance of each is explained below:

  • 0 copies this dimension from the input to the output shape:
zeros([2, 3, 4]).reshape([4, 0, 2]).shape # => [4, 3, 2]
zeros([2, 3, 4]).reshape([2, 0, 0]).shape # => [2, 3, 4]
  • -1 infers the dimension of the output shape by using the remainder of the input dimensions, keeping the size of the new array the same as that of the input array. At most one dimension can be -1:
zeros([2, 3, 4]).reshape([6, 1, -1]).shape # => [6, 1, 4]
zeros([2, 3, 4]).reshape([3, -1, 8]).shape # => [3, 1, 8]
zeros([2, 3, 4]).reshape([-1]).shape # => [24]
  • -2 copies all/the remainder of the input dimensions to the output shape:
zeros([2, 3, 4]).reshape([-2]).shape # => [2, 3, 4]
zeros([2, 3, 4]).reshape([2, -2]).shape # => [2, 3, 4]
zeros([2, 3, 4]).reshape([-2, 1, 1]).shape # => [2, 3, 4, 1, 1]
  • -3 uses the product of two consecutive dimensions of the input shape as the output dimension:
zeros([2, 3, 4]).reshape([-3, 4]).shape # => [6, 4]
zeros([2, 3, 4, 5]).reshape([-3, -3]).shape # => [6, 20]
zeros([2, 3, 4]).reshape([0, -3]).shape # => [2, 12]
zeros([2, 3, 4]).reshape([-3, -2]).shape # => [6, 4]
  • -4 splits one dimension of the input into the two dimensions passed subsequent to -4 (which can contain -1):
zeros([2, 3, 4]).reshape([-4, 1, 2, -2]).shape # => [1, 2, 3, 4]
zeros([2, 3, 4]).reshape([2, -4, -1, 3, -2]).shape # => [2, 1, 3, 4]

Parameters

  • data (NDArray, required) Input data.
  • shape (Int or Array(Int)) The target shape.
  • reverse (Bool, optional, default false) If true then the special values are inferred from right to left.
  • out (NDArray, optional) The output array.

def self.reshape_like(lhs, rhs, **kwargs) #

Reshape some or all dimensions of lhs to have the same shape as some or all dimensions of rhs.

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

Assume x and y are arrays with the following elements:

[1, 2, 3, 4, 5, 6]        # x
[[0, -4], [3, 2], [2, 2]] # y

Then:

reshape_like(x, y) # => [[1, 2], [3, 4], [5, 6]]

Parameters

  • lhs (NDArray, required) The first input.
  • rhs (NDArray, required) The second input.
  • out (NDArray, optional) The output array.

def self.rint(data, **kwargs) #

Returns element-wise rounded value to the nearest integer.

Note:

  • For input N.5 rint returns N while round returns N+1.
  • For input -N.5 both rint and round return -N-1.

Assume x is an array with the following elements:

[-2.1, -1.9, 1.5, 1.9, 2.1]

Then:

rint(x) = [-2.0, -2.0, 1.0, 2.0, 2.0]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.round(data, **kwargs) #

Returns element-wise rounded value to the nearest integer.

Assume x is an array with the following elements:

[-2.1, -1.9, 1.5, 1.9, 2.1]

Then:

round(x) = [-2.0, -2.0, 2.0, 2.0, 2.0]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.rsqrt(data, **kwargs) #

Returns element-wise inverse square-root value of the input.

rsqrt(x) = 1/sqrt(x)

Assume x is an array with the following elements:

[4, 9, 16]

Then:

rsqrt(x) = [0.5, 0.33333, 0.25]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.sample_exponential(lam : self, shape = [] of Int32, **kwargs) #

Draws concurrent samples from exponential distributions.

Samples are drawn from multiple exponential distributions with parameters lam (rate).

The parameters of the distributions are provided as an input array. Let [s] be the shape of the input array, n be the dimension of [s], [t] be the shape specified as the parameter of the operator, and m be the dimension of [t]. Then the output will be a (n+m)-dimensional array with shape [s]x[t].

For any valid n-dimensional index i with respect to the input array, output[i] will be an m-dimensional array that holds randomly drawn samples from the distribution which is parameterized by the input value at index i. If the shape parameter of the operator is not set, then one sample will be drawn per distribution and the output array has the same shape as the input array.

Assume lam is an array with the following elements:

[1.0, 8.5]

Then:

sample_exponential(lam)             # => [0.51837951, 0.09994757]
sample_exponential(lam, shape: [2]) # => [[0.51837951, 0.19866663], [0.09994757, 0.50447971]]

Parameters

  • lam (NDArray) Lambda parameters (rates) of the exponential distributions.
  • shape (Int or Array(Int)) Shape to be sampled from each random distribution.
  • dtype (::Symbol, default = :float32) The data type of the output in case this can’t be inferred.
  • out (NDArray, optional) The output array.

def self.sample_gamma(alpha : self, beta : self, shape = [] of Int32, **kwargs) #

Draws random samples from gamma distributions.

Samples are drawn from multiple gamma distributions with parameters alpha (shape) and beta (scale).

The parameters of the distributions are provided as input arrays. Let [s] be the shape of the input arrays, n be the dimension of [s], [t] be the shape specified as the parameter of the operator, and m be the dimension of [t]. Then the output will be a (n+m)-dimensional array with shape [s]x[t].

For any valid n-dimensional index i with respect to the input arrays, output[i] will be an m-dimensional array that holds randomly drawn samples from the distribution which is parameterized by the input values at index i. If the shape parameter of the operator is not set, then one sample will be drawn per distribution and the output array has the same shape as the input arrays.

Assume alpha and beta are arrays with the following elements:

[0.0, 2.5] # alpha
[1.0, 0.7] # beta

Then:

sample_gamma(alpha, beta)             # => [0.0, 2.25797319]
sample_gamma(alpha, beta, shape: [2]) # => [[0.0, 0.0], [2.25797319, 1.70734084]]

Parameters

  • alpha (NDArray) Alpha parameters (shapes) of the distributions.
  • beta (NDArray) Beta parameters (scales) of the distributions.
  • shape (Int or Array(Int)) Shape to be sampled from each random distribution.
  • dtype (::Symbol, default = :float32) The data type of the output in case this can’t be inferred.
  • out (NDArray, optional) The output array.

def self.sample_multinomial(data : self, get_prob : Bool = false, **kwargs) #

Draws random samples from multinomial distributions.

Samples are drawn from multiple multinomial distributions. Note that the input distribution must be normalized (data must sum to 1 along its last axis).

data is an n dimensional array whose last dimension has length k, where k is the number of possible outcomes of each multinomial distribution. This operator will draw shape samples from each distribution. If #shape is empty one sample will be drawn from each distribution.

If get_prob is true, a second array containing log likelihood of the drawn samples will also be returned. This is usually used for reinforcement learning where you can provide reward as head gradient for this array to estimate gradient.

Given:

probs = [[0.0, 0.1, 0.2, 0.3, 0.4], [0.4, 0.3, 0.2, 0.1, 0.0]]

Then:

sample_multinomial(probs)                 # => [3, 0]
sample_multinomial(probs, shape: [2])     # => [[4, 2], [0, 0]]
sample_multinomial(probs, get_prob: true) # => [2, 1], [0.2, 0.3]

Parameters

  • data (NDArray) Distribution probabilities. Must sum to one on the last axis.
  • get_prob (Bool, default = false) Whether to also return the log probabilities of sampled results. This is usually used for differentiating through stochastic variables, e.g. in reinforcement learning.
  • shape (Int or Array(Int)) Shape to be sampled from each random distribution.
  • dtype (::Symbol, default = :float32) The data type of the output in case this can’t be inferred.
  • out (NDArray, optional) The output array.

def self.sample_normal(mu : self, sigma : self, shape = [] of Int32, **kwargs) #

Draws concurrent samples from normal (Gaussian) distributions.

Samples are drawn from multiple normal distributions with parameters mu (mean) and sigma (standard deviation).

The parameters of the distributions are provided as input arrays. Let [s] be the shape of the input arrays, n be the dimension of [s], [t] be the shape specified as the parameter of the operator, and m be the dimension of [t]. Then the output will be a (n+m)-dimensional array with shape [s]x[t].

For any valid n-dimensional index i with respect to the input arrays, output[i] will be an m-dimensional array that holds randomly drawn samples from the distribution which is parameterized by the input values at index i. If the shape parameter of the operator is not set, then one sample will be drawn per distribution and the output array has the same shape as the input arrays.

Assume mu and sigma are arrays with the following elements:

[0.0, 2.5] # mu
[1.0, 3.7] # sigma

Then:

sample_normal(mu, sigma)             # => [-0.56410581, 0.95934606]
sample_normal(mu, sigma, shape: [2]) # => [[-0.56410581, 0.2928229 ], [0.95934606, 4.48287058]]

Parameters

  • mu (NDArray) Means of the distributions.
  • sigma (NDArray) Standard deviations of the distributions.
  • shape (Int or Array(Int)) Shape to be sampled from each random distribution.
  • dtype (::Symbol, default = :float32) The data type of the output in case this can’t be inferred.
  • out (NDArray, optional) The output array.

def self.sample_poisson(lam : self, shape = [] of Int32, **kwargs) #

Draws concurrent samples from Poisson distributions.

Samples are drawn from multiple Poisson distributions with parameters lam (rate). Samples will always be returned as a floating point data type.

The parameters of the distributions are provided as an input array. Let [s] be the shape of the input array, n be the dimension of [s], [t] be the shape specified as the parameter of the operator, and m be the dimension of [t]. Then the output will be a (n+m)-dimensional array with shape [s]x[t].

For any valid n-dimensional index i with respect to the input array, output[i] will be an m-dimensional array that holds randomly drawn samples from the distribution which is parameterized by the input value at index i. If the shape parameter of the operator is not set, then one sample will be drawn per distribution and the output array has the same shape as the input array.

Assume lam is an array with the following elements:

[1.0, 8.5]

Then:

sample_poisson(lam)             # => [0.0, 13.0]
sample_poisson(lam, shape: [2]) # => [[0.0, 4.0], [13.0, 8.0]]

Parameters

  • lam (NDArray) Lambda parameters (rates) of the Poisson distributions.
  • shape (Int or Array(Int)) Shape to be sampled from each random distribution.
  • dtype (::Symbol, default = :float32) The data type of the output in case this can’t be inferred.
  • out (NDArray, optional) The output array.

def self.sample_uniform(low : self, high : self, shape = [] of Int32, **kwargs) #

Draws concurrent samples from uniform distributions.

Samples are drawn from multiple uniform distributions on the intervals given by [low, high).

The parameters of the distributions are provided as input arrays. Let [s] be the shape of the input arrays, n be the dimension of [s], [t] be the shape specified as the parameter of the operator, and m be the dimension of [t]. Then the output will be a (n+m)-dimensional array with shape [s]x[t].

For any valid n-dimensional index i with respect to the input arrays, output[i] will be an m-dimensional array that holds randomly drawn samples from the distribution which is parameterized by the input values at index i. If the shape parameter of the operator is not set, then one sample will be drawn per distribution and the output array has the same shape as the input arrays.

Assume low and high are arrays with the following elements:

[0.0, 2.5] # low
[1.0, 3.7] # high

Then:

sample_uniform(low, high)             # => [0.40451524, 3.18687344]
sample_uniform(low, high, shape: [2]) # => [[0.40451524, 0.18017688], [3.18687344, 3.68352246]]

Parameters

  • low (NDArray) Lower bounds of the distributions.
  • high (NDArray) Upper bounds of the distributions.
  • shape (Int or Array(Int)) Shape to be sampled from each random distribution.
  • dtype (::Symbol, default = :float32) The data type of the output in case this can’t be inferred.
  • out (NDArray, optional) The output array.

def self.save(fname, data) #

Saves arrays to a file.

Examples of filenames:

  • /path/to/file
  • s3://my-bucket/path/to/file (if MXNet is compiled with AWS S3 supports)
  • hdfs://path/to/file (if MXNet is compiled with HDFS supports)

Parameters

  • fname (String) The filename.
  • data (NDArray or Enumerable({String, NDArray}) or Enumerable(NDArray)) The data to save.

[View source]
def self.sgd_mom_update(weight : self, grad : self, mom : self, lr : Float, **kwargs) #

Momentum update function for Stochastic Gradient Descent (SGD) optimizer.

Momentum update has better convergence rates on neural networks.

Parameters:

  • weight (NDArray, required) Weights.
  • grad (NDArray, required) Gradients.
  • mom (NDArray, required) Momentum.
  • lr (Float, required) Learning rate.
  • momentum (Float, optional, default = 0) The decay rate of momentum estimates at each epoch.
  • wd (Float, optional, default = 0) Weight decay augments the objective function with a regularization term that penalizes large weights. The penalty scales with the square of the magnitude of each weight.
  • rescale_grad (Float, optional, default = 1.0) Rescale gradient to grad = rescale_grad * grad.
  • clip_gradient (Float, optional, default = -1.0) Clip gradient to the range of [-clip_gradient, clip_gradient]. If clip_gradient <= 0, gradient clipping is turned off.
  • lazy_update (Bool, optional, default = true) If true, lazy updates are applied if gradient's stype is row_sparse.
  • out (NDArray, optional) The output array.

def self.sgd_update(weight : self, grad : self, lr : Float, **kwargs) #

Update function for Stochastic Gradient Descent (SGD) optimizer.

SGD updates the weights using:

weight = weight - learning_rate * (gradient + wd * weight)

Parameters

  • weight (NDArray, required) Weights.
  • grad (NDArray, required) Gradients.
  • lr (Float, required) Learning rate.
  • wd (Float, optional, default = 0) Weight decay augments the objective function with a regularization term that penalizes large weights. The penalty scales with the square of the magnitude of each weight.
  • rescale_grad (Float, optional, default = 1.0) Rescale gradient to grad = rescale_grad * grad.
  • clip_gradient (Float, optional, default = -1.0) Clip gradient to the range of [-clip_gradient, clip_gradient]. If clip_gradient <= 0, gradient clipping is turned off.
  • lazy_update (Bool, optional, default = true) If true, lazy updates are applied if gradient's stype is row_sparse.
  • out (NDArray, optional) The output array.

def self.shape_array(data, **kwargs) #

Returns a 1-D array containing the shape of the data.

Assume x is an array with the following elements:

[[1, 2, 3, 4], [5, 6, 7, 8]]

Then:

shape_array(x) = [2, 4]

Parameters:

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.shuffle(data, **kwargs) #

Randomly shuffles the elements.

Shuffles the array along the first axis. The order of the elements in each subarray does not change. For example, if a 2-D array is given, the order of the rows randomly changes, but the order of the elements in each row does not change.

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.sigmoid(data, **kwargs) #

Computes the sigmoid activation.

y=1/(1+exp(−x))

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.sign(data, **kwargs) #

Returns the element-wise sign of the input.

Assume x is an array with the following elements:

[-2, 0, 3]

Then:

sign(x) # => [-1, 0, 1]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.sin(data, **kwargs) #

Computes the element-wise sine of the input array.

The input should be in radians (2\𝜋 radians equals 360 degrees).

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

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.sinh(data, **kwargs) #

Returns the hyperbolic sine of the input array, computed element-wise.

sinh(x) = (exp(x) - exp(-x)) / 2

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.size_array(data, **kwargs) #

Returns a 1-D array containing the size of the data.

Assume x is an array with the following elements:

[[1, 2, 3, 4], [5, 6, 7, 8]]

Then:

size_array(x) = [8]

Parameters:

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.slice(data, begin _begin, end _end, **kwargs) #

Slices a region of the array.

This function returns a sliced array between the indices given by begin and end with the corresponding step.

For an input array of shape=[d_0, d_1, ..., d_n-1], a slice operation with begin=[b_0, b_1, ..., b_m-1], end=[e_0, e_1, ..., e_m-1], and step=[s_0, s_1, ..., s_m-1], where m <= n, results in an array with the shape (|e_0-b_0|/|s_0|, ..., |e_m-1-b_m-1|/|s_m-1|, d_m, ..., d_n-1).

The resulting array's k-th dimension contains elements from the k-th dimension of the input array starting from index b_k (inclusive) with step s_k until reaching e_k (exclusive).

If the k-th elements are nil in the sequence of begin, end, and step, the following rule will be used to set default values: if s_k is nil, set s_k = 1. If s_k > 0, set b_k = 0, e_k = d_k, else set b_k = d_k-1, e_k = -1.

Parameters

  • data (NDArray, required) Input data.
  • begin (Array(Int), required) Beginning indices for the slice operation, supports negative indices.
  • end (Array(Int), required) Ending indices for the slice operation, supports negative indices.
  • step (Array(Int), optional) Step for the slice operation, supports negative values.
  • out (NDArray, optional) The output array.

def self.slice_axis(data, axis, begin _begin, end _end, **kwargs) #

Slices along a given axis.

Returns an array slice along a given axis starting from the begin index to the end index.

Assume x is an array with the following elements:

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

Then:

slice_axis(x, axis: 1, begin: 0, end: 2) # => [[1, 2], [5, 6], [9, 10]]

Parameters

  • data (NDArray, required) Input data.
  • axis (Int, required) Axis along which to slice. Supports negative indexes.
  • begin (Int, required) The beginning index along the axis to be sliced. Supports negative indexes.
  • end (Int or nil, required) The ending index along the axis to be sliced. Supports negative indexes.
  • out (NDArray, optional) The output array.

def self.slice_like(data, shape_like, **kwargs) #

Slices like the shape of another array.

This function is similar to .slice, however, the begin values are always 0 and the end values of specific axes are inferred from the second input shape_like.

Given a value of shape_like of shape=[d_0, d_1, ..., d_n-1] and default empty axes, .slice_like performs the following operation:

out = slice(input, begin: [0, 0, ..., 0], end: [d_0, d_1, ..., d_n-1])

When axes is present, it is used to specify which axes are being sliced.

It is allowed to have first and second inputs with different dimensions, however, you have to make sure axes are specified and do not exceed the dimension limits.

For example, given an input a with shape=[2, 3, 4, 5] and an input b with shape=[1, 2, 3], the following is not allowed because the number of dimensions of a is 4 and the number of dimension of b is 3:

out = slice_like(a, b)

The following is allowed in this situation:

out = slice_like(a, b, axes: [0, 2])

Assume x and y are arrays with the following elements:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] # x
[[0, 0, 0], [0, 0, 0]]                        # y

slice_like(x, y) = [[1, 2, 3], [5, 6, 7]]
slice_like(x, y, axes: [0, 1]) = [[1, 2, 3], [5, 6, 7]]
slice_like(x, y, axes: [0]) = [[1, 2, 3, 4], [5, 6, 7, 8]]
slice_like(x, y, axes: [-1]) = [[1, 2, 3], [5, 6, 7], [9, 10, 11]]

Parameters

  • data (NDArray, required) Input data.
  • shape_like (NDArray) Input to shape like.
  • axes (Int or Array(Int)) List of axes on which input data will be sliced according to the corresponding size of the second input. By default it will slice on all axes. Negative axes are supported.
  • out (NDArray, optional) The output array.

def self.softmax(data, **kwargs) #

Applies the softmax function.

The resulting array contains elements in the range (0, 1) and the elements along the given axis sum up to 1.

Assume x is an array with the following elements:

[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]

Then:

softmax(x, axis: 0) # => [[0.5, 0.5, 0.5], [0.5, 0.5, 0.5]]
softmax(x, axis: 1) # => [[0.3334, 0.3334, 0.3334], [0.3334, 0.3334, 0.3334]]

Parameters

  • data (NDArray, required) Input data.
  • axis (Int, optional, default = -1) The axis along which to compute softmax.
  • temperature (Float, optional, default = 1.0) Temperature parameter in softmax.
  • dtype (::Symbol, :float16, :float32 or :float64, optional) Type of the output in case this can't be inferred. Defaults to the same type as the input if not defined.
  • out (NDArray, optional) The output array.

def self.sort(data, **kwargs) #

Returns a sorted copy of an input array along the given axis.

Assume x is an array with the following elements:

[[1, 4], [3, 1]]

Then:

sort(x) = [[1, 4], [1, 3]]
sort(x, axis: 0) = [[1, 1], [3, 4]]
sort(x, axis: None) = [1, 1, 3, 4]
sort(x, is_ascend: false) = [[4, 1], [3, 1]]

Parameters

  • data (NDArray, required) Input data.
  • axis (Int or None, optional, default = -1) The axis along which to choose sort the input tensor. If omitted, the last axis is used. If None, the flattened array is used.
  • is_ascend (Bool, optional, default = false) Whether to sort in ascending or descending order.
  • out (NDArray, optional) The output array.

def self.sqrt(data, **kwargs) #

Returns element-wise square-root value of the input.

Assume x is an array with the following elements:

[4, 9, 16]

Then:

sqrt(x) # => [2, 3, 4]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.square(data, **kwargs) #

Returns element-wise squared value of the input.

Assume x is an array with the following elements:

[2, 3, 4]

Then:

square(x) # => [4, 9, 16]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.subtract(lhs : self | Number, rhs : self | Number) #

Returns element-wise difference of the input arrays.

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

Equivalent to lhs - rhs. Equivalent to .broadcast_sub and .broadcast_minus for NDArray arguments.

Parameters


[View source]
def self.sum(data, **kwargs) #

Computes the sum of array elements over given axes.

Assume x is an array with the following elements:

[[[1, 2], [2, 3], [1, 3]],
 [[1, 4], [4, 3], [5, 2]],
 [[7, 1], [7, 2], [7, 3]]]

Then:

sum(x, axis: 1) # => [[4, 8], [10, 9], [21, 6]]
sum(x, axis: [1, 2]) # => [12, 19, 27]

Parameters

  • data (NDArray, required) Input data.
  • axis (Int or Array(Int), optional) The axis or axes along which to perform the reduction. axis: [] or axis: nil will compute over all elements into a scalar array with shape [1]. If axis is an Int, a reduction is performed on a particular axis. If axis is an array of Int, a reduction is performed on all the axes specified in the array. If exclude is true, reduction will be performed on the axes that are not in axis instead. Negative values means indexing from right to left.
  • keepdims (Bool, optional, default = false) If this is set to true, the reduced axes are left in the result as dimension with size one.
  • exclude (Bool, optional, default = false) Whether to perform reduction on axis that are not in axis instead.
  • out (NDArray, optional) The output array.

def self.take(a, indices, **kwargs) #

Takes elements from an input array along the given axis.

This function slices the input array along a particular axis with the provided indices.

Given data tensor of rank r >= 1, and indices tensor of rank q, gather entries of the axis dimension of data (by default outer-most one as axis=0) indexed by indices, and concatenate them in an output tensor of rank q + (r - 1).

Assume x and i are arrays with the following elements:

[[1, 2], [3, 4], [5, 6]] # x
[[0, 1], [1, 2]]]        # i

Then:

# get rows 0 and 1, then 1 and 2, along axis 0
take(x, i) # => [[[1, 2], [3, 4]], [[3, 4], [5, 6]]]

Parameters

  • a (NDArray, required) The input array.
  • indices (NDArray, required) The indices of the values to be extracted.
  • axis (Int, optional, default = 0) The axis of input array to be taken. For input tensor of rank r, it could be in the range of [-r, r-1].
  • mode (::Symbol, :clip or :wrap, optional, default = :clip) Specify how out-of-bound indices bahave. :clip means to clip to the range. If all indices mentioned are too large, they are replaced by the index that addresses the last element along an axis. :wrap means to wrap around.
  • out (NDArray, optional) The output array.

def self.tan(data, **kwargs) #

Computes the element-wise tangent of the input array.

The input should be in radians (2\𝜋 radians equals 360 degrees).

tan([0, 𝜋, 𝜋/2]) = [0, 1, -∞)]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.tanh(data, **kwargs) #

Returns the hyperbolic tangent of the input array, computed element-wise.

tanh(x) = sinh(x) / cosh(x)

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.tile(data, reps, **kwargs) #

Repeats the array multiple times.

Assume x is an array with the following elements:

[[1, 2], [3, 4]]

If reps has length d, and the input array has a corresponding dimension of n. There are three cases:

  • n=d. Repeat i-th dimension of the input reps[i] times:
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. For an input shape [2, 3], reps: [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. 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]]]

Parameters

  • data (NDArray, required) Input data.
  • reps (Array(Int)) The number of times to repeat the input array. Each element of reps must be a positive integer.
  • out (NDArray, optional) The output array.

def self.topk(data, **kwargs) #

Returns the top k elements in an input array along the given axis.

Examples::

Assume x is an array with the following elements:

[[0.3, 0.2, 0.4], [0.1, 0.3, 0.2]]

Then:

topk(x) = [[2.0], [1.0]]
topk(x, ret_typ: :value, k: 2) = [[0.4, 0.3], [0.3, 0.2]]
topk(x, ret_typ: :value, k: 2, is_ascend: true) = [[0.2, 0.3], [0.1, 0.2]]
topk(x, axis: 0, k: 2) = [[0.0, 1.0, 0.0], [1.0, 0.0, 1.0]]

Parameters

  • data (NDArray, required) Input data.
  • axis (Int or None, optional, default = -1) Axis along which to choose the top k indices. If omitted, the last axis is used. If None, the flattened array is used.
  • k (Int, optional, default = 1) Number of top elements to select. It should be always smaller than or equal to the element number in the given axis.
  • ret_typ (::Symbol, :value, :indices, :mask, :both, optional, default = :indices) The return type. :value means to return the top k values, :indices means to return the indices of the top k values, :mask means to return a mask array containing 0 and 1 (1 means the top k value). :both means to return a list of both values and indices of top k elements.
  • is_ascend (Bool, optional, default = false) Whether to choose k largest or k smallest elements. Top k largest elements will be chosen if set to false.
  • dtype (::Symbol, optional, default = :float32) The data type of the output indices when ret_typ is :indices or :both.
  • out (NDArray, optional) The output array.

def self.transpose(data, **kwargs) #

Permutes the dimensions of an array.

Assume x and y are arrays with the following elements:

[[[1, 2], [3, 4], [5, 6], [7, 8]]] # x
[[1, 2], [3, 4]]                   # y

Then:

transpose(x) # => [[[1], [3], [5], [7]], [[2], [4], [6], [8]]]
transpose(x, axes: [1, 0, 2]) # => [[[1, 2]], [[3, 4]], [[5, 6]], [[7, 8]]]
transpose(y) # => [[1, 3], [2, 4]]

Parameters

  • data (NDArray, required) Input data.
  • axes (Int or Array(Int), optional) Target axis order. By default the axes will be inverted.
  • out (NDArray, optional) The output array.

def self.trunc(data, **kwargs) #

Return the element-wise truncated value of the input.

The truncated value of x is the nearest integer i which is closer to zero than x is. In short, the fractional part of the signed number x is discarded.

Assume x is an array with the following elements:

[-2.1, -1.9, 1.5, 1.9, 2.1]

Then:

trunc(x) = [-2.0, -1.0, 1.0, 1.0, 2.0]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

def self.where(condition, x, y, **kwargs) #

Returns elements, either from x or y, depending on the condition.

Given three arrays, condition, x and y, return an array with the elements from x or y, depending on whether 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 condition is true and from y if false.

If condition does not have the same shape as x, it must be a 1-D array whose size is the same as the size of the first dimension of x. Each row of the output array is from x if the corresponding element from condition is true and from y if false.

Note: all non-zero values are interpreted as true in condition.

Assume x, y and condition are arrays with the following elements:

[[1, 2], [3, 4]]  # x
[[5, 6], [7, 8]]  # y
[[0, 1], [-1, 0]] # condition

Then:

where(condition, x, y) = [[5, 2], [3, 8]]

Parameters

  • condition ((NDArray, required)) The condition array.
  • x ((NDArray, required)) The first input.
  • y ((NDArray, required)) The second input.
  • out (NDArray, optional) The output array.

def self.zeros(shape : Int | Array(Int), ctx = Context.current, **kwargs) #

Returns an array filled with all zeros, with the given shape.

Parameters

  • data (NDArray, required) Input data.
  • shape (Int or Array(Int)) The shape of the array.
  • dtype (::Symbol, default = :float32) The data type of the output array.
  • ctx (Context, optional) Device context (default is the current context). Only used for imperative calls.
  • out (NDArray, optional) The output array.

def self.zeros_like(data, **kwargs) #

Returns an array of zeros with the same shape, data type and storage type as the input array.

Assume x is an array with the following elements:

[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]

Then:

zeros_like(x) # => [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]

Parameters

  • data (NDArray, required) Input data.
  • out (NDArray, optional) The output array.

Instance Method Detail

def !=(other) #

Performs element-wise not equal to (#!=) comparison operation with broadcasting.


[View source]
def %(other) #

Performs element-wise modulo with broadcasting.


[View source]
def &(other) #

Performs element-wise logical and (#&) comparison operation with broadcasting.


[View source]
def *(other) #

Performs element-wise multiplication with broadcasting.


[View source]
def **(other) #

Returns the result of the first array elements raised to powers from the second array (or scalar), element-wise with broadcasting.


[View source]
def +(other) #

Performs element-wise addition with broadcasting.


[View source]
def + #

Leaves the values unchanged.


[View source]
def -(other) #

Performs element-wise subtraction with broadcasting.


[View source]
def - #

Performs element-wise numerical negative.


[View source]
def /(other) #

Performs element-wise division with broadcasting.


[View source]
def <(other) #

Performs element-wise less than (#<) comparison operation with broadcasting.


[View source]
def <=(other) #

Performs element-wise less than or equal to (#<=) comparison operation with broadcasting.


[View source]
def ==(other) #

Performs element-wise equal to (#==) comparison operation with broadcasting.


[View source]
def >(other) #

Performs element-wise greater than (#>) comparison operation with broadcasting.


[View source]
def >=(other) #

Performs element-wise greater than or equal to (#>=) comparison operation with broadcasting.


[View source]
def [](keys : Array(Int | Range(Int?, Int?))) #

Returns a sliced view of this array.

This method assumes the key is Array of Int or Range(Int, Int). A macro is provided that rewrites a key presented as a variable number of Int or Range(Int, Int) arguments to array syntax.

Parameters

  • keys (Array(Int | Range(Int, Int))) Indexing key.

Using variable argument syntax:

a = MXNet::NDArray.array([1, 2, 3, 4])
a[1] # => MXNet::NDArray.array([2])
a[1...3] # => MXNet::NDArray.array([2, 3])
a[1..-2] # => MXNet::NDArray.array([2, 3])
b = MXNet::NDArray.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 0], [1, 2]]])
b[1...3, 1] # => MXNet::NDArray.array([[7, 8], [1, 2]])
b[1...3, 1...2] # => MXNet::NDArray.array([[[7, 8]], [[1, 2]]])
b[1, 1...2] # => MXNet::NDArray.array([[7, 8]])

[View source]
def []=(keys : Array(Int | Range(Int?, Int?)), value : Number | self) #

Sets sliced view of this array to the specified value.

This method assumes the key is Array of Int or Range(Int, Int). A macro is provided that rewrites a key presented as a variable number of Int or Range(Int, Int) arguments to array syntax.

Parameters

  • keys (Array(Int | Range(Int, Int))) Indexing key.
  • value (Number or MXNet::NDArray)) The value to set.

Using variable argument syntax:

a = MXNet::NDArray.array([1, 2, 3, 4])
a[1] = 99
a # => MXNet::NDArray.array([1, 99, 3, 4])
a[1..-2] = 98
a # => MXNet::NDArray.array([1, 98, 98, 4])
a[1...3] = MXNet::NDArray.array([97, 97])
a # => MXNet::NDArray.array([1, 97, 97, 4])
b = MXNet::NDArray.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 0], [1, 2]]])
b[1...3, 1] = 99
b # => MXNet::NDArray.array([[[1, 2], [3, 4]], [[5, 6], [99, 99]], [[9, 0], [99, 99]]])
b[1...3, 1...2] = MXNet::NDArray.array([[[98, 98]], [[98, 98]]])
b # => MXNet::NDArray.array([[[1, 2], [3, 4]], [[5, 6], [98, 98]], [[9, 0], [98, 98]]])
b[1, 1...2] = MXNet::NDArray.array([[97, 97]])
b # => MXNet::NDArray.array([[[1, 2], [3, 4]], [[5, 6], [97, 97]], [[9, 0], [98, 98]]])

[View source]
def ^(other) #

Performs element-wise logical xor (#^) comparison operation with broadcasting.


[View source]
def abs(**kwargs) #

Convenience fluent method for .abs.


def arccos(**kwargs) #

Convenience fluent method for .arccos.


def arccosh(**kwargs) #

Convenience fluent method for .arccosh.


def arcsin(**kwargs) #

Convenience fluent method for .arcsin.


def arcsinh(**kwargs) #

Convenience fluent method for .arcsinh.


def arctan(**kwargs) #

Convenience fluent method for .arctan.


def arctanh(**kwargs) #

Convenience fluent method for .arctanh.


def argmax(**kwargs) #

Convenience fluent method for .argmax.


def argmin(**kwargs) #

Convenience fluent method for .argmin.


def argsort(**kwargs) #

Convenience fluent method for .argsort.


def as_in_context(context : Context, copy = false) #

Returns a copy of the array on the target device with the same values as this array.

Parameters

  • context (Context) The target context.
  • copy (Bool, default = false) By default, if the target context is the same as this context, this array is returned and no copy is made. If copy is set to true, and the target context is the same as this context, a copy is returned instead.

[View source]
def as_scalar #

Returns a scalar whose value is copied from this array.

The array must have shape [1].

MXNet::NDArray.zeros([1], dtype: :float64).as_scalar # => 0.0

[View source]
def as_type(dtype : ::Symbol, copy = true) #

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

Parameters

  • dtype (::Symbol) The type of the copy.
  • copy (Bool, default = true) By default, #as_type always returns a newly allocated array on the same context. If copy is set to false, and the dtype requested is the same as this array's dtype, this array is returned instead of a copy.

[View source]
def attach_grad(grad_req = :write) #

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

Parameters

  • grad_req (::Symbol, default = :write)
    • :write: gradient will be overwritten on every backward pass
    • :add: gradient will be added to existing value on every backward pass
    • :null: do not compute gradient

[View source]
def backward(gradient = nil, retain_graph = false, train_mode = true) #

Compute the gradients of this array with respect to previously marked variables.

Parameters

  • gradient (MXNet::NDArray, optional) Gradient with respect to this array.
  • retain_graph (Bool, default = false) Whether to keep computation graph to differentiate again, instead of clearing history and releasing memory.
  • train_mode (Bool, default = true) Whether the backward pass is in training or predicting mode.

[View source]
def broadcast_add(rhs, **kwargs) #

Convenience fluent method for .broadcast_add.


def broadcast_axis(**kwargs) #

Convenience fluent method for .broadcast_axis.


def broadcast_div(rhs, **kwargs) #

Convenience fluent method for .broadcast_div.


def broadcast_equal(rhs, **kwargs) #

Convenience fluent method for .broadcast_equal.


def broadcast_greater(rhs, **kwargs) #

Convenience fluent method for .broadcast_greater.


def broadcast_greater_equal(rhs, **kwargs) #

Convenience fluent method for .broadcast_greater_equal.


def broadcast_lesser(rhs, **kwargs) #

Convenience fluent method for .broadcast_lesser.


def broadcast_lesser_equal(rhs, **kwargs) #

Convenience fluent method for .broadcast_lesser_equal.


def broadcast_like(rhs, **kwargs) #

Convenience fluent method for .broadcast_like.


def broadcast_logical_and(rhs, **kwargs) #

Convenience fluent method for .broadcast_logical_and.


def broadcast_logical_or(rhs, **kwargs) #

Convenience fluent method for .broadcast_logical_or.


def broadcast_logical_xor(rhs, **kwargs) #

Convenience fluent method for .broadcast_logical_xor.


def broadcast_maximum(rhs, **kwargs) #

Convenience fluent method for .broadcast_maximum.


def broadcast_minimum(rhs, **kwargs) #

Convenience fluent method for .broadcast_minimum.


def broadcast_minus(rhs, **kwargs) #

Convenience fluent method for .broadcast_minus.


def broadcast_mul(rhs, **kwargs) #

Convenience fluent method for .broadcast_mul.


def broadcast_not_equal(rhs, **kwargs) #

Convenience fluent method for .broadcast_not_equal.


def broadcast_plus(rhs, **kwargs) #

Convenience fluent method for .broadcast_plus.


def broadcast_power(rhs, **kwargs) #

Convenience fluent method for .broadcast_power.


def broadcast_sub(rhs, **kwargs) #

Convenience fluent method for .broadcast_sub.


def broadcast_to(**kwargs) #

Convenience fluent method for .broadcast_to.


def cbrt(**kwargs) #

Convenience fluent method for .cbrt.


def ceil(**kwargs) #

Convenience fluent method for .ceil.


def clip(a_min, a_max, **kwargs) #

Convenience fluent method for .clip.


def context #

[View source]
def copy_to(other : Context | self) #

Copies the values of this array to another array.

If other is a NDArray object, then other.shape and self.shape must be the same. This method copies the data from self to other.

If other is a Context object, then a new NDArray will be created on the target context, and the method copies the data from self to the new array.

Parameters


[View source]
def cos(**kwargs) #

Convenience fluent method for .cos.


def cosh(**kwargs) #

Convenience fluent method for .cosh.


def degrees(**kwargs) #

Convenience fluent method for .degrees.


def diag(**kwargs) #

Convenience fluent method for .diag.


def dot(rhs, **kwargs) #

Convenience fluent method for .dot.


def dtype #

[View source]
def exp(**kwargs) #

Convenience fluent method for .exp.


def expand_dims(axis, **kwargs) #

Convenience fluent method for .expand_dims.


def expm1(**kwargs) #

Convenience fluent method for .expm1.


def fix(**kwargs) #

Convenience fluent method for .fix.


def flatten(**kwargs) #

Convenience fluent method for .flatten.


def flip(axis, **kwargs) #

Convenience fluent method for .flip.


def floor(**kwargs) #

Convenience fluent method for .floor.


def gamma(**kwargs) #

Convenience fluent method for .gamma.


def gammaln(**kwargs) #

Convenience fluent method for .gammaln.


def grad #

Returns gradient buffer attached to this array.


[View source]
def log(**kwargs) #

Convenience fluent method for .log.


def log10(**kwargs) #

Convenience fluent method for .log10.


def log1p(**kwargs) #

Convenience fluent method for .log1p.


def log2(**kwargs) #

Convenience fluent method for .log2.


def log_softmax(**kwargs) #

Convenience fluent method for .log_softmax.


def logical_not(**kwargs) #

Convenience fluent method for .logical_not.


def max(**kwargs) #

Convenience fluent method for .max.


def mean(**kwargs) #

Convenience fluent method for .mean.


def min(**kwargs) #

Convenience fluent method for .min.


def nanprod(**kwargs) #

Convenience fluent method for .nanprod.


def nansum(**kwargs) #

Convenience fluent method for .nansum.


def norm(**kwargs) #

Convenience fluent method for .norm.


def one_hot(depth, **kwargs) #

Convenience fluent method for .one_hot.


def ones_like(**kwargs) #

Convenience fluent method for .ones_like.


def pick(index, **kwargs) #

Convenience fluent method for .pick.


def prod(**kwargs) #

Convenience fluent method for .prod.


def radians(**kwargs) #

Convenience fluent method for .radians.


def rcbrt(**kwargs) #

Convenience fluent method for .rcbrt.


def reciprocal(**kwargs) #

Convenience fluent method for .reciprocal.


def relu(**kwargs) #

Convenience fluent method for .relu.


def reshape(shape, **kwargs) #

Convenience fluent method for .reshape.


def reshape_like(rhs, **kwargs) #

Convenience fluent method for .reshape_like.


def rint(**kwargs) #

Convenience fluent method for .rint.


def round(**kwargs) #

Convenience fluent method for .round.


def rsqrt(**kwargs) #

Convenience fluent method for .rsqrt.


def shape #

[View source]
def shape_array(**kwargs) #

Convenience fluent method for .shape_array.


def shuffle(**kwargs) #

Convenience fluent method for .shuffle.


def sigmoid(**kwargs) #

Convenience fluent method for .sigmoid.


def sign(**kwargs) #

Convenience fluent method for .sign.


def sin(**kwargs) #

Convenience fluent method for .sin.


def sinh(**kwargs) #

Convenience fluent method for .sinh.


def size #
Description copied from module Indexable(MXNet::NDArray)

Returns the number of elements in this container.


[View source]
def size_array(**kwargs) #

Convenience fluent method for .size_array.


def slice(begin _begin, end _end, **kwargs) #

Convenience fluent method for .slice.


def slice_axis(axis, begin _begin, end _end, **kwargs) #

Convenience fluent method for .slice_axis.


def slice_like(shape_like, **kwargs) #

Convenience fluent method for .slice_like.


def softmax(**kwargs) #

Convenience fluent method for .softmax.


def sort(**kwargs) #

Convenience fluent method for .sort.


def sqrt(**kwargs) #

Convenience fluent method for .sqrt.


def square(**kwargs) #

Convenience fluent method for .square.


def sum(**kwargs) #

Convenience fluent method for .sum.


def take(indices, **kwargs) #

Convenience fluent method for .take.


def tan(**kwargs) #

Convenience fluent method for .tan.


def tanh(**kwargs) #

Convenience fluent method for .tanh.


def tile(reps, **kwargs) #

Convenience fluent method for .tile.


def to_a(as : Array(Array(Array(T))).class) : Array(Array(Array(Array(T)))) forall T #

Returns an Array with values copied from this array.

Only supports arrays up to 4 dimensions (shape.size <= 4).

MXNet::NDArray.zeros([4], dtype: :float32).to_a(Float32) # => [0.0, 0.0, 0.0, 0.0]

To return a 1-dimensional array without checking and restricting the return type, see #to_a.

Parameters

  • as (Class) The class of the contained item. For example, to check and restrict the return type to Array(Float32) specify Float32.

[View source]
def to_a(as : Array(Array(T)).class) : Array(Array(Array(T))) forall T #

Returns an Array with values copied from this array.

Only supports arrays up to 4 dimensions (shape.size <= 4).

MXNet::NDArray.zeros([4], dtype: :float32).to_a(Float32) # => [0.0, 0.0, 0.0, 0.0]

To return a 1-dimensional array without checking and restricting the return type, see #to_a.

Parameters

  • as (Class) The class of the contained item. For example, to check and restrict the return type to Array(Float32) specify Float32.

[View source]
def to_a(as : Array(T).class) : Array(Array(T)) forall T #

Returns an Array with values copied from this array.

Only supports arrays up to 4 dimensions (shape.size <= 4).

MXNet::NDArray.zeros([4], dtype: :float32).to_a(Float32) # => [0.0, 0.0, 0.0, 0.0]

To return a 1-dimensional array without checking and restricting the return type, see #to_a.

Parameters

  • as (Class) The class of the contained item. For example, to check and restrict the return type to Array(Float32) specify Float32.

[View source]
def to_a(as : T.class) : Array(T) forall T #

Returns an Array with values copied from this array.

Only supports arrays up to 4 dimensions (shape.size <= 4).

MXNet::NDArray.zeros([4], dtype: :float32).to_a(Float32) # => [0.0, 0.0, 0.0, 0.0]

To return a 1-dimensional array without checking and restricting the return type, see #to_a.

Parameters

  • as (Class) The class of the contained item. For example, to check and restrict the return type to Array(Float32) specify Float32.

[View source]
def to_a #

Returns an Array with values copied from this array.

Only supports 1-dimensional arrays (shape.size == 1).

MXNet::NDArray.zeros([4], dtype: :float64).to_a # => [0.0, 0.0, 0.0, 0.0]

The return type of this method is the union of all possible array types (e.g. Array(Float32) | Array(Float64) | ...). To return an array and check and restrict the return type in a single operation, see #to_a(as).


[View source]
def to_s(io) #

[View source]
def topk(**kwargs) #

Convenience fluent method for .topk.


def transpose(**kwargs) #

Convenience fluent method for .transpose.


def trunc(**kwargs) #

Convenience fluent method for .trunc.


def unsafe_fetch(idx) #

[View source]
def where(x, y, **kwargs) #

Convenience fluent method for .where.


def zeros_like(**kwargs) #

Convenience fluent method for .zeros_like.


def |(other) #

Performs element-wise logical or (#|) comparison operation with broadcasting.


[View source]