Transformers.js documentation

utils/maths

You are viewing main version, which requires installation from source. If you'd like regular npm install, checkout the latest stable version (v3.8.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

utils/maths

Numerical helpers — softmax, dot product, cosine similarity, and the typed-array utilities shared across the library.

Functions

softmax(arr)

Compute the softmax of an array of numbers.

Parameters

  • arr (TypedArray | number[]) — The array of numbers to compute the softmax of.

Returns: TypedArray | number[] — The softmax array.

log_softmax(arr)

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

Parameters

  • arr (TypedArray | number[]) — The input array to calculate the log_softmax function for.

Returns: TypedArray | number[] — The resulting log_softmax array.

dot(arr1, arr2)

Calculates the dot product of two arrays.

Parameters

  • arr1 (number[]) — The first array.
  • arr2 (number[]) — The second array.

Returns: number — The dot product of arr1 and arr2.

cos_sim(arr1, arr2)

Computes the cosine similarity between two arrays.

Parameters

  • arr1 (number[]) — The first array.
  • arr2 (number[]) — The second array.

Returns: number — The cosine similarity between the two arrays.

Type Definitions

TypedArray

Type: Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float16Array | Float32Array | Float64Array

BigTypedArray

Type: BigInt64Array | BigUint64Array

AnyTypedArray

Type: TypedArray | BigTypedArray

Update on GitHub