Helper module for Tensor processing.
These functions and classes are only used internally, meaning an end-user shouldn’t need to access anything here.
new Tensor(...args).dims : Array.<number>.type : DataType.data : DataArray.size : number.location : string.Symbol.iterator() ⇒ Iterator._getitem(index) ⇒ Tensor.indexOf(item) ⇒ number._subarray(index, iterSize, iterDims) ⇒ Tensor.item() ⇒ number | bigint.tolist() ⇒ Array.sigmoid() ⇒ Tensor.sigmoid_() ⇒ Tensor.map(callback) ⇒ Tensor.map_(callback) ⇒ Tensor.mul(val) ⇒ Tensor.mul_(val) ⇒ Tensor.div(val) ⇒ Tensor.div_(val) ⇒ Tensor.add(val) ⇒ Tensor.add_(val) ⇒ Tensor.sub(val) ⇒ Tensor.sub_(val) ⇒ Tensor.permute(...dims) ⇒ Tensor.sum([dim], keepdim) ⇒.norm([p], [dim], [keepdim]) ⇒ Tensor.normalize_([p], [dim]) ⇒ Tensor.normalize([p], [dim]) ⇒ Tensor.stride() ⇒ Array.<number>.squeeze([dim]) ⇒ Tensor.squeeze_().unsqueeze(dim) ⇒ Tensor.unsqueeze_().flatten_().flatten(start_dim, end_dim) ⇒ Tensor.view(...dims) ⇒ Tensor.clamp_().clamp(min, max) ⇒ Tensor.round_().round() ⇒ Tensor.to(type) ⇒ Tensor.permute(tensor, axes) ⇒ Tensor.interpolate(input, size, mode, align_corners) ⇒ Tensor.interpolate_4d(input, options) ⇒ Promise.<Tensor>.matmul(a, b) ⇒ Promise.<Tensor>.rfft(x, a) ⇒ Promise.<Tensor>.topk(x, k) ⇒ *.mean_pooling(last_hidden_state, attention_mask) ⇒ Tensor.layer_norm(input, normalized_shape, options) ⇒ Tensor.cat(tensors, dim) ⇒ Tensor.stack(tensors, dim) ⇒ Tensor.std_mean(input, dim, correction, keepdim) ⇒ Array.<Tensor>.mean(input, dim, keepdim) ⇒ Tensor.full(size, fill_value) ⇒ Tensor.ones(size) ⇒ Tensor.ones_like(tensor) ⇒ Tensor.zeros(size) ⇒ Tensor.zeros_like(tensor) ⇒ Tensor.quantize_embeddings(tensor, precision) ⇒ Tensor~args[0] : ONNXTensor~reshape(data, dimensions) ⇒ *~reshapedArray : any~DataArray : *~NestArray : *Kind: static class of utils/tensor
new Tensor(...args).dims : Array.<number>.type : DataType.data : DataArray.size : number.location : string.Symbol.iterator() ⇒ Iterator._getitem(index) ⇒ Tensor.indexOf(item) ⇒ number._subarray(index, iterSize, iterDims) ⇒ Tensor.item() ⇒ number | bigint.tolist() ⇒ Array.sigmoid() ⇒ Tensor.sigmoid_() ⇒ Tensor.map(callback) ⇒ Tensor.map_(callback) ⇒ Tensor.mul(val) ⇒ Tensor.mul_(val) ⇒ Tensor.div(val) ⇒ Tensor.div_(val) ⇒ Tensor.add(val) ⇒ Tensor.add_(val) ⇒ Tensor.sub(val) ⇒ Tensor.sub_(val) ⇒ Tensor.permute(...dims) ⇒ Tensor.sum([dim], keepdim) ⇒.norm([p], [dim], [keepdim]) ⇒ Tensor.normalize_([p], [dim]) ⇒ Tensor.normalize([p], [dim]) ⇒ Tensor.stride() ⇒ Array.<number>.squeeze([dim]) ⇒ Tensor.squeeze_().unsqueeze(dim) ⇒ Tensor.unsqueeze_().flatten_().flatten(start_dim, end_dim) ⇒ Tensor.view(...dims) ⇒ Tensor.clamp_().clamp(min, max) ⇒ Tensor.round_().round() ⇒ Tensor.to(type) ⇒ TensorCreate a new Tensor or copy an existing Tensor.
| Param | Type |
|---|---|
| ...args | * |
Dimensions of the tensor.
Kind: instance property of Tensor
Type of the tensor.
Kind: instance property of Tensor
The data stored in the tensor.
Kind: instance property of Tensor
The number of elements in the tensor.
Kind: instance property of Tensor
The location of the tensor data.
Kind: instance property of Tensor
Returns an iterator object for iterating over the tensor data in row-major order. If the tensor has more than one dimension, the iterator will yield subarrays.
Kind: instance method of Tensor
Returns: Iterator - An iterator object for iterating over the tensor data in row-major order.
Index into a Tensor object.
Kind: instance method of Tensor
Returns: Tensor - The data at the specified index.
| Param | Type | Description |
|---|---|---|
| index | number | The index to access. |
Kind: instance method of Tensor
Returns: number - The index of the first occurrence of item in the tensor data.
| Param | Type | Description |
|---|---|---|
| item | number | bigint | The item to search for in the tensor |
Kind: instance method of Tensor
| Param | Type |
|---|---|
| index | number |
| iterSize | number |
| iterDims | any |
Returns the value of this tensor as a standard JavaScript Number. This only works
for tensors with one element. For other cases, see Tensor.tolist().
Kind: instance method of Tensor
Returns: number | bigint - The value of this tensor as a standard JavaScript Number.
Throws:
Error If the tensor has more than one element.Convert tensor data to a n-dimensional JS list
Kind: instance method of Tensor
Return a new Tensor with the sigmoid function applied to each element.
Kind: instance method of Tensor
Returns: Tensor - The tensor with the sigmoid function applied.
Applies the sigmoid function to the tensor in place.
Kind: instance method of Tensor
Returns: Tensor - Returns this.
Return a new Tensor with a callback function applied to each element.
Kind: instance method of Tensor
Returns: Tensor - A new Tensor with the callback function applied to each element.
| Param | Type | Description |
|---|---|---|
| callback | function | The function to apply to each element. It should take three arguments: the current element, its index, and the tensor's data array. |
Apply a callback function to each element of the tensor in place.
Kind: instance method of Tensor
Returns: Tensor - Returns this.
| Param | Type | Description |
|---|---|---|
| callback | function | The function to apply to each element. It should take three arguments: the current element, its index, and the tensor's data array. |
Return a new Tensor with every element multiplied by a constant.
Kind: instance method of Tensor
Returns: Tensor - The new tensor.
| Param | Type | Description |
|---|---|---|
| val | number | The value to multiply by. |
Multiply the tensor by a constant in place.
Kind: instance method of Tensor
Returns: Tensor - Returns this.
| Param | Type | Description |
|---|---|---|
| val | number | The value to multiply by. |
Return a new Tensor with every element divided by a constant.
Kind: instance method of Tensor
Returns: Tensor - The new tensor.
| Param | Type | Description |
|---|---|---|
| val | number | The value to divide by. |
Divide the tensor by a constant in place.
Kind: instance method of Tensor
Returns: Tensor - Returns this.
| Param | Type | Description |
|---|---|---|
| val | number | The value to divide by. |
Return a new Tensor with every element added by a constant.
Kind: instance method of Tensor
Returns: Tensor - The new tensor.
| Param | Type | Description |
|---|---|---|
| val | number | The value to add by. |
Add the tensor by a constant in place.
Kind: instance method of Tensor
Returns: Tensor - Returns this.
| Param | Type | Description |
|---|---|---|
| val | number | The value to add by. |
Return a new Tensor with every element subtracted by a constant.
Kind: instance method of Tensor
Returns: Tensor - The new tensor.
| Param | Type | Description |
|---|---|---|
| val | number | The value to subtract by. |
Subtract the tensor by a constant in place.
Kind: instance method of Tensor
Returns: Tensor - Returns this.
| Param | Type | Description |
|---|---|---|
| val | number | The value to subtract by. |
Return a permuted version of this Tensor, according to the provided dimensions.
Kind: instance method of Tensor
Returns: Tensor - The permuted tensor.
| Param | Type | Description |
|---|---|---|
| ...dims | number | Dimensions to permute. |
Returns the sum of each row of the input tensor in the given dimension dim.
Kind: instance method of Tensor
Returns: The summed tensor
| Param | Type | Default | Description |
|---|---|---|---|
| [dim] | number | | The dimension or dimensions to reduce. If |
| keepdim | boolean | false | Whether the output tensor has |
Returns the matrix norm or vector norm of a given tensor.
Kind: instance method of Tensor
Returns: Tensor - The norm of the tensor.
| Param | Type | Default | Description |
|---|---|---|---|
| [p] | number | string | 'fro' | The order of norm |
| [dim] | number | | Specifies which dimension of the tensor to calculate the norm across. If dim is None, the norm will be calculated across all dimensions of input. |
| [keepdim] | boolean | false | Whether the output tensors have dim retained or not. |
Performs L_p normalization of inputs over specified dimension. Operates in place.
Kind: instance method of Tensor
Returns: Tensor - this for operation chaining.
| Param | Type | Default | Description |
|---|---|---|---|
| [p] | number | 2 | The exponent value in the norm formulation |
| [dim] | number | 1 | The dimension to reduce |
Performs L_p normalization of inputs over specified dimension.
Kind: instance method of Tensor
Returns: Tensor - The normalized tensor.
| Param | Type | Default | Description |
|---|---|---|---|
| [p] | number | 2 | The exponent value in the norm formulation |
| [dim] | number | 1 | The dimension to reduce |
Compute and return the stride of this tensor. Stride is the jump necessary to go from one element to the next one in the specified dimension dim.
Kind: instance method of Tensor
Returns: Array.<number> - The stride of this tensor.
Returns a tensor with all specified dimensions of input of size 1 removed.
NOTE: The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other.
If you would like a copy, use tensor.clone() before squeezing.
Kind: instance method of Tensor
Returns: Tensor - The squeezed tensor
| Param | Type | Default | Description |
|---|---|---|---|
| [dim] | number | | If given, the input will be squeezed only in the specified dimensions. |
In-place version of @see Tensor.squeeze
Kind: instance method of Tensor
Returns a new tensor with a dimension of size one inserted at the specified position.
NOTE: The returned tensor shares the same underlying data with this tensor.
Kind: instance method of Tensor
Returns: Tensor - The unsqueezed tensor
| Param | Type | Default | Description |
|---|---|---|---|
| dim | number | | The index at which to insert the singleton dimension |
In-place version of @see Tensor.unsqueeze
Kind: instance method of Tensor
In-place version of @see Tensor.flatten
Kind: instance method of Tensor
Flattens input by reshaping it into a one-dimensional tensor.
If start_dim or end_dim are passed, only dimensions starting with start_dim
and ending with end_dim are flattened. The order of elements in input is unchanged.
Kind: instance method of Tensor
Returns: Tensor - The flattened tensor.
| Param | Type | Default | Description |
|---|---|---|---|
| start_dim | number | 0 | the first dim to flatten |
| end_dim | number | the last dim to flatten |
Returns a new tensor with the same data as the self tensor but of a different shape.
Kind: instance method of Tensor
Returns: Tensor - The tensor with the same data but different shape
| Param | Type | Description |
|---|---|---|
| ...dims | number | the desired size |
In-place version of @see Tensor.clamp
Kind: instance method of Tensor
Clamps all elements in input into the range [ min, max ]
Kind: instance method of Tensor
Returns: Tensor - the output tensor.
| Param | Type | Description |
|---|---|---|
| min | number | lower-bound of the range to be clamped to |
| max | number | upper-bound of the range to be clamped to |
In-place version of @see Tensor.round
Kind: instance method of Tensor
Rounds elements of input to the nearest integer.
Kind: instance method of Tensor
Returns: Tensor - the output tensor.
Performs Tensor dtype conversion.
Kind: instance method of Tensor
Returns: Tensor - The converted tensor.
| Param | Type | Description |
|---|---|---|
| type | DataType | The desired data type. |
Permutes a tensor according to the provided axes.
Kind: static method of utils/tensor
Returns: Tensor - The permuted tensor.
| Param | Type | Description |
|---|---|---|
| tensor | any | The input tensor to permute. |
| axes | Array | The axes to permute the tensor along. |
Interpolates an Tensor to the given size.
Kind: static method of utils/tensor
Returns: Tensor - The interpolated tensor.
| Param | Type | Description |
|---|---|---|
| input | Tensor | The input tensor to interpolate. Data must be channel-first (i.e., [c, h, w]) |
| size | Array.<number> | The output size of the image |
| mode | string | The interpolation mode |
| align_corners | boolean | Whether to align corners. |
Down/up samples the input. Inspired by https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html.
Kind: static method of utils/tensor
Returns: Promise.<Tensor> - The interpolated tensor.
| Param | Type | Default | Description |
|---|---|---|---|
| input | Tensor | the input tensor | |
| options | Object | the options for the interpolation | |
| [options.size] | * | | output spatial size. |
| [options.mode] | "bilinear" | "bicubic" | 'bilinear' | algorithm used for upsampling |
Matrix product of two tensors. Inspired by https://pytorch.org/docs/stable/generated/torch.matmul.html
Kind: static method of utils/tensor
Returns: Promise.<Tensor> - The matrix product of the two tensors.
| Param | Type | Description |
|---|---|---|
| a | Tensor | the first tensor to be multiplied |
| b | Tensor | the second tensor to be multiplied |
Computes the one dimensional Fourier transform of real-valued input. Inspired by https://pytorch.org/docs/stable/generated/torch.fft.rfft.html
Kind: static method of utils/tensor
Returns: Promise.<Tensor> - the output tensor.
| Param | Type | Description |
|---|---|---|
| x | Tensor | the real input tensor |
| a | Tensor | The dimension along which to take the one dimensional real FFT. |
Returns the k largest elements of the given input tensor. Inspired by https://pytorch.org/docs/stable/generated/torch.topk.html
Kind: static method of utils/tensor
Returns: * - the output tuple of (Tensor, LongTensor) of top-k elements and their indices.
| Param | Type | Description |
|---|---|---|
| x | Tensor | the input tensor |
| k | number | the k in "top-k" |
Perform mean pooling of the last hidden state followed by a normalization step.
Kind: static method of utils/tensor
Returns: Tensor - Returns a new Tensor of shape [batchSize, embedDim].
| Param | Type | Description |
|---|---|---|
| last_hidden_state | Tensor | Tensor of shape [batchSize, seqLength, embedDim] |
| attention_mask | Tensor | Tensor of shape [batchSize, seqLength] |
Apply Layer Normalization for last certain number of dimensions.
Kind: static method of utils/tensor
Returns: Tensor - The normalized tensor.
| Param | Type | Default | Description |
|---|---|---|---|
| input | Tensor | The input tensor | |
| normalized_shape | Array.<number> | input shape from an expected input of size | |
| options | Object | The options for the layer normalization | |
| [options.eps] | number | 1e-5 | A value added to the denominator for numerical stability. |
Concatenates an array of tensors along a specified dimension.
Kind: static method of utils/tensor
Returns: Tensor - The concatenated tensor.
| Param | Type | Description |
|---|---|---|
| tensors | Array.<Tensor> | The array of tensors to concatenate. |
| dim | number | The dimension to concatenate along. |
Stack an array of tensors along a specified dimension.
Kind: static method of utils/tensor
Returns: Tensor - The stacked tensor.
| Param | Type | Description |
|---|---|---|
| tensors | Array.<Tensor> | The array of tensors to stack. |
| dim | number | The dimension to stack along. |
Calculates the standard deviation and mean over the dimensions specified by dim. dim can be a single dimension or null to reduce over all dimensions.
Kind: static method of utils/tensor
Returns: Array.<Tensor> - A tuple of (std, mean) tensors.
| Param | Type | Description |
|---|---|---|
| input | Tensor | the input tenso |
| dim | number | null | the dimension to reduce. If None, all dimensions are reduced. |
| correction | number | difference between the sample size and sample degrees of freedom. Defaults to Bessel's correction, correction=1. |
| keepdim | boolean | whether the output tensor has dim retained or not. |
Returns the mean value of each row of the input tensor in the given dimension dim.
Kind: static method of utils/tensor
Returns: Tensor - A new tensor with means taken along the specified dimension.
| Param | Type | Description |
|---|---|---|
| input | Tensor | the input tensor. |
| dim | number | null | the dimension to reduce. |
| keepdim | boolean | whether the output tensor has dim retained or not. |
Creates a tensor of size size filled with fill_value. The tensor’s dtype is inferred from fill_value.
Kind: static method of utils/tensor
Returns: Tensor - The filled tensor.
| Param | Type | Description |
|---|---|---|
| size | Array.<number> | A sequence of integers defining the shape of the output tensor. |
| fill_value | number | bigint | The value to fill the output tensor with. |
Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.
Kind: static method of utils/tensor
Returns: Tensor - The ones tensor.
| Param | Type | Description |
|---|---|---|
| size | Array.<number> | A sequence of integers defining the shape of the output tensor. |
Returns a tensor filled with the scalar value 1, with the same size as input.
Kind: static method of utils/tensor
Returns: Tensor - The ones tensor.
| Param | Type | Description |
|---|---|---|
| tensor | Tensor | The size of input will determine size of the output tensor. |
Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size.
Kind: static method of utils/tensor
Returns: Tensor - The zeros tensor.
| Param | Type | Description |
|---|---|---|
| size | Array.<number> | A sequence of integers defining the shape of the output tensor. |
Returns a tensor filled with the scalar value 0, with the same size as input.
Kind: static method of utils/tensor
Returns: Tensor - The zeros tensor.
| Param | Type | Description |
|---|---|---|
| tensor | Tensor | The size of input will determine size of the output tensor. |
Quantizes the embeddings tensor to binary or unsigned binary precision.
Kind: static method of utils/tensor
Returns: Tensor - The quantized tensor.
| Param | Type | Description |
|---|---|---|
| tensor | Tensor | The tensor to quantize. |
| precision | 'binary' | 'ubinary' | The precision to use for quantization. |
Kind: inner property of utils/tensor
Reshapes a 1-dimensional array into an n-dimensional array, according to the provided dimensions.
Kind: inner method of utils/tensor
Returns: * - The reshaped array.
| Param | Type | Description |
|---|---|---|
| data | Array<T> | DataArray | The input array to reshape. |
| dimensions | DIM | The target shape/dimensions. |
Example
reshape([10 ], [1 ]); // Type: number[] Value: [10]
reshape([1, 2, 3, 4 ], [2, 2 ]); // Type: number[][] Value: [[1, 2], [3, 4]]
reshape([1, 2, 3, 4, 5, 6, 7, 8], [2, 2, 2]); // Type: number[][][] Value: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
reshape([1, 2, 3, 4, 5, 6, 7, 8], [4, 2 ]); // Type: number[][] Value: [[1, 2], [3, 4], [5, 6], [7, 8]]Kind: inner property of reshape
Kind: inner typedef of utils/tensor
This creates a nested array of a given type and depth (see examples).
Kind: inner typedef of utils/tensor
Example
NestArray<string, 1>; // string[]Example
NestArray<number, 2>; // number[][]Example
NestArray<string, 3>; // string[][][] etc.