Class Tensor

java.lang.Object
io.github.kirstenali.deepj.tensor.Tensor

public class Tensor extends Object
  • Field Details

    • data

      public final float[] data
      Flat row-major storage: element (r, c) lives at data[r * cols + c].
    • rows

      public final int rows
    • cols

      public final int cols
  • Constructor Details

    • Tensor

      public Tensor(int rows, int cols)
    • Tensor

      public Tensor(Tensor source)
      Copy constructor — creates an independent deep copy.
  • Method Details

    • getGpuTag

      public Object getGpuTag()
      Get the GPU handle (used by GPU backends).
    • setGpuTag

      public void setGpuTag(Object tag)
      Set the GPU handle (used by GPU backends).
    • setBackend

      public static void setBackend(TensorBackend backend)
    • backend

      public static TensorBackend backend()
    • rowData

      public float[] rowData(int r)
      Returns a fresh float[] copy of row r. Convenience for tests and debugging; not a view.
    • materialize

      public void materialize()
      Ensures this tensor's data[] is up to date with any pending GPU computation. No-op if this tensor has no GPU handle or is already materialized. Call this before reading data[] directly.
    • matmul

      public Tensor matmul(Tensor other)
    • add

      public Tensor add(Tensor other)
    • subtract

      public Tensor subtract(Tensor other)
    • multiply

      public Tensor multiply(Tensor other)
    • divide

      public Tensor divide(Tensor other)
    • addRowVector

      public Tensor addRowVector(Tensor rowVector)
    • addBroadcastCols

      public Tensor addBroadcastCols(Tensor colVector)
    • divideBroadcastCols

      public Tensor divideBroadcastCols(Tensor colVector)
    • subtractBroadcastCols

      public Tensor subtractBroadcastCols(Tensor colVector)
    • multiplyBroadcastCols

      public Tensor multiplyBroadcastCols(Tensor colVector)
    • addBroadcastRows

      public Tensor addBroadcastRows(Tensor rowVector)
    • multiplyBroadcastRows

      public Tensor multiplyBroadcastRows(Tensor rowVector)
    • multiplyScalar

      public Tensor multiplyScalar(float s)
    • addScalar

      public Tensor addScalar(float s)
    • divideScalar

      public Tensor divideScalar(float s)
    • sumRows

      public Tensor sumRows()
    • sumAlongRows

      public Tensor sumAlongRows()
    • sumAlongCols

      public Tensor sumAlongCols()
    • meanAlongRows

      public Tensor meanAlongRows()
    • varianceAlongRows

      public Tensor varianceAlongRows()
    • transpose

      public Tensor transpose()
    • sqrt

      public Tensor sqrt()
    • neg

      public Tensor neg()
    • exp

      public Tensor exp()
    • log

      public Tensor log()
    • tanhActivation

      public Tensor tanhActivation()
    • sigmoidActivation

      public Tensor sigmoidActivation()
    • reluActivation

      public Tensor reluActivation()
    • reluBackward

      public Tensor reluBackward(Tensor gradOutput)
    • geluActivation

      public Tensor geluActivation()
    • geluBackward

      public Tensor geluBackward(Tensor gradOutput)
    • addInPlace

      public Tensor addInPlace(Tensor b)
    • subtractInPlace

      public Tensor subtractInPlace(Tensor b)
    • multiplyInPlace

      public Tensor multiplyInPlace(Tensor b)
    • divideInPlace

      public Tensor divideInPlace(Tensor b)
    • multiplyScalarInPlace

      public Tensor multiplyScalarInPlace(float s)
    • addScalarInPlace

      public Tensor addScalarInPlace(float s)
    • divideScalarInPlace

      public Tensor divideScalarInPlace(float s)
    • sqrtInPlace

      public Tensor sqrtInPlace()
    • negInPlace

      public Tensor negInPlace()
    • expInPlace

      public Tensor expInPlace()
    • logInPlace

      public Tensor logInPlace()
    • reluInPlace

      public Tensor reluInPlace()
    • geluInPlace

      public Tensor geluInPlace()
    • tanhInPlace

      public Tensor tanhInPlace()
    • sigmoidInPlace

      public Tensor sigmoidInPlace()
    • softmaxRows

      public Tensor softmaxRows()
    • softmaxBackward

      public Tensor softmaxBackward(Tensor softmaxOut)
    • crossEntropyGradient

      public Tensor crossEntropyGradient(int[] targets)
    • adamWUpdate

      public static void adamWUpdate(Tensor w, Tensor g, Tensor mt, Tensor vt, float lr, float beta1, float beta2, float eps, float weightDecay, float bc1, float bc2)
    • layerNormBackward

      public static Tensor layerNormBackward(Tensor dXHat, Tensor xHat, Tensor std, int dim)
    • maxAlongRows

      public Tensor maxAlongRows()
    • clamp

      public Tensor clamp(float min, float max)
    • pow

      public Tensor pow(float exponent)
    • scatterAddRows

      public static void scatterAddRows(Tensor target, int[] indices, Tensor grad)
    • from2D

      public static Tensor from2D(float[][] data)
      Build a tensor from 2-D row-major data. Preferred API for literal matrix construction.
    • sum

      public float sum()
    • sumAbs

      public float sumAbs()
    • crossEntropyLoss

      public float crossEntropyLoss(int[] targets)
    • get

      public float get(int r, int c)
    • set

      public void set(int r, int c, float value)
    • getRow

      public Tensor getRow(int row)
    • setRow

      public void setRow(int row, Tensor source, int srcRow)
    • sliceRows

      public static Tensor sliceRows(Tensor t, int[] rowIndices, int cols)
    • sampleRows

      public static Tensor sampleRows(Tensor t, int n, Random rnd)
    • print

      public void print(String label)
    • zeros

      public static Tensor zeros(int rows, int cols)
    • ones

      public static Tensor ones(int rows, int cols)
    • random

      public static Tensor random(int rows, int cols, Random rand)
    • causalMask

      public static Tensor causalMask(int size)
    • requireSameShape

      public static void requireSameShape(Tensor a, Tensor b, String op)
    • requireTargetsMatchRows

      public static void requireTargetsMatchRows(Tensor logits, int[] targets)