Skip to main content
Tensors are the core data structure in Deepbox - N-dimensional arrays for numerical computation. This guide shows you how to create and work with tensors.

Creating Tensors

1
Create from JavaScript arrays
2
Create 1D, 2D, and higher-dimensional tensors from nested arrays:
3
import { tensor } from "deepbox/ndarray";

// 1D tensor (vector)
const vector = tensor([1, 2, 3, 4, 5]);
console.log(vector.toString());
console.log(`Shape: [${vector.shape}], Size: ${vector.size}`);

// 2D tensor (matrix)
const matrix = tensor([
  [1, 2, 3],
  [4, 5, 6],
]);
console.log(matrix.toString());
console.log(`Shape: [${matrix.shape}], Size: ${matrix.size}`);

// 3D tensor
const tensor3d = tensor([
  [
    [1, 2],
    [3, 4],
  ],
  [
    [5, 6],
    [7, 8],
  ],
]);
console.log(`Shape: [${tensor3d.shape}], Size: ${tensor3d.size}`);
4
Output:
5
1D Tensor (vector):
Tensor([1, 2, 3, 4, 5])
Shape: [5], Size: 5

2D Tensor (matrix):
Tensor([[1, 2, 3],
        [4, 5, 6]])
Shape: [2, 3], Size: 6

3D Tensor:
Shape: [2, 2, 2], Size: 8
6
Create special tensors
7
Use factory functions to create common tensor types:
8
import { zeros, ones, eye } from "deepbox/ndarray";

// Matrix of zeros
const zeroMatrix = zeros([3, 3]);
console.log("3x3 Zero Matrix:");
console.log(zeroMatrix.toString());

// Matrix of ones
const onesMatrix = ones([2, 4]);
console.log("2x4 Ones Matrix:");
console.log(onesMatrix.toString());

// Identity matrix
const identity = eye(4);
console.log("4x4 Identity Matrix:");
console.log(identity.toString());
9
Output:
10
3x3 Zero Matrix:
Tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])

2x4 Ones Matrix:
Tensor([[1, 1, 1, 1],
        [1, 1, 1, 1]])

4x4 Identity Matrix:
Tensor([[1, 0, 0, 0],
        [0, 1, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1]])
11
Generate sequences
12
Create ranges and linearly spaced values:
13
import { arange, linspace } from "deepbox/ndarray";

// Range with step size
const range = arange(0, 10, 2); // start, stop, step
console.log("Range [0, 10) with step 2:");
console.log(range.toString());

// Linearly spaced values
const linspaced = linspace(0, 1, 5); // start, stop, num_points
console.log("5 values linearly spaced between 0 and 1:");
console.log(linspaced.toString());
14
Output:
15
Range [0, 10) with step 2:
Tensor([0, 2, 4, 6, 8])

5 values linearly spaced between 0 and 1:
Tensor([0, 0.25, 0.5, 0.75, 1])
16
Reshape tensors
17
Change tensor dimensions without copying data:
18
import { reshape } from "deepbox/ndarray";

const flat = tensor([1, 2, 3, 4, 5, 6]);
const reshaped = reshape(flat, [2, 3]);
console.log("Reshaped [6] -> [2, 3]:");
console.log(reshaped.toString());
19
Output:
20
Reshaped [6] -> [2, 3]:
Tensor([[1, 2, 3],
        [4, 5, 6]])

Next Steps

Tensor Operations

Learn about arithmetic, linear algebra, and other tensor operations

DataFrames

Work with tabular data using DataFrames

Build docs developers (and LLMs) love