Class Tensor

  • All Implemented Interfaces:
    java.io.Serializable

    public class Tensor
    extends java.lang.Object
    implements java.io.Serializable
    This class represents multidimensional array/matrix (can be 1D, 2D, 3D or 4D). https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html
    Author:
    Zoran Sevarac
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      Tensor​(float... values)
      Creates a single row tensor with specified values.
      Tensor​(float[][] vals)
      Creates a 2D tensor / matrix with specified values.
      Tensor​(float[][][] vals)
      Creates a 3D tensor from specified 3D array
      Tensor​(float[][][][] vals)  
      Tensor​(int cols)
      Creates an empty single row tensor with specified number of columns.
      Tensor​(int cols, float val)  
      Tensor​(int rows, int cols)
      Creates a tensor with specified number of rows and columns.
      Tensor​(int rows, int cols, float[] values)  
      Tensor​(int rows, int cols, int depth)
      Creates a 3D tensor with specified number of rows, cols and depth.
      Tensor​(int rows, int cols, int depth, float[] values)  
      Tensor​(int rows, int cols, int depth, int fourthDim)  
      Tensor​(int rows, int cols, int depth, int fourthDim, float[] values)  
      Tensor​(Shape shape, float... values)  
      Tensor​(Tensor t)
      Public deep copy / clone constructor.
    • Method Summary

      Modifier and Type Method Description
      void add​(float value, int idx)  
      void add​(float value, int row, int col)
      Adds specified value to matrix value at position x, y
      void add​(float value, int row, int col, int z)  
      void add​(float value, int row, int col, int z, int fourth)  
      void add​(Tensor t)
      Adds specified tensor t to this tensor.
      void apply​(java.util.function.Function<java.lang.Float,​java.lang.Float> f)  
      Tensor clone()  
      Tensor copy()  
      void copyFrom​(float[] src)  
      static Tensor create​(int rows, int cols, float[] values)
      Factory method for creating tensor instance,
      static Tensor create​(int rows, int cols, int depth, float[] values)  
      static Tensor create​(int rows, int cols, int depth, int fourthDim, float[] values)  
      void div​(float value)
      Divide all values in this tensor with specified value.
      void div​(float[] divisors)  
      void div​(Tensor t)  
      boolean equals​(Tensor t2, float delta)  
      boolean equals​(java.lang.Object obj)  
      void fill​(float value)
      Fills the entire tensor with specified value.
      static void fill​(float[] array, float val)  
      float get​(int idx)
      Gets value at specified index position.
      float get​(int row, int col)
      Returns matrix value at row, col
      float get​(int row, int col, int z)
      Returns value at specified index [row, col, z].
      float get​(int row, int col, int z, int fourth)  
      int getCols()  
      int getDepth()  
      int getFourthDim()  
      int getRank()
      Rank corresponds to number of dimensions in tensor.
      int getRows()  
      float[] getValues()  
      float getWithStride​(int[] idxs)  
      int hashCode()  
      float mean()  
      void multiply​(float m)  
      void multiply​(float mul, int idx)  
      void multiplyElementWise​(Tensor tensor2)  
      void randomize()
      Randomize all values in tensor
      void replace​(float toReplace, float replaceWith)  
      float set​(float val, int idx)
      Sets value at specified index position.
      void set​(float val, int row, int col)
      Set value at specified tensor's [row, col] position.
      void set​(float val, int row, int col, int z)  
      void set​(float val, int row, int col, int z, int fourth)  
      void setValues​(float... values)  
      void setValuesFromString​(java.lang.String values)
      Sets tensor values from csv string.
      int size()
      Total number of values in tensor.
      void sqrt()  
      float std​(float mean)  
      void sub​(float val)  
      void sub​(float val, int row, int col)  
      void sub​(float val, int row, int col, int z)  
      void sub​(float val, int row, int col, int z, int fourth)  
      void sub​(Tensor t)
      Subtracts specified tensor t from this tensor.
      static void sub​(Tensor t1, Tensor t2)
      Subtracts tensor t2 from t1.
      float sumAbs()
      Returns sum of abs values of this tensor - L1 norm
      float sumSqr()
      Returns sum of squared values of this tensor - L2 norm
      java.lang.String toString()  
      static java.lang.String valuesAsString​(Tensor[] tensors)  
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Tensor

        public Tensor​(float... values)
        Creates a single row tensor with specified values.
        Parameters:
        values - values of column tensor
      • Tensor

        public Tensor​(Shape shape,
                      float... values)
      • Tensor

        public Tensor​(float[][] vals)
        Creates a 2D tensor / matrix with specified values.
        Parameters:
        vals -
      • Tensor

        public Tensor​(float[][][] vals)
        Creates a 3D tensor from specified 3D array
        Parameters:
        vals - 2D array of tensor values
      • Tensor

        public Tensor​(float[][][][] vals)
      • Tensor

        public Tensor​(int cols)
        Creates an empty single row tensor with specified number of columns.
        Parameters:
        cols - number of columns
      • Tensor

        public Tensor​(int cols,
                      float val)
      • Tensor

        public Tensor​(int rows,
                      int cols)
        Creates a tensor with specified number of rows and columns.
        Parameters:
        rows - number of rows
        cols - number of columns
      • Tensor

        public Tensor​(int rows,
                      int cols,
                      float[] values)
      • Tensor

        public Tensor​(int rows,
                      int cols,
                      int depth)
        Creates a 3D tensor with specified number of rows, cols and depth.
        Parameters:
        rows - number of rows
        cols - number of columns
        depth - tensor depth
      • Tensor

        public Tensor​(int rows,
                      int cols,
                      int depth,
                      int fourthDim)
      • Tensor

        public Tensor​(int rows,
                      int cols,
                      int depth,
                      int fourthDim,
                      float[] values)
      • Tensor

        public Tensor​(int rows,
                      int cols,
                      int depth,
                      float[] values)
      • Tensor

        public Tensor​(Tensor t)
        Public deep copy / clone constructor.
        Parameters:
        t -
    • Method Detail

      • get

        public final float get​(int idx)
        Gets value at specified index position.
        Parameters:
        idx -
        Returns:
      • set

        public final float set​(float val,
                               int idx)
        Sets value at specified index position.
        Parameters:
        idx -
        val -
        Returns:
      • get

        public final float get​(int row,
                               int col)
        Returns matrix value at row, col
        Parameters:
        col -
        row -
        Returns:
        value at [row, col]
      • set

        public final void set​(float val,
                              int row,
                              int col)
        Set value at specified tensor's [row, col] position.
        Parameters:
        row - tensor's row index
        col - tensor's col index
        val - value to set
      • get

        public final float get​(int row,
                               int col,
                               int z)
        Returns value at specified index [row, col, z].
        Parameters:
        col -
        row -
        z -
        Returns:
      • set

        public final void set​(float val,
                              int row,
                              int col,
                              int z)
      • get

        public final float get​(int row,
                               int col,
                               int z,
                               int fourth)
      • set

        public final void set​(float val,
                              int row,
                              int col,
                              int z,
                              int fourth)
      • getWithStride

        public final float getWithStride​(int[] idxs)
      • getValues

        public final float[] getValues()
      • setValues

        public final void setValues​(float... values)
      • copyFrom

        public final void copyFrom​(float[] src)
      • getCols

        public final int getCols()
      • getRows

        public final int getRows()
      • getDepth

        public final int getDepth()
      • getRank

        public final int getRank()
        Rank corresponds to number of dimensions in tensor.
        Returns:
      • size

        public final int size()
        Total number of values in tensor.
        Returns:
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • add

        public final void add​(float value,
                              int idx)
      • add

        public final void add​(float value,
                              int row,
                              int col)
        Adds specified value to matrix value at position x, y
        Parameters:
        col -
        row -
        value -
      • add

        public final void add​(float value,
                              int row,
                              int col,
                              int z)
      • add

        public final void add​(float value,
                              int row,
                              int col,
                              int z,
                              int fourth)
      • add

        public final void add​(Tensor t)
        Adds specified tensor t to this tensor.
        Parameters:
        t - tensor to add
      • sub

        public final void sub​(float val,
                              int row,
                              int col)
      • sub

        public final void sub​(float val,
                              int row,
                              int col,
                              int z)
      • sub

        public final void sub​(float val,
                              int row,
                              int col,
                              int z,
                              int fourth)
      • sub

        public final void sub​(Tensor t)
        Subtracts specified tensor t from this tensor.
        Parameters:
        t - tensor to subtract
      • sub

        public final void sub​(float val)
      • sub

        public static final void sub​(Tensor t1,
                                     Tensor t2)
        Subtracts tensor t2 from t1. The result is t1.
        Parameters:
        t1 -
        t2 -
      • div

        public final void div​(float value)
        Divide all values in this tensor with specified value.
        Parameters:
        value -
      • div

        public final void div​(float[] divisors)
      • fill

        public final void fill​(float value)
        Fills the entire tensor with specified value.
        Parameters:
        value - value used to fill tensor
      • fill

        public static final void fill​(float[] array,
                                      float val)
      • apply

        public void apply​(java.util.function.Function<java.lang.Float,​java.lang.Float> f)
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(Tensor t2,
                              float delta)
      • setValuesFromString

        public void setValuesFromString​(java.lang.String values)
        Sets tensor values from csv string.
        Parameters:
        values - csv string with values
      • create

        public static Tensor create​(int rows,
                                    int cols,
                                    float[] values)
        Factory method for creating tensor instance,
        Parameters:
        rows -
        cols -
        values -
        Returns:
      • create

        public static Tensor create​(int rows,
                                    int cols,
                                    int depth,
                                    float[] values)
      • create

        public static Tensor create​(int rows,
                                    int cols,
                                    int depth,
                                    int fourthDim,
                                    float[] values)
      • sumAbs

        public float sumAbs()
        Returns sum of abs values of this tensor - L1 norm
        Returns:
        L1 norm
      • sumSqr

        public float sumSqr()
        Returns sum of squared values of this tensor - L2 norm
        Returns:
        L2 norm
      • randomize

        public void randomize()
        Randomize all values in tensor
      • multiply

        public void multiply​(float m)
      • multiply

        public void multiply​(float mul,
                             int idx)
      • sqrt

        public void sqrt()
      • mean

        public float mean()
      • std

        public float std​(float mean)
      • replace

        public void replace​(float toReplace,
                            float replaceWith)