Class TensorBase

java.lang.Object
deepnetts.tensor.TensorBase
All Implemented Interfaces:
Tensor, Serializable
Direct Known Subclasses:
Tensor1D, Tensor2D, Tensor3D, Tensor4D

public class TensorBase extends Object implements Tensor, Serializable
The base call for multidimensional array/tensors. It us used as a basic data structure for storing inputs, outputs and internal parameters(weights) of the deep learning model. Provides common arithmetic operations on tensors, and interface to accelerator devices like GPU.
See Also:
  • Field Details

    • vectorizationImpl

      protected static final TensorVectorizationProvider vectorizationImpl
    • shape

      protected Shape shape
    • numDimensions

      protected int numDimensions
    • values

      protected float[] values
      Values stored in this tensor make it final , only input layer and tests sets values
  • Constructor Details

    • TensorBase

      protected TensorBase(Shape shape)
    • TensorBase

      public TensorBase(Shape shape, float... values)
    • TensorBase

      protected TensorBase(TensorBase t)
      Public deep copy / clone constructor.
      Parameters:
      t -
  • Method Details

    • clone

      public Object clone()
      Overrides:
      clone in class Object
    • getValues

      public final float[] getValues()
      Description copied from interface: Tensor
      Returns an array of values/elements stored in this tensor.
      Specified by:
      getValues in interface Tensor
      Returns:
    • setValues

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

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

      public final void copyFrom(TensorBase src)
      Copies values from specified tensor
      Parameters:
      src -
    • numDimensions

      public final int numDimensions()
      Rank corresponds to number of dimensions in tensor.
      Specified by:
      numDimensions in interface Tensor
      Returns:
    • setShape

      protected void setShape(Shape shape)
    • shape

      public Shape shape()
      Description copied from interface: Tensor
      Returns a shape of this tensor.
      Specified by:
      shape in interface Tensor
      Returns:
    • numElements

      public final int numElements()
      Total number of values in tensor.
      Specified by:
      numElements in interface Tensor
      Returns:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toTsr

      public String toTsr()
    • add

      public final TensorBase add(TensorBase t)
      Adds specified tensor t to this tensor.
      Parameters:
      t - tensor to add
    • addInto

      public final TensorBase addInto(TensorBase t, TensorBase result)
    • add

      public final TensorBase add(float val)
    • sub

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

      public final TensorBase sub(TensorBase t, TensorBase result)
    • sub

      public final void sub(float val)
    • subInplace

      public static final void subInplace(TensorBase t1, TensorBase t2)
      Subtracts tensor t2 from t1. The result is stored in 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
    • div

      public final void div(TensorBase t)
      Element-wise divison with specified tensor.
      Parameters:
      t -
    • copy

      public TensorBase copy()
    • apply

      public void apply(Function<Float,Float> f)
      Applies specified function to all elements in tensor in-place.
      Parameters:
      f - function to apply to all elements.
    • apply

      public TensorBase apply(ActivationFunction af)
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

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

      public boolean equals(TensorBase t2, float delta)
    • valuesAsString

      public static String valuesAsString(TensorBase[] tensors)
    • setValuesFromString

      public void setValuesFromString(String values)
      Sets tensor values from CSV string.
      Parameters:
      values - string with comma separated tensor values
    • setValuesFromStringTransposed

      public void setValuesFromStringTransposed(String values)
    • sum

      public float sum()
    • 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
    • multiplyElementWise

      public void multiplyElementWise(TensorBase tensor2)
    • multiply

      public TensorBase multiply(float m)
      Multiplies all the values in tensor with a specified input parameter.
      Parameters:
      m - a multiplier for all values in tensor
    • multiply

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

      public TensorBase sqrt()
    • sqr

      public TensorBase sqr()
    • mean

      public float mean()
    • std

      public float std(float mean)
    • replace

      public void replace(float toReplace, float replaceWith)
    • getOrCreateAccBridge

      public AcceleratorTensorBridge getOrCreateAccBridge()
    • createAcceleratorBridge

      public AcceleratorTensorBridge createAcceleratorBridge()
    • getAcceleratorBridge

      public AcceleratorTensorBridge getAcceleratorBridge()
    • copyToGPU

      public void copyToGPU()
    • copyFromGPU

      public void copyFromGPU()
    • containsNaN

      public boolean containsNaN()