Transformers.js documentation

utils/maths

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

utils/maths

Helper module for mathematical processing.

These functions and classes are only used internally, meaning an end-user shouldn’t need to access anything here.


utils/maths.interpolate_data(input)

Kind: static method of utils/maths

ParamType
inputTypedArray

utils/maths.permute_data(array, dims, axes) β‡’ <code> * </code>

Helper method to permute a AnyTypedArray directly

Kind: static method of utils/maths
Returns: * - The permuted array and the new shape.

ParamType
arrayT
dimsArray.<number>
axesArray.<number>

utils/maths.softmax(arr) β‡’ <code> T </code>

Compute the softmax of an array of numbers.

Kind: static method of utils/maths
Returns: T - The softmax array.

ParamTypeDescription
arrT

The array of numbers to compute the softmax of.


utils/maths.log_softmax(arr) β‡’ <code> T </code>

Calculates the logarithm of the softmax function for the input array.

Kind: static method of utils/maths
Returns: T - The resulting log_softmax array.

ParamTypeDescription
arrT

The input array to calculate the log_softmax function for.


utils/maths.dot(arr1, arr2) β‡’ <code> number </code>

Calculates the dot product of two arrays.

Kind: static method of utils/maths
Returns: number - The dot product of arr1 and arr2.

ParamTypeDescription
arr1Array.<number>

The first array.

arr2Array.<number>

The second array.


utils/maths.getTopItems(items, [top_k]) β‡’ <code> * </code>

Get the top k items from an iterable, sorted by descending order

Kind: static method of utils/maths
Returns: * - The top k items, sorted by descending order

ParamTypeDefaultDescription
itemsArray<any> | TypedArray

The items to be sorted

[top_k]number | null0

The number of top items to return (default: 0 = return all)


utils/maths.cos_sim(arr1, arr2) β‡’ <code> number </code>

Computes the cosine similarity between two arrays.

Kind: static method of utils/maths
Returns: number - The cosine similarity between the two arrays.

ParamTypeDescription
arr1Array.<number>

The first array.

arr2Array.<number>

The second array.


utils/maths.magnitude(arr) β‡’ <code> number </code>

Calculates the magnitude of a given array.

Kind: static method of utils/maths
Returns: number - The magnitude of the array.

ParamTypeDescription
arrArray.<number>

The array to calculate the magnitude of.


utils/maths.min(arr) β‡’ <code> Array. < number > </code>

Returns the value and index of the minimum element in an array.

Kind: static method of utils/maths
Returns: Array.<number> - the value and index of the minimum element, of the form: [valueOfMin, indexOfMin]
Throws:

  • Error If array is empty.
ParamTypeDescription
arrArray<number> | TypedArray

array of numbers.


utils/maths.max(arr) β‡’ <code> * </code>

Returns the value and index of the maximum element in an array.

Kind: static method of utils/maths
Returns: * - the value and index of the maximum element, of the form: [valueOfMax, indexOfMax]
Throws:

  • Error If array is empty.
ParamTypeDescription
arrArray<number> | AnyTypedArray

array of numbers.


utils/maths.medianFilter(data, windowSize)

Performs median filter on the provided data. Padding is done by mirroring the data.

Kind: static method of utils/maths

ParamTypeDescription
dataAnyTypedArray

The input array

windowSizenumber

The window size


utils/maths.round(num, decimals) β‡’ <code> number </code>

Helper function to round a number to a given number of decimals

Kind: static method of utils/maths
Returns: number - The rounded number

ParamTypeDescription
numnumber

The number to round

decimalsnumber

The number of decimals


utils/maths.bankers_round(x) β‡’ <code> number </code>

Helper function to round a number to the nearest integer, with ties rounded to the nearest even number. Also known as β€œbankers’ rounding”. This is the default rounding mode in python. For example: 1.5 rounds to 2 and 2.5 rounds to 2.

Kind: static method of utils/maths
Returns: number - The rounded number

ParamTypeDescription
xnumber

The number to round


utils/maths~P2FFT

Implementation of Radix-4 FFT.

P2FFT class provides functionality for performing Fast Fourier Transform on arrays which are a power of two in length. Code adapted from https://www.npmjs.com/package/fft.js

Kind: inner class of utils/maths


new P2FFT(size)

Throws:

  • Error FFT size must be a power of two larger than 1.
ParamTypeDescription
sizenumber

The size of the input array. Must be a power of two larger than 1.


p2FFT.createComplexArray() β‡’ <code> Float64Array </code>

Create a complex number array with size 2 * size

Kind: instance method of P2FFT
Returns: Float64Array - A complex number array with size 2 * size


p2FFT.fromComplexArray(complex, [storage]) β‡’ <code> Array. < number > </code>

Converts a complex number representation stored in a Float64Array to an array of real numbers.

Kind: instance method of P2FFT
Returns: Array.<number> - An array of real numbers representing the input complex number representation.

ParamTypeDescription
complexFloat64Array

The complex number representation to be converted.

[storage]Array.<number>

An optional array to store the result in.


p2FFT.toComplexArray(input, [storage]) β‡’ <code> Float64Array </code>

Convert a real-valued input array to a complex-valued output array.

Kind: instance method of P2FFT
Returns: Float64Array - The complex-valued output array.

ParamTypeDescription
inputFloat64Array

The real-valued input array.

[storage]Float64Array

Optional buffer to store the output array.


p2FFT.transform(out, data) β‡’ <code> void </code>

Performs a Fast Fourier Transform (FFT) on the given input data and stores the result in the output buffer.

Kind: instance method of P2FFT
Throws:

  • Error Input and output buffers must be different.
ParamTypeDescription
outFloat64Array

The output buffer to store the result.

dataFloat64Array

The input data to transform.


p2FFT.realTransform(out, data)

Performs a real-valued forward FFT on the given input buffer and stores the result in the given output buffer. The input buffer must contain real values only, while the output buffer will contain complex values. The input and output buffers must be different.

Kind: instance method of P2FFT
Throws:

  • Error If the input and output buffers are the same.
ParamTypeDescription
outFloat64Array

The output buffer.

dataFloat64Array

The input buffer containing real values.


p2FFT.inverseTransform(out, data) β‡’ <code> void </code>

Performs an inverse FFT transformation on the given data array, and stores the result in out. The out array must be a different buffer than the data array. The out array will contain the result of the transformation. The data array will not be modified.

Kind: instance method of P2FFT
Throws:

  • Error If `out` and `data` refer to the same buffer.
ParamTypeDescription
outFloat64Array

The output buffer for the transformed data.

dataFloat64Array

The input data to transform.


p2FFT._transform4(out, data, inv) β‡’ <code> void </code>

Performs a radix-4 implementation of a discrete Fourier transform on a given set of data.

Kind: instance method of P2FFT

ParamTypeDescription
outFloat64Array

The output buffer for the transformed data.

dataFloat64Array

The input buffer of data to be transformed.

invnumber

A scaling factor to apply to the transform.


p2FFT._singleTransform2(data, out, outOff, off, step) β‡’ <code> void </code>

Performs a radix-2 implementation of a discrete Fourier transform on a given set of data.

Kind: instance method of P2FFT

ParamTypeDescription
dataFloat64Array

The input buffer of data to be transformed.

outFloat64Array

The output buffer for the transformed data.

outOffnumber

The offset at which to write the output data.

offnumber

The offset at which to begin reading the input data.

stepnumber

The step size for indexing the input data.


p2FFT._singleTransform4(data, out, outOff, off, step, inv) β‡’ <code> void </code>

Performs radix-4 transformation on input data of length 8

Kind: instance method of P2FFT

ParamTypeDescription
dataFloat64Array

Input data array of length 8

outFloat64Array

Output data array of length 8

outOffnumber

Index of output array to start writing from

offnumber

Index of input array to start reading from

stepnumber

Step size between elements in input array

invnumber

Scaling factor for inverse transform


p2FFT._realTransform4(out, data, inv)

Real input radix-4 implementation

Kind: instance method of P2FFT

ParamTypeDescription
outFloat64Array

Output array for the transformed data

dataFloat64Array

Input array of real data to be transformed

invnumber

The scale factor used to normalize the inverse transform


p2FFT._singleRealTransform2(data, out, outOff, off, step) β‡’ <code> void </code>

Performs a single real input radix-2 transformation on the provided data

Kind: instance method of P2FFT

ParamTypeDescription
dataFloat64Array

The input data array

outFloat64Array

The output data array

outOffnumber

The output offset

offnumber

The input offset

stepnumber

The step


p2FFT._singleRealTransform4(data, out, outOff, off, step, inv)

Computes a single real-valued transform using radix-4 algorithm. This method is only called for len=8.

Kind: instance method of P2FFT

ParamTypeDescription
dataFloat64Array

The input data array.

outFloat64Array

The output data array.

outOffnumber

The offset into the output array.

offnumber

The offset into the input array.

stepnumber

The step size for the input array.

invnumber

The value of inverse.


utils/maths~NP2FFT

NP2FFT class provides functionality for performing Fast Fourier Transform on arrays which are not a power of two in length. In such cases, the chirp-z transform is used.

For more information, see: https://math.stackexchange.com/questions/77118/non-power-of-2-ffts/77156#77156

Kind: inner class of utils/maths


new NP2FFT(fft_length)

Constructs a new NP2FFT object.

ParamTypeDescription
fft_lengthnumber

The length of the FFT


utils/maths~AnyTypedArray : <code> Int8Array </code> | <code> Uint8Array </code> | <code> Uint8ClampedArray </code> | <code> Int16Array </code> | <code> Uint16Array </code> | <code> Int32Array </code> | <code> Uint32Array </code> | <code> Float32Array </code> | <code> Float64Array </code>

Kind: inner typedef of utils/maths


< > Update on GitHub