Package deepboof

Class Tensor<T extends Tensor>

All Implemented Interfaces:
ITensor
Direct Known Subclasses:
Tensor_F32, Tensor_F64, Tensor_S32, Tensor_S64, Tensor_U8

public abstract class Tensor<T extends Tensor>
extends BaseTensor
Base class for Tensors. A tensor is an N-dimensional array. Array elements are stored in a 1-D array in row-major ordering. This class and its direct children provide only a light weight wrapper around the array.

Sub-tensors are tensors which are wrapped around an external array which it doesn't own. Most of the time, sub-tensors will have a non-zero startIndex. They can't be reshaped if the change in shape requires the data array to grow.

  • Field Summary

    Fields
    Modifier and Type Field Description
    int startIndex
    The index in the input array that this tensor starts at.
    int[] strides
    Stride for each axis
    boolean subtensor
    If this tensor is wrapped around another array which it doesn't own then it is a sub-tensor.

    Fields inherited from class deepboof.BaseTensor

    shape
  • Constructor Summary

    Constructors
    Constructor Description
    Tensor()  
  • Method Summary

    Modifier and Type Method Description
    void computeStrides()
    Computes how many indexes must be steped over to increment a value in each axis
    T copy()
    Returns a copy of this tensor
    void copyShape​(int[] shape)
    Copies the shape of this tensor into the provided int[]
    abstract T create​(int... shape)
    Creates a tensor of the same type with the specified shape
    int[] createCoor()
    Creates a new coordinate for this tensor.
    T createLike()
    Creates a tensor of the same type and same shape as this one
    abstract Object getData()
    Returns internal array used to store tensor data.
    abstract double getDouble​(int... coordinate)
    Accessor function which allows any tensor's element to be read as a double.
    int idx​(int axis0)
    Specialized version of idx(int...) for 1-D tensors.
    int idx​(int... coordinate)
    Returns the index of the coordinate.
    int idx​(int axis1, int axis0)
    Specialized version of idx(int...) for 2-D tensors.
    int idx​(int axis2, int axis1, int axis0)
    Specialized version of idx(int...) for 3-D tensors.
    int idx​(int axis3, int axis2, int axis1, int axis0)
    Specialized version of idx(int...) for 4-D tensors.
    int idx​(int axis4, int axis3, int axis2, int axis1, int axis0)
    Specialized version of idx(int...) for 5-D tensors.
    int[] indexToCoor​(int index, int[] storage)
    Converts an array index into a coordinate
    protected abstract void innerArrayGrow​(int N)
    Re-declare inner array so that it is at least of length N
    protected abstract int innerArrayLength()
    Length of inner array as returned by "data.length"
    boolean isSub()
    Returns true if it is a sub-tensor.
    int length()
    Length of used elements in Tensor's data array.
    int length​(int dimension)
    Returns the length of a dimension/axis.
    void reshape()
    Reshape for when the inner shape variable has already been adjusted.
    void reshape​(int length0)
    Reshape for 1-D tensors.
    void reshape​(int... shape)
    Reshape for an arbitrary number of dimensions
    void reshape​(int length1, int length0)
    Reshape for 2-D tensors.
    void reshape​(int length2, int length1, int length0)
    Reshape for 3-D tensors.
    void reshape​(int length3, int length2, int length1, int length0)
    Reshape for 4-D tensors.
    void reshape​(int length4, int length3, int length2, int length1, int length0)
    Reshape for 5-D tensors.
    abstract void setData​(Object data)
    Used to change the internal array in an abstract way
    void setTo​(T original)
    Turns 'this' tensor into a copy of the provided tensor.
    int stride​(int index)
    Returns the stride at the specified dimension.
    T subtensor​(int startIndex, int[] shape)
    Creates a subtensor from this tensor.
    abstract void zero()
    Sets all elements in the tensor to the value of zero

    Methods inherited from class deepboof.BaseTensor

    getDimension, getShape, isShape

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface deepboof.ITensor

    getDataType
  • Field Details

    • startIndex

      public int startIndex
      The index in the input array that this tensor starts at. This allows for a tensor to be inside a much larger array.
    • subtensor

      public boolean subtensor
      If this tensor is wrapped around another array which it doesn't own then it is a sub-tensor. Most of the time, sub-tensors will have a non-zero startIndex. They can't be resized.
    • strides

      public int[] strides
      Stride for each axis
  • Constructor Details

    • Tensor

      public Tensor()
  • Method Details

    • getDouble

      public abstract double getDouble​(int... coordinate)
      Accessor function which allows any tensor's element to be read as a double.
      Parameters:
      coordinate - Coordinate of the element which is to be read
      Returns:
      Tensor elements's value as a double
    • getData

      public abstract Object getData()
      Returns internal array used to store tensor data. Data is stored in a row-major order in a single array.
      Returns:
      tensor data.
    • setData

      public abstract void setData​(Object data)
      Used to change the internal array in an abstract way
    • reshape

      public void reshape​(int... shape)
      Reshape for an arbitrary number of dimensions
      Parameters:
      shape - New shape. Length must be the same as the number of dimensions. Array reference not saved internally. Highest to lowest dimension
    • reshape

      public void reshape​(int length0)
      Reshape for 1-D tensors. Convenience function, but can be used to avoid declaring a new array
      Parameters:
      length0 - Length of axis-0
    • reshape

      public void reshape​(int length1, int length0)
      Reshape for 2-D tensors. Convenience function, but can be used to avoid declaring a new array
      Parameters:
      length1 - Length of axis-1
      length0 - Length of axis-0
    • reshape

      public void reshape​(int length2, int length1, int length0)
      Reshape for 3-D tensors. Convenience function, but can be used to avoid declaring a new array
      Parameters:
      length2 - Length of axis-2
      length1 - Length of axis-1
      length0 - Length of axis-0
    • reshape

      public void reshape​(int length3, int length2, int length1, int length0)
      Reshape for 4-D tensors. Convenience function, but can be used to avoid declaring a new array
      Parameters:
      length3 - Length of axis-3
      length2 - Length of axis-2
      length1 - Length of axis-1
      length0 - Length of axis-0
    • reshape

      public void reshape​(int length4, int length3, int length2, int length1, int length0)
      Reshape for 5-D tensors. Convenience function, but can be used to avoid declaring a new array
      Parameters:
      length4 - Length of axis-4
      length3 - Length of axis-3
      length2 - Length of axis-2
      length1 - Length of axis-1
      length0 - Length of axis-0
    • reshape

      public void reshape()
      Reshape for when the inner shape variable has already been adjusted. Useful for when calls to new are being minimized.
    • innerArrayGrow

      protected abstract void innerArrayGrow​(int N)
      Re-declare inner array so that it is at least of length N
      Parameters:
      N - Desired minimum length of inner array
    • innerArrayLength

      protected abstract int innerArrayLength()
      Length of inner array as returned by "data.length"
      Returns:
      Length of inner array
    • idx

      public int idx​(int... coordinate)
      Returns the index of the coordinate. Data array is encoded in a row-major format
      Parameters:
      coordinate - Coordinate from highest to lowest axis number/dimension
      Returns:
      index of the index in internal data array
    • idx

      public int idx​(int axis0)
      Specialized version of idx(int...) for 1-D tensors.
      Parameters:
      axis0 - axis-0 of coordinate
      Returns:
      index in internal data array
    • idx

      public int idx​(int axis1, int axis0)
      Specialized version of idx(int...) for 2-D tensors.
      Parameters:
      axis1 - axis-1 of coordinate
      axis0 - axis-0 of coordinate
      Returns:
      index in internal data array
    • idx

      public int idx​(int axis2, int axis1, int axis0)
      Specialized version of idx(int...) for 3-D tensors.
      Parameters:
      axis2 - axis-2 of coordinate
      axis1 - axis-1 of coordinate
      axis0 - axis-0 of coordinate
      Returns:
      index in internal data array
    • idx

      public int idx​(int axis3, int axis2, int axis1, int axis0)
      Specialized version of idx(int...) for 4-D tensors.
      Parameters:
      axis3 - axis-3 of coordinate
      axis2 - axis-2 of coordinate
      axis1 - axis-1 of coordinate
      axis0 - axis-0 of coordinate
      Returns:
      index in internal data array
    • idx

      public int idx​(int axis4, int axis3, int axis2, int axis1, int axis0)
      Specialized version of idx(int...) for 5-D tensors.
      Parameters:
      axis4 - axis-4 of coordinate
      axis3 - axis-3 of coordinate
      axis2 - axis-2 of coordinate
      axis1 - axis-1 of coordinate
      axis0 - axis-0 of coordinate
      Returns:
      index in internal data array
    • computeStrides

      public void computeStrides()
      Computes how many indexes must be steped over to increment a value in each axis
    • stride

      public int stride​(int index)
      Returns the stride at the specified dimension. If negative it will start counting from the tail
    • copyShape

      public void copyShape​(int[] shape)
      Copies the shape of this tensor into the provided int[]
      Parameters:
      shape - Where the shape is to be written to. Must be the correct size
    • length

      public int length​(int dimension)
      Returns the length of a dimension/axis. If a negative number is passed in it will return the distance relative to the end. E.g. -1 = length-1, -2 = length-2
      Specified by:
      length in interface ITensor
      Overrides:
      length in class BaseTensor
      Parameters:
      dimension - The dimension/axis
      Returns:
      length
    • length

      public int length()
      Length of used elements in Tensor's data array. Note that the actual data array can be larger
      Returns:
      length of used region in data array
    • create

      public abstract T create​(int... shape)
      Creates a tensor of the same type with the specified shape
      Parameters:
      shape - Shape of the new tensor
      Returns:
      New tensor with the specified shape
    • createLike

      public T createLike()
      Creates a tensor of the same type and same shape as this one
      Returns:
      New tensor
    • createCoor

      public int[] createCoor()
      Creates a new coordinate for this tensor.
      Specified by:
      createCoor in interface ITensor
      Overrides:
      createCoor in class BaseTensor
      Returns:
      coordinate/int array of appropriate length
    • indexToCoor

      public int[] indexToCoor​(int index, int[] storage)
      Converts an array index into a coordinate
      Parameters:
      index - internal array index offset from startIndex
      storage - (Optional) storage for coordinate. If null a new instance is created
      Returns:
      coordinate
    • setTo

      public void setTo​(T original)
      Turns 'this' tensor into a copy of the provided tensor.
      Parameters:
      original - Original tensor that's to be copied into this one. Not modified.
    • zero

      public abstract void zero()
      Sets all elements in the tensor to the value of zero
    • copy

      public T copy()
      Returns a copy of this tensor
    • isSub

      public boolean isSub()
      Returns true if it is a sub-tensor.
      Returns:
      true if sub-tensor or false if not
    • subtensor

      public T subtensor​(int startIndex, int[] shape)
      Creates a subtensor from this tensor.
      Parameters:
      startIndex - The start index in this tensor's internal data array
      shape - Shape of the output tensor
      Returns:
      The new sub-tensor.