Skip to main content

Tensor

cols​

source

constructor​

Tensor(data: typeof NULL | Data, shape: Shape, ptr: OptionalNumber) → Tensor

source

ready​

ready() → Promise<void>

source

rows​

source

setWasmPath​

setWasmPath(path: string) → Promise<void>

source

shape​

source

Accessing Data​

array​

Retrieve a copy of the data into a new Array with same shape.

array() → number | Array1d | Array2d

const mat = tf.tensor([ [ 1, 2 ], [ 3, 4 ] ]);
const arr = mat.array();
source

buffer​

Access the raw typed array from wasm memory, useful for in-place operations.

buffer() → Float32Array

const mat = tf.tensor([1, 2, 3, 4]);
const buf = mat.buffer();
source

data​

Retrieve a copy of the data into a new buffer.

data() → Float32Array<ArrayBuffer>

const mat = tf.tensor([1, 2, 3, 4]);
const data = mat.data();
source

Arithmetic​

add​

Add a tensor or scalar, a + b. Supports broadcasting.

add(input: InputData) → Tensor

// scalar
const mat = ft.tensor([1, 2, 3, 4]).add(2);
// tensor
ft.tensor([1, 2, 3, 4]).add(mat);
source

div​

Divides a tensor or scalar element-wise, a / b. Supports broadcasting.

div(input: InputData, noNan: boolean) → Tensor

// scalar
const mat = ft.tensor([1, 2, 3, 4]).div(2);
// tensor
ft.tensor([1, 2, 3, 4]).div(mat);
source

divNoNan​

Divide a tensor or scalar element-wise, a / b. Return 0 (instead of NaN) if denominator is 0. Supports broadcasting.

divNoNan(input: InputData) → Tensor

// scalar
const mat = ft.tensor([1, 2, 3, 4]).divNoNan(2);
// tensor
ft.tensor([1, 2, 3, 4]).divNoNan(mat);
source

maximum​

Max of a and b (a > b ? a : b) element-wise. Supports broadcasting.

maximum(input: InputData) → Tensor

// scalar
const mat = ft.tensor([1, 2, 3, 4]).maximum(2);
// tensor
ft.tensor([1, 3, 4, 5]).maximum(mat);
source

minimum​

Min of a and b (a < b ? a : b) element-wise. Supports broadcasting.

minimum(input: InputData) → Tensor

// scalar
const mat = ft.tensor([1, 2, 3, 4]).minimum(2);
// tensor
ft.tensor([0, 1, 2, 3]).minimum(mat);
source

mod​

Modulo of a and b element-wise, a % b. Supports broadcasting.

mod(input: InputData) → Tensor

// scalar
const mat = ft.tensor([1, 2, 3, 4]).mod(2);
// tensor
ft.tensor([2, 4, 6, 8]).mod(mat);
source

mul​

Multiply a tensor or scalar element-wise, a * b. Supports broadcasting.

mul(input: InputData) → Tensor

// scalar
const mat = ft.tensor([1, 2, 3, 4]).mul(2);
// tensor
ft.tensor([1, 2, 3, 4]).mul(mat);
source

pow​

Power of a and b element-wise, a^b. Supports broadcasting.

pow(input: InputData) → Tensor

// scalar
const mat = ft.tensor([1, 2, 3, 4]).pow(2);
// tensor
ft.tensor([0, 1, 2, 3]).pow(mat);
source

squaredDifference​

Squared difference of a and b element-wise, (a - b) * (a - b). Supports broadcasting.

squaredDifference(input: InputData) → Tensor

// scalar
const mat = ft.tensor([1, 2, 3, 4]).squaredDifference(2);
// tensor
ft.tensor([0, 1, 2, 3]).squaredDifference(mat);
source

sub​

Subtract a tensor or scalar element-wise, a - b. Supports broadcasting.

sub(input: InputData) → Tensor

// scalar
const mat = ft.tensor([1, 2, 3, 4]).sub(2);
// tensor
ft.tensor([1, 2, 3, 4]).sub(mat);
source

Basic Math​

abs​

abs() → Tensor

source

acos​

acos() → Tensor

source

acosh​

acosh() → Tensor

source

asin​

asin() → Tensor

source

asinh​

asinh() → Tensor

source

atan​

atan() → Tensor

source

atan2​

atan2(input: InputData) → Tensor

source

atanh​

atanh() → Tensor

source

ceil​

ceil() → Tensor

source

clipByValue​

clipByValue(lower: number, upper: number) → Tensor

source

cos​

cos() → Tensor

source

cosh​

cosh() → Tensor

source

floor​

floor() → Tensor

source

square​

square() → Tensor

source

Creation​

clone​

Deep copy the current tensor.

clone() → Tensor

const mat = ft.tensor([1, 2]);
const clone = mat.clone();
source

diag​

diag() → Tensor

const mat = ft.tensor([ [ 1, 2 ], [ 3, 4 ] ]);
const diag = mat.diag();
source

eye​

eye() → Tensor

const eye = ft.eye([2, 2]);
// also can be used on an instance
const mat = ft.tensor([ [ 1, 2 ], [ 3, 4 ] ]);
const matEye = mat.eye();
source

ones​

ones() → Tensor

const ones = ft.ones([2, 2]);
// also can be used on an instance
const mat = ft.tensor([1, 2, 3, 4]);
const matOnes = mat.ones();
source

zeros​

zeros() → Tensor

const zeros = ft.zeros([2, 2]);
// also can be used on an instance
const mat = ft.tensor([1, 2, 3, 4]);
const matZeros = mat.zeros();
source

Linear Algebra​

qr​

qr() → [Tensor, Tensor]

source

Matrices​

matMul​

matMul(tensor: Tensor) → Tensor

source

norm​

norm(ord: undefined | null | "L2" | "L1" | "max", axis: OptionalNumber, keepdims: OptionalBool) → Tensor

source

transpose​

transpose() → Tensor

source

Performance / Memory​

beginScope​

Start a scope to track any instances created. Should be used with .

beginScope() → void

ft.beginScope();
const a = tf.ones([2,2]);
const b = a.add(2);
const result = b.data();
ft.endScope();
// a and b will have been freed from memory
source

delete​

Delete the instance from WASM backend to avoid OOM.

delete() → void

const mat = tf.ones([2, 2]);
// do some things ...
const result = mat.data();
mat.delete();
// accessing "mat" beyond this point is unsafe
source

endScope​

End a scope of tracked instances. Should be used with .

endScope() → void

ft.beginScope();
const a = tf.ones([2,2]);
const b = a.add(2);
const result = b.data();
ft.endScope();
// a and b will have been freed from memory
source

memory​

Returns the count of active pointers, useful for debugging memory management.

memory() → { pointers: number }

ft.memory();
source

scope​

Start a scope to track any instances created, will automatically clear out any references not returned.

scope(callback: () => unknown) → unknown

ft.scope(() => {
const a = tf.ones([2,2]);
const b = a.add(2);
const result = b.data();
return result;
});
// "a" and "b" will have been freed from memory
const result = ft.scope(() => {
const a = tf.ones([2,2]);
const b = a.add(2);
return b;
});
// only "a" will have been freed from memory
// you will need to manually delete the instance
result.delete();
source

Reduction​

all​

Returns the logical "and" of values along an axis.

all(axis: OptionalNumber, keepdims: OptionalBool) → Tensor

source

any​

Returns the logical "or" of values along an axis.

any(axis: OptionalNumber, keepdims: OptionalBool) → Tensor

source

argMax​

Returns the indices of the maximum values along an axis.

argMax(axis: OptionalNumber) → Tensor

source

argMin​

Returns the indices of the minimum values along an axis.

argMin(axis: OptionalNumber) → Tensor

source

max​

Computes the maximum of all elements across the axis.

max(axis: OptionalNumber, keepdims: OptionalBool) → Tensor

source

mean​

Computes the mean of all elements across the axis.

mean(axis: OptionalNumber, keepdims: OptionalBool) → Tensor

source

min​

Computes the minimum of all elements across the axis.

min(axis: OptionalNumber, keepdims: OptionalBool) → Tensor

source

prod​

Computes the product of all elements across the axis.

prod(axis: OptionalNumber, keepdims: OptionalBool) → Tensor

source

sum​

Computes the sum of all elements across the axis.

sum(axis: OptionalNumber, keepdims: OptionalBool) → Tensor

source

Slicing And Joining​

reverse​

reverse(axis: OptionalNumber) → Tensor

source

stack​

stack(matrices: Tensor[]) → Tensor

source

Transformations​

flatten​

flatten() → Tensor

source

pad​

pad(paddings: Array1d | Array2d, constant: number) → Tensor

source

reshape​

reshape(shapeOrRows: number | Shape, cols: number) → Tensor

source