Tensor Creation
Tensor Properties
- shape: readonly number[] — dimensions of the tensor (e.g., [2, 3] for a 2×3 matrix)
- dtype: DType — data type of elements (float32, float64, int32, int64, uint8, bool, string)
- device: Device — compute device ('cpu')
- data: TypedArray | readonly string[] — raw backing storage
- strides: readonly number[] — step sizes for each dimension
- offset: number — starting index into the data buffer
- size: number — total number of elements
- ndim: number — number of dimensions (shape.length)
- toArray(): NestedArray — convert to nested JavaScript arrays
- at(...indices): number | string | bigint — access a single element by coordinates
Create a tensor from a nested array, TypedArray, scalar, or other TensorLike value. Infers shape and dtype from the data. The primary way to create tensors.
data: TensorLike - Nested array, TypedArray, scalar number/boolean/string/bigintopts.dtype: DType - Override inferred dtypeopts.device: Device - Target device (default: config default)Create a tensor filled with zeros. Useful for initializing accumulators, masks, and placeholder tensors.
shape: Shape - Desired output shapeCreate a tensor filled with ones. Commonly used for bias initialization and identity-like operations.
Create a tensor filled with a constant value.
Create an uninitialized tensor. Contents are undefined — use only when you will immediately write all values.
Create an identity matrix (ones on the diagonal, zeros elsewhere). Supports rectangular matrices and shifted diagonals.
n: number - Number of rowsm: number - Number of columns (default: n)k: number - Diagonal offset (0=main, positive=above, negative=below)Create a 1D tensor with evenly spaced values in [start, stop). Similar to Python's range() but returns a Tensor.
start: number - Start value (inclusive)stop: number - End value (exclusive)step: number - Step size (default: 1)Create a 1D tensor with num evenly spaced values between start and stop (both inclusive). Useful for creating coordinate grids and evaluation points.
num: number - Number of points to generateCreate a 1D tensor with values logarithmically spaced between 10^start and 10^stop. Useful for logarithmic scales (e.g., learning rate sweeps).
Create a 1D tensor with values geometrically spaced between start and stop. Each value is a constant multiple of the previous.
Create a tensor with values from the standard normal distribution (mean=0, std=1). Uses Box-Muller transform.
import { tensor, zeros, ones, eye, arange, linspace, full, randn } from "deepbox/ndarray";// From nested arrayconst a = tensor([[1, 2, 3], [4, 5, 6]]); // shape: [2, 3], dtype: float32// With explicit dtypeconst b = tensor([1, 2, 3], { dtype: "int32" });// From TypedArrayconst c = tensor(new Float64Array([1.5, 2.5, 3.5]));// Creation functionsconst z = zeros([3, 3]); // 3×3 zerosconst o = ones([2, 2, 2]); // 2×2×2 onesconst I = eye(4); // 4×4 identityconst f = full([2, 3], 7); // 2×3 filled with 7const r = arange(0, 10, 2); // [0, 2, 4, 6, 8]const l = linspace(0, 1, 11); // [0, 0.1, 0.2, ..., 1.0]const n = randn([100, 50]); // 100×50 standard normal// Tensor propertiesconsole.log(a.shape); // [2, 3]console.log(a.dtype); // 'float32'console.log(a.size); // 6console.log(a.ndim); // 2console.log(a.at(0, 1)); // 2