Example 01
beginner
01
Tensors
Basics

Tensor Basics

Tensors are the core data structure in Deepbox. This example teaches you how to create tensors from nested JavaScript arrays, TypedArrays, and factory functions like zeros(), ones(), eye(), arange(), and linspace(). You will inspect tensor properties (shape, dtype, ndim, size), access individual elements with .at(), convert tensors back to nested arrays with .toArray(), and understand how dtype inference works.

Deepbox Modules Used

deepbox/ndarray

What You Will Learn

  • Create tensors from nested arrays, scalars, and TypedArrays
  • Use factory functions: zeros(), ones(), eye(), full(), arange(), linspace(), randn()
  • Inspect properties: shape, dtype, ndim, size
  • Access elements with .at() and convert back with .toArray()
  • Control dtype explicitly via options or let Deepbox infer it

Source Code

01-tensor-basics/index.ts
1import { arange, eye, linspace, ones, reshape, tensor, zeros } from "deepbox/ndarray";23console.log("=== Tensor Basics ===\n");45// 1. Creating tensors from JavaScript arrays6const vector = tensor([1, 2, 3, 4, 5]);7console.log("1D Tensor (vector):");8console.log(vector.toString());9console.log(`Shape: [${vector.shape}], Size: ${vector.size}\n`);1011// 2. Create a 2D tensor (matrix)12const matrix = tensor([13  [1, 2, 3],14  [4, 5, 6],15]);16console.log("2D Tensor (matrix):");17console.log(matrix.toString());18console.log(`Shape: [${matrix.shape}], Size: ${matrix.size}\n`);1920// 3. Create a 3D tensor (higher dimensional)21const tensor3d = tensor([22  [23    [1, 2],24    [3, 4],25  ],26  [27    [5, 6],28    [7, 8],29  ],30]);31console.log("3D Tensor:");32console.log(`Shape: [${tensor3d.shape}], Size: ${tensor3d.size}\n`);3334// 4. Special tensor creation functions35const zeroMatrix = zeros([3, 3]);36console.log("3x3 Zero Matrix:");37console.log(`${zeroMatrix.toString()}\n`);3839const onesMatrix = ones([2, 4]);40console.log("2x4 Ones Matrix:");41console.log(`${onesMatrix.toString()}\n`);4243// 5. Create identity matrix (diagonal of ones)44const identity = eye(4);45console.log("4x4 Identity Matrix:");46console.log(`${identity.toString()}\n`);4748// 6. Create range of values with step49const range = arange(0, 10, 2); // start, stop, step50console.log("Range [0, 10) with step 2:");51console.log(`${range.toString()}\n`);5253// 7. Create linearly spaced values54const linspaced = linspace(0, 1, 5); // start, stop, num_points55console.log("5 values linearly spaced between 0 and 1:");56console.log(`${linspaced.toString()}\n`);5758// 8. Reshape tensors to different dimensions59const flat = tensor([1, 2, 3, 4, 5, 6]);60const reshaped = reshape(flat, [2, 3]);61console.log("Reshaped [6] -> [2, 3]:");62console.log(`${reshaped.toString()}\n`);6364console.log("✓ Tensor basics complete!");

Console Output

$ npx tsx 01-tensor-basics/index.ts
=== Tensor Basics ===

1D Tensor (vector):
tensor([1, 2, 3, 4, 5], dtype=float32)
Shape: [5], Size: 5

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

3D Tensor:
Shape: [2,2,2], Size: 8

3x3 Zero Matrix:
tensor([[0, 0, 0]
       [0, 0, 0]
       [0, 0, 0]], dtype=float32)

2x4 Ones Matrix:
tensor([[1, 1, 1, 1]
       [1, 1, 1, 1]], dtype=float32)

4x4 Identity Matrix:
tensor([[1, 0, 0, 0]
       [0, 1, 0, 0]
       [0, 0, 1, 0]
       [0, 0, 0, 1]], dtype=float32)

Range [0, 10) with step 2:
tensor([0, 2, 4, 6, 8], dtype=float32)

5 values linearly spaced between 0 and 1:
tensor([0, 0.2500, 0.5000, 0.7500, 1], dtype=float32)

Reshaped [6] -> [2, 3]:
tensor([[1, 2, 3]
       [4, 5, 6]], dtype=float32)

✓ Tensor basics complete!