Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Tensor<DTpe>

Multi-dimensional array ala numpy.

A tensor is any multidimensional array. The number of dimensions is called the rank, and the size of all dimensions the shape.

example
const a = [[1,2,3],[4,5,6]];

here a has rank 2 and shape [2,3].

Tensors store values of a particular data type like floats or integers. The datatype can be accessed via the dtype property.

Many operations can be done on tensors. For fast execution, three different backends exist:

  • CPU: Simple to use and works in any browser, but not particularly fast
  • WebAssembly: Reasonably fast and works in most modern browsers
  • WebGL: Very fast when a GPU is available, but comes with some restrictions

Type parameters

  • DTpe: DType = "float32"

Hierarchy

Index

Constructors

constructor

  • new Tensor<DTpe>(dtype: DTpe): Tensor<DTpe>

Properties

dtype

dtype: DTpe

Data type of the tensor

Methods

Abstract abs

  • Takes the absolute of each value of the tensor

    Note that this can only be called on tensors with a signed data type (float*, int32, int16, int8)

    Returns Tensor<DTpe>

Abstract acos

  • Takes the arcus cosine of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract acosh

  • Takes the inverse hyperbolic cosine of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

add

  • add(tensor: Tensor<DTpe>, alpha?: number, beta?: number): Tensor<DTpe>
  • Adds two tensors. Supports broadcasting

    example
    const a = new CPUTensor([2,2],[1,2,3,4]);
    const b = new CPUTensor([2,2],[5,6,7,8]);
    const c = new CPUTensor([1],[2]);
    
    a.add(b);
    //Will be
    // [[6,8],
    //  [10,12]]
    
    a.add(c);
    //Will be
    // [[3,4],
    //  [5,6]]
    

    Parameters

    • tensor: Tensor<DTpe>
    • Optional alpha: number
    • Optional beta: number

    Returns Tensor<DTpe>

Abstract addMultiplyScalar

  • addMultiplyScalar(factor: number, add: number): Tensor<DTpe>

addScalar

  • addScalar(value: number): Tensor<DTpe>

Abstract add_impl

  • add_impl(th: Tensor<DTpe>, tensor: Tensor<DTpe>, resultShape: readonly number[], alpha: number, beta: number): Tensor<DTpe>

alignShapes

  • alignShapes(shape1: readonly number[], shape2: readonly number[]): readonly number[][]
  • Parameters

    • shape1: readonly number[]
    • shape2: readonly number[]

    Returns readonly number[][]

alignTensor

  • alignTensor(tensor: Tensor<DTpe>): (Tensor<DTpe> | readonly number[])[] | (any[] | Tensor<DTpe>)[]

Abstract asin

  • Takes the arcus sinus of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract asinh

  • Takes the inverse hyperbolic sinus of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract atan

  • Takes the arcus tangens of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract atanh

  • Takes the inverse hyperbolic tangens of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

averagePool

  • averagePool(kernelShape: number[], pads?: number[], strides?: number[], includePad?: boolean): Tensor<DTpe>
  • Performs average pooling over the spatial dimensions of this tensor with shape [N,C,D1,D2,..]

    Parameters

    • kernelShape: number[]

      Size of the average pooling dimension

    • Optional pads: number[]

      Padding of the input specified as [startpad_D1,startpad_D2,...,startpad_DN,endpad_D1,endpad_D2,...] Padding value will be 0. Defaults to 0 for all axes

    • Optional strides: number[]

      Stride size of the average pooling kernel. Defaults to 1 for all axes

    • Optional includePad: boolean

      Wether padded values should be included in the average (or masked out). Defaults to false

    Returns Tensor<DTpe>

Protected Abstract averagePool_impl

  • averagePool_impl(kernelShape: number[], pads: number[], strides: number[], includePad: boolean): Tensor<DTpe>
  • Parameters

    • kernelShape: number[]
    • pads: number[]
    • strides: number[]
    • includePad: boolean

    Returns Tensor<DTpe>

Abstract cast

  • cast<DTpe2>(dtype: DTpe2): Tensor<DTpe2>

Abstract ceil

  • Rounds each tensor value to the nearest upper integer

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract clip

  • clip(min?: number, max?: number): Tensor<DTpe>
  • Clips the tensor values between the minimum and maximum

    Parameters

    • Optional min: number

      Minimum value. Defaults to the minimum possible value

    • Optional max: number

      Maximum value. Defaults to the maximum possible value

    Returns Tensor<DTpe>

Abstract clipBackward

  • clipBackward(grad: Tensor<DTpe>, min?: number, max?: number): Tensor<DTpe>
  • Backward pass for clip

    Parameters

    • grad: Tensor<DTpe>

      Gradient from which values should be selected

    • Optional min: number

      Minimum value. Defaults to the minimum possible value

    • Optional max: number

      Maximum value. Defaults to the maximum possible value

    Returns Tensor<DTpe>

compare

  • compare(tensor: Tensor<DTpe>, epsilon?: number): Promise<boolean>
  • Compares this tensor to another tensor.

    example
    const a = new CPUTensor([2,2], [1,2,3,4]);
    const b = new CPUTensor([2,2], [1.1,2.1,2.9,4.05]);
    const c = new CPUTensor([4], [1,2,3,4]);
    a.compare(b, 0.5).then(equal => {
     //equal will be true
    });
    
    a.compare(b).then(equal => {
     //equal will be false
    });
    
    a.compare(c).then(equal => {
     //equal will be false since the shapes of the tensors do not match
    });
    

    Parameters

    • tensor: Tensor<DTpe>

      Tensor to compare to

    • Optional epsilon: number

      Optional maximum difference between the tensors. If not specified the tensors have to be exactly equal

    Returns Promise<boolean>

Abstract concat

  • Concatenate the two tensors along the given axis

    example
    const a = new CPUTensor([2,2],[1,2,3,4]);
    const b = new CPUTensor([2,2],[5,6,7,8]);
    
    a.concat(b,0);
    //Will be
    // [[1,2],
    //  [3,4],
    //  [5,6],
    //  [7,8]]
    
    a.concat(b,1);
    //Will be
    // [[1,2,5,6],
    //  [3,4,7,8],
    

    Parameters

    • tensor: Tensor<DTpe>
    • axis: number

    Returns Tensor<DTpe>

Abstract constantLike

  • constantLike(value: number): Tensor<DTpe>
  • Constructs a tensor with the same shape and the given value everywhere

    Parameters

    • value: number

    Returns Tensor<DTpe>

conv

  • conv(kernel: Tensor<DTpe>, bias?: Tensor<DTpe>, dilations?: number[], group?: number, pads?: number[], strides?: number[], activation?: "id" | "relu" | "relu6"): Tensor<DTpe>
  • Convolves this tensor with the specified kernel.

    This tensor should have shape [N,C,D1,D2,...] where D1,D2,... are the spatial dimensions.

    Behaves according to https://github.com/onnx/onnx/blob/master/docs/Operators.md#Conv

    Parameters

    • kernel: Tensor<DTpe>

      Convolution kernel with shape [M,C/G,K1,K2] where G is the group parameter

    • Optional bias: Tensor<DTpe>

      Optional bias to add to the result with shape [M]

    • Optional dilations: number[]

      Per axis dilations for the spatial dimension. Defaults to 1 for all axes

    • Optional group: number

      Group parameter

    • Optional pads: number[]

      Padding to add to the input for each spatial dimension. Defaults to 0 for all axes

    • Optional strides: number[]

      Convolution stride for each spatial dimension. Defaults to 1 for all axes

    • Optional activation: "id" | "relu" | "relu6"

      Optional activation to apply. Defaults to the identity (so no activation)

    Returns Tensor<DTpe>

convTranspose

  • convTranspose(kernel: Tensor<DTpe>, dilations?: number[], group?: number, pads?: number[], strides?: number[]): Tensor<DTpe>
  • Calculates the transpose convolution

    This tensor should have shape [N,C,D1,D2,...] where D1,D2,... are the spatial dimensions.

    Parameters

    • kernel: Tensor<DTpe>

      Convolution kernel with shape [M,C/G,K1,K2] where G is the group parameter

    • Optional dilations: number[]

      Per axis dilations for the spatial dimension. Defaults to 1 for all axes

    • Optional group: number

      Group parameter

    • Optional pads: number[]

      Padding to add to the input for each spatial dimension. Defaults to 0 for all axes

    • Optional strides: number[]

      Convolution stride for each spatial dimension. Defaults to 1 for all axes

    Returns Tensor<DTpe>

Protected Abstract convTranspose_impl

  • convTranspose_impl(kernel: Tensor<DTpe>, dilations: number[], group: number, pads: number[], strides: number[]): Tensor<DTpe>
  • Parameters

    • kernel: Tensor<DTpe>
    • dilations: number[]
    • group: number
    • pads: number[]
    • strides: number[]

    Returns Tensor<DTpe>

Protected Abstract conv_impl

  • conv_impl(kernel: Tensor<DTpe>, dilations: number[], group: number, pads: number[], strides: number[], activation: Activation, bias?: Tensor<DTpe>): Tensor<DTpe>

Abstract copy

  • Copy the tensor. If the tensor is a GPU tensor, you can specify a precision (16/32)

    Returns Tensor<DTpe>

Abstract cos

  • Takes the cosine of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract cosh

  • Takes the hyperbolic cosine of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract delete

  • delete(): void
  • Deletes the tensor. Has the following effects depending on the backend of the tensor:

    • CPU: Deletes the values
    • WASM: Releases all memory back to the WASM runtime
    • GPU: Releases the associated framebuffer back to the gpu memory allocator

    Returns void

divide

  • divide(tensor: Tensor<DTpe>, alpha?: number): Tensor<DTpe>
  • Divides two tensors. Supports broadcasting

    example
    const a = new CPUTensor([2,2],[5,6,7,8]);
    const b = new CPUTensor([2,2],[1,2,3,4]);
    const c = new CPUTensor([1],[2]);
    
    a.divide(b);
    //Will be
    // [[5,3],
    //  [2.333,2]]
    
    a.divide(c);
    //Will be
    // [[2.5,3],
    //  [3.5,4]]
    

    Parameters

    • tensor: Tensor<DTpe>
    • Optional alpha: number

    Returns Tensor<DTpe>

Abstract divide_impl

  • divide_impl(th: Tensor<DTpe>, tensor: Tensor<DTpe>, resultShape: readonly number[], alpha: number): Tensor<DTpe>

Abstract exp

  • Takes the exponential of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract expand

  • expand(shape: readonly number[]): Tensor<DTpe>

flatten

  • flatten(axis?: number): Tensor<DTpe>

Abstract floor

  • Rounds each tensor value to the nearest lower integer

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract gather

  • Gather values along the given axis, according to the indices

    example
    const a = new CPUTensor([2,2],[1,2,3,4]);
    const indices = new CPUTensor([2],[0,0])
    
    a.gather(0,indices);
    //Will be
    // [[1,2],
    //  [1,2]]
    
    a.gather(1,indices);
    //Will be
    // [[1,1],
    //  [3,3]]
    

    Parameters

    Returns Tensor<DTpe>

gemm

  • gemm(b: Tensor<DTpe>, aTranspose?: boolean, bTranspose?: boolean, alpha?: number, c?: Tensor<DTpe>, beta?: number): Tensor<DTpe>
  • A and B can have batch dimensions. Their last two dimensions should correspond to the dimensions for the matrix product

    Parameters

    • b: Tensor<DTpe>

      Second matrix for the matrix product

    • Optional aTranspose: boolean

      If the last two dimensions of a are transposed. Defaults to false

    • Optional bTranspose: boolean

      If the last two dimensions of a are transposed. Defaults to false

    • Optional alpha: number

      Alpha parameter. Defaults to 1.0

    • Optional c: Tensor<DTpe>

      Optional tensor to add to the result.

    • Optional beta: number

      Beta parameter, only used if c is specified. Defaults to 1.0

    Returns Tensor<DTpe>

Abstract gemm_impl

  • gemm_impl(b: Tensor<DTpe>, aTranspose: boolean, bTranspose: boolean, alpha: number, beta: number, C?: Tensor<DTpe>): Tensor<DTpe>
  • Parameters

    • b: Tensor<DTpe>
    • aTranspose: boolean
    • bTranspose: boolean
    • alpha: number
    • beta: number
    • Optional C: Tensor<DTpe>

    Returns Tensor<DTpe>

Protected getAxes

  • getAxes(axes?: number | number[]): number[]
  • Parameters

    • Optional axes: number | number[]

    Returns number[]

Abstract getShape

  • getShape(): readonly number[]
  • Get the shape of the tensor

    Returns readonly number[]

Abstract getValues

  • getValues(): Promise<TensorValues[DTpe]>
  • Gets the values of the tensor as a Float32 or Int32 Array

    example
    const t = new CPUTensor([2,2], [1,2,3,4]);
    t.getValues().then(values => {
     //values will be a Float32Array with values [1,2,3,4]
    });
    

    Returns Promise<TensorValues[DTpe]>

Abstract hardSigmoid

  • hardSigmoid(alpha: number, beta: number): Tensor<DTpe>
  • Computes the element wise hard sigmoid of all values given by y = max(0, min(1, alpha * x + beta))

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Parameters

    • alpha: number
    • beta: number

    Returns Tensor<DTpe>

Abstract log

  • Takes the natural logarithm of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract matMul

  • Calculates the matrix product. This tensor should have shape [M,N]

    result

    Tensor with shape [M,O]

    Parameters

    • tensor: Tensor<DTpe>

      Matrix to multiply with. Should have shape [N,O]

    Returns Tensor<DTpe>

max

  • max(axes?: number | number[], keepDims?: boolean): Tensor<DTpe>
  • Takes the maximum over specified axis/axes.

    example
    const a = new CPUTensor([2,3], [1,2,3,4,5,6]);
    
    a.max(); //Will be [6]
    a.max(0); //Will be [4,5,6]
    a.max(1); //Will [3,6]
    a.max(0, true); //Will be [[4,5,6]]
    

    Parameters

    • Optional axes: number | number[]

      One or multiple axes to take the maximum over. If not specified this will be all axes

    • Optional keepDims: boolean

      Wether the maximum axes will be kept with size 1

    Returns Tensor<DTpe>

Protected Abstract max_impl

  • max_impl(axes: number[], keepDims: boolean): Tensor<DTpe>

min

  • min(axes?: number | number[], keepDims?: boolean): Tensor<DTpe>
  • Takes the minimum over specified axis/axes.

    example
    const a = new CPUTensor([2,3], [1,2,3,4,5,6]);
    
    a.min(); //Will be [1]
    a.min(0); //Will be [1,2,3]
    a.min(1); //Will [1,4]
    a.min(0, true); //Will be [[1,2,3]]
    

    Parameters

    • Optional axes: number | number[]

      One or multiple axes to take the minimum over. If not specified this will be all axes

    • Optional keepDims: boolean

      Wether the minimum axes will be kept with size 1

    Returns Tensor<DTpe>

Protected Abstract min_impl

  • min_impl(axes: number[], keepDims: boolean): Tensor<DTpe>

multiply

  • multiply(tensor: Tensor<DTpe>, alpha?: number): Tensor<DTpe>
  • Multiplies two tensors. Supports broadcasting

    example
    const a = new CPUTensor([2,2],[1,2,3,4]);
    const b = new CPUTensor([2,2],[5,6,7,8]);
    const c = new CPUTensor([1],[2]);
    
    a.multiply(b);
    //Will be
    // [[5,12],
    //  [21,32]]
    
    a.multiply(c);
    //Will be
    // [[2,4]
        [6,8]]
    

    Parameters

    • tensor: Tensor<DTpe>
    • Optional alpha: number

    Returns Tensor<DTpe>

multiplyScalar

  • multiplyScalar(value: number): Tensor<DTpe>

Abstract multiply_impl

  • multiply_impl(th: Tensor<DTpe>, tensor: Tensor<DTpe>, resultShape: readonly number[], alpha: number): Tensor<DTpe>

Abstract negate

  • Negates all entries of the tensor

    Note that this can only be called on tensors with a signed data type (float*, int32, int16, int8)

    Returns Tensor<DTpe>

Abstract normalize

  • Normalizes the tensor according to the following formula:

    x' = (x-mean)/sqrt(variance + epsilon)
    x'' = x'*scale + bias
    

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Parameters

    Returns Tensor<DTpe>

pad

  • pad(pads: number[], mode?: "constant" | "reflect" | "edge", value?: number): Tensor<DTpe>
  • Pads the input according to the padding mode. The input has shape [D1,D2,..]

    example
    const a = new CPUTensor([2,2],[1,2,3,4]);
    a.pad([1,1,1,1],'constant',5);
    //Result will be:
    // [[5,5,5,5],
    //  [5,1,2,5],
    //  [5,3,4,5],
    //  [5,5,5,5]]
    a.pad([1,1,1,1],'edge');
    //Result will be:
    // [[1,1,2,2],
    //  [1,1,2,2],
    //  [3,3,4,4],
    //  [3,3,4,4]]
    
    a.pad([2,2,2,2],'reflect');
    //Result will be:
    // [[4,3,3,4,4,3],
    //  [2,1,1,2,2,1],
    //  [2,1,1,2,2,1],
    //  [4,3,3,4,4,3],
    //  [4,3,3,4,4,3],
    //  [2,1,1,2,2,1]]
    

    Parameters

    • pads: number[]

      Padding size of each input. Specified as [startpad_D1,startpad_D2,...,startpad_DN,endpad_D1,endpad_D2,...]

    • Optional mode: "constant" | "reflect" | "edge"

      Padding mode. One of 'constant', 'edge', 'reflect'. Defaults to 'constant'

    • Optional value: number

      Value for constant padding. Defaults to 0.0

    Returns Tensor<DTpe>

Protected Abstract pad_impl

  • pad_impl(pads: number[], mode: PadMode, value: number): Tensor<DTpe>

power

  • Takes the positionwise power. Supports broadcasting

    example
    const a = new CPUTensor([2,2],[5,6,7,8]);
    const b = new CPUTensor([2,2],[2,3,2,3]);
    const c = new CPUTensor([1],[2]);
    
    a.power(b);
    //Will be
    // [[25,216],
    //  [49,512]]
    
    a.power(c);
    //Will be
    // [[25,36],
    //  [49,64]]
    

    Parameters

    Returns Tensor<DTpe>

Abstract powerScalar

  • powerScalar(power: number, factor: number): Tensor<DTpe>
  • Takes element wise power and multiplies with the given factor

    Parameters

    • power: number
    • factor: number

    Returns Tensor<DTpe>

Abstract power_impl

  • power_impl(th: Tensor<DTpe>, tensor: Tensor<DTpe>, resultShape: readonly number[]): Tensor<DTpe>

product

  • product(axes?: number | number[], keepDims?: boolean): Tensor<DTpe>
  • Takes the product over specified axis/axes.

    example
    const a = new CPUTensor([2,3], [1,2,3,4,5,6]);
    
    a.product(); //Will be [720]
    a.product(0); //Will be [4,10,18]
    a.product(1); //Will [6,120]
    a.product(0, true); //Will be [[4,10,18]]
    

    Parameters

    • Optional axes: number | number[]

      One or multiple axes to take the product over. If not specified this will be all axes

    • Optional keepDims: boolean

      Wether the product axes will be kept with size 1

    Returns Tensor<DTpe>

Protected Abstract product_impl

  • product_impl(axes: number[], keepDims: boolean): Tensor<DTpe>

reduceLogSum

  • reduceLogSum(axes?: number | number[], keepDims?: boolean): Tensor<DTpe>
  • Takes the log of the sum over the specified axis This is equal to a.sum(axes, keepDims).log() (where sumSize is the number of entries in the summation axes) but faster.

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Parameters

    • Optional axes: number | number[]

      One or multiple axes to take the mean over. If not specified this will take the mean over all axes

    • Optional keepDims: boolean

      Wether the mean axes will be kept with size 1

    Returns Tensor<DTpe>

reduceLogSumExp

  • reduceLogSumExp(axes?: number | number[], keepDims?: boolean): Tensor<DTpe>
  • Takes the log of the sum over the exp of the specified axis This is equal to a.sum(axes, keepDims).log() (where sumSize is the number of entries in the summation axes) but faster.

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Parameters

    • Optional axes: number | number[]

      One or multiple axes to take the mean over. If not specified this will take the mean over all axes

    • Optional keepDims: boolean

      Wether the mean axes will be kept with size 1

    Returns Tensor<DTpe>

Protected Abstract reduceLogSumExp_impl

  • reduceLogSumExp_impl(axes: number[], keepDims: boolean): Tensor<DTpe>

Protected Abstract reduceLogSum_impl

  • reduceLogSum_impl(axes: number[], keepDims: boolean): Tensor<DTpe>

reduceMean

  • reduceMean(axes?: number | number[], keepDims?: boolean): Tensor<DTpe>
  • Takes the mean over the specified axis/axes. This is equal to a.sum(axes, keepDims).divide(sumSize) (where sumSize is the number of entries in the summation axes) but faster.

    Parameters

    • Optional axes: number | number[]

      One or multiple axes to take the mean over. If not specified this will take the mean over all axes

    • Optional keepDims: boolean

      Wether the mean axes will be kept with size 1

    Returns Tensor<DTpe>

reduceMeanSquare

  • reduceMeanSquare(axes?: number | number[], keepDims?: boolean): Tensor<DTpe>
  • Takes the mean over the specified axis/axes with the entries of the tensor squared. This is equal to a.multiply(a).sum(axes, keepDims).divide(sumSize) (where sumSize is the number of entries in the summation axes) but faster.

    Parameters

    • Optional axes: number | number[]

      One or multiple axes to take the mean over. If not specified this will take the mean over all axes

    • Optional keepDims: boolean

      Wether the mean axes will be kept with size 1

    Returns Tensor<DTpe>

Protected Abstract reduceMeanSquare_impl

  • reduceMeanSquare_impl(axes: number[], keepDims: boolean): Tensor<DTpe>

Protected Abstract reduceMean_impl

  • reduceMean_impl(axes: number[], keepDims: boolean): Tensor<DTpe>

Abstract repeat

  • repeat(repeats: number[]): Tensor<DTpe>
  • Repeat the tensor along each dimension

    example
    const a = new CPUTensor([2,2],[1,2,3,4]);
    
    a.repeat([2,1]);
    //Will be
    // [[1,2],
    //  [3,4],
    //  [1,2],
    //  [3,4]]
    
    a.repeat([1,2]);
    //Will be
    // [[1,2,1,2],
    //  [3,4,3,4],
    

    Parameters

    • repeats: number[]

      Number of repetitions along each dimension

    Returns Tensor<DTpe>

reshape

  • reshape(shape: readonly number[], copy?: boolean): Tensor<DTpe>
  • Reshape the tensor to the specified shape

    At most one value in the shape can be -1, which will be replaced by the inferred size for this dimension.

    Parameters

    • shape: readonly number[]

      New shape of the tensor

    • Optional copy: boolean

      Wether the tensor values should be copied. Only has an effect on GPU tensors

    Returns Tensor<DTpe>

Protected Abstract reshape_impl

  • reshape_impl(shape: readonly number[], copy: boolean): Tensor<DTpe>
  • Parameters

    • shape: readonly number[]
    • copy: boolean

    Returns Tensor<DTpe>

Abstract round

  • Rounds each tensor value to the nearest integer. When the value is 0.5 it rounds up.

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract setValues

  • setValues(values: Tensor<DTpe>, starts: number[]): Tensor<DTpe>
  • Sets the values in the current tensor to the given values. Starts at the specified start indices at each axis. Note that this will not occur in place, but will generate a new tensor.

    example
    const a = new CPUTensor([2,2],[1,2,3,4]);
    const b = new CPUTensor([1,2],[5,6])
    
    a.set(b,[1,0]);
    //Will be
    // [[1,2],
    //  [5,6]]
    

    Parameters

    • values: Tensor<DTpe>
    • starts: number[]

    Returns Tensor<DTpe>

Abstract sigmoid

  • Computes the element wise sigmoid of all values

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract sign

  • Computes the value-wise sign which is:

    • (-1) if x < 0
    • 0 if x == 0
    • 1 otherwise

    Note that this can only be called on tensors with a signed data type (float*, int32, int16, int8)

    Returns Tensor<DTpe>

Abstract sin

  • Takes the sinus of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract singleConstant

  • singleConstant(value: number): Tensor<DTpe>
  • Constructs a tensor with shape [1] and the given value everywhere

    Parameters

    • value: number

    Returns Tensor<DTpe>

Abstract sinh

  • Takes the hyperbolic sinus of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

slice

  • slice(starts: number[], ends: number[], axes?: number[], steps?: number[]): Tensor<DTpe>
  • Takes a slice of the tensor along the specified axes.

    example
    const a = new CPUTensor([2,2],[5,6,7,8]);
    
    a.slice([0],[1],[0]);
    //Will be
    // [[5,6]]
    
    a.slice([0],[1],[1]);
    //Will be
    // [[5],
        [6]]
    

    Parameters

    • starts: number[]

      Start of the slice for each axis

    • ends: number[]

      End of the slice for each axis - Exclusive (the end index will not be included in the slice)

    • Optional axes: number[]

      Axes to slice. Defaults to all axes

    • Optional steps: number[]

    Returns Tensor<DTpe>

Protected Abstract slice_impl

  • slice_impl(starts: number[], ends: number[], axes: number[], steps: number[]): Tensor<DTpe>
  • Parameters

    • starts: number[]
    • ends: number[]
    • axes: number[]
    • steps: number[]

    Returns Tensor<DTpe>

softmax

  • softmax(axis: number): Tensor<DTpe>

Abstract sqrt

  • Takes the square root of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

squeeze

subtract

  • subtract(tensor: Tensor<DTpe>, alpha?: number, beta?: number): Tensor<DTpe>
  • Subtracts two tensors. Supports broadcasting

    example
    const a = new CPUTensor([2,2],[5,6,7,8]);
    const b = new CPUTensor([2,2],[1,2,3,4]);
    const c = new CPUTensor([1],[2]);
    
    a.subtract(b);
    //Will be
    // [[4,4],
    //  [4,4]]
    
    a.subtract(c);
    //Will be
    // [[3,4],
    //  [5,6]]
    

    Parameters

    • tensor: Tensor<DTpe>
    • Optional alpha: number
    • Optional beta: number

    Returns Tensor<DTpe>

Abstract subtract_impl

  • subtract_impl(th: Tensor<DTpe>, tensor: Tensor<DTpe>, resultShape: readonly number[], alpha: number, beta: number): Tensor<DTpe>

sum

  • sum(axes?: number | number[], keepDims?: boolean): Tensor<DTpe>
  • Sums over the specified axis/axes.

    example
    const a = new CPUTensor([2,3], [1,2,3,4,5,6]);
    
    a.sum(); //Will be [21]
    a.sum(0); //Will be [5,7,9]
    a.sum(1); //Will [6,15]
    a.sum(0, true); //Will be [[5,7,9]]
    

    Parameters

    • Optional axes: number | number[]

      One or multiple axes to sum over. If not specified this will sum over all axes

    • Optional keepDims: boolean

      Wether the summation axes will be kept with size 1

    Returns Tensor<DTpe>

sumSquare

  • sumSquare(axes?: number | number[], keepDims?: boolean): Tensor<DTpe>
  • Sums over the specified axis/axes with the entries of the tensor squared. This is equal to a.multiply(a).sum(axes, keepDims) but faster

    Parameters

    • Optional axes: number | number[]

      One or multiple axes to sum over. If not specified this will sum over all axes

    • Optional keepDims: boolean

      Wether the summation axes will be kept with size 1

    Returns Tensor<DTpe>

Protected Abstract sumSquare_impl

  • sumSquare_impl(axes: number[], keepDims: boolean): Tensor<DTpe>

Protected Abstract sum_impl

  • sum_impl(axes: number[], keepDims: boolean): Tensor<DTpe>

Abstract tan

  • Takes the tangens of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

Abstract tanh

  • Takes the hyperbolic tangens of each value of the tensor

    Note that this can only be called on tensors with a float data type (float64, float32, float16)

    Returns Tensor<DTpe>

transpose

  • transpose(permutation?: number[]): Tensor<DTpe>
  • Transposes the tensor according to the given permutation

    example
    const a = new CPUTensor([2,2],[5,6,7,8]);
    
    a.transpose();
    //Will be
    // [[5,7],
    //  [6,8]]
    

    Parameters

    • Optional permutation: number[]

      Permutation for the axes. Default is the reverse axis order

    Returns Tensor<DTpe>

Protected Abstract transpose_impl

  • transpose_impl(permutation: number[]): Tensor<DTpe>

Abstract upsample

  • upsample(scales: number[]): Tensor<DTpe>
  • Scales the tensor up/down according to the specified scales. Uses nearest neighbor sampling

    example
    const a = new CPUTensor([2,2],[1,2,3,4]);
    
    a.upsample([2,2]);
    //Will be
    // [[1,1,2,2],
    //  [1,1,2,2],
    //  [3,3,4,4],
    //  [3,3,4,4]]
    

    Parameters

    • scales: number[]

    Returns Tensor<DTpe>

Generated using TypeDoc