class MXNet::NDArray
- MXNet::NDArray
- MXNet::Base
- Reference
- Object
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:
NDArray
adopts an imperative programming style -- namely expressions are executed step-by-step so that the results can be obtained immediately, whereasSymbol
adopts a declarative style.- Most binary operators in
NDArray
such as#+
and#>
have broadcasting enabled by default.
Included Modules
- Indexable(MXNet::NDArray)
- MXNet::Operations
Extended Modules
Defined in:
mxnet/ndarray.crClass Method Summary
-
.abs(data, **kwargs)
Returns the element-wise absolute value of the input.
-
.activation(data : self, act_type, **kwargs)
Applies an activation function element-wise to the input.
-
.add(lhs : self | Number, rhs : self | Number)
Returns element-wise sum of the input arrays.
-
.add_n(data : Array(self), **kwargs)
Adds all input arguments element-wise.
-
.arange(start : Number, stop : Number? = nil, ctx = Context.current, **kwargs)
Returns evenly spaced values within a given interval.
-
.arccos(data, **kwargs)
Returns element-wise inverse cosine of the input array.
-
.arccosh(data, **kwargs)
Returns the inverse hyperbolic cosine of the input array, computed element-wise.
-
.arcsin(data, **kwargs)
Returns element-wise inverse sine of the input array.
-
.arcsinh(data, **kwargs)
Returns the inverse hyperbolic sine of the input array, computed element-wise.
-
.arctan(data, **kwargs)
Returns element-wise inverse tangent of the input array.
-
.arctanh(data, **kwargs)
Returns the inverse hyperbolic tangent of the input array, computed element-wise.
-
.argmax(data, **kwargs)
Returns indices of the maximum values along an axis.
-
.argmin(data, **kwargs)
Returns indices of the minimum values along an axis.
-
.argsort(data, **kwargs)
Returns the indices that would sort an input array along the given axis.
-
.array(source : Enumerable(T), dtype = nil, ctx = nil) forall T
Creates an MXNet array from any enumerable object.
-
.broadcast_add(lhs, rhs, **kwargs)
Returns element-wise sum of the input arrays with broadcasting.
-
.broadcast_axis(data, **kwargs)
Broadcasts the input array over particular axis.
-
.broadcast_div(lhs, rhs, **kwargs)
Returns element-wise division of the input arrays with broadcasting.
-
.broadcast_equal(lhs, rhs, **kwargs)
Returns the result of element-wise equal to (
#==
) comparison operation with broadcasting. -
.broadcast_greater(lhs, rhs, **kwargs)
Returns the result of element-wise greater than (
#>
) comparison operation with broadcasting. -
.broadcast_greater_equal(lhs, rhs, **kwargs)
Returns the result of element-wise greater than or equal to (
#>=
) comparison operation with broadcasting. -
.broadcast_lesser(lhs, rhs, **kwargs)
Returns the result of element-wise less than (
#<
) comparison operation with broadcasting. -
.broadcast_lesser_equal(lhs, rhs, **kwargs)
Returns the result of element-wise less than or equal to (
#<=
) comparison operation with broadcasting. -
.broadcast_like(lhs, rhs, **kwargs)
Broadcasts the left hand side to have the same shape as right hand side.
-
.broadcast_logical_and(lhs, rhs, **kwargs)
Returns element-wise logical and of the input arrays with broadcasting.
-
.broadcast_logical_or(lhs, rhs, **kwargs)
Returns element-wise logical or of the input arrays with broadcasting.
-
.broadcast_logical_xor(lhs, rhs, **kwargs)
Returns element-wise logical xor of the input arrays with broadcasting.
-
.broadcast_maximum(lhs, rhs, **kwargs)
Returns element-wise maximum of the input arrays with broadcasting.
-
.broadcast_minimum(lhs, rhs, **kwargs)
Returns element-wise minimum of the input arrays with broadcasting.
-
.broadcast_minus(lhs, rhs, **kwargs)
Returns element-wise difference of the input arrays with broadcasting.
-
.broadcast_mul(lhs, rhs, **kwargs)
Returns element-wise product of the input arrays with broadcasting.
-
.broadcast_not_equal(lhs, rhs, **kwargs)
Returns the result of element-wise not equal to (
#!=
) comparison operation with broadcasting. -
.broadcast_plus(lhs, rhs, **kwargs)
Returns element-wise sum of the input arrays with broadcasting.
-
.broadcast_power(lhs, rhs, **kwargs)
Returns result of first array elements raised to powers from second array, element-wise with broadcasting.
-
.broadcast_sub(lhs, rhs, **kwargs)
Returns element-wise difference of the input arrays with broadcasting.
-
.broadcast_to(data, **kwargs)
Broadcasts the input array to a new shape.
-
.cbrt(data, **kwargs)
Returns element-wise cube-root value of the input.
-
.ceil(data, **kwargs)
Returns element-wise ceiling of the input.
-
.clip(data, a_min, a_max, **kwargs)
Clips (limits) the values in an array.
-
.concat(data : Array(self), **kwargs)
Joins input arrays along a given axis.
-
.convolution(data : self, weight : self?, bias : self?, kernel, num_filter, **kwargs)
Compute N-D convolution on (N+2)-D input.
-
.cos(data, **kwargs)
Computes the element-wise cosine of the input array.
-
.cosh(data, **kwargs)
Returns the hyperbolic cosine of the input array, computed element-wise.
-
.degrees(data, **kwargs)
Converts each element of the input array from radians to degrees.
-
.diag(data, **kwargs)
Extracts a diagonal or constructs a diagonal array.
-
.divide(lhs : self | Number, rhs : self | Number)
Returns element-wise division of the input arrays.
-
.dot(lhs, rhs, **kwargs)
Computes the dot product of two arrays.
-
.empty(shape : Int | Array(Int), dtype = :float32, ctx = nil)
Returns an MXNet array of given shape and type, without initializing entries.
-
.equal(lhs : self | Number, rhs : self | Number)
Returns the result of element-wise equal to (
#==
) comparison operation. -
.exp(data, **kwargs)
Returns element-wise exponential value of the input.
-
.expand_dims(data, axis, **kwargs)
Inserts a new axis of size 1 into the array shape.
-
.expm1(data, **kwargs)
Returns
exp(x) - 1
computed element-wise on the input. -
.fix(data, **kwargs)
Returns element-wise rounded value to the nearest integer towards zero.
-
.flatten(data, **kwargs)
Flattens the input array into a 2-D array by collapsing the higher dimensions.
-
.flip(data, axis, **kwargs)
Reverses the order of elements along given axis while preserving array shape.
-
.floor(data, **kwargs)
Returns the element-wise floor of the input.
-
.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.
-
.fully_connected(data : self, weight : self?, bias : self?, num_hidden : Int, **kwargs)
Applies a linear transformation: Y = XWᵀ + b.
-
.gamma(data, **kwargs)
Returns the gamma function (extension of the factorial function to the reals), computed element-wise on the input array.
-
.gammaln(data, **kwargs)
Returns the log of the absolute value of the gamma function, computed element-wise on the input array.
-
.greater(lhs : self | Number, rhs : self | Number)
Returns the result of element-wise greater than (
#>
) comparison operation. -
.greater_equal(lhs : self | Number, rhs : self | Number)
Returns the result of element-wise greater than or equal to (
#>=
) comparison operation. -
.hypot(lhs : self, rhs : self, **kwargs)
Given the legs of a right triangle, return its hypotenuse.
-
.imperative_invoke(op, *ndargs, out _out : self? = nil, **kwargs)
TODO cache op handles
-
.lesser(lhs : self | Number, rhs : self | Number)
Returns the result of element-wise less than (
#<
) comparison operation. -
.lesser_equal(lhs : self | Number, rhs : self | Number)
Returns the result of element-wise less than or equal to (
#<=
) comparison operation. -
.load(fname)
Loads arrays from a file.
-
.log(data, **kwargs)
Returns element-wise natural logarithmic value of the input.
-
.log10(data, **kwargs)
Returns element-wise base-10 logarithmic value of the input.
-
.log1p(data, **kwargs)
Returns
.log(1 + x)
computed element-wise on the input. -
.log2(data, **kwargs)
Returns element-wise base-2 logarithmic value of the input.
-
.log_softmax(data, **kwargs)
Computes the log softmax of the input.
-
.logical_and(lhs : self | Number, rhs : self | Number)
Returns the result of element-wise logical and (
#&
) comparison operation. -
.logical_not(data, **kwargs)
Performs element-wise logical not of the input array.
-
.logical_or(lhs : self | Number, rhs : self | Number)
Returns the result of element-wise logical or (
#|
) comparison operation. -
.logical_xor(lhs : self | Number, rhs : self | Number)
Returns the result of element-wise logical xor (
#^
) comparison operation. -
.max(data, **kwargs)
Computes the max of array elements over given axes.
-
.maximum(lhs : self | Number, rhs : self | Number)
Returns element-wise maximum of the input arrays.
-
.mean(data, **kwargs)
Computes the mean of array elements over given axes.
-
.min(data, **kwargs)
Computes the min of array elements over given axes.
-
.minimum(lhs : self | Number, rhs : self | Number)
Returns element-wise minimum of the input arrays.
-
.modulo(lhs : self | Number, rhs : self | Number)
Returns element-wise modulo of the input arrays.
-
.multiply(lhs : self | Number, rhs : self | Number)
Returns element-wise product of the input arrays.
-
.nanprod(data, **kwargs)
Computes the product of array elements over given axes treating not-a-number values (NaN) as one.
-
.nansum(data, **kwargs)
Computes the sum of array elements over given axes treating not-a-number values (NaN) as zero.
-
.norm(data, **kwargs)
Computes the norm.
-
.not_equal(lhs : self | Number, rhs : self | Number)
Returns the result of element-wise not equal to (
#!=
) comparison operation. -
.one_hot(indices, depth, **kwargs)
Returns a one-hot array.
-
.ones(shape : Int | Array(Int), ctx = Context.current, **kwargs)
Returns an array filled with all ones, with the given shape.
-
.ones_like(data, **kwargs)
Returns an array of ones with the same shape, data type and storage type as the input array.
-
.pick(data, index, **kwargs)
Picks elements from an input array according to the indices along the given axis.
-
.pooling(data : self, **kwargs)
Performs pooling on the input.
-
.power(base : self | Number, exp : self | Number)
Returns result of first array elements raised to powers from second array, element-wise.
-
.prod(data, **kwargs)
Computes the product of array elements over given axes.
-
.radians(data, **kwargs)
Converts each element of the input array from degrees to radians.
-
.random_exponential(lam : Number = 1.0, ctx : Context = Context.current, **kwargs)
Draws random samples from an exponential distribution.
-
.random_gamma(alpha : Number = 1.0, beta : Number = 1.0, ctx : Context = Context.current, **kwargs)
Draws random samples from a gamma distribution.
-
.random_normal(loc : Number = 0.0, scale : Number = 1.0, ctx : Context = Context.current, **kwargs)
Draws random samples from a normal (Gaussian) distribution.
-
.random_poisson(lam : Number = 1.0, ctx : Context = Context.current, **kwargs)
Draws random samples from a Poisson distribution.
-
.random_randint(low : Int, high : Int, ctx : Context = Context.current, **kwargs)
Draws random samples from a discrete uniform distribution.
-
.random_uniform(low : Number = 0.0, high : Number = 1.0, ctx : Context = Context.current, **kwargs)
Draws random samples from a uniform distribution.
-
.rcbrt(data, **kwargs)
Returns element-wise inverse cube-root value of the input.
-
.reciprocal(data, **kwargs)
Returns the reciprocal of the argument, element-wise.
-
.relu(data, **kwargs)
Computes the rectified linear activation.
-
.reshape(data, shape, **kwargs)
Reshapes the input array.
-
.reshape_like(lhs, rhs, **kwargs)
Reshape some or all dimensions of lhs to have the same shape as some or all dimensions of rhs.
-
.rint(data, **kwargs)
Returns element-wise rounded value to the nearest integer.
-
.round(data, **kwargs)
Returns element-wise rounded value to the nearest integer.
-
.rsqrt(data, **kwargs)
Returns element-wise inverse square-root value of the input.
-
.sample_exponential(lam : self, shape = [] of Int32, **kwargs)
Draws concurrent samples from exponential distributions.
-
.sample_gamma(alpha : self, beta : self, shape = [] of Int32, **kwargs)
Draws random samples from gamma distributions.
-
.sample_multinomial(data : self, get_prob : Bool = false, **kwargs)
Draws random samples from multinomial distributions.
-
.sample_normal(mu : self, sigma : self, shape = [] of Int32, **kwargs)
Draws concurrent samples from normal (Gaussian) distributions.
-
.sample_poisson(lam : self, shape = [] of Int32, **kwargs)
Draws concurrent samples from Poisson distributions.
-
.sample_uniform(low : self, high : self, shape = [] of Int32, **kwargs)
Draws concurrent samples from uniform distributions.
-
.save(fname, data)
Saves arrays to a file.
-
.sgd_mom_update(weight : self, grad : self, mom : self, lr : Float, **kwargs)
Momentum update function for Stochastic Gradient Descent (SGD) optimizer.
-
.sgd_update(weight : self, grad : self, lr : Float, **kwargs)
Update function for Stochastic Gradient Descent (SGD) optimizer.
-
.shape_array(data, **kwargs)
Returns a 1-D array containing the shape of the data.
-
.shuffle(data, **kwargs)
Randomly shuffles the elements.
-
.sigmoid(data, **kwargs)
Computes the sigmoid activation.
-
.sign(data, **kwargs)
Returns the element-wise sign of the input.
-
.sin(data, **kwargs)
Computes the element-wise sine of the input array.
-
.sinh(data, **kwargs)
Returns the hyperbolic sine of the input array, computed element-wise.
-
.size_array(data, **kwargs)
Returns a 1-D array containing the size of the data.
-
.slice(data, begin _begin, end _end, **kwargs)
Slices a region of the array.
-
.slice_axis(data, axis, begin _begin, end _end, **kwargs)
Slices along a given axis.
-
.slice_like(data, shape_like, **kwargs)
Slices like the shape of another array.
-
.softmax(data, **kwargs)
Applies the softmax function.
-
.sort(data, **kwargs)
Returns a sorted copy of an input array along the given axis.
-
.sqrt(data, **kwargs)
Returns element-wise square-root value of the input.
-
.square(data, **kwargs)
Returns element-wise squared value of the input.
-
.subtract(lhs : self | Number, rhs : self | Number)
Returns element-wise difference of the input arrays.
-
.sum(data, **kwargs)
Computes the sum of array elements over given axes.
-
.take(a, indices, **kwargs)
Takes elements from an input array along the given axis.
-
.tan(data, **kwargs)
Computes the element-wise tangent of the input array.
-
.tanh(data, **kwargs)
Returns the hyperbolic tangent of the input array, computed element-wise.
-
.tile(data, reps, **kwargs)
Repeats the array multiple times.
-
.topk(data, **kwargs)
Returns the top k elements in an input array along the given axis.
-
.transpose(data, **kwargs)
Permutes the dimensions of an array.
-
.trunc(data, **kwargs)
Return the element-wise truncated value of the input.
-
.where(condition, x, y, **kwargs)
Returns elements, either from x or y, depending on the condition.
-
.zeros(shape : Int | Array(Int), ctx = Context.current, **kwargs)
Returns an array filled with all zeros, with the given shape.
-
.zeros_like(data, **kwargs)
Returns an array of zeros with the same shape, data type and storage type as the input array.
Instance Method Summary
-
#!=(other)
Performs element-wise not equal to (
#!=
) comparison operation with broadcasting. -
#%(other)
Performs element-wise modulo with broadcasting.
-
#&(other)
Performs element-wise logical and (
#&
) comparison operation with broadcasting. -
#*(other)
Performs element-wise multiplication with broadcasting.
-
#**(other)
Returns the result of the first array elements raised to powers from the second array (or scalar), element-wise with broadcasting.
-
#+(other)
Performs element-wise addition with broadcasting.
-
#+
Leaves the values unchanged.
-
#-(other)
Performs element-wise subtraction with broadcasting.
-
#-
Performs element-wise numerical negative.
-
#/(other)
Performs element-wise division with broadcasting.
-
#<(other)
Performs element-wise less than (
#<
) comparison operation with broadcasting. -
#<=(other)
Performs element-wise less than or equal to (
#<=
) comparison operation with broadcasting. -
#==(other)
Performs element-wise equal to (
#==
) comparison operation with broadcasting. -
#>(other)
Performs element-wise greater than (
#>
) comparison operation with broadcasting. -
#>=(other)
Performs element-wise greater than or equal to (
#>=
) comparison operation with broadcasting. -
#[](keys : Array(Int | Range(Int?, Int?)))
Returns a sliced view of this array.
-
#[]=(keys : Array(Int | Range(Int?, Int?)), value : Number | self)
Sets sliced view of this array to the specified value.
-
#^(other)
Performs element-wise logical xor (
#^
) comparison operation with broadcasting. -
#abs(**kwargs)
Convenience fluent method for
.abs
. -
#arccos(**kwargs)
Convenience fluent method for
.arccos
. -
#arccosh(**kwargs)
Convenience fluent method for
.arccosh
. -
#arcsin(**kwargs)
Convenience fluent method for
.arcsin
. -
#arcsinh(**kwargs)
Convenience fluent method for
.arcsinh
. -
#arctan(**kwargs)
Convenience fluent method for
.arctan
. -
#arctanh(**kwargs)
Convenience fluent method for
.arctanh
. -
#argmax(**kwargs)
Convenience fluent method for
.argmax
. -
#argmin(**kwargs)
Convenience fluent method for
.argmin
. -
#argsort(**kwargs)
Convenience fluent method for
.argsort
. -
#as_in_context(context : Context, copy = false)
Returns a copy of the array on the target device with the same values as this array.
-
#as_scalar
Returns a scalar whose value is copied from this array.
-
#as_type(dtype : ::Symbol, copy = true)
Returns a copy of the array after casting to the specified type.
-
#attach_grad(grad_req = :write)
Attach a gradient buffer to this array, so that
#backward
can compute gradient with respect to it. -
#backward(gradient = nil, retain_graph = false, train_mode = true)
Compute the gradients of this array with respect to previously marked variables.
-
#broadcast_add(rhs, **kwargs)
Convenience fluent method for
.broadcast_add
. -
#broadcast_axis(**kwargs)
Convenience fluent method for
.broadcast_axis
. -
#broadcast_div(rhs, **kwargs)
Convenience fluent method for
.broadcast_div
. -
#broadcast_equal(rhs, **kwargs)
Convenience fluent method for
.broadcast_equal
. -
#broadcast_greater(rhs, **kwargs)
Convenience fluent method for
.broadcast_greater
. -
#broadcast_greater_equal(rhs, **kwargs)
Convenience fluent method for
.broadcast_greater_equal
. -
#broadcast_lesser(rhs, **kwargs)
Convenience fluent method for
.broadcast_lesser
. -
#broadcast_lesser_equal(rhs, **kwargs)
Convenience fluent method for
.broadcast_lesser_equal
. -
#broadcast_like(rhs, **kwargs)
Convenience fluent method for
.broadcast_like
. -
#broadcast_logical_and(rhs, **kwargs)
Convenience fluent method for
.broadcast_logical_and
. -
#broadcast_logical_or(rhs, **kwargs)
Convenience fluent method for
.broadcast_logical_or
. -
#broadcast_logical_xor(rhs, **kwargs)
Convenience fluent method for
.broadcast_logical_xor
. -
#broadcast_maximum(rhs, **kwargs)
Convenience fluent method for
.broadcast_maximum
. -
#broadcast_minimum(rhs, **kwargs)
Convenience fluent method for
.broadcast_minimum
. -
#broadcast_minus(rhs, **kwargs)
Convenience fluent method for
.broadcast_minus
. -
#broadcast_mul(rhs, **kwargs)
Convenience fluent method for
.broadcast_mul
. -
#broadcast_not_equal(rhs, **kwargs)
Convenience fluent method for
.broadcast_not_equal
. -
#broadcast_plus(rhs, **kwargs)
Convenience fluent method for
.broadcast_plus
. -
#broadcast_power(rhs, **kwargs)
Convenience fluent method for
.broadcast_power
. -
#broadcast_sub(rhs, **kwargs)
Convenience fluent method for
.broadcast_sub
. -
#broadcast_to(**kwargs)
Convenience fluent method for
.broadcast_to
. -
#cbrt(**kwargs)
Convenience fluent method for
.cbrt
. -
#ceil(**kwargs)
Convenience fluent method for
.ceil
. -
#clip(a_min, a_max, **kwargs)
Convenience fluent method for
.clip
. - #context
-
#copy_to(other : Context | self)
Copies the values of this array to another array.
-
#cos(**kwargs)
Convenience fluent method for
.cos
. -
#cosh(**kwargs)
Convenience fluent method for
.cosh
. -
#degrees(**kwargs)
Convenience fluent method for
.degrees
. -
#diag(**kwargs)
Convenience fluent method for
.diag
. -
#dot(rhs, **kwargs)
Convenience fluent method for
.dot
. - #dtype
-
#exp(**kwargs)
Convenience fluent method for
.exp
. -
#expand_dims(axis, **kwargs)
Convenience fluent method for
.expand_dims
. -
#expm1(**kwargs)
Convenience fluent method for
.expm1
. -
#fix(**kwargs)
Convenience fluent method for
.fix
. -
#flatten(**kwargs)
Convenience fluent method for
.flatten
. -
#flip(axis, **kwargs)
Convenience fluent method for
.flip
. -
#floor(**kwargs)
Convenience fluent method for
.floor
. -
#gamma(**kwargs)
Convenience fluent method for
.gamma
. -
#gammaln(**kwargs)
Convenience fluent method for
.gammaln
. -
#grad
Returns gradient buffer attached to this array.
-
#log(**kwargs)
Convenience fluent method for
.log
. -
#log10(**kwargs)
Convenience fluent method for
.log10
. -
#log1p(**kwargs)
Convenience fluent method for
.log1p
. -
#log2(**kwargs)
Convenience fluent method for
.log2
. -
#log_softmax(**kwargs)
Convenience fluent method for
.log_softmax
. -
#logical_not(**kwargs)
Convenience fluent method for
.logical_not
. -
#max(**kwargs)
Convenience fluent method for
.max
. -
#mean(**kwargs)
Convenience fluent method for
.mean
. -
#min(**kwargs)
Convenience fluent method for
.min
. -
#nanprod(**kwargs)
Convenience fluent method for
.nanprod
. -
#nansum(**kwargs)
Convenience fluent method for
.nansum
. -
#norm(**kwargs)
Convenience fluent method for
.norm
. -
#one_hot(depth, **kwargs)
Convenience fluent method for
.one_hot
. -
#ones_like(**kwargs)
Convenience fluent method for
.ones_like
. -
#pick(index, **kwargs)
Convenience fluent method for
.pick
. -
#prod(**kwargs)
Convenience fluent method for
.prod
. -
#radians(**kwargs)
Convenience fluent method for
.radians
. -
#rcbrt(**kwargs)
Convenience fluent method for
.rcbrt
. -
#reciprocal(**kwargs)
Convenience fluent method for
.reciprocal
. -
#relu(**kwargs)
Convenience fluent method for
.relu
. -
#reshape(shape, **kwargs)
Convenience fluent method for
.reshape
. -
#reshape_like(rhs, **kwargs)
Convenience fluent method for
.reshape_like
. -
#rint(**kwargs)
Convenience fluent method for
.rint
. -
#round(**kwargs)
Convenience fluent method for
.round
. -
#rsqrt(**kwargs)
Convenience fluent method for
.rsqrt
. - #shape
-
#shape_array(**kwargs)
Convenience fluent method for
.shape_array
. -
#shuffle(**kwargs)
Convenience fluent method for
.shuffle
. -
#sigmoid(**kwargs)
Convenience fluent method for
.sigmoid
. -
#sign(**kwargs)
Convenience fluent method for
.sign
. -
#sin(**kwargs)
Convenience fluent method for
.sin
. -
#sinh(**kwargs)
Convenience fluent method for
.sinh
. -
#size
Returns the number of elements in this container.
-
#size_array(**kwargs)
Convenience fluent method for
.size_array
. -
#slice(begin _begin, end _end, **kwargs)
Convenience fluent method for
.slice
. -
#slice_axis(axis, begin _begin, end _end, **kwargs)
Convenience fluent method for
.slice_axis
. -
#slice_like(shape_like, **kwargs)
Convenience fluent method for
.slice_like
. -
#softmax(**kwargs)
Convenience fluent method for
.softmax
. -
#sort(**kwargs)
Convenience fluent method for
.sort
. -
#sqrt(**kwargs)
Convenience fluent method for
.sqrt
. -
#square(**kwargs)
Convenience fluent method for
.square
. -
#sum(**kwargs)
Convenience fluent method for
.sum
. -
#take(indices, **kwargs)
Convenience fluent method for
.take
. -
#tan(**kwargs)
Convenience fluent method for
.tan
. -
#tanh(**kwargs)
Convenience fluent method for
.tanh
. -
#tile(reps, **kwargs)
Convenience fluent method for
.tile
. -
#to_a(as : Array(Array(Array(T))).class) : Array(Array(Array(Array(T)))) forall T
Returns an
Array
with values copied from this array. -
#to_a(as : Array(Array(T)).class) : Array(Array(Array(T))) forall T
Returns an
Array
with values copied from this array. -
#to_a(as : Array(T).class) : Array(Array(T)) forall T
Returns an
Array
with values copied from this array. -
#to_a(as : T.class) : Array(T) forall T
Returns an
Array
with values copied from this array. -
#to_a
Returns an
Array
with values copied from this array. - #to_s(io)
-
#topk(**kwargs)
Convenience fluent method for
.topk
. -
#transpose(**kwargs)
Convenience fluent method for
.transpose
. -
#trunc(**kwargs)
Convenience fluent method for
.trunc
. - #unsafe_fetch(idx)
-
#where(x, y, **kwargs)
Convenience fluent method for
.where
. -
#zeros_like(**kwargs)
Convenience fluent method for
.zeros_like
. -
#|(other)
Performs element-wise logical or (
#|
) comparison operation with broadcasting.
Class Method Detail
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
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
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
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
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.
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
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
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
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.
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.
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
orNone
, optional, default =-1
) The axis along which to choose sort the input tensor. If omitted, the last axis is used. IfNone
, 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.
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).
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
ornil
, 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"
ornil
, 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.
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
Returns the hyperbolic cosine of the input array, computed element-wise.
cosh(x) = (exp(x) + exp(-x)) / 2
Parameters
Converts each element of the input array from radians to degrees.
degrees([0, 𝜋/2, 𝜋, 3𝜋/2, 2𝜋]) = [0, 90, 180, 270, 360]
Parameters
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. Usek > 0
for diagonals above the main diagonal, andk < 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.
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
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
Returns an MXNet array of given shape and type, without initializing entries.
Parameters
- shape (
Int
orArray(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).
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
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
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
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
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
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
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
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
Returns a new array of given shape, filled with the given value.
Parameters
- shape (
Int
orArray(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).
Returns the gamma function (extension of the factorial function to the reals), computed element-wise on the input array.
Parameters
Returns the log of the absolute value of the gamma function, computed element-wise on the input array.
Parameters
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
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
TODO cache op handles
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
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
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.
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
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
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.
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
Performs element-wise logical not of the input array.
logical_not([-2, 0, 1]) = [0, 1, 0]
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
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
Computes the max of array elements over given axes.
Parameters
- data (
NDArray
, required) Input data. - axis (
Int
orArray(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 isInt
, a reduction is performed on a particular axis. If axis isArray(Int)
, a reduction is performed on all the axes specified in the list. If exclude istrue
, 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) Iftrue
, 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.
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
Computes the mean of array elements over given axes.
Parameters
- data (
NDArray
, required) Input data. - axis (
Int
orArray(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 isInt
, a reduction is performed on a particular axis. If axis isArray(Int)
, a reduction is performed on all the axes specified in the list. If exclude istrue
, 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) Iftrue
, 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.
Computes the min of array elements over given axes.
Parameters
- data (
NDArray
, required) Input data. - axis (
Int
orArray(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 isInt
, a reduction is performed on a particular axis. If axis isArray(Int)
, a reduction is performed on all the axes specified in the list. If exclude istrue
, 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) Iftrue
, 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.
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
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
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
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
orArray(Int)
, optional) The axis or axes along which to perform the reduction.axis: []
oraxis: nil
will compute over all elements into a scalar array with shape[1]
. If axis is anInt
, a reduction is performed on a particular axis. If axis is an array ofInt
, 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.
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
orArray(Int)
, optional) The axis or axes along which to perform the reduction.axis: []
oraxis: nil
will compute over all elements into a scalar array with shape[1]
. If axis is anInt
, a reduction is performed on a particular axis. If axis is an array ofInt
, 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.
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. Currentlyord: 1
andord: 2
are supported. - axis (
Int
orArray(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 isInt
, a reduction is performed on a particular axis. If axis isArray(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) Iftrue
, the reduced axes are left in the result as a dimension with size one. - out (
NDArray
, optional) The output array.
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
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.
Returns an array filled with all ones, with the given shape.
Parameters
- data (
NDArray
, required) Input data. - shape (
Int
orArray(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.
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
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
ornil
, optional, default = -1) The axis to pick the elements. Negative values mean indexing from right to left. Ifnil
, 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.
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"
ornil
, 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.
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
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
orArray(Int)
, optional) The axis or axes along which to perform the reduction.axis: []
oraxis: nil
will compute over all elements into a scalar array with shape[1]
. If axis is anInt
, a reduction is performed on a particular axis. If axis is an array ofInt
, 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.
Converts each element of the input array from degrees to radians.
radians([0, 90, 180, 270, 360]) = [0, 𝜋/2, 𝜋, 3𝜋/2, 2𝜋]
Parameters
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
orArray(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.
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
orArray(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.
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
orArray(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.
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
orArray(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.
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
orArray(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.
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
orArray(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.
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
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
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
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
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
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
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
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
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
orArray(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.
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
orArray(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.
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
orArray(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.
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
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
orArray(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.
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
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 tograd = rescale_grad * grad
. - clip_gradient (
Float
, optional, default = -1.0) Clip gradient to the range of [-clip_gradient, clip_gradient]. Ifclip_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.
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 tograd = rescale_grad * grad
. - clip_gradient (
Float
, optional, default = -1.0) Clip gradient to the range of [-clip_gradient, clip_gradient]. Ifclip_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.
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:
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
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
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
Returns the hyperbolic sine of the input array, computed element-wise.
sinh(x) = (exp(x) - exp(-x)) / 2
Parameters
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:
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.
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
ornil
, required) The ending index along the axis to be sliced. Supports negative indexes. - out (
NDArray
, optional) The output array.
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
orArray(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.
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.
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
orNone
, optional, default =-1
) The axis along which to choose sort the input tensor. If omitted, the last axis is used. IfNone
, 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.
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
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
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
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
orArray(Int)
, optional) The axis or axes along which to perform the reduction.axis: []
oraxis: nil
will compute over all elements into a scalar array with shape[1]
. If axis is anInt
, a reduction is performed on a particular axis. If axis is an array ofInt
, 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.
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.
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
Returns the hyperbolic tangent of the input array, computed element-wise.
tanh(x) = sinh(x) / cosh(x)
Parameters
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
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
orNone
, optional, default =-1
) Axis along which to choose the top k indices. If omitted, the last axis is used. IfNone
, 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 tofalse
. - 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.
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
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
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
Returns an array filled with all zeros, with the given shape.
Parameters
- data (
NDArray
, required) Input data. - shape (
Int
orArray(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.
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
Instance Method Detail
Returns the result of the first array elements raised to powers from the second array (or scalar), element-wise with broadcasting.
Performs element-wise less than or equal to (#<=
) comparison
operation with broadcasting.
Performs element-wise greater than or equal to (#>=
) comparison
operation with broadcasting.
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]])
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
orMXNet::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]]])
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 totrue
, and the target context is the same as this context, a copy is returned instead.
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
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 tofalse
, and the dtype requested is the same as this array's dtype, this array is returned instead of a copy.
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
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.
Convenience fluent method for .broadcast_greater_equal
.
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
Returns the number of elements in this container.
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 toArray(Float32)
specifyFloat32
.
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 toArray(Float32)
specifyFloat32
.
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 toArray(Float32)
specifyFloat32
.
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 toArray(Float32)
specifyFloat32
.
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)
.