/** * Dtype strings supported by TyTorch */ export type Dtype = 'float32' | 'float64' | 'int32' | 'int64' | 'int16' | 'int8' | 'uint8' | 'bool' | 'f32' | 'f64' | 'i32' | 'i64' | 'i16' | 'i8' | 'u8' | 'float' | 'double' | 'int' | 'long'; /** * Device strings supported by TyTorch */ export type Device = 'cpu' | 'cuda' | 'mps' | `cuda:${number}` | `mps:${number}`; /** * Options for tensor creation */ export interface TensorOptions { dtype?: Dtype; device?: Device; requires_grad?: boolean; } /** * Tensor class - wraps the native tensor with TypeScript */ export declare class Tensor { private _native; constructor(data?: number[] | any, options?: TensorOptions); /** * Add a tensor or scalar */ add(other: Tensor | number): Tensor; /** * Subtract a tensor or scalar */ sub(other: Tensor | number): Tensor; /** * Multiply by a tensor or scalar (element-wise) */ mul(other: Tensor | number): Tensor; /** * Divide by a tensor or scalar (element-wise) */ div(other: Tensor | number): Tensor; /** * Add a tensor or scalar in-place */ add_(other: Tensor | number): this; /** * Subtract a tensor or scalar in-place */ sub_(other: Tensor | number): this; /** * Multiply by a tensor or scalar in-place (element-wise) */ mul_(other: Tensor | number): this; /** * Divide by a tensor or scalar in-place (element-wise) */ div_(other: Tensor | number): this; /** * Matrix multiplication */ matmul(other: Tensor): Tensor; /** * Sum all elements in the tensor */ sum(): Tensor; /** * Mean of all elements in the tensor */ mean(): Tensor; /** * Convert tensor to another device or dtype * @example * ```typescript * tensor.to('cuda') * tensor.to({ device: 'mps', dtype: 'float64' }) * tensor.to('cuda', 'float32') * ``` */ to(device: Device): Tensor; to(options: TensorOptions): Tensor; to(device: Device, dtype: Dtype): Tensor; /** * Move tensor to CPU */ cpu(): Tensor; /** * Move tensor to CUDA */ cuda(): Tensor; /** * Move tensor to MPS (Apple Silicon GPU) */ mps(): Tensor; /** * Convert to float32 */ float(): Tensor; /** * Convert to float64 */ double(): Tensor; /** * Convert to int32 */ int(): Tensor; /** * Convert to int64 */ long(): Tensor; /** * Get the shape of the tensor */ get shape(): number[]; /** * Get the data type of the tensor */ get dtype(): string; /** * Get the device the tensor is on */ get device(): string; /** * Reshape tensor to new shape * @param shape - Array of integers specifying the new shape * @returns New tensor with specified shape * @example * const a = torch.ones([6]); * const b = a.reshape([2, 3]); // Shape: [2, 3] */ reshape(shape: number[]): Tensor; /** * Flatten tensor to 1D or flatten specified dimensions * @param start_dim - First dimension to flatten (default: 0) * @param end_dim - Last dimension to flatten (default: -1, meaning last dimension) * @returns Flattened tensor * @example * const a = torch.ones([2, 3, 4]); * const b = a.flatten(); // Shape: [24] * const c = a.flatten(1); // Shape: [2, 12] (flatten from dim 1 onwards) */ flatten(start_dim?: number, end_dim?: number): Tensor; /** * Add a dimension of size 1 at the specified position * @param dim - Position where to add the dimension (can be negative) * @returns Tensor with added dimension * @example * const a = torch.tensor([1, 2, 3]); // Shape: [3] * const b = a.unsqueeze(0); // Shape: [1, 3] (add batch dimension) * const c = a.unsqueeze(1); // Shape: [3, 1] (add column dimension) * const d = a.unsqueeze(-1); // Shape: [3, 1] (negative indexing) */ unsqueeze(dim: number): Tensor; /** * Remove dimensions of size 1 from the tensor * @param dim - Optional dimension to squeeze. If not provided, removes all dimensions of size 1 * @returns Tensor with singleton dimensions removed * @example * const a = torch.ones([1, 3, 1, 4]); // Shape: [1, 3, 1, 4] * const b = a.squeeze(); // Shape: [3, 4] (remove all singleton dimensions) * const c = a.squeeze(0); // Shape: [3, 1, 4] (remove only dimension 0) * const d = a.squeeze(2); // Shape: [1, 3, 4] (remove only dimension 2) */ squeeze(dim?: number): Tensor; /** * Swap two dimensions of the tensor * @param dim0 - First dimension to swap * @param dim1 - Second dimension to swap * @returns Tensor with swapped dimensions * @example * const a = torch.ones([2, 3, 4]); // Shape: [2, 3, 4] * const b = a.transpose(0, 1); // Shape: [3, 2, 4] (swap dims 0 and 1) * const c = a.transpose(1, 2); // Shape: [2, 4, 3] (swap dims 1 and 2) * const d = a.transpose(-1, -2); // Shape: [2, 4, 3] (negative indexing) */ transpose(dim0: number, dim1: number): Tensor; /** * Reorder dimensions of the tensor * @param dims - Array of dimension indices in the desired order * @returns Tensor with reordered dimensions * @example * const a = torch.ones([2, 3, 4]); // Shape: [2, 3, 4] * const b = a.permute([2, 0, 1]); // Shape: [4, 2, 3] (reorder to [dim2, dim0, dim1]) * const c = a.permute([0, 2, 1]); // Shape: [2, 4, 3] (swap last two dims) * const d = torch.ones([8, 10, 3, 256]); // Batch, sequence, heads, features * const e = d.permute([0, 2, 1, 3]); // [8, 3, 10, 256] (rearrange for attention) */ permute(dims: number[]): Tensor; /** * Get whether this tensor requires gradient computation * @returns true if this tensor tracks gradients, false otherwise * @example * const t = torch.tensor([1, 2, 3]); * console.log(t.requires_grad); // false * * const t2 = torch.tensor([1, 2, 3], { requires_grad: true }); * console.log(t2.requires_grad); // true */ get requires_grad(): boolean; /** * Set whether this tensor requires gradient computation * This modifies the tensor in-place * @param value - true to enable gradient tracking, false to disable * @example * const t = torch.tensor([1, 2, 3]); * t.requires_grad = true; * console.log(t.requires_grad); // true */ set requires_grad(value: boolean); /** * Compute gradients via backpropagation * For scalar tensors, call without arguments. * For non-scalar tensors, pass a gradient tensor of the same shape. * @param gradient - Optional gradient tensor for non-scalar backward * @example * const x = torch.tensor([2.0, 3.0], { requires_grad: true }); * const y = x.mul(x); * const z = y.sum(); * z.backward(); * console.log(x.grad.toArray()); // [4.0, 6.0] */ backward(gradient?: Tensor): void; /** * Get the gradient tensor computed by backward() * Returns null if no gradient has been computed * @returns Gradient tensor or null * @example * const x = torch.tensor([2.0, 3.0], { requires_grad: true }); * console.log(x.grad); // null * const z = x.sum(); * z.backward(); * console.log(x.grad.toArray()); // [1.0, 1.0] */ get grad(): Tensor | null; /** * Clear the gradient by setting it to None * This is typically called before computing new gradients in training loops * @example * const x = torch.tensor([1.0, 2.0], { requires_grad: true }); * const y = x.mul(2); * y.sum().backward(); * console.log(x.grad); // Tensor with gradients * x.zeroGrad(); * console.log(x.grad); // null */ zeroGrad(): void; /** * Detach tensor from the computation graph * Returns a new tensor that shares storage with the original but doesn't track gradients * @returns Detached tensor * @example * const x = torch.tensor([1.0, 2.0], { requires_grad: true }); * const y = x.mul(2); * const z = y.detach(); // z shares data with y but doesn't track gradients * console.log(z.requires_grad); // false */ detach(): Tensor; /** * Apply ReLU (Rectified Linear Unit) activation function * Returns max(0, x) element-wise * @returns New tensor with ReLU applied * @example * const x = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0]); * const y = x.relu(); * console.log(y.toArray()); // [0.0, 0.0, 0.0, 1.0, 2.0] */ relu(): Tensor; /** * Apply sigmoid activation function * Returns σ(x) = 1 / (1 + exp(-x)) element-wise * @returns New tensor with sigmoid applied * @example * const x = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0]); * const y = x.sigmoid(); * console.log(y.toArray()); // [0.119, 0.269, 0.5, 0.731, 0.881] */ sigmoid(): Tensor; /** * Apply hyperbolic tangent (tanh) activation function * Returns tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x)) element-wise * Outputs values in range (-1, 1) * @returns New tensor with tanh applied * @example * const x = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0]); * const y = x.tanh(); * console.log(y.toArray()); // [-0.964, -0.762, 0.0, 0.762, 0.964] */ tanh(): Tensor; /** * Apply softmax activation function along a dimension * Normalizes values to a probability distribution that sums to 1 * Formula: softmax(x_i) = exp(x_i) / sum(exp(x_j)) for all j in dimension * Commonly used for multi-class classification output layers * @param dim Dimension along which to apply softmax (default: -1, last dimension) * @returns New tensor with softmax applied * @example * // Classification logits for 2 samples, 3 classes * const logits = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]); * const probs = logits.softmax(); // Default dim=-1 (across classes) * console.log(probs.toArray()); * // [[0.09, 0.24, 0.67], [0.09, 0.24, 0.67]] * // Each row sums to 1.0 * * @example * // Apply across different dimension * const probs_dim0 = logits.softmax(0); // Across batch dimension */ softmax(dim?: number): Tensor; /** * Apply log-softmax activation function along a dimension * Computes log(softmax(x)) in a numerically stable way * Formula: log_softmax(x_i) = x_i - log(sum(exp(x_j))) for all j in dimension * This is more numerically stable than computing log(softmax(x)) separately * Commonly used with NLL loss for classification * @param dim Dimension along which to apply log_softmax (default: -1, last dimension) * @returns New tensor with log_softmax applied * @example * // Classification logits for 2 samples, 3 classes * const logits = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]); * const log_probs = logits.log_softmax(); // Default dim=-1 (across classes) * console.log(log_probs.toArray()); * // [[-2.41, -1.41, -0.41], [-2.41, -1.41, -0.41]] * // exp(log_probs) gives the same result as softmax * * @example * // More numerically stable than log(softmax(x)) * const stable = logits.log_softmax(); * const unstable = logits.softmax().log(); // Can lose precision */ log_softmax(dim?: number): Tensor; /** * Compute Mean Squared Error (MSE) loss between predictions and targets * Formula: loss = mean((input - target)^2) * Commonly used for regression tasks * @param target Target tensor with same shape as input * @param reduction Reduction mode: "mean" (default), "sum", or "none" * @returns Loss tensor (scalar if reduction is "mean" or "sum", tensor if "none") * @example * // Regression example * const predictions = torch.tensor([2.5, 0.0, 2.0, 8.0]); * const targets = torch.tensor([3.0, -0.5, 2.0, 7.0]); * const loss = predictions.mse_loss(targets); * console.log(loss.toArray()); // [0.375] (mean of squared errors) * * @example * // With different reductions * const loss_mean = predictions.mse_loss(targets, "mean"); // Scalar: average loss * const loss_sum = predictions.mse_loss(targets, "sum"); // Scalar: sum of losses * const loss_none = predictions.mse_loss(targets, "none"); // Tensor: per-element losses */ mse_loss(target: Tensor, reduction?: "mean" | "sum" | "none"): Tensor; /** * Compute Cross Entropy loss for classification tasks * Formula: -sum(target * log(softmax(input))) * Commonly used for multi-class classification * @param target Target tensor with class indices (long dtype) or probabilities * @param weight Optional weight tensor for each class (1D tensor with size equal to number of classes) * @param reduction Reduction mode: "mean" (default), "sum", or "none" * @param ignore_index Optional class index to ignore in loss computation (default: -100) * @returns Loss tensor (scalar if reduction is "mean" or "sum", tensor if "none") * @example * // Classification example with class indices * const logits = torch.tensor([[2.0, 1.0, 0.1], [0.5, 2.5, 0.3]]); // 2 samples, 3 classes * const targets = torch.tensor([0, 1], { dtype: 'long' }); // Class indices * const loss = logits.cross_entropy(targets); * console.log(loss.toArray()); // Scalar loss value * * // With class weights * const weights = torch.tensor([1.0, 2.0, 1.0]); // Weight class 1 more heavily * const loss_weighted = logits.cross_entropy(targets, weights); * * // With ignore_index * const targets_with_ignore = torch.tensor([0, -100], { dtype: 'long' }); // Ignore second sample * const loss_ignore = logits.cross_entropy(targets_with_ignore, null, "mean", -100); */ cross_entropy(target: Tensor, weight?: Tensor | null, reduction?: "mean" | "sum" | "none", ignore_index?: number): Tensor; /** * Compute Negative Log-Likelihood loss * Formula: -sum(target * log_probs) * Used for classification when input is already log-probabilities (e.g., after log_softmax) * @param target Target tensor with class indices (long dtype) * @param weight Optional weight tensor for each class (1D tensor with size equal to number of classes) * @param reduction Reduction mode: "mean" (default), "sum", or "none" * @param ignore_index Optional class index to ignore in loss computation (default: -100) * @returns Loss tensor (scalar if reduction is "mean" or "sum", tensor if "none") * @example * // NLL loss with log-probabilities * const logits = torch.tensor([[2.0, 1.0, 0.1], [0.5, 2.5, 0.3]]); * const log_probs = logits.log_softmax(1); // Apply log_softmax first * const targets = torch.tensor([0, 1], { dtype: 'long' }); * const loss = log_probs.nll_loss(targets); * console.log(loss.toArray()); // Scalar loss value * * // With class weights * const weights = torch.tensor([1.0, 2.0, 1.0]); // Weight class 1 more heavily * const loss_weighted = log_probs.nll_loss(targets, weights); * * // With ignore_index * const targets_with_ignore = torch.tensor([0, -100], { dtype: 'long' }); // Ignore second sample * const loss_ignore = log_probs.nll_loss(targets_with_ignore, null, "mean", -100); */ nll_loss(target: Tensor, weight?: Tensor | null, reduction?: "mean" | "sum" | "none", ignore_index?: number): Tensor; /** * Compute Binary Cross Entropy loss for binary classification * Formula: -[y * log(x) + (1 - y) * log(1 - x)] * Used for binary classification tasks where input is probabilities (after sigmoid) * @param target Target tensor with binary labels (0 or 1) * @param weight Optional weight tensor for each element * @param reduction Reduction mode: "mean" (default), "sum", or "none" * @returns Loss tensor (scalar if reduction is "mean" or "sum", tensor if "none") * @example * // Binary classification with sigmoid outputs * const logits = torch.tensor([0.5, 2.0, -1.0]); * const probs = logits.sigmoid(); // Convert to probabilities * const targets = torch.tensor([1.0, 1.0, 0.0]); * const loss = probs.binary_cross_entropy(targets); * console.log(loss.toArray()); // Scalar loss value * * // With weights * const weights = torch.tensor([1.0, 2.0, 1.0]); // Weight second sample more * const loss_weighted = probs.binary_cross_entropy(targets, weights); * * // With reduction none (per-element losses) * const losses = probs.binary_cross_entropy(targets, null, "none"); * console.log(losses.toArray()); // [loss1, loss2, loss3] */ binary_cross_entropy(target: Tensor, weight?: Tensor | null, reduction?: "mean" | "sum" | "none"): Tensor; /** * Convert tensor to JavaScript array */ toArray(): number[]; /** * String representation of the tensor */ toString(): string; /** * @internal * Expose native handle for internal use */ get _nativeHandle(): any; } /** * TyTorch namespace - main API */ export declare namespace torch { /** * Create a tensor of zeros */ function zeros(shape: number[], options?: TensorOptions): Tensor; /** * Create a tensor of ones */ function ones(shape: number[], options?: TensorOptions): Tensor; /** * Create a tensor with random values from normal distribution */ function randn(shape: number[], options?: TensorOptions): Tensor; /** * Add two tensors (functional form) */ function add(a: Tensor, b: Tensor | number): Tensor; /** * Subtract two tensors (functional form) */ function sub(a: Tensor, b: Tensor | number): Tensor; /** * Multiply two tensors (functional form) */ function mul(a: Tensor, b: Tensor | number): Tensor; /** * Divide two tensors (functional form) */ function div(a: Tensor, b: Tensor | number): Tensor; /** * Matrix multiplication (functional form) */ function matmul(a: Tensor, b: Tensor): Tensor; /** * Create tensor from array */ function tensor(data: number[], options?: TensorOptions): Tensor; /** * Execute a callback function with gradient tracking disabled * This is useful for inference and evaluation where gradients are not needed * @param callback Function to execute without gradient tracking * @example * const x = torch.tensor([1.0, 2.0, 3.0], { requires_grad: true }); * const y = x.mul(2); // y.requires_grad = true * * torch.noGrad(() => { * const z = x.mul(3); // z.requires_grad = false * console.log(z.requires_grad); // false * }); * * const w = x.mul(4); // w.requires_grad = true (back to normal) */ function noGrad(callback: () => void): void; } export default torch;