GitHub
deepbox/ndarray

Tensor Creation

Tensor construction, dtype-aware buffers, and the core Tensor/AnyTensor abstractions.
Creation
Tensor core

Overview

  • Use tensor, zeros, ones, eye, arange, linspace, geomspace, and related factories to create dense arrays.
  • The Tensor class is the common runtime object for dense numerical data, while AnyTensor covers Tensor and GradTensor together.
  • Complex arrays, Float16/BFloat16 buffers, and TensorCreateOptions support newer numerical workflows introduced in v1.0.0.
type AnyTensor
export type AnyTensor = TensorClass | GradTensorClass;
Union type representing either a Tensor or GradTensor.
type NestedArray
export type NestedArray = number | boolean | NestedArray[];
Recursive type for nested number arrays.
type SliceRange
export type SliceRange = number | { readonly start?: number; readonly end?: number; readonly step?: number; };
A single index or a start/end/step range for tensor slicing.
type TensorCreateOptions
export type TensorCreateOptions = { readonly dtype?: DType; readonly device?: Device; };
Options for the creation function.
type TensorOptions
export type TensorOptions = { readonly dtype: DType; readonly device: Device; };
Options for tensor dtype and device configuration.
type expandDims
export declare const expandDims: typeof unsqueeze;
expandDims is a public const in deepbox/ndarray.

Complex

Complex number types and typed arrays for complex64/complex128 support.

Complex64Array

Complex64 typed array — each element is two float32 values (real, imag).

Complex128Array

Complex128 typed array — each element is two float64 values (real, imag).

BFloat16Array

Software BFloat16 (Brain Floating Point) array.

Float16Array

Software IEEE 754 half-precision floating-point array.

Tensor

Multi-dimensional array (tensor) with typed storage.

arange
export declare function arange(start: number, stop?: number, step?: number, opts?: TensorCreateOptions): Tensor;

Range.

empty
export declare function empty(shape: Shape, opts?: TensorCreateOptions): Tensor;

Fill with a scalar value.

eye
export declare function eye(n: number, m?: number, k?: number, opts?: TensorCreateOptions): Tensor;

Identity matrix.

flatten
export declare function flatten(t: Tensor): Tensor;

Flatten to 1D.

full
export declare function full(shape: Shape, value: number | string, opts?: TensorCreateOptions): Tensor;

full is exported by deepbox/ndarray.

gather
export declare function gather(t: Tensor, indices: Tensor, axis: Axis): Tensor;

Gather values along an axis specified by indices.

geomspace
export declare function geomspace(start: number, stop: number, num?: number, endpoint?: boolean, opts?: TensorCreateOptions): Tensor;

Numbers spaced evenly on a log scale (geometric progression).

linspace
export declare function linspace(start: number, stop: number, num?: number, endpoint?: boolean, opts?: TensorCreateOptions): Tensor;

Evenly spaced numbers over a specified interval.

logspace
export declare function logspace(start: number, stop: number, num?: number, base?: number, endpoint?: boolean, opts?: TensorCreateOptions): Tensor;

Numbers spaced evenly on a log scale.

ones
export declare function ones(shape: Shape, opts?: TensorCreateOptions): Tensor;

All ones.

randn
export declare function randn(shape: Shape, opts?: TensorCreateOptions): Tensor;

Return a tensor filled with random samples from a standard normal distribution.

reshape
export declare function reshape(t: Tensor, rawShape: Shape): Tensor;

Change shape (view) without copying.

slice
export declare function slice(t: Tensor, ...ranges: SliceRange[]): Tensor;

Slice a tensor.

tensor
export declare function tensor(data: NestedArray | StringNestedArray | TypedArray, opts?: TensorCreateOptions): Tensor;

tensor is exported by deepbox/ndarray.

transpose
export declare function transpose(t: Tensor, axes?: readonly number[]): Tensor;

Transpose tensor dimensions.

zeros
export declare function zeros(shape: Shape, opts?: TensorCreateOptions): Tensor;

All zeros.

squeeze
export declare function squeeze(t: Tensor, axis?: Axis | Axis[]): Tensor;

Remove single-dimensional entries from the shape.

unsqueeze
export declare function unsqueeze(t: Tensor, axis: number): Tensor;

Expand the shape by inserting a new axis.

ndarray-tensor.ts
import {  Complex,  arange,  eye,  reshape,  tensor,  zeros,} from "deepbox/ndarray";const a = tensor([[1, 2], [3, 4]]);const b = zeros([2, 2]);const c = reshape(arange(0, 6), [2, 3]);const i = eye(3);const z = new Complex(1, -2);console.log(a.shape, b.dtype, c.toString(), i.toString(), z.toString());