Tensor
cols
sourceconstructor
Tensor(data: typeof NULL | Data, shape: Shape, ptr: OptionalNumber)
→ Tensor
ready
ready()
→ Promise<void>
rows
sourcesetWasmPath
setWasmPath(path: string)
→ Promise<void>
shape
sourceAccessing 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();
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();
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();
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
Basic Math
abs
abs()
→ Tensor
acos
acos()
→ Tensor
acosh
acosh()
→ Tensor
asin
asin()
→ Tensor
asinh
asinh()
→ Tensor
atan
atan()
→ Tensor
atan2
atan2(input: InputData)
→ Tensor
atanh
atanh()
→ Tensor
ceil
ceil()
→ Tensor
clipByValue
clipByValue(lower: number, upper: number)
→ Tensor
cos
cos()
→ Tensor
cosh
cosh()
→ Tensor
floor
floor()
→ Tensor
square
square()
→ Tensor
Creation
clone
Deep copy the current tensor.
clone()
→ Tensor
const mat = ft.tensor([1, 2]);
const clone = mat.clone();
diag
diag()
→ Tensor
const mat = ft.tensor([ [ 1, 2 ], [ 3, 4 ] ]);
const diag = mat.diag();
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();
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();
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();
Linear Algebra
qr
qr()
→ [Tensor, Tensor]
Matrices
matMul
matMul(tensor: Tensor)
→ Tensor
norm
norm(ord: undefined | null | "L2" | "L1" | "max", axis: OptionalNumber, keepdims: OptionalBool)
→ Tensor
transpose
transpose()
→ Tensor
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
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
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
memory
Returns the count of active pointers, useful for debugging memory management.
memory()
→ { pointers: number }
ft.memory();
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();
Reduction
all
Returns the logical "and" of values along an axis.
all(axis: OptionalNumber, keepdims: OptionalBool)
→ Tensor
any
Returns the logical "or" of values along an axis.
any(axis: OptionalNumber, keepdims: OptionalBool)
→ Tensor
argMax
Returns the indices of the maximum values along an axis.
argMax(axis: OptionalNumber)
→ Tensor
argMin
Returns the indices of the minimum values along an axis.
argMin(axis: OptionalNumber)
→ Tensor
max
Computes the maximum of all elements across the axis.
max(axis: OptionalNumber, keepdims: OptionalBool)
→ Tensor
mean
Computes the mean of all elements across the axis.
mean(axis: OptionalNumber, keepdims: OptionalBool)
→ Tensor
min
Computes the minimum of all elements across the axis.
min(axis: OptionalNumber, keepdims: OptionalBool)
→ Tensor
prod
Computes the product of all elements across the axis.
prod(axis: OptionalNumber, keepdims: OptionalBool)
→ Tensor
sum
Computes the sum of all elements across the axis.
sum(axis: OptionalNumber, keepdims: OptionalBool)
→ Tensor
Slicing And Joining
reverse
reverse(axis: OptionalNumber)
→ Tensor
stack
stack(matrices: Tensor[])
→ Tensor
Transformations
flatten
flatten()
→ Tensor
pad
pad(paddings: Array1d | Array2d, constant: number)
→ Tensor
reshape
reshape(shapeOrRows: number | Shape, cols: number)
→ Tensor