Class Transforms


  • public class Transforms
    extends Object
    • Method Detail

      • cosineSim

        public static double cosineSim​(@NonNull
                                       @NonNull INDArray d1,
                                       @NonNull
                                       @NonNull INDArray d2)
        Cosine similarity
        Parameters:
        d1 - the first vector
        d2 - the second vector
        Returns:
        the cosine similarities between the 2 arrays
      • cosineDistance

        public static double cosineDistance​(@NonNull
                                            @NonNull INDArray d1,
                                            @NonNull
                                            @NonNull INDArray d2)
      • hammingDistance

        public static double hammingDistance​(@NonNull
                                             @NonNull INDArray d1,
                                             @NonNull
                                             @NonNull INDArray d2)
      • jaccardDistance

        public static double jaccardDistance​(@NonNull
                                             @NonNull INDArray d1,
                                             @NonNull
                                             @NonNull INDArray d2)
      • allCosineSimilarities

        public static INDArray allCosineSimilarities​(@NonNull
                                                     @NonNull INDArray d1,
                                                     @NonNull
                                                     @NonNull INDArray d2,
                                                     int... dimensions)
      • allCosineDistances

        public static INDArray allCosineDistances​(@NonNull
                                                  @NonNull INDArray d1,
                                                  @NonNull
                                                  @NonNull INDArray d2,
                                                  int... dimensions)
      • allEuclideanDistances

        public static INDArray allEuclideanDistances​(@NonNull
                                                     @NonNull INDArray d1,
                                                     @NonNull
                                                     @NonNull INDArray d2,
                                                     int... dimensions)
      • allManhattanDistances

        public static INDArray allManhattanDistances​(@NonNull
                                                     @NonNull INDArray d1,
                                                     @NonNull
                                                     @NonNull INDArray d2,
                                                     int... dimensions)
      • dot

        public static INDArray dot​(INDArray x,
                                   INDArray y)
        Dot product, new INDArray instance will be returned.
        Note that the Nd4J design is different from Numpy. Numpy dot on 2d arrays is matrix multiplication. Nd4J is full array dot product reduction.
        Parameters:
        x - the first vector
        y - the second vector
        Returns:
        the dot product between the 2 arrays
      • manhattanDistance

        public static double manhattanDistance​(@NonNull
                                               @NonNull INDArray d1,
                                               @NonNull
                                               @NonNull INDArray d2)
        Parameters:
        d1 -
        d2 -
        Returns:
      • atan2

        public static INDArray atan2​(@NonNull
                                     @NonNull INDArray x,
                                     @NonNull
                                     @NonNull INDArray y)
        Atan2 operation, new INDArray instance will be returned Note the order of x and y parameters is opposite to that of Math.atan2(double, double)
        Parameters:
        x - the abscissa coordinate
        y - the ordinate coordinate
        Returns:
        the theta from point (r, theta) when converting (x,y) from to cartesian to polar coordinates
      • euclideanDistance

        public static double euclideanDistance​(@NonNull
                                               @NonNull INDArray d1,
                                               @NonNull
                                               @NonNull INDArray d2)
        Parameters:
        d1 -
        d2 -
        Returns:
      • normalizeZeroMeanAndUnitVariance

        public static INDArray normalizeZeroMeanAndUnitVariance​(INDArray toNormalize)
        Normalize data to zero mean and unit variance substract by the mean and divide by the standard deviation
        Parameters:
        toNormalize - the ndarray to normalize
        Returns:
        the normalized ndarray
      • unitVec

        public static INDArray unitVec​(INDArray toScale)
        Scale by 1 / norm2 of the matrix
        Parameters:
        toScale - the ndarray to scale
        Returns:
        the scaled ndarray
      • neg

        public static INDArray neg​(INDArray ndArray)
        Returns the negative of an ndarray
        Parameters:
        ndArray - the ndarray to take the negative of
        Returns:
        the negative of the ndarray
      • floor

        public static INDArray floor​(INDArray ndArray)
        Binary matrix of whether the number at a given index is greater than
        Parameters:
        ndArray -
        Returns:
      • ceiling

        public static INDArray ceiling​(INDArray ndArray)
        Binary matrix of whether the number at a given index is greater than
        Parameters:
        ndArray -
        Returns:
      • ceiling

        public static INDArray ceiling​(INDArray ndArray,
                                       boolean copyOnOps)
        Ceiling function
        Parameters:
        ndArray -
        copyOnOps -
        Returns:
      • sign

        public static INDArray sign​(INDArray toSign)
        Signum function of this ndarray
        Parameters:
        toSign -
        Returns:
      • stabilize

        public static INDArray stabilize​(INDArray ndArray,
                                         double k)
        Parameters:
        ndArray -
        k -
        Returns:
      • sin

        public static INDArray sin​(INDArray in)
        Sin function
        Parameters:
        in -
        Returns:
      • sin

        public static INDArray sin​(INDArray in,
                                   boolean copy)
        Sin function
        Parameters:
        in -
        copy -
        Returns:
      • atanh

        public static INDArray atanh​(INDArray in)
        Sin function
        Parameters:
        in -
        Returns:
      • atanh

        public static INDArray atanh​(INDArray in,
                                     boolean copy)
        Sin function
        Parameters:
        in -
        copy -
        Returns:
      • sinh

        public static INDArray sinh​(INDArray in)
        Sinh function
        Parameters:
        in -
        Returns:
      • sinh

        public static INDArray sinh​(INDArray in,
                                    boolean copy)
        Sinh function
        Parameters:
        in -
        copy -
        Returns:
      • cosh

        public static INDArray cosh​(INDArray in,
                                    boolean copy)
        Parameters:
        in -
        copy -
        Returns:
      • cos

        public static INDArray cos​(INDArray in,
                                   boolean copy)
        Parameters:
        in -
        copy -
        Returns:
      • leakyRelu

        public static INDArray leakyRelu​(INDArray arr,
                                         double cutoff)
      • leakyRelu

        public static INDArray leakyRelu​(INDArray in,
                                         double cutoff,
                                         boolean copy)
      • leakyReluDerivative

        public static INDArray leakyReluDerivative​(INDArray arr,
                                                   double cutoff)
      • leakyReluDerivative

        public static INDArray leakyReluDerivative​(INDArray in,
                                                   double cutoff,
                                                   boolean copy)
      • softsignDerivative

        public static INDArray softsignDerivative​(INDArray arr)
      • softsignDerivative

        public static INDArray softsignDerivative​(INDArray in,
                                                  boolean copy)
      • softmax

        public static INDArray softmax​(INDArray in,
                                       boolean copy)
        Parameters:
        in -
        copy -
        Returns:
      • timesOneMinus

        public static INDArray timesOneMinus​(INDArray in,
                                             boolean copy)
        out = in * (1-in)
        Parameters:
        in - Input array
        copy - If true: copy. False: apply in-place
        Returns:
      • abs

        public static INDArray abs​(INDArray ndArray)
        Abs function
        Parameters:
        ndArray -
        Returns:
      • exp

        public static INDArray exp​(INDArray ndArray)
        Run the exp operation
        Parameters:
        ndArray -
        Returns:
      • hardTanh

        public static INDArray hardTanh​(INDArray ndArray,
                                        boolean dup)
        Hard tanh
        Parameters:
        ndArray - the input
        dup - whether to duplicate the ndarray and return it as the result
        Returns:
        the output
      • hardSigmoid

        public static INDArray hardSigmoid​(INDArray arr,
                                           boolean dup)
      • hardTanhDerivative

        public static INDArray hardTanhDerivative​(INDArray ndArray)
      • hardTanhDerivative

        public static INDArray hardTanhDerivative​(INDArray ndArray,
                                                  boolean dup)
        Hard tanh
        Parameters:
        ndArray - the input
        dup - whether to duplicate the ndarray and return it as the result
        Returns:
        the output
      • identity

        public static INDArray identity​(INDArray ndArray)
        Parameters:
        ndArray -
        Returns:
      • pow

        public static INDArray pow​(INDArray ndArray,
                                   Number power)
        Pow function
        Parameters:
        ndArray - the ndarray to raise hte power of
        power - the power to raise by
        Returns:
        the ndarray raised to this power
      • pow

        public static INDArray pow​(INDArray ndArray,
                                   INDArray power)
        Element-wise power function - x^y, performed element-wise. Not performed in-place: the input arrays are not modified.
        Parameters:
        ndArray - the ndarray to raise to the power of
        power - the power to raise by
        Returns:
        a copy of the ndarray raised to the specified power (element-wise)
      • pow

        public static INDArray pow​(INDArray ndArray,
                                   INDArray power,
                                   boolean dup)
        Element-wise power function - x^y, performed element-wise
        Parameters:
        ndArray - the ndarray to raise to the power of
        power - the power to raise by
        dup - if true:
        Returns:
        the ndarray raised to this power
      • round

        public static INDArray round​(INDArray ndArray)
        Rounding function
        Parameters:
        ndArray -
        Returns:
      • sigmoid

        public static INDArray sigmoid​(INDArray ndArray)
        Sigmoid function
        Parameters:
        ndArray -
        Returns:
      • sigmoid

        public static INDArray sigmoid​(INDArray ndArray,
                                       boolean dup)
        Sigmoid function
        Parameters:
        ndArray -
        dup -
        Returns:
      • sigmoidDerivative

        public static INDArray sigmoidDerivative​(INDArray ndArray)
        Sigmoid function
        Parameters:
        ndArray -
        Returns:
      • sigmoidDerivative

        public static INDArray sigmoidDerivative​(INDArray ndArray,
                                                 boolean dup)
        Sigmoid function
        Parameters:
        ndArray -
        dup -
        Returns:
      • sqrt

        public static INDArray sqrt​(INDArray ndArray)
        Sqrt function
        Parameters:
        ndArray -
        Returns:
      • tan

        public static INDArray tan​(INDArray ndArray)
        Element-wise tan function. Copies the array
        Parameters:
        ndArray - Input array
      • tan

        public static INDArray tan​(INDArray ndArray,
                                   boolean dup)
        Element-wise tan function. Copies the array
        Parameters:
        ndArray - Input array
      • tanh

        public static INDArray tanh​(INDArray ndArray)
        Tanh function
        Parameters:
        ndArray -
        Returns:
      • log

        public static INDArray log​(INDArray ndArray,
                                   double base)
        Log on arbitrary base
        Parameters:
        ndArray -
        base -
        Returns:
      • log

        public static INDArray log​(INDArray ndArray,
                                   double base,
                                   boolean duplicate)
        Log on arbitrary base
        Parameters:
        ndArray -
        base -
        Returns:
      • greaterThanOrEqual

        public static INDArray greaterThanOrEqual​(INDArray first,
                                                  INDArray ndArray)
        1 if greater than or equal to 0 otherwise (at each element)
        Parameters:
        first -
        ndArray -
        Returns:
      • lessThanOrEqual

        public static INDArray lessThanOrEqual​(INDArray first,
                                               INDArray ndArray)
        1 if less than or equal to 0 otherwise (at each element)
        Parameters:
        first -
        ndArray -
        Returns:
      • lessThanOrEqual

        public static INDArray lessThanOrEqual​(INDArray first,
                                               INDArray ndArray,
                                               boolean dup)
        Eps function
        Parameters:
        ndArray -
        Returns:
      • greaterThanOrEqual

        public static INDArray greaterThanOrEqual​(INDArray first,
                                                  INDArray ndArray,
                                                  boolean dup)
        Eps function
        Parameters:
        ndArray -
        Returns:
      • floor

        public static INDArray floor​(INDArray ndArray,
                                     boolean dup)
        Floor function
        Parameters:
        ndArray -
        Returns:
      • sign

        public static INDArray sign​(INDArray toSign,
                                    boolean dup)
        Signum function of this ndarray
        Parameters:
        toSign -
        Returns:
      • max

        public static INDArray max​(INDArray ndArray,
                                   double k,
                                   boolean dup)
        Maximum function with a scalar
        Parameters:
        ndArray - tbe ndarray
        k -
        dup -
        Returns:
      • max

        public static INDArray max​(INDArray ndArray,
                                   double k)
        Maximum function with a scalar
        Parameters:
        ndArray - tbe ndarray
        k -
        Returns:
      • max

        public static INDArray max​(INDArray first,
                                   INDArray second,
                                   boolean dup)
        Element wise maximum function between 2 INDArrays
        Parameters:
        first -
        second -
        dup -
        Returns:
      • max

        public static INDArray max​(INDArray first,
                                   INDArray second)
        Element wise maximum function between 2 INDArrays
        Parameters:
        first -
        second -
        Returns:
      • min

        public static INDArray min​(INDArray ndArray,
                                   double k,
                                   boolean dup)
        Minimum function with a scalar
        Parameters:
        ndArray - tbe ndarray
        k -
        dup -
        Returns:
      • min

        public static INDArray min​(INDArray ndArray,
                                   double k)
        Maximum function with a scalar
        Parameters:
        ndArray - tbe ndarray
        k -
        Returns:
      • min

        public static INDArray min​(INDArray first,
                                   INDArray second,
                                   boolean dup)
        Element wise minimum function between 2 INDArrays
        Parameters:
        first -
        second -
        dup -
        Returns:
      • min

        public static INDArray min​(INDArray first,
                                   INDArray second)
        Element wise minimum function between 2 INDArrays
        Parameters:
        first -
        second -
        Returns:
      • stabilize

        public static INDArray stabilize​(INDArray ndArray,
                                         double k,
                                         boolean dup)
        Stabilize to be within a range of k
        Parameters:
        ndArray - tbe ndarray
        k -
        dup -
        Returns:
      • abs

        public static INDArray abs​(INDArray ndArray,
                                   boolean dup)
        Abs function
        Parameters:
        ndArray -
        dup -
        Returns:
      • exp

        public static INDArray exp​(INDArray ndArray,
                                   boolean dup)
        Exp function
        Parameters:
        ndArray -
        dup -
        Returns:
      • expm1

        public static INDArray expm1​(INDArray ndArray,
                                     boolean dup)
        Elementwise exponential - 1 function
        Parameters:
        ndArray -
        dup -
        Returns:
      • identity

        public static INDArray identity​(INDArray ndArray,
                                        boolean dup)
        Identity function
        Parameters:
        ndArray -
        dup -
        Returns:
      • pow

        public static INDArray pow​(INDArray ndArray,
                                   Number power,
                                   boolean dup)
        Pow function
        Parameters:
        ndArray -
        power -
        dup -
        Returns:
      • round

        public static INDArray round​(INDArray ndArray,
                                     boolean dup)
        Rounding function
        Parameters:
        ndArray - the ndarray
        dup -
        Returns:
      • sqrt

        public static INDArray sqrt​(INDArray ndArray,
                                    boolean dup)
        Sqrt function
        Parameters:
        ndArray -
        dup -
        Returns:
      • tanh

        public static INDArray tanh​(INDArray ndArray,
                                    boolean dup)
        Tanh function
        Parameters:
        ndArray -
        dup -
        Returns:
      • log

        public static INDArray log​(INDArray ndArray,
                                   boolean dup)
        Log function
        Parameters:
        ndArray -
        dup -
        Returns:
      • log1p

        public static INDArray log1p​(INDArray ndArray,
                                     boolean dup)
        Log of x + 1 function
        Parameters:
        ndArray -
        dup -
        Returns:
      • neg

        public static INDArray neg​(INDArray ndArray,
                                   boolean dup)
        Negative
        Parameters:
        ndArray -
        dup -
        Returns:
      • mpow

        public static INDArray mpow​(INDArray in,
                                    int n,
                                    boolean dup)
        Raises a square matrix to a power n, which can be positive, negative, or zero. The behavior is similar to the numpy matrix_power() function. The algorithm uses repeated squarings to minimize the number of mmul() operations needed

        If n is zero, the identity matrix is returned.

        If n is negative, the matrix is inverted and raised to the abs(n) power.

        Parameters:
        in - A square matrix to raise to an integer power, which will be changed if dup is false.
        n - The integer power to raise the matrix to.
        dup - If dup is true, the original input is unchanged.
        Returns:
        The result of raising in to the nth power.
      • broadcastResultShape

        protected static long[] broadcastResultShape​(INDArray first,
                                                     INDArray second)