stargrad
Version:
A JavaScript library for automatic gradient calculation, inspired by PyTorch
67 lines (66 loc) • 1.64 kB
TypeScript
export declare class Tensor {
private data;
private shape;
private grad;
private requiresGrad;
private operation;
private inputs;
constructor(data: number | number[] | number[][] | unknown, requiresGrad?: boolean, operation?: string | null, inputs?: Tensor[] | null);
/**
* Flattens a nested array of any dimension into a 1D array
*/
private flattenArray;
private getShapeFromArray;
/**
* Gets the element at the specified indices
*/
get(indices: number[]): number;
/**
* Sets the element at the specified indices
*/
set(indices: number[], value: number): void;
/**
* Adds two tensors element-wise
*/
add(other: Tensor): Tensor;
/**
* Multiplies two tensors element-wise
*/
mul(other: Tensor): Tensor;
/**
* Subtracts two tensors element-wise
*/
sub(other: Tensor): Tensor;
/**
* Divides two tensors element-wise
*/
div(other: Tensor): Tensor;
/**
* Raises the tensor to a power element-wise
*/
pow(exponent: number): Tensor;
/**
* Calculates the natural logarithm of the tensor element-wise
*/
log(): Tensor;
/**
* Calculates the gradient of the tensor
*/
backward(gradient?: number | number[]): void;
/**
* Returns the current gradient
*/
getGrad(): Tensor | null;
/**
* Returns the tensor data
*/
getData(): number[];
/**
* Returns the tensor shape
*/
getShape(): number[];
/**
* Returns a string representation of the tensor
*/
toString(): string;
}