Class NDArrayAdapter
- java.lang.Object
-
- ai.djl.ndarray.NDArrayAdapter
-
- All Implemented Interfaces:
BytesSupplier,NDArray,NDResource,java.lang.AutoCloseable
public abstract class NDArrayAdapter extends java.lang.Object implements NDArray
A base implementation of theNDArraythat does nothing. This can be used for overriding the NDArray with only part of the interface implemented.This interface should only be used for the NDArray implementations that do not plan to implement a large portion of the interface. For the ones that do, they should directly implement
NDArrayso that the unsupported operations are better highlighted in the code.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedNDArrayAdapter(NDManager manager, NDManager alternativeManager, Shape shape, DataType dataType, java.lang.String uid)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description NDArrayabs()Returns the absolute value of thisNDArrayelement-wise.NDArrayacos()Returns the inverse trigonometric cosine of thisNDArrayelement-wise.NDArrayacosh()Returns the inverse hyperbolic cosine of thisNDArrayelement-wise.NDArrayadd(NDArray other)Adds otherNDArrays to thisNDArrayelement-wise.NDArrayadd(java.lang.Number n)Adds a number to thisNDArrayelement-wise.NDArrayaddi(NDArray other)Adds otherNDArrays to thisNDArrayelement-wise in place.NDArrayaddi(java.lang.Number n)Adds a number to thisNDArrayelement-wise in place.NDArrayargMax()Returns the indices of the maximum values into the flattenedNDArray.NDArrayargMax(int axis)Returns the indices of the maximum values along given axis.NDArrayargMin()Returns the indices of the minimum values into the flattenedNDArray.NDArrayargMin(int axis)Returns the indices of the minimum values along given axis.NDArrayargSort(int axis, boolean ascending)Returns the indices that would sort thisNDArraygiven the axis.NDArrayasin()Returns the inverse trigonometric sine of thisNDArrayelement-wise.NDArrayasinh()Returns the inverse hyperbolic sine of thisNDArrayelement-wise.NDArrayatan()Returns the inverse trigonometric tangent of thisNDArrayelement-wise.NDArrayatanh()Returns the inverse hyperbolic tangent of thisNDArrayelement-wise.voidattach(NDManager manager)Attaches thisNDResourceto the specifiedNDManager.NDArraybatchDot(NDArray other)Batchwise product of thisNDArrayand the otherNDArray.NDArraybatchMatMul(NDArray other)Batch product matrix of thisNDArrayand the otherNDArray.NDArraybooleanMask(NDArray index, int axis)Returns portion of thisNDArraygiven the index booleanNDArrayalong given axis.NDArraybroadcast(Shape shape)Broadcasts thisNDArrayto be the given shape.NDArraycbrt()Returns the cube-root of thisNDArrayelement-wise.NDArrayceil()Returns the ceiling of thisNDArrayelement-wise.NDArrayclip(java.lang.Number min, java.lang.Number max)Clips (limit) the values in thisNDArray.voidclose()NDArraycomplex()Convert a general NDArray to its complex math format.booleancontentEquals(NDArray other)booleancontentEquals(java.lang.Number number)Returnstrueif all elements in thisNDArrayare equal to theNumber.NDArraycos()Returns the trigonometric cosine of thisNDArrayelement-wise.NDArraycosh()Returns the hyperbolic cosine of thisNDArrayelement-wise.NDArraycumProd(int axis)Returns the cumulative product of elements of input in the dimension dim.NDArraycumProd(int axis, DataType dataType)Returns the cumulative product of elements of input in the dimension dim.NDArraycumSum()Returns the cumulative sum of the elements in the flattenedNDArray.NDArraycumSum(int axis)Return the cumulative sum of the elements along a given axis.NDArraydiv(NDArray other)Divides thisNDArrayby the otherNDArrayelement-wise.NDArraydiv(java.lang.Number n)Divides thisNDArrayby a number element-wise.NDArraydivi(NDArray other)Divides thisNDArrayby the otherNDArrayelement-wise in place.NDArraydivi(java.lang.Number n)Divides thisNDArrayby a number element-wise in place.NDArraydot(NDArray other)Dot product of thisNDArrayand the otherNDArray.NDArrayeq(NDArray other)Returns the booleanNDArrayfor element-wise "Equals" comparison.NDArrayeq(java.lang.Number n)Returns the booleanNDArrayfor element-wise "Equals" comparison.booleanequals(java.lang.Object obj)NDArrayerfinv()Returns element-wise inverse gauss error function of theNDArray.NDArrayexp()Returns the exponential value of thisNDArrayelement-wise.NDArrayexpandDims(int axis)Expands theShapeof aNDArray.NDArrayfft(long length, long axis)Computes the one-dimensional discrete Fourier Transform.NDArrayflatten()Flattens thisNDArrayinto a 1-DNDArrayin row-major order.NDArrayflatten(int startDim, int endDim)Flattens thisNDArrayinto a partially flattenNDArray.NDArrayflip(int... axes)Returns the reverse order of elements in an array along the given axis.NDArrayfloor()Returns the floor of thisNDArrayelement-wise.NDArraygammaln()Return the log of the absolute value of the gamma function of thisNDArrayelement-wise.NDArraygather(NDArray index, int axis)Returns a partialNDArraypointed by the indexed array.NDArraygatherNd(NDArray index)Returns a partialNDArraypointed by the indexed array.NDArrayget(NDIndex index)Returns a partialNDArray.DataTypegetDataType()Returns theDataTypeof thisNDArray.DevicegetDevice()Returns theDeviceof thisNDArray.NDArraygetGradient()Returns the gradientNDArrayattached to thisNDArray.NDManagergetManager()Returns theNDManagerthat manages this.java.lang.StringgetName()Returns the name of thisNDArray.ai.djl.ndarray.internal.NDArrayExgetNDArrayInternal()Returns an internal representative of NativeNDArray.ShapegetShape()Returns theShapeof thisNDArray.SparseFormatgetSparseFormat()Returns theSparseFormatof thisNDArray.java.lang.StringgetUid()Returns unique identifier of thisNDArray.NDArraygt(NDArray other)Returns the booleanNDArrayfor element-wise "Greater Than" comparison.NDArraygt(java.lang.Number n)Returns the booleanNDArrayfor element-wise "Greater" comparison.NDArraygte(NDArray other)Returns the booleanNDArrayfor element-wise "Greater or equals" comparison.NDArraygte(java.lang.Number n)Returns the booleanNDArrayfor element-wise "Greater or equals" comparison.booleanhasGradient()Returns true if the gradient calculation is required for thisNDArray.inthashCode()NDArrayinverse()Computes the inverse of squareNDArrayif it exists.NDArrayisInfinite()Returns the booleanNDArraywith valuetruewhere thisNDArray's entries are infinite, orfalsewhere they are not infinite.NDArrayisNaN()Returns the booleanNDArraywith valuetruewhere thisNDArray's entries are NaN, orfalsewhere they are not NaN.booleanisReleased()Returnstrueif this NDArray has been released.NDArraylog()Returns the natural logarithmic value of thisNDArrayelement-wise.NDArraylog10()Returns the base 10 logarithm of thisNDArrayelement-wise.NDArraylog2()Returns the base 2 logarithm of thisNDArrayelement-wise.NDArraylogicalAnd(NDArray other)Returns the truth value of thisNDArrayAND the otherNDArrayelement-wise.NDArraylogicalNot()Computes the truth value of NOT thisNDArrayelement-wise.NDArraylogicalOr(NDArray other)Computes the truth value of thisNDArrayOR the otherNDArrayelement-wise.NDArraylogicalXor(NDArray other)Computes the truth value of thisNDArrayXOR the otherNDArrayelement-wise.NDArraylogSoftmax(int axis)Applies the softmax function followed by a logarithm.NDArraylt(NDArray other)Returns the booleanNDArrayfor element-wise "Less" comparison.NDArraylt(java.lang.Number n)Returns the booleanNDArrayfor element-wise "Less" comparison.NDArraylte(NDArray other)Returns the booleanNDArrayfor element-wise "Less or equals" comparison.NDArraylte(java.lang.Number n)Returns the booleanNDArrayfor element-wise "Less or equals" comparison.NDArraymatMul(NDArray other)Product matrix of thisNDArrayand the otherNDArray.NDArraymax()Returns the maximum of thisNDArray.NDArraymax(int[] axes, boolean keepDims)Returns the maximum of thisNDArrayalong given axes.NDArraymaximum(NDArray other)Returns the maximum of thisNDArrayand the otherNDArrayelement-wise.NDArraymaximum(java.lang.Number n)Returns the maximum of thisNDArrayand a number element-wise.NDArraymean()Returns the average of thisNDArray.NDArraymean(int[] axes, boolean keepDims)Returns the average of thisNDArrayalong given axes.NDArraymedian()Returns median value for thisNDArray.NDArraymedian(int[] axes)Returns median value along given axes.NDArraymin()Returns the minimum of thisNDArray.NDArraymin(int[] axes, boolean keepDims)Returns the minimum of thisNDArrayalong given axes.NDArrayminimum(NDArray other)Returns the minimum of thisNDArrayand the otherNDArrayelement-wise.NDArrayminimum(java.lang.Number n)Returns the minimum of thisNDArrayand a number element-wise.NDArraymod(NDArray other)Returns element-wise remainder of division.NDArraymod(java.lang.Number n)Returns element-wise remainder of division.NDArraymodi(NDArray other)Returns in place element-wise remainder of division in place.NDArraymodi(java.lang.Number n)Returns element-wise remainder of division in place.NDArraymul(NDArray other)Multiplies thisNDArrayby otherNDArrays element-wise.NDArraymul(java.lang.Number n)Multiplies thisNDArrayby a number element-wise.NDArraymuli(NDArray other)Multiplies thisNDArrayby otherNDArrayelement-wise in place.NDArraymuli(java.lang.Number n)Multiplies thisNDArrayby a number element-wise in place.NDArrayneg()Returns the numerical negativeNDArrayelement-wise.NDArraynegi()Returns the numerical negativeNDArrayelement-wise in place.NDArrayneq(NDArray other)Returns the booleanNDArrayfor element-wise "Not equals" comparison.NDArrayneq(java.lang.Number n)Returns the booleanNDArrayfor element-wise "Not equals" comparison.NDArraynonzero()Returns the indices of elements that are non-zero.NDArraynorm(boolean keepDims)Returns the norm of thisNDArray.NDArraynorm(int ord, int[] axes, boolean keepDims)Returns the norm of thisNDArray.NDArraynormalize(double p, long dim, double eps)Performs Lp normalization of the array over specified dimension.NDArrayoneHot(int depth, float onValue, float offValue, DataType dataType)Returns a one-hotNDArray.NDArraypercentile(java.lang.Number percentile)Returns percentile for thisNDArray.NDArraypercentile(java.lang.Number percentile, int[] axes)Returns median along given dimension(s).NDArraypow(NDArray other)Takes the power of thisNDArraywith the otherNDArrayelement-wise.NDArraypow(java.lang.Number n)Takes the power of thisNDArraywith a number element-wise.NDArraypowi(NDArray other)Takes the power of thisNDArraywith the otherNDArrayelement-wise in place.NDArraypowi(java.lang.Number n)Takes the power of thisNDArraywith a number element-wise in place.NDArrayprod()Returns the product of thisNDArray.NDArrayprod(int[] axes, boolean keepDims)Returns the product of thisNDArrayelements over the given axes.NDArrayput(NDArray index, NDArray value)Sets the entries ofNDArraypointed by index, according to linear indexing, to be the numbers in value.NDArrayreal()Convert a complex NDArray to its real math format.NDArrayrepeat(int axis, long repeats)Repeats element of thisNDArraythe number of times given repeats along given axis.NDArrayrepeat(long repeats)Repeats element of thisNDArraythe number of times given repeats.NDArrayrepeat(long[] repeats)Repeats element of thisNDArraythe number of times given repeats along each axis.NDArrayrepeat(Shape desiredShape)Repeats element of thisNDArrayto match the desired shape.NDArrayreshape(Shape shape)Reshapes thisNDArrayto the givenShape.NDArrayrotate90(int times, int[] axes)Rotates an array by 90 degrees in the plane specified by axes.NDArrayround()Returns the round of thisNDArrayelement-wise.NDArrayscatter(NDArray index, NDArray value, int axis)Writes all values from the tensor value into self at the indices specified in the index tensor.NDArraysequenceMask(NDArray sequenceLength)Sets all elements outside the sequence to 0.NDArraysequenceMask(NDArray sequenceLength, float value)Sets all elements outside the sequence to a constant value.voidset(NDIndex index, NDArray value)Sets the specified index in thisNDArraywith the given values.voidset(NDIndex index, java.lang.Number value)Sets the specified index in thisNDArraywith the given value.voidset(NDIndex index, java.util.function.Function<NDArray,NDArray> function)Sets the specific index by a function.voidset(NDArray index, java.lang.Number value)Sets theNDArrayby boolean mask or integer index.voidset(java.nio.Buffer buffer)Sets thisNDArrayvalue fromBuffer.voidsetName(java.lang.String name)Sets name of thisNDArray.voidsetRequiresGradient(boolean requiresGrad)Attaches a gradientNDArrayto thisNDArrayand marks it soGradientCollector.backward(NDArray)can compute the gradient with respect to it.voidsetScalar(NDIndex index, java.lang.Number value)Sets the specified scalar in thisNDArraywith the given value.NDArraysign()Returns the element-wise sign.NDArraysigni()Returns the element-wise sign in-place.NDArraysin()Returns the trigonometric sine of thisNDArrayelement-wise.NDArraysinh()Returns the hyperbolic sine of thisNDArrayelement-wise.NDArraysoftmax(int axis)Applies the softmax function along the given axis.NDArraysort()Sorts the flattenedNDArray.NDArraysort(int axis)Sorts the flattenedNDArray.NDListsplit(long[] indices, int axis)Splits thisNDArrayinto multiple sub-NDArrays given indices along given axis.NDListsplit(long sections, int axis)Splits thisNDArrayinto multiple subNDArrays given sections along the given axis.NDArraysqrt()Returns the square root of thisNDArrayelement-wise.NDArraysquare()Returns the square of thisNDArrayelement-wise.NDArraysqueeze(int[] axes)Removes singleton dimensions at the given axes.NDArraystft(long nFft, long hopLength, boolean center, NDArray window, boolean normalize, boolean returnComplex)Computes the Short Time Fourier Transform (STFT).NDArraystopGradient()Returns an NDArray equal to this that stop gradient propagation through it.NDArraysub(NDArray other)Subtracts the otherNDArrayfrom thisNDArrayelement-wise.NDArraysub(java.lang.Number n)Subtracts a number from thisNDArrayelement-wise.NDArraysubi(NDArray other)Subtracts the otherNDArrayfrom thisNDArrayelement-wise in place.NDArraysubi(java.lang.Number n)Subtracts a number from thisNDArrayelement-wise in place.NDArraysum()Returns the sum of thisNDArray.NDArraysum(int[] axes, boolean keepDims)Returns the sum of thisNDArrayalong given axes.NDArraytake(NDManager manager, NDArray index)Returns a partialNDArraypointed by index according to linear indexing, and the of output is of the same shape as index.NDArraytan()Returns the trigonometric tangent of thisNDArrayelement-wise.NDArraytanh()Returns the hyperbolic tangent of thisNDArrayelement-wise.voidtempAttach(NDManager manager)Temporarily attaches thisNDResourceto the specifiedNDManager.NDArraytile(int axis, long repeats)Constructs aNDArrayby repeating thisNDArraythe number of times given by repeats along given axis.NDArraytile(long repeats)Constructs aNDArrayby repeating thisNDArraythe number of times given repeats.NDArraytile(long[] repeats)Constructs aNDArrayby repeating thisNDArraythe number of times given by repeats.NDArraytile(Shape desiredShape)Constructs aNDArrayby repeating thisNDArraythe number of times to match the desired shape.NDArraytoDegrees()Converts thisNDArrayfrom radians to degrees element-wise.NDArraytoDense()Returns a dense representation of the sparseNDArray.NDArraytoDevice(Device device, boolean copy)Moves thisNDArrayto a differentDevice.NDListtopK(int k, int axis, boolean largest, boolean sorted)Returns (values, indices) of the top k values along given axis.NDArraytoRadians()Converts thisNDArrayfrom degrees to radians element-wise.NDArraytoSparse(SparseFormat fmt)Returns a sparse representation ofNDArray.java.lang.StringtoString()java.lang.String[]toStringArray(java.nio.charset.Charset charset)Converts thisNDArrayto a String array with the specified charset.NDArraytoType(DataType dataType, boolean copy)Converts thisNDArrayto a differentDataType.NDArraytrace(int offset, int axis1, int axis2)Returns the sum along diagonals of thisNDArray.NDArraytranspose()Returns thisNDArraywith axes transposed.NDArraytranspose(int... axes)Returns thisNDArraywith given axes transposed.NDArraytrunc()Returns the truncated value of thisNDArrayelement-wise.NDListunique(java.lang.Integer dim, boolean sorted, boolean returnInverse, boolean returnCounts)Returns the unique elements of the input tensor.NDArrayxlogy(NDArray other)Computes this * log(other).-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface ai.djl.ndarray.BytesSupplier
getAsBytes, getAsObject, getAsString, toByteBuffer
-
Methods inherited from interface ai.djl.ndarray.NDArray
all, allClose, allClose, any, argSort, argSort, booleanMask, broadcast, concat, concat, copyTo, countNonzero, countNonzero, duplicate, encode, fft, get, get, get, get, get, getBoolean, getByte, getDouble, getFloat, getInt, getLong, getResourceNDArrays, getScalar, getUint8, intern, isEmpty, isScalar, isSparse, like, max, mean, min, none, norm, norm, norm, normalize, normalize, oneHot, oneHot, onesLike, prod, reshape, scaleGradient, set, set, set, set, set, shapeEquals, size, size, split, split, squeeze, squeeze, stack, stack, stft, sum, swapAxes, take, toArray, toBooleanArray, toByteArray, toDebugString, toDebugString, toDebugString, toDoubleArray, toFloatArray, toIntArray, toLongArray, topK, toShortArray, toStringArray, toUint8Array, toUnsignedIntArray, toUnsignedShortArray, trace, trace, unique, unique, zerosLike
-
Methods inherited from interface ai.djl.ndarray.NDResource
detach, returnResource
-
-
-
-
Method Detail
-
getManager
public NDManager getManager()
Returns theNDManagerthat manages this.- Specified by:
getManagerin interfaceNDResource- Returns:
- the
NDManagerthat manages this.
-
attach
public void attach(NDManager manager)
Attaches thisNDResourceto the specifiedNDManager.Attached resource will be closed when the
NDManageris closed.- Specified by:
attachin interfaceNDResource- Parameters:
manager- theNDManagerto be attached to
-
tempAttach
public void tempAttach(NDManager manager)
Temporarily attaches thisNDResourceto the specifiedNDManager.Attached resource will be returned to the original manager when the
NDManageris closed.- Specified by:
tempAttachin interfaceNDResource- Parameters:
manager- theNDManagerto be attached to
-
getSparseFormat
public SparseFormat getSparseFormat()
Returns theSparseFormatof thisNDArray.- Specified by:
getSparseFormatin interfaceNDArray- Returns:
- the
SparseFormatof thisNDArray
-
getName
public java.lang.String getName()
Returns the name of thisNDArray.
-
setName
public void setName(java.lang.String name)
Sets name of thisNDArray.
-
getUid
public java.lang.String getUid()
Returns unique identifier of thisNDArray.
-
getDevice
public Device getDevice()
-
getDataType
public DataType getDataType()
Returns theDataTypeof thisNDArray.DataTypeis a definition of the precision level of theNDArray. All values inside the sameNDArraywould have the sameDataType.- Specified by:
getDataTypein interfaceNDArray- Returns:
- the
DataTypeof thisNDArray
-
getShape
public Shape getShape()
-
toDevice
public NDArray toDevice(Device device, boolean copy)
Moves thisNDArrayto a differentDevice.
-
toType
public NDArray toType(DataType dataType, boolean copy)
Converts thisNDArrayto a differentDataType.
-
setRequiresGradient
public void setRequiresGradient(boolean requiresGrad)
Attaches a gradientNDArrayto thisNDArrayand marks it soGradientCollector.backward(NDArray)can compute the gradient with respect to it.- Specified by:
setRequiresGradientin interfaceNDArray- Parameters:
requiresGrad- ifNDArrayrequires gradient or not
-
getGradient
public NDArray getGradient()
Returns the gradientNDArrayattached to thisNDArray.- Specified by:
getGradientin interfaceNDArray- Returns:
- the gradient
NDArray
-
hasGradient
public boolean hasGradient()
Returns true if the gradient calculation is required for thisNDArray.- Specified by:
hasGradientin interfaceNDArray- Returns:
- true if the gradient calculation is required for this
NDArrayelse false
-
stopGradient
public NDArray stopGradient()
Returns an NDArray equal to this that stop gradient propagation through it.- Specified by:
stopGradientin interfaceNDArray- Returns:
- an NDArray equal to this that stops gradient propagation through it
-
toStringArray
public java.lang.String[] toStringArray(java.nio.charset.Charset charset)
Converts thisNDArrayto a String array with the specified charset.This method is only applicable to the String typed NDArray and not for printing purpose
- Specified by:
toStringArrayin interfaceNDArray- Parameters:
charset- to charset for the string- Returns:
- Array of Strings
-
gather
public NDArray gather(NDArray index, int axis)
Returns a partialNDArraypointed by the indexed array.out[i][j][k] = input[index[i][j][k]][j][k] # if axis == 0 out[i][j][k] = input[i][index[i][j][k]][k] # if axis == 1 out[i][j][k] = input[i][j][index[i][j][k]] # if axis == 2
-
gatherNd
public NDArray gatherNd(NDArray index)
Returns a partialNDArraypointed by the indexed array.Given NDArray arr and NDArray idx. idx is the following structure: \( idx = [ idx[0, ...], idx[1, ...],..., idx[indexingDepth,...] ] \) corresponding to x, y, z index, i.e. [idx_x, idx_y, idx_z, ...].
So indexingDepth smaller than or equal to data.shape[0] If indexingDepth is smaller than data.shape[0], for instance, data.shape[0]=3, i.e. x,y,z but indexingDepth = 2, i.e. [idx_x, idx_y], then the tail co-rank = data.shape[0] - indexingDepth will be kept.
With it, the output shape = idx_y.shape appended by data.shape[indexingDepth:] mx.symbol.gather_nd
-
take
public NDArray take(NDManager manager, NDArray index)
Returns a partialNDArraypointed by index according to linear indexing, and the of output is of the same shape as index.
-
put
public NDArray put(NDArray index, NDArray value)
Sets the entries ofNDArraypointed by index, according to linear indexing, to be the numbers in value.Value has to be of the same shape as index.
-
scatter
public NDArray scatter(NDArray index, NDArray value, int axis)
Writes all values from the tensor value into self at the indices specified in the index tensor.This is the reverse operation of the manner described in gather(). self[index[i][j][k]][j][k] = value[i][j][k] # if axis == 0 self[i][index[i][j][k]][k] = value[i][j][k] # if axis == 1 self[i][j][index[i][j][k]] = value[i][j][k] # if axis == 2
torch.Tensor.scatter_- Specified by:
scatterin interfaceNDArray- Parameters:
index- the indices of elements to scatter, can be either empty or of the same dimensionality as value. When empty, the operation returns self unchangedvalue- the source element(s) to scatteraxis- the axis along which to index- Returns:
- the NDArray with updated values
-
set
public void set(java.nio.Buffer buffer)
Sets thisNDArrayvalue fromBuffer.
-
set
public void set(NDIndex index, NDArray value)
Sets the specified index in thisNDArraywith the given values.
-
set
public void set(NDIndex index, java.lang.Number value)
Sets the specified index in thisNDArraywith the given value.
-
set
public void set(NDIndex index, java.util.function.Function<NDArray,NDArray> function)
Sets the specific index by a function.
-
set
public void set(NDArray index, java.lang.Number value)
Sets theNDArrayby boolean mask or integer index.
-
setScalar
public void setScalar(NDIndex index, java.lang.Number value)
Sets the specified scalar in thisNDArraywith the given value.
-
booleanMask
public NDArray booleanMask(NDArray index, int axis)
Returns portion of thisNDArraygiven the index booleanNDArrayalong given axis.- Specified by:
booleanMaskin interfaceNDArray- Parameters:
index- booleanNDArraymaskaxis- an integer that represents the axis ofNDArrayto mask from- Returns:
- the result
NDArray
-
sequenceMask
public NDArray sequenceMask(NDArray sequenceLength, float value)
Sets all elements outside the sequence to a constant value.This function takes an n-dimensional input array of the form [batch_size, max_sequence_length, ....] and returns an array of the same shape. Parameter
sequenceLengthis used to handle variable-length sequences. sequence_length should be an input array of positive ints of dimension [batch_size].- Specified by:
sequenceMaskin interfaceNDArray- Parameters:
sequenceLength- used to handle variable-length sequencesvalue- the constant value to be set- Returns:
- the result
NDArray
-
sequenceMask
public NDArray sequenceMask(NDArray sequenceLength)
Sets all elements outside the sequence to 0.This function takes an n-dimensional input array of the form [batch_size, max_sequence_length, ....] and returns an array of the same shape. Parameter
sequenceLengthis used to handle variable-length sequences. sequence_length should be an input array of positive ints of dimension [batch_size].- Specified by:
sequenceMaskin interfaceNDArray- Parameters:
sequenceLength- used to handle variable-length sequences- Returns:
- the result
NDArray
-
contentEquals
public boolean contentEquals(java.lang.Number number)
Returnstrueif all elements in thisNDArrayare equal to theNumber.Examples
jshell> NDArray array = manager.ones(new Shape(2, 3)); jshell> array.contentEquals(1); // return true instead of boolean NDArray true
- Specified by:
contentEqualsin interfaceNDArray- Parameters:
number- the number to compare- Returns:
- the boolean result
-
contentEquals
public boolean contentEquals(NDArray other)
Returnstrueif all elements in thisNDArrayare equal to the otherNDArray.Examples
jshell> NDArray array1 = manager.arange(6f).reshape(2, 3); jshell> NDArray array2 = manager.create(new float[] {0f, 1f, 2f, 3f, 4f, 5f}, new Shape(2, 3)); jshell> array1.contentEquals(array2); // return true instead of boolean NDArray true- Specified by:
contentEqualsin interfaceNDArray- Parameters:
other- the otherNDArrayto compare- Returns:
- the boolean result
-
eq
public NDArray eq(java.lang.Number n)
Returns the booleanNDArrayfor element-wise "Equals" comparison.Examples
jshell> NDArray array = manager.ones(new Shape(1)); jshell> array.eq(1); ND: (1) cpu() boolean [ true]
-
eq
public NDArray eq(NDArray other)
Returns the booleanNDArrayfor element-wise "Equals" comparison.Examples
jshell> NDArray array1 = manager.create(new float[] {0f, 1f, 3f}); jshell> NDArray array2 = manager.arange(3f); jshell> array1.eq(array2); ND: (3) cpu() boolean [ true, true, false]
-
neq
public NDArray neq(java.lang.Number n)
Returns the booleanNDArrayfor element-wise "Not equals" comparison.Examples
jshell> NDArray array = manager.arange(4f).reshape(2, 2); jshell> array.neq(1); ND: (2, 2) cpu() boolean [[ true, false], [ true, true], ]
-
neq
public NDArray neq(NDArray other)
Returns the booleanNDArrayfor element-wise "Not equals" comparison.Examples
jshell> NDArray array1 = manager.create(new float[] {1f, 2f}); jshell> NDArray array2 = manager.create(new float[] {1f, 3f}); jshell> array1.neq(array2); ND: (2) cpu() boolean [false, true] jshell> NDArray array1 = manager.create(new float[] {1f, 2f}); jshell> NDArray array2 = manager.create(new float[] {1f, 3f, 1f, 4f}, new Shape(2, 2)); jshell> array1.neq(array2); // broadcasting ND: (2, 2) cpu() boolean [[false, true], [false, true], ]
-
gt
public NDArray gt(java.lang.Number n)
Returns the booleanNDArrayfor element-wise "Greater" comparison.Examples
jshell> NDArray array = manager.create(new float[] {4f, 2f}); jshell> array.gt(2f); ND: (2) cpu() boolean [ true, false]
-
gt
public NDArray gt(NDArray other)
Returns the booleanNDArrayfor element-wise "Greater Than" comparison.Examples
jshell> NDArray array1 = manager.create(new float[] {4f, 2f}); jshell> NDArray array2 = manager.create(new float[] {2f, 2f}); jshell> array1.neq(array2); ND: (2) cpu() boolean [ true, false]
-
gte
public NDArray gte(java.lang.Number n)
Returns the booleanNDArrayfor element-wise "Greater or equals" comparison.jshell> NDArray array = manager.create(new float[] {4f, 2f}); jshell> array.gte(2f); ND: (2) cpu() boolean [ true, true]
-
gte
public NDArray gte(NDArray other)
Returns the booleanNDArrayfor element-wise "Greater or equals" comparison.Examples
jshell> NDArray array1 = manager.create(new float[] {4f, 2f}); jshell> NDArray array2 = manager.create(new float[] {2f, 2f}); jshell> array1.gte(array2); ND: (2) cpu() boolean [ true, true]
-
lt
public NDArray lt(java.lang.Number n)
Returns the booleanNDArrayfor element-wise "Less" comparison.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f}); jshell> array.lt(2f); ND: (2) cpu() boolean [ true, false]
-
lt
public NDArray lt(NDArray other)
Returns the booleanNDArrayfor element-wise "Less" comparison.Examples
jshell> NDArray array1 = manager.create(new float[] {1f, 2f}); jshell> NDArray array2 = manager.create(new float[] {2f, 2f}); jshell> array1.lt(array2); ND: (2) cpu() boolean [ true, false]
-
lte
public NDArray lte(java.lang.Number n)
Returns the booleanNDArrayfor element-wise "Less or equals" comparison.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f}); jshell> array.lte(2f); ND: (2) cpu() boolean [ true, true]
-
lte
public NDArray lte(NDArray other)
Returns the booleanNDArrayfor element-wise "Less or equals" comparison.Examples
jshell> NDArray array1 = manager.create(new float[] {1f, 2f}); jshell> NDArray array2 = manager.create(new float[] {2f, 2f}); jshell> array1.lte(array2); ND: (2) cpu() boolean [ true, true]
-
add
public NDArray add(java.lang.Number n)
Adds a number to thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f}); jshell> array.add(2f); ND: (2) cpu() float32 [3., 4.]
-
add
public NDArray add(NDArray other)
Adds otherNDArrays to thisNDArrayelement-wise.The shapes of this
NDArrayand otherNDArraymust be broadcastable.Examples
jshell> NDArray array1 = manager.arange(9f).reshape(3, 3); jshell> NDArray array2 = manager.arange(3f); jshell> array1.add(array2); // broadcasting ND: (3, 3) cpu() float32 [[ 0., 2., 4.], [ 3., 5., 7.], [ 6., 8., 10.], ]
-
sub
public NDArray sub(java.lang.Number n)
Subtracts a number from thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f}); jshell> array.sub(2f); ND: (2) cpu() float32 [-1., 0.]
-
sub
public NDArray sub(NDArray other)
Subtracts the otherNDArrayfrom thisNDArrayelement-wise.The shapes of this
NDArrayand otherNDArrays must be broadcastable.Examples
jshell> NDArray array1 = manager.arange(9).reshape(3, 3); jshell> NDArray array2 = manager.arange(3); jshell> array1.sub(array2); // broadcasting ND: (3, 3) cpu() float32 [[0., 0., 0.], [3., 3., 3.], [6., 6., 6.], ]
-
mul
public NDArray mul(java.lang.Number n)
Multiplies thisNDArrayby a number element-wise.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f}); jshell> array.mul(3f); ND: (2) cpu() float32 [3., 6.]
-
mul
public NDArray mul(NDArray other)
Multiplies thisNDArrayby otherNDArrays element-wise.The shapes of this
NDArrayand otherNDArraymust be broadcastable.Examples
jshell> NDArray array1 = manager.arange(9f).reshape(3, 3); jshell> NDArray array2 = manager.arange(3f); jshell> array1.mul(array2); // broadcasting ND: (3, 3) cpu() float32 [[ 0., 1., 4.], [ 0., 4., 10.], [ 0., 7., 16.], ]
-
div
public NDArray div(java.lang.Number n)
Divides thisNDArrayby a number element-wise.Examples
jshell> NDArray array = manager.arange(5f); jshell> array.div(4f); ND: (5) cpu() float32 [0. , 0.25, 0.5 , 0.75, 1. ]
-
div
public NDArray div(NDArray other)
Divides thisNDArrayby the otherNDArrayelement-wise.The shapes of this
NDArrayand the otherNDArraymust be broadcastable.Examples
jshell> NDArray array1 = manager.arange(9f).reshape(3, 3); jshell> NDArray array2 = manager.ones(new Shape(3)).mul(10); jshell> array1.div(array2); // broadcasting ND: (3, 3) cpu() float32 [[0. , 0.1, 0.2], [0.3, 0.4, 0.5], [0.6, 0.7, 0.8], ]
-
mod
public NDArray mod(java.lang.Number n)
Returns element-wise remainder of division.Examples
jshell> NDArray array = manager.arange(7f); jshell> array.mod(5f); ND: (7) cpu() float32 [0., 1., 2., 3., 4., 0., 1.]
-
mod
public NDArray mod(NDArray other)
Returns element-wise remainder of division.The shapes of this
NDArrayand the otherNDArraymust be broadcastable.Examples
jshell> NDArray array1 = manager.create(new float[] {4f, 7f}); jshell> NDArray array2 = manager.create(new float[] {2f, 3f}); jshell> array1.mod(array2); ND: (2) cpu() float32 [0., 1.]
-
pow
public NDArray pow(java.lang.Number n)
Takes the power of thisNDArraywith a number element-wise.Examples
jshell> NDArray array = manager.arange(5f); jshell> array.pow(4f); ND: (6) cpu() float32 [ 0., 1., 8., 27., 64., 125.]
-
pow
public NDArray pow(NDArray other)
Takes the power of thisNDArraywith the otherNDArrayelement-wise.Examples
jshell> NDArray array1 = manager.arange(6f).reshape(3, 2); jshell> NDArray array2 = manager.create(new float[] {2f, 3f}); jshell> array1.pow(array2); // broadcasting ND: (3, 2) cpu() float32 [[ 0., 1.], [ 4., 27.], [ 16., 125.], ]
-
addi
public NDArray addi(java.lang.Number n)
Adds a number to thisNDArrayelement-wise in place.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f}); jshell> array.addi(2f); ND: (2) cpu() float32 [3., 4.] jshell> array; ND: (2) cpu() float32 [3., 4.]
-
addi
public NDArray addi(NDArray other)
Adds otherNDArrays to thisNDArrayelement-wise in place.The shapes of this
NDArrayand otherNDArrays must be broadcastable.Examples
jshell> NDArray array1 = manager.create(new float[] {1f, 2f}); jshell> NDArray array2 = manager.create(new float[] {3f, 4f}); jshell> array1.addi(array2); ND: (2) cpu() float32 [4., 6.] jshell> array; ND: (2) cpu() float32 [4., 6.]
-
subi
public NDArray subi(java.lang.Number n)
Subtracts a number from thisNDArrayelement-wise in place.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f}); jshell> array.subi(2f); ND: (2) cpu() float32 [-1., 0.] jshell> array; ND: (2) cpu() float32 [-1., 0.]
-
subi
public NDArray subi(NDArray other)
Subtracts the otherNDArrayfrom thisNDArrayelement-wise in place.The shapes of this
NDArrayand otherNDArrays must be broadcastable.Examples
jshell> NDArray array1 = manager.arange(9f).reshape(3, 3); jshell> NDArray array2 = manager.arange(3f); jshell> array1.subi(array2); // broadcasting ND: (3, 3) cpu() float32 [[0., 0., 0.], [3., 3., 3.], [6., 6., 6.], ] jshell> array1; [[0., 0., 0.], [3., 3., 3.], [6., 6., 6.], ]
-
muli
public NDArray muli(java.lang.Number n)
Multiplies thisNDArrayby a number element-wise in place.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f}); jshell> array.muli(3f); ND: (2) cpu() float32 [3., 6.] jshell> array; ND: (2) cpu() float32 [3., 6.]
-
muli
public NDArray muli(NDArray other)
Multiplies thisNDArrayby otherNDArrayelement-wise in place.The shapes of this
NDArrayand otherNDArrays must be broadcastable.Examples
jshell> NDArray array1 = manager.arange(9f).reshape(3, 3); jshell> NDArray array2 = manager.arange(3f); jshell> array1.muli(array2); // broadcasting ND: (3, 3) cpu() float32 [[ 0., 1., 4.], [ 0., 4., 10.], [ 0., 7., 16.], ] jshell> array1; ND: (3, 3) cpu() float32 [[ 0., 1., 4.], [ 0., 4., 10.], [ 0., 7., 16.], ]
-
divi
public NDArray divi(java.lang.Number n)
Divides thisNDArrayby a number element-wise in place.Examples
jshell> NDArray array = manager.arange(5f); jshell> array.divi(4f); ND: (5) cpu() float32 [0. , 0.25, 0.5 , 0.75, 1. ] jshell> array; ND: (5) cpu() float32 [0. , 0.25, 0.5 , 0.75, 1. ]
-
divi
public NDArray divi(NDArray other)
Divides thisNDArrayby the otherNDArrayelement-wise in place.The shapes of this
NDArrayand the otherNDArraymust be broadcastable.Examples
jshell> NDArray array1 = manager.arange(9f).reshape(3, 3); jshell> NDArray array2 = manager.ones(new Shape(3)).mul(10); jshell> array1.divi(array2); // broadcasting ND: (3, 3) cpu() float32 [[0. , 0.1, 0.2], [0.3, 0.4, 0.5], [0.6, 0.7, 0.8], ] jshell> array1; [[0. , 0.1, 0.2], [0.3, 0.4, 0.5], [0.6, 0.7, 0.8], ]
-
modi
public NDArray modi(java.lang.Number n)
Returns element-wise remainder of division in place.Examples
jshell> NDArray array = manager.arange(7f); jshell> array.modi(5f); ND: (7) cpu() float32 [0., 1., 2., 3., 4., 0., 1.] jshell> array; ND: (7) cpu() float32 [0., 1., 2., 3., 4., 0., 1.]
-
modi
public NDArray modi(NDArray other)
Returns in place element-wise remainder of division in place.The shapes of this
NDArrayand the otherNDArraymust be broadcastable.Examples
jshell> NDArray array1 = manager.create(new float[] {4f, 7f}); jshell> NDArray array2 = manager.create(new float[] {2f, 3f}); jshell> array1.modi(array2); ND: (2) cpu() float32 [0., 1.] jshell> array1; ND: (2) cpu() float32 [0., 1.]
-
powi
public NDArray powi(java.lang.Number n)
Takes the power of thisNDArraywith a number element-wise in place.Examples
jshell> NDArray array = manager.arange(5f); jshell> array.powi(4f); ND: (6) cpu() float32 [ 0., 1., 8., 27., 64., 125.] jshell> array; ND: (6) cpu() float32 [ 0., 1., 8., 27., 64., 125.]
-
powi
public NDArray powi(NDArray other)
Takes the power of thisNDArraywith the otherNDArrayelement-wise in place.The shapes of this
NDArrayand the otherNDArraymust be broadcastable.Examples
jshell> NDArray array1 = manager.arange(6f).reshape(3, 2); jshell> NDArray array2 = manager.create(new float[] {2f, 3f}); jshell> array1.powi(array2); // broadcasting ND: (3, 2) cpu() float32 [[ 0., 1.], [ 4., 27.], [ 16., 125.], ] jshell> array1; ND: (3, 2) cpu() float32 [[ 0., 1.], [ 4., 27.], [ 16., 125.], ]
-
sign
public NDArray sign()
Returns the element-wise sign.
-
signi
public NDArray signi()
Returns the element-wise sign in-place.
-
maximum
public NDArray maximum(java.lang.Number n)
Returns the maximum of thisNDArrayand a number element-wise.Examples
jshell> NDArray array = manager.create(new float[] {2f, 3f, 4f}); jshell> array.maximum(3f); ND: (3) cpu() float32 [3., 3., 4.]
-
maximum
public NDArray maximum(NDArray other)
Returns the maximum of thisNDArrayand the otherNDArrayelement-wise.The shapes of this
NDArrayand the otherNDArraymust be broadcastable.Examples
jshell> NDArray array1 = manager.create(new float[] {2f, 3f, 4f}); jshell> NDArray array2 = manager.create(new float[] {1f, 5f, 2f}); jshell> array1.maximum(array2); ND: (3) cpu() float32 [2., 5., 4.] jshell> NDArray array1 = manager.eye(2); jshell> NDArray array2 = manager.create(new float[] {0.5f, 2f}); jshell> array1.maximum(array2); // broadcasting ND: (2, 2) cpu() float32 [[1. , 2. ], [0.5, 2. ], ]
-
minimum
public NDArray minimum(java.lang.Number n)
Returns the minimum of thisNDArrayand a number element-wise.Examples
jshell> NDArray array = manager.create(new float[] {2f, 3f, 4f}); jshell> array.minimum(3f); ND: (3) cpu() float32 [2., 3., 3.]
-
minimum
public NDArray minimum(NDArray other)
Returns the minimum of thisNDArrayand the otherNDArrayelement-wise.The shapes of this
NDArrayand the otherNDArraymust be broadcastable.Examples
jshell> NDArray array1 = manager.create(new float[] {2f, 3f, 4f}); jshell> NDArray array2 = manager.create(new float[] {1f, 5f, 2f}); jshell> array1.minimum(array2); ND: (3) cpu() float32 [1., 3., 2.] jshell> NDArray array1 = manager.eye(2); jshell> NDArray array2 = manager.create(new float[] {0.5f, 2f}); jshell> array1.minimum(array2); // broadcasting ND: (2, 2) cpu() float32 [[0.5, 0. ], [0. , 1. ], ]
-
neg
public NDArray neg()
Returns the numerical negativeNDArrayelement-wise.jshell> NDArray array = manager.arange(5f); jshell> array.neg(); ND: (5) cpu() float32 [-0., -1., -2., -3., -4.]
-
negi
public NDArray negi()
Returns the numerical negativeNDArrayelement-wise in place.jshell> NDArray array = manager.arange(5f); jshell> array.negi(); jshell> array; ND: (5) cpu() float32 [-0., -1., -2., -3., -4.]
-
abs
public NDArray abs()
Returns the absolute value of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {-1f, -2f}); jshell> array.abs(); ND: (2) cpu() float32 [1., 2.]
-
square
public NDArray square()
Returns the square of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {2f, -3f}); jshell> array.square(); ND: (2) cpu() float32 [4., 9.]
-
sqrt
public NDArray sqrt()
Returns the square root of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {4f}); jshell> array.sqrt(); ND: (1) cpu() float32 [2., ]
-
cbrt
public NDArray cbrt()
Returns the cube-root of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {1f, 8f, 27f}); jshell> array.cbrt(); ND: (3) cpu() float32 [1., 2., 3.]
-
floor
public NDArray floor()
Returns the floor of thisNDArrayelement-wise.The floor of the scalar x is the largest integer i, such that i <= x.
Examples
jshell> NDArray array = manager.create(new float[] {-1.7f, -1.5f, -0.2f, 0.2f, 1.5f, 1.7f, 2.0f}); jshell> array.floor(); ND: (7) cpu() float32 [-2., -2., -1., 0., 1., 1., 2.]
-
ceil
public NDArray ceil()
Returns the ceiling of thisNDArrayelement-wise.The ceil of the scalar x is the smallest integer i, such that i >= x.
Examples
jshell> NDArray array = manager.create(new float[] {-1.7f, -1.5f, -0.2f, 0.2f, 1.5f, 1.7f, 2.0f}); jshell> array.ceil(); ND: (7) cpu() float32 [-1., -1., -0., 1., 2., 2., 2.]
-
round
public NDArray round()
Returns the round of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {-1.7f, -1.5f, -0.2f, 0.2f, 1.5f, 1.7f, 2.0f}); jshell> array.round(); ND: (7) cpu() float32 [-2., -2., -0., 0., 2., 2., 2.]
-
trunc
public NDArray trunc()
Returns the truncated value of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {-1.7f, -1.5f, -0.2f, 0.2f, 1.5f, 1.7f, 2.0f}); jshell> array.trunc(); ND: (7) cpu() float32 [-1., -1., -0., 0., 1., 1., 2.]
-
exp
public NDArray exp()
Returns the exponential value of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {0f, 2.5f}); jshell> array.exp(); ND: (2) cpu() float32 [ 1. , 12.1825]
-
gammaln
public NDArray gammaln()
Return the log of the absolute value of the gamma function of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {0.5f, 1f, 1.5f}); jshell> array.gammaln(); ND: (2) cpu() float32 [ 0.5724, 0.0000, -0.1208]
-
log
public NDArray log()
Returns the natural logarithmic value of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {0f, 2.5f}); jshell> array.log(); ND: (2) cpu() float32 [ -inf, 0.9163]
-
log10
public NDArray log10()
Returns the base 10 logarithm of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {1000f, 1f, 150f}); jshell> array.log10(); ND: (3) cpu() float32 [3. , 0. , 2.1761]
-
log2
public NDArray log2()
Returns the base 2 logarithm of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new float[] {8, 1f, 5f}); jshell> array.log2(); ND: (3) cpu() float32 [3. , 0. , 2.3219]
-
sin
public NDArray sin()
Returns the trigonometric sine of thisNDArrayelement-wise.The input should be in radians (2 Pi radians equals 360 degrees).
Examples
jshell> NDArray array = manager.create(new float[] {0f, 30f, 45f, 60f, 90f}); jshell> array = array.mul(Math.PI).div(180f); jshell> array.sin(); ND: (5) cpu() float32 [0. , 0.5 , 0.7071, 0.866 , 1. ]
-
cos
public NDArray cos()
Returns the trigonometric cosine of thisNDArrayelement-wise.The input should be in radians (2 Pi radians equals 360 degrees).
Examples
jshell> NDArray array = manager.create(new double[] {0, Math.PI/2, Math.PI}); jshell> array.cos(); ND: (3) cpu() float64 [ 1.0000000e+00, 6.1232340e-17, -1.0000000e+00],
-
tan
public NDArray tan()
Returns the trigonometric tangent of thisNDArrayelement-wise.The input should be in radians (2 Pi radians equals 360 degrees).
Examples
jshell> NDArray array = manager.create(new double[] {-Math.PI, Math.PI/2, Math.PI}); jshell> array.tan(); ND: (3) cpu() float64 [ 1.2246468e-16, 1.6331239e+16, -1.2246468e-16],
-
asin
public NDArray asin()
Returns the inverse trigonometric sine of thisNDArrayelement-wise.The input should be in the range [-1, 1]. The output is in the closed interval of [-Pi/2, Pi/2].
Examples
jshell> NDArray array = manager.create(new float[] {1f, -1f, 0f}); jshell> array.asin(); ND: (3) cpu() float64 [ 1.5708, -1.5708, 0. ]
-
acos
public NDArray acos()
Returns the inverse trigonometric cosine of thisNDArrayelement-wise.The input should be in the range [-1, 1]. The output is in the closed interval of [-Pi/2, Pi/2].
Examples
jshell> NDArray array = manager.create(new float[] {1f, -1f}); jshell> array.acos(); ND: (2) cpu() float64 [0. , 3.1416]
-
atan
public NDArray atan()
Returns the inverse trigonometric tangent of thisNDArrayelement-wise.The input should be in the range [-1, 1]. The output is in the closed interval of [-Pi/2, Pi/2].
Examples
jshell> NDArray array = manager.create(new float[] {0f, 1f}); jshell> array.atan(); ND: (2) cpu() float64 [0. , 0.7854]
-
sinh
public NDArray sinh()
Returns the hyperbolic sine of thisNDArrayelement-wise.sinh(x)=0.5*(exp(x) - exp(-x))
Examples
jshell> NDArray array = manager.create(new double[] {0, Math.PI}); jshell> array.sinh(); ND: (2) cpu() float64 [ 0. , 11.5487]
-
cosh
public NDArray cosh()
Returns the hyperbolic cosine of thisNDArrayelement-wise.cosh(x)=0.5*(exp(x)+exp(-x))
Examples
jshell> NDArray array = manager.create(new double[] {0, Math.PI}); jshell> array.cosh(); ND: (2) cpu() float64 [ 1. , 11.592 ]
-
tanh
public NDArray tanh()
Returns the hyperbolic tangent of thisNDArrayelement-wise.tanh(x)=sinh(x)/cosh(x)
Examples
jshell> NDArray array = manager.create(new double[] {0, Math.PI}); jshell> array.tanh(); ND: (2) cpu() float64 [0. , 0.9963]
-
asinh
public NDArray asinh()
Returns the inverse hyperbolic sine of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new double[] {Math.E, 10}); jshell> array.asinh(); ND: (2) cpu() float64 [1.7254, 2.9982]
-
acosh
public NDArray acosh()
Returns the inverse hyperbolic cosine of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new double[] {Math.E, 10}); jshell> array.acosh(); ND: (2) cpu() float64 [1.6575, 2.9932]
-
atanh
public NDArray atanh()
Returns the inverse hyperbolic tangent of thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new double[] {0, -0.5}); jshell> array.atanh(); ND: (2) cpu() float64 [ 0. , -0.5493]
-
toDegrees
public NDArray toDegrees()
Converts thisNDArrayfrom radians to degrees element-wise.Examples
jshell> NDArray array = manager.arange(6f).mul(Math.PI / 3); jshell> array.toDegrees(); ND: (6) cpu() float32 [ 0., 60., 120., 180., 240., 300.]
-
toRadians
public NDArray toRadians()
Converts thisNDArrayfrom degrees to radians element-wise.Examples
jshell> NDArray array = manager.arange(6f).mul(60); jshell> array.toRadians(); ND: (6) cpu() float32 [0. , 1.0472, 2.0944, 3.1416, 4.1888, 5.236 ]
-
max
public NDArray max()
Returns the maximum of thisNDArray.Examples
jshell> NDArray array = manager.arange(4f).reshape(2,2); jshell> array; ND: (2, 2) cpu() float32 [[0., 1.], [2., 3.], ] jshell> array.max(); // Maximum of the flattened array ND: () cpu() float32 3. jshell> array.max().getFloat() // Use getFloat() to get native float 3.0
-
max
public NDArray max(int[] axes, boolean keepDims)
Returns the maximum of thisNDArrayalong given axes.Examples
jshell> NDArray array = manager.arange(4f).reshape(2,2); jshell> array; ND: (2, 2) cpu() float32 [[0., 1.], [2., 3.], ] jshell> array.max(new int[]{0}, true); // Maximum along the first axis and keep dimension ND: (1, 2) cpu() float32 [[2., 3.], ] jshell> array.max(new int[]{1}, true); // Maximum along the second axis and keep dimension ND: (2, 1) cpu() float32 [[1.], [3.], ]
-
min
public NDArray min()
Returns the minimum of thisNDArray.Examples
jshell> NDArray array = manager.arange(4f).reshape(2,2); jshell> array; ND: (2, 2) cpu() float32 [[0., 1.], [2., 3.], ] jshell> array.min(); // Minimum of the flattened array ND: () cpu() float32 0. jshell> array.min().getFloat(); // Use getFloat() to get native float 0.0
-
min
public NDArray min(int[] axes, boolean keepDims)
Returns the minimum of thisNDArrayalong given axes.Examples
jshell> NDArray array = manager.arange(4f).reshape(2,2); jshell> array ND: (2, 2) cpu() float32 [[0., 1.], [2., 3.], ] jshell> array.min(new int[]{0}, true) // Minimum along the first axis and keep dimension ND: (1, 2) cpu() float32 [[0., 1.], ] jshell> array.min(new int[]{1}, true) // Minimum along the second axis and keep dimension ND: (2, 1) cpu() float32 [[0.], [2.], ]
-
sum
public NDArray sum()
Returns the sum of thisNDArray.Examples
jshell> NDArray array = manager.create(new float[] {0.5f, 1.5f}); jshell> array.sum(); ND: () cpu() float32 2. jshell> array.sum().getFloat(); // Use getFloat() to get native float 2.0 jshell> NDArray array = manager.create(new float[] {0f, 1f, 0f, 5f}, new Shape(2, 2)); jshell> array.sum(); ND: () cpu() float32 6.
-
sum
public NDArray sum(int[] axes, boolean keepDims)
Returns the sum of thisNDArrayalong given axes.Examples
jshell> NDArray array = manager.create(new float[] {0f, 1f, 0f, 5f}, new Shape(2, 2)); jshell> array; ND: (2, 2) cpu() float32 [[0., 1.], [0., 5.], ] jshell> array.sum(new int[] {0}, true); ND: (1, 2) cpu() float32 [[0., 6.], ] jshell> array.sum(new int[] {1}, true); ND: (2, 2) cpu() float32 [[0., 1.], [0., 5.], ]
-
cumProd
public NDArray cumProd(int axis)
Returns the cumulative product of elements of input in the dimension dim. For example, if input is a vector of size N, the result will also be a vector of size N, with elements. [x1, x1 * x2, x1 * x2 *x3 ...]
-
cumProd
public NDArray cumProd(int axis, DataType dataType)
Returns the cumulative product of elements of input in the dimension dim. For example, if input is a vector of size N, the result will also be a vector of size N, with elements. [x1, x1 * x2, x1 * x2 *x3 ...]
-
prod
public NDArray prod()
Returns the product of thisNDArray.Examples
jshell> NDArray array = manager.create(new float[] {2f, 3f}); jshell> array.prod(); ND: () cpu() float32 6. jshell> array.prod().getFloat(); // Use getFloat to get native float 6.0 jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2)); jshell> array.prod(); ND: () cpu() float32 24.
-
prod
public NDArray prod(int[] axes, boolean keepDims)
Returns the product of thisNDArrayelements over the given axes.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2)); jshell> array; ND: (2, 2) cpu() float32 [[1., 2.], [3., 4.], ] jshell> array.prod(new int[] {0}, true); ND: (1, 2) cpu() float32 [[3., 8.], ] jshell> array.prod(new int[] {1}, true); ND: (2, 1) cpu() float32 [[ 2.], [12.], ]
-
mean
public NDArray mean()
Returns the average of thisNDArray.Examples
jshell> NDArray array = manager.create(new float[] {2f, 3f}); jshell> array.mean(); ND: () cpu() float32 2.5 jshell> array.mean().getFloat(); // Use getFloat() to get native float 2.5 jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2)); jshell> array.mean(); ND: () cpu() float32 2.5
-
mean
public NDArray mean(int[] axes, boolean keepDims)
Returns the average of thisNDArrayalong given axes.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2)); jshell> array; ND: (2, 2) cpu() float32 [[1., 2.], [3., 4.], ] jshell> array.mean(new int[] {0}, true); ND: (1, 2) cpu() float32 [[2., 3.], ] jshell> array.mean(new int[] {1}, true); ND: (2, 1) cpu() float32 [[1.5], [3.5], ]
-
normalize
public NDArray normalize(double p, long dim, double eps)
Performs Lp normalization of the array over specified dimension.Examples
jshell> NDArray array = manager.create(new float[] {1, 2, 3, 4, 5, 6}, new Shape(2, 3)); jshell> array; ND: (2, 2) cpu() float32 [[1., 2., 3.], [4., 5., 6.], ] jshell> array.normalize(2, 1, 1e-12); ND: (2, 3) cpu() float32 [[0.2673, 0.5345, 0.8018], [0.4558, 0.5698, 0.6838], ]
-
rotate90
public NDArray rotate90(int times, int[] axes)
Rotates an array by 90 degrees in the plane specified by axes.Rotation direction is from the first towards the second axis.
-
trace
public NDArray trace(int offset, int axis1, int axis2)
Returns the sum along diagonals of thisNDArray.If this
NDArrayis 2-D, the sum along its diagonal with the given offset is returned, i.e., the sum of elements a[i,i+offset] for all i. If thisNDArrayhas more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-arrays whose traces are returned. TheShapeof the resulting array is the same as thisNDArraywith axis1 and axis2 removed.Examples
jshell> NDArray array = manager.arange(8f).reshape(2, 2, 2); jshell> array; ND: (2, 2, 2) cpu() float32 [[[0., 1.], [2., 3.], ], [[4., 5.], [6., 7.], ], ] jshell> array.trace(1,1,2); ND: (2) cpu() float32 [1., 5.]
- Specified by:
tracein interfaceNDArray- Parameters:
offset- offset of the diagonal from the main diagonal. Can be both positive and negative.axis1- axes to be used as the first axis of the 2-D sub-arrays from which the diagonals should be takenaxis2- axes to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken- Returns:
- the sum along diagonals of this
NDArray
-
split
public NDList split(long sections, int axis)
Splits thisNDArrayinto multiple subNDArrays given sections along the given axis.Examples
jshell> NDArray array = manager.arange(18f).reshape(2, 9); jshell> array; ND: (2, 9) cpu() float32 [[ 0., 1., 2., 3., 4., 5., 6., 7., 8.], [ 9., 10., 11., 12., 13., 14., 15., 16., 17.], ] jshell> array.split(3, 1).forEach(System.out::println); ND: (2, 3) cpu() float32 [[ 0., 1., 2.], [ 9., 10., 11.], ] ND: (2, 3) cpu() float32 [[ 3., 4., 5.], [12., 13., 14.], ] ND: (2, 3) cpu() float32 [[ 6., 7., 8.], [15., 16., 17.], ]
-
split
public NDList split(long[] indices, int axis)
Splits thisNDArrayinto multiple sub-NDArrays given indices along given axis.Examples
jshell> NDArray array = manager.arange(18f).reshape(2, 9); jshell> array; ND: (2, 9) cpu() float32 [[ 0., 1., 2., 3., 4., 5., 6., 7., 8.], [ 9., 10., 11., 12., 13., 14., 15., 16., 17.], ] jshell> array.split(new int[] {2,4,5}, 1).forEach(System.out::println); ND: (2, 2) cpu() float32 [[ 0., 1.], [ 9., 10.], ] ND: (2, 2) cpu() float32 [[ 2., 3.], [11., 12.], ] ND: (2, 1) cpu() float32 [[ 4.], [13.], ] ND: (2, 4) cpu() float32 [[ 5., 6., 7., 8.], [14., 15., 16., 17.], ]- Specified by:
splitin interfaceNDArray- Parameters:
indices- the entries indicate where along axis thisNDArrayis split. If an index exceeds the dimension of thisNDArrayalong axis, an empty sub-array is returned correspondinglyaxis- the axis to split along- Returns:
- an
NDListwith numOutputsNDArrays withShape(this.shape.axis /= axis)
-
flatten
public NDArray flatten()
Flattens thisNDArrayinto a 1-DNDArrayin row-major order.To flatten in column-major order, first transpose this
NDArrayExamples
jshell> NDArray array = manager.create(new float[]{1f, 2f, 3f, 4f}, new Shape(2, 2)); jshell> array.flatten(); ND: (4) cpu() float32 [1., 2., 3., 4.]
-
flatten
public NDArray flatten(int startDim, int endDim)
Flattens thisNDArrayinto a partially flattenNDArray.Examples
jshell> NDArray array = manager.create(new float[]{1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f}, new Shape(2, 2, 2)); jshell> array.flatten(0, 1); ND: (4) cpu() float32 [[1., 2], [3., 4.], [5., 6.], [7., 8.]]
-
fft
public NDArray fft(long length, long axis)
Computes the one-dimensional discrete Fourier Transform.
-
stft
public NDArray stft(long nFft, long hopLength, boolean center, NDArray window, boolean normalize, boolean returnComplex)
Computes the Short Time Fourier Transform (STFT).- Specified by:
stftin interfaceNDArray- Parameters:
nFft- size of Fourier transformhopLength- the distance between neighboring sliding window frames. Default can be: floor(n_fft / 4)center- whether to pad input on both sides.window- Desired window to use. Recommend for HanningWindownormalize- controls whether to return the normalized STFT resultsreturnComplex- whether to return a complex tensor, or a real tensor with an extra last dimension for the real and imaginary components.- Returns:
- A NDArray containing the STFT result with shape described above
-
reshape
public NDArray reshape(Shape shape)
Reshapes thisNDArrayto the givenShape.You can reshape it to match another NDArray by calling
a.reshape(b.getShape())Examples
jshell> NDArray array = manager.arange(6f); jshell> array; ND: (6) cpu() float32 [0., 1., 2., 3., 4., 5.] jshell> array.reshape(new Shape(2, 3)); ND: (2, 3) cpu() float32 [[0., 1., 2.], [3., 4., 5.], ]
-
expandDims
public NDArray expandDims(int axis)
Expands theShapeof aNDArray.Inserts a new axis that will appear at the axis position in the expanded
NDArrayshape.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f}); jshell> array; ND: (2) cpu() float32 [1., 2.] jshell> array.expandDims(0); ND: (1, 2) cpu() float32 [[1., 2.], ] jshell> array.expandDims(1); ND: (2, 1) cpu() float32 [[1.], [2.], ]- Specified by:
expandDimsin interfaceNDArray- Parameters:
axis- the position in the expanded axes where the new axis is placed- Returns:
- the result
NDArray. The number of dimensions is one greater than that of theNDArray
-
squeeze
public NDArray squeeze(int[] axes)
Removes singleton dimensions at the given axes.Examples
jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f}, new Shape(1, 3, 1)); jshell> array; ND: (1, 3, 1) cpu() float32 [[[0.], [1.], [2.], ], ] jshell> array.squeeze(new int[] {0, 2}); ND: (3) cpu() float32 [0., 1., 2.]
-
unique
public NDList unique(java.lang.Integer dim, boolean sorted, boolean returnInverse, boolean returnCounts)
Returns the unique elements of the input tensor.Examples
jshell> NDArray array = manager.create(new float[] {3f, 1f, 2f, 3f, 1f, 2f, 1f, 3f, 2f}, new Shape(3, 3)); jshell> array; ND: (3, 3) cpu() float32 [[[3., 1., 2.], [3., 1., 2.], [1., 3., 2.], ], ] jshell> NDList output = array.unique(0, true, true, true); jshell> output.get(0); jshell> output.get(1); jshell> output.get(2); ND: (2, 3) cpu() float32 [[1., 3., 2.], [3., 1., 2.], ] ND: (3) cpu() int64 [ 1, 1, 0] ND: (2) cpu() int64 [ 1, 2]- Specified by:
uniquein interfaceNDArray- Parameters:
dim- the dimension to apply uniquesorted- whether to sort the unique elements in ascending order before returning as outputreturnInverse- return the indices which, fed into the output unique array as indices, will recover the original arrayreturnCounts- return the counts for each unique element- Returns:
- An
NDListcontaining: output (Tensor): the output list of unique elements or low-rank tensors. inverse_indices (Tensor): (optional) if return_inverse is True, there will be an additional returned tensor (same shape as input) representing the indices for where elements in the original input map to in the output; otherwise, this function will only return a single tensor. counts (Tensor): (optional) if return_counts is True, there will be an additional returned tensor (same shape as output or output.size(dim), if dim was specified) representing the number of occurrences for each unique value or tensor.
-
logicalAnd
public NDArray logicalAnd(NDArray other)
Returns the truth value of thisNDArrayAND the otherNDArrayelement-wise.The shapes of this
NDArrayand the otherNDArraymust be broadcastable.Examples
jshell> NDArray array1 = manager.create(new boolean[] {true}); jshell> NDArray array2 = manager.create(new boolean[] {false}); jshell> array1.logicalAnd(array2); ND: (1) cpu() boolean [false] jshell> array1 = manager.create(new boolean[] {true, false}); jshell> array2 = manager.create(new boolean[] {false, false}); jshell> array1.logicalAnd(array2); ND: (2) cpu() boolean [false, false]jshell> NDArray array = manager.arange(5f); jshell> array.gt(1).logicalAnd(array.lt(4)); ND: (5) cpu() boolean [false, false, true, true, false]
- Specified by:
logicalAndin interfaceNDArray- Parameters:
other- the otherNDArrayto operate on- Returns:
- the boolean
NDArrayof the logical AND operation applied to the elements of thisNDArrayand the otherNDArray
-
logicalOr
public NDArray logicalOr(NDArray other)
Computes the truth value of thisNDArrayOR the otherNDArrayelement-wise.The shapes of this
NDArrayand the otherNDArraymust be broadcastable.Examples
jshell> NDArray array1 = manager.create(new boolean[] {true}); jshell> NDArray array2 = manager.create(new boolean[] {false}); jshell> array1.logicalOr(array2); ND: (1) cpu() boolean [ true] jshell> array1 = manager.create(new boolean[] {true, false}); jshell> array2 = manager.create(new boolean[] {false, false}); jshell> array1.logicalOr(array2); ND: (2) cpu() boolean [ true, false]jshell> NDArray array = manager.arange(5f); jshell> array.lt(1).logicalOr(array.gt(3)); ND: (5) cpu() boolean [ true, false, false, false, true]
-
logicalXor
public NDArray logicalXor(NDArray other)
Computes the truth value of thisNDArrayXOR the otherNDArrayelement-wise.The shapes of this
NDArrayand the otherNDArraymust be broadcastable.Examples
jshell> NDArray array = manager.create(new boolean[] {true}); jshell> array1.logicalXor(array2); ND: (1) cpu() boolean [ true] jshell> array1 = manager.create(new boolean[] {true, false}); jshell> array2 = manager.create(new boolean[] {false, false}); jshell> array1.logicalXor(array2); ND: (2) cpu() boolean [ true, false]jshell> NDArray array = manager.arange(5f); jshell> array.lt(1).logicalXor(array.gt(3)); ND: (5) cpu() boolean [ true, false, false, false, true]
- Specified by:
logicalXorin interfaceNDArray- Parameters:
other- the otherNDArrayto operate on- Returns:
- the boolean
NDArrayof the logical XOR operation applied to the elements of thisNDArrayand the otherNDArray
-
logicalNot
public NDArray logicalNot()
Computes the truth value of NOT thisNDArrayelement-wise.Examples
jshell> NDArray array = manager.create(new boolean[] {true}); jshell> array.logicalNot(); ND: (1) cpu() boolean [ false]jshell> NDArray array = manager.arange(5f); jshell> array.lt(1).logicalNot(); ND: (5) cpu() boolean [false, true, true, true, true]
- Specified by:
logicalNotin interfaceNDArray- Returns:
- the boolean
NDArray
-
argSort
public NDArray argSort(int axis, boolean ascending)
Returns the indices that would sort thisNDArraygiven the axis.Perform an indirect sort along the given axis. It returns a
NDArrayof indices of the sameShapeas thisNDArray.Examples
jshell> NDArray array = manager.create(new float[] {0f, 3f, 2f, 2f}, new Shape(2, 2)); jshell> array.argSort(0, false); ND: (2, 2) cpu() int64 [[ 1, 0], [ 0, 1], ]- Specified by:
argSortin interfaceNDArray- Parameters:
axis- the axis to sort alongascending- whether to sort ascending- Returns:
- a
NDArrayof indices corresponding to elements in thisNDArrayon the axis, the output DataType is alwaysDataType.INT64
-
sort
public NDArray sort()
Sorts the flattenedNDArray.Examples
jshell> NDArray array = manager.create(new float[] {1f, 4f, 3f, 1f}, new Shape(2, 2)); jshell> array; ND: (2, 2) cpu() float32 [[1., 4.], [3., 1.], ] jshell> array.sort(); // sort the flattened array ND: (2, 2) cpu() float32 [[1., 4.], [1., 3.], ]
-
sort
public NDArray sort(int axis)
Sorts the flattenedNDArray.Examples
jshell> NDArray array = manager.create(new float[] {1f, 4f, 3f, 1f}, new Shape(2, 2)); jshell> array; ND: (2, 2) cpu() float32 [[1., 4.], [3., 1.], ] jshell> array.sort(0); // sort along the first axis ND: (2, 2) cpu() float32 [[1., 1.], [3., 4.], ]
-
softmax
public NDArray softmax(int axis)
Applies the softmax function along the given axis.- Specified by:
softmaxin interfaceNDArray- Parameters:
axis- the axis along which to apply- Returns:
- the result
NDArray - See Also:
- softmax,
NDArray.softmax(int)
-
logSoftmax
public NDArray logSoftmax(int axis)
Applies the softmax function followed by a logarithm.Mathematically equivalent to calling softmax and then log. This single operator is faster than calling two operators and numerically more stable when computing gradients.
- Specified by:
logSoftmaxin interfaceNDArray- Parameters:
axis- the axis along which to apply- Returns:
- the result
NDArray
-
cumSum
public NDArray cumSum()
Returns the cumulative sum of the elements in the flattenedNDArray.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f, 5f, 6f}, new Shape(2, 3)); jshell> array; ND: (2, 3) cpu() float32 [[1., 2., 3.], [4., 5., 6.], ] jshell> array.cumSum(); // cumSum on flattened array ND: (6) cpu() float32 [ 1., 3., 6., 10., 15., 21.]
-
cumSum
public NDArray cumSum(int axis)
Return the cumulative sum of the elements along a given axis.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f, 5f, 6f}, new Shape(2, 3)); jshell> array; ND: (2, 3) cpu() float32 [[1., 2., 3.], [4., 5., 6.], ] jshell> array.cumSum(0); ND: (2, 3) cpu() float32 [[1., 2., 3.], [5., 7., 9.], ] jshell> array.cumSum(1); ND: (2, 3) cpu() float32 [[ 1., 3., 6.], [ 4., 9., 15.], ]
-
isInfinite
public NDArray isInfinite()
Returns the booleanNDArraywith valuetruewhere thisNDArray's entries are infinite, orfalsewhere they are not infinite.- Specified by:
isInfinitein interfaceNDArray- Returns:
- the boolean
NDArraywith valuetrueif thisNDArray's entries are infinite
-
isNaN
public NDArray isNaN()
Returns the booleanNDArraywith valuetruewhere thisNDArray's entries are NaN, orfalsewhere they are not NaN.Examples
jshell> NDArray array = manager.create(new float[] {Float.POSITIVE_INFINITY, 0, Float.NaN}); jshell> array.isNaN(); ND: (3) cpu() boolean [false, false, true]
-
tile
public NDArray tile(long repeats)
Constructs aNDArrayby repeating thisNDArraythe number of times given repeats.Examples
jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f}); jshell> array.tile(2); ND: (6) cpu() float32 [0., 1., 2., 0., 1., 2.]
-
tile
public NDArray tile(int axis, long repeats)
Constructs aNDArrayby repeating thisNDArraythe number of times given by repeats along given axis.Examples
jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f}); jshell> array.tile(1, 2); ND: (1, 6) cpu() float32 [[0., 1., 2., 0., 1., 2.], ]
-
tile
public NDArray tile(long[] repeats)
Constructs aNDArrayby repeating thisNDArraythe number of times given by repeats.Examples
jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f}); jshell> array.tile(new long[] {2, 2}); ND: (2, 6) cpu() float32 [[0., 1., 2., 0., 1., 2.], [0., 1., 2., 0., 1., 2.], ]
-
tile
public NDArray tile(Shape desiredShape)
Constructs aNDArrayby repeating thisNDArraythe number of times to match the desired shape.If the desired
Shapehas fewer dimensions than thisNDArray, it will tile against the last axis.Examples
jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f}); jshell> array.tile(new Shape(6)); ND: (6) cpu() float32 [0., 1., 2., 0., 1., 2.]
-
repeat
public NDArray repeat(long repeats)
Repeats element of thisNDArraythe number of times given repeats.Examples
jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f}); jshell> array.repeat(2); ND: (6) cpu() float32 [0., 0., 1., 1., 2., 2.]
-
repeat
public NDArray repeat(int axis, long repeats)
Repeats element of thisNDArraythe number of times given repeats along given axis.Examples
jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f, 3f}, new Shape(2, 2)); jshell> array.repeat(1, 2); ND: (6) cpu() float32 [[0., 0., 1., 1.], [2., 2., 3., 3.]]
-
repeat
public NDArray repeat(long[] repeats)
Repeats element of thisNDArraythe number of times given repeats along each axis.Examples
jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f, 3f}, new Shape(2, 2)); jshell> array.repeat(new long[] {2, 2}); ND: (12) cpu() float32 [0., 0., 0., 0., 1., 1., 1., 1., 2., 2., 2., 2.]
-
repeat
public NDArray repeat(Shape desiredShape)
Repeats element of thisNDArrayto match the desired shape.If the desired
Shapehas fewer dimensions that the array, it will repeat against the last axis.Examples
jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f, 3f}, new Shape(2, 2)); jshell> array.repeat(new Shape(4, 4)); ND: (4, 4) cpu() float32 [[0., 0., 1., 1.], [0., 0., 1., 1.], [2., 2., 3., 3.], [2., 2., 3., 3.], ]
-
dot
public NDArray dot(NDArray other)
Dot product of thisNDArrayand the otherNDArray.- If both this
NDArrayand the otherNDArrayare 1-DNDArrays, it is inner product of vectors (without complex conjugation). - If both this
NDArrayand the otherNDArrayare 2-DNDArrays, it is matrix multiplication. - If either this
NDArrayor the otherNDArrayis 0-DNDArray(scalar), it is equivalent to mul. - If this
NDArrayis N-DNDArrayand the otherNDArrayis 1-DNDArray, it is a sum product over the last axis of those. - If this
NDArrayis N-DNDArrayand the otherNDArrayis M-DNDArray(where M>=2), it is a sum product over the last axis of thisNDArrayand the second-to-last axis of the otherNDArray
Examples
jshell> NDArray array1 = manager.create(new float[] {1f, 2f, 3f}); jshell> NDArray array2 = manager.create(new float[] {4f, 5f, 6f}); jshell> array1.dot(array2); // inner product ND: () cpu() float32 32. jshell> array1 = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2)); jshell> array2 = manager.create(new float[] {5f, 6f, 7f, 8f}, new Shape(2, 2)); jshell> array1.dot(array2); // matrix multiplication ND: (2, 2) cpu() float32 [[19., 22.], [43., 50.], ] jshell> array1 = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2)); jshell> array2 = manager.create(5f); jshell> array1.dot(array2); ND: (2, 2) cpu() float32 [[ 5., 10.], [15., 20.], ] jshell> array1 = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2)); jshell> array2 = manager.create(new float[] {1f, 2f}); jshell> array1.dot(array2); ND: (2) cpu() float32 [ 5., 11.] jshell> array1 = manager.create(new float[] {1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f}, new Shape(2, 2, 2)); jshell> array2 = manager.create(new float[] {1f, 2f, 3f ,4f}, new Shape(2, 2)); jshell> array1.dot(array2); ND: (2, 2, 2) cpu() float32 [[[ 7., 10.], [15., 22.], ], [[23., 34.], [31., 46.], ], ] - If both this
-
matMul
public NDArray matMul(NDArray other)
Product matrix of thisNDArrayand the otherNDArray.The behavior depends on the arguments in the following way.
- If both this
NDArrayand the otherNDArrayare 2-DNDArrays, they are multiplied like conventional matrices - If either this
NDArrayor the otherNDArrayis N-DNDArray, N > 2 , it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly. - If this
NDArrayis 1-DNDArray, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed. - If other
NDArrayis 1-DNDArray, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed.
Examples
jshell> NDArray array1 = manager.create(new float[] {1f, 0f, 0f, 1f}, new Shape(2, 2)); jshell> NDArray array2 = manager.create(new float[] {4f, 1f, 2f, 2f}, new Shape(2, 2)); jshell> array1.matMul(array2); // for 2-D arrays, it is the matrix product ND: (2, 2) cpu() float32 [[4., 1.], [2., 2.], ] jshell> array1 = manager.create(new float[] {1f, 0f, 0f, 1f}, new Shape(2, 2)); jshell> array2 = manager.create(new float[] {1f, 2f}); jshell> array1.matMul(array2); ND: (2) cpu() float32 [1., 2.] jshell> array1 = manager.create(new float[] {1f, 0f, 0f, 1f}, new Shape(2, 2)); jshell> array2 = manager.create(new float[] {1f, 2f}); jshell> array1.matMul(array2); ND: (2) cpu() float32 [1., 2.] jshell> array1 = manager.arange(2f * 2f * 4f).reshape(2, 2, 4); jshell> array2 = manager.arange(2f * 2f * 4f).reshape(2, 4, 2); jshell> array1.matMul(array2).get("0, 1, 1"); ND: () cpu() float32 98. - If both this
-
batchMatMul
public NDArray batchMatMul(NDArray other)
Batch product matrix of thisNDArrayand the otherNDArray.- Specified by:
batchMatMulin interfaceNDArray- Parameters:
other- the otherNDArrayto perform matrix product with- Returns:
- the result
NDArray
-
clip
public NDArray clip(java.lang.Number min, java.lang.Number max)
Clips (limit) the values in thisNDArray.Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
Examples
jshell> NDArray array = manager.arange(10f); jshell> array.clip(1, 8); ND: (10) cpu() float32 [1., 1., 2., 3., 4., 5., 6., 7., 8., 8.]
-
flip
public NDArray flip(int... axes)
Returns the reverse order of elements in an array along the given axis.The shape of the array is preserved, but the elements are reordered.
-
transpose
public NDArray transpose()
Returns thisNDArraywith axes transposed.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f ,3f, 4f}, new Shape(2, 2)); jshell> array; ND: (2, 2) cpu() float32 [[1., 2.], [3., 4.], ] jshell> array.transpose(); ND: (2, 2) cpu() float32 [[1., 3.], [2., 4.], ]
-
transpose
public NDArray transpose(int... axes)
Returns thisNDArraywith given axes transposed.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f ,3f, 4f}, new Shape(2, 2)); jshell> array; ND: (2, 2) cpu() float32 [[1., 2.], [3., 4.], ] jshell> array.transpose(1, 0); ND: (2, 2) cpu() float32 [[1., 3.], [2., 4.], ] jshell> array = manager.arange(8f).reshape(2, 2, 2); jshell> array; ND: (2, 2, 2) cpu() float32 [[[0., 1.], [2., 3.], ], [[4., 5.], [6., 7.], ], ] jshell> array.transpose(1, 0, 2); ND: (2, 2, 2) cpu() float32 [[[0., 1.], [4., 5.], ], [[2., 3.], [6., 7.], ], ]
-
broadcast
public NDArray broadcast(Shape shape)
Broadcasts thisNDArrayto be the given shape.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f ,3f, 4f}, new Shape(2, 2)); jshell> array; ND: (2, 2) cpu() float32 [[1., 2.], [3., 4.], ] jshell> array.broadcast(new Shape(2, 2, 2)); ND: (2, 2, 2) cpu() float32 [[[1., 2.], [3., 4.], ], [[1., 2.], [3., 4.], ], ]
-
argMax
public NDArray argMax()
Returns the indices of the maximum values into the flattenedNDArray.Examples
jshell> NDArray array = manager.arange(6f).reshape(2, 3); jshell> array; ND: (2, 3) cpu() float32 [[0., 1., 2.], [3., 4., 5.], ] jshell> array.argMax(); ND: () cpu() int64 5.
-
argMax
public NDArray argMax(int axis)
Returns the indices of the maximum values along given axis.Examples
jshell> NDArray array = manager.arange(6f).reshape(2, 3); jshell> array; ND: (2, 3) cpu() float32 [[0., 1., 2.], [3., 4., 5.], ] jshell> array.argMax(0); ND: (3) cpu() int64 [1, 1, 1] jshell> array.argMax(1); ND: (2) cpu() int64 [2, 2]
-
topK
public NDList topK(int k, int axis, boolean largest, boolean sorted)
Returns (values, indices) of the top k values along given axis.
-
argMin
public NDArray argMin()
Returns the indices of the minimum values into the flattenedNDArray.Examples
jshell> NDArray array = manager.arange(6f).reshape(2, 3); jshell> array; ND: (2, 3) cpu() float32 [[0., 1., 2.], [3., 4., 5.], ] jshell> array.argMin(); ND: () cpu() int64 0.
-
argMin
public NDArray argMin(int axis)
Returns the indices of the minimum values along given axis.Examples
jshell> NDArray array = manager.arange(6f).reshape(2, 3); jshell> array; ND: (2, 3) cpu() float32 [[0., 1., 2.], [3., 4., 5.], ] jshell> array.argMin(0); ND: (3) cpu() int64 [0, 0, 0] jshell> array.argMin(1); ND: (2) cpu() int64 [0, 0]
-
percentile
public NDArray percentile(java.lang.Number percentile)
Returns percentile for thisNDArray.- Specified by:
percentilein interfaceNDArray- Parameters:
percentile- the target percentile in range of 0..100- Returns:
- the result
NDArray
-
percentile
public NDArray percentile(java.lang.Number percentile, int[] axes)
Returns median along given dimension(s).- Specified by:
percentilein interfaceNDArray- Parameters:
percentile- the target percentile in range of 0..100axes- the dimension to calculate percentile for- Returns:
- the result
NDArrayNDArray
-
median
public NDArray median()
Returns median value for thisNDArray.
-
median
public NDArray median(int[] axes)
Returns median value along given axes.
-
toDense
public NDArray toDense()
Returns a dense representation of the sparseNDArray.
-
toSparse
public NDArray toSparse(SparseFormat fmt)
Returns a sparse representation ofNDArray.- Specified by:
toSparsein interfaceNDArray- Parameters:
fmt- theSparseFormatof thisNDArray- Returns:
- the result
NDArray
-
nonzero
public NDArray nonzero()
Returns the indices of elements that are non-zero.Note that the behavior is slightly different from numpy.nonzero. Numpy returns a tuple of NDArray, one for each dimension of NDArray. DJL nonzero returns only one
NDArraywith last dimension containing all dimension of indices.Examples
jshell> NDArray array = manager.create(new float[] {1f, 1f, 1f, 0f, 1f}); jshell> array.nonzero(); ND: (4, 1) cpu() int64 [[ 0], [ 1], [ 2], [ 4], ] jshell> array = manager.create(new float[] {3f, 0f, 0f, 0f, 4f, 0f, 5f, 6f, 0f}).reshape(3, 3); jshell> array; ND: (3, 3) cpu() float32 [[3., 0., 0.], [0., 4., 0.], [5., 6., 0.], ] jshell> array.nonzero(); ND: (4, 2) cpu() int64 [[ 0, 0], [ 1, 1], [ 2, 0], [ 2, 1], ]
-
erfinv
public NDArray erfinv()
Returns element-wise inverse gauss error function of theNDArray.Examples
jshell> NDArray array = manager.create(new float[] {0f, 0.5f, -1f}); jshell> array.erfinv(); ND: (3) cpu() float32 [0., 0.4769, -inf]
-
inverse
public NDArray inverse()
Computes the inverse of squareNDArrayif it exists.
-
norm
public NDArray norm(boolean keepDims)
Returns the norm of thisNDArray.Examples
jshell> NDArray array = manager.create(new float[] {-3f, -4f}); jshell> array.norm(true); ND: () cpu() float32 5. jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2)); jshell> array.norm(true); ND: () cpu() float32 [[5.4772], ]
-
norm
public NDArray norm(int ord, int[] axes, boolean keepDims)
Returns the norm of thisNDArray.Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2)); jshell> array.norm(2, new int[] {0}, true); ND: (1, 2) cpu() float32 [[3.1623, 4.4721], ] jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2)); jshell> array.norm(2, new int[] {0}, false); ND: (2) cpu() float32 [3.1623, 4.4721]- Specified by:
normin interfaceNDArray- Parameters:
ord- Order of the norm.axes- If axes contains an integer, it specifies the axis of x along which to compute the vector norms. If axis contains 2 integers, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed.keepDims- keepDims If this is set to True, the axes which are normed over are left in the result as dimensions with size one. With this option the result will broadcast correctly against the original x.- Returns:
- the norm of this
NDArray
-
oneHot
public NDArray oneHot(int depth, float onValue, float offValue, DataType dataType)
Returns a one-hotNDArray.- The locations represented by indices take value onValue, while all other locations take value offValue.
- If the input
NDArrayis rank N, the output will have rank N+1. The new axis is appended at the end. - If
NDArrayis a scalar the output shape will be a vector of length depth. - If
NDArrayis a vector of length features, the output shape will be features x depth. - If
NDArrayis a matrix with shape [batch, features], the output shape will be batch x features x depth.
Examples
jshell> NDArray array = manager.create(new int[] {1, 0, 2, 0}); jshell> array.oneHot(3, 8f, 1f, array.getDataType()); ND: (4, 3) cpu() int32 [[ 1, 8, 1], [ 8, 1, 1], [ 1, 1, 8], [ 8, 1, 1], ]- Specified by:
oneHotin interfaceNDArray- Parameters:
depth- Depth of the one hot dimension.onValue- The value assigned to the locations represented by indices.offValue- The value assigned to the locations not represented by indices.dataType- dataType of the output.- Returns:
- one-hot encoding of this
NDArray - See Also:
- Classification-problems
-
batchDot
public NDArray batchDot(NDArray other)
Batchwise product of thisNDArrayand the otherNDArray.- batchDot is used to compute dot product of x and y when x and y are data in batch, namely N-D (N greater or equal to 3) arrays in shape of (B0, …, B_i, :, :). For example, given x with shape (B_0, …, B_i, N, M) and y with shape (B_0, …, B_i, M, K), the result array will have shape (B_0, …, B_i, N, K), which is computed by: batch_dot(x,y)[b_0, ..., b_i, :, :] = dot(x[b_0, ..., b_i, :, :], y[b_0, ..., b_i, :, :])
Examples
jshell> NDArray array1 = manager.ones(new Shape(2, 1, 4)); jshell> NDArray array2 = manager.ones(new Shape(2, 4, 6)); jshell> array1.batchDot(array2); ND: (2, 1, 6) cpu() float32 [[[4., 4., 4., 4., 4., 4.], ], [[4., 4., 4., 4., 4., 4.], ], ]
-
complex
public NDArray complex()
Convert a general NDArray to its complex math format.example: [10f, 12f] float32 -> [10+12j] in complex64
-
real
public NDArray real()
Convert a complex NDArray to its real math format. example: [10+12j] in complex64 -> [10f, 12f] float32
-
getNDArrayInternal
public ai.djl.ndarray.internal.NDArrayEx getNDArrayInternal()
Returns an internal representative of NativeNDArray.This method should only be used by Engine provider
- Specified by:
getNDArrayInternalin interfaceNDArray- Returns:
- an internal representative of Native
NDArray
-
isReleased
public boolean isReleased()
Returnstrueif this NDArray has been released.- Specified by:
isReleasedin interfaceNDArray- Returns:
trueif this NDArray has been released
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfaceNDArray- Specified by:
closein interfaceNDResource
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-