deepbox/nn
Module, Containers & Trainer
The Module base class, sequential containers, parameter collections, trainer orchestration, and training utilities.
Training loop
Devices & training
- Module.to(device) moves a module's parameters, gradients, and buffers to the target device. With a registered WebGPU backend the forward pass, autograd, and optimizer step all run on-device, so the training loop stays resident on the GPU.
- Unless you register and route ops through WebGPU or WASM backends, neural network training uses the default CPU execution path—do not assume .to('webgpu') accelerates training without that integration work.
type clipGradNorm
export declare const clipGradNorm: typeof clip_grad_norm_;
clipGradNorm is a public const in deepbox/nn.
type clipGradNorm_
export declare const clipGradNorm_: typeof clip_grad_norm_;
clipGradNorm_ is a public const in deepbox/nn.
type clipGradValue
export declare const clipGradValue: typeof clip_grad_value_;
clipGradValue is a public const in deepbox/nn.
type clipGradValue_
export declare const clipGradValue_: typeof clip_grad_value_;
clipGradValue_ is a public const in deepbox/nn.
type ForwardHook
export type ForwardHook = (module: Module, inputs: AnyTensor[], output: AnyTensor) => AnyTensor | undefined;
Hook function called after the forward pass.
type ForwardPreHook
export type ForwardPreHook = (module: Module, inputs: AnyTensor[]) => AnyTensor[] | undefined;
Hook function called before the forward pass.
type EpochInfo
export type EpochInfo = { readonly epoch: number; readonly trainLoss: number; readonly valLoss: number | undefined; };
Information about a completed epoch.
type LossFn
export type LossFn = (output: AnyTensor, target: Tensor) => AnyTensor;
A loss function that takes model output and targets, returning a scalar loss.
type TrainerCallback
export type TrainerCallback = (info: EpochInfo) => void;
A callback invoked at the end of each epoch.
type TrainerOptions
export type TrainerOptions = { /** Number of training epochs. Default: 10 */ readonly epochs?: number; /** Early stopping configuration. Omit to disable. */ readonly earlyStopping?: { readonly patience?: number; readonl…
Options for the .
type TrainerResult
export type TrainerResult = { readonly history: EpochInfo[]; readonly stoppedEarly: boolean; readonly bestEpoch: number | undefined; };
Result returned by .
ModuleDict
Holds submodules in a dictionary.
ModuleList
Holds submodules in a list.
ParameterDict
Holds parameters in a dictionary.
ParameterList
Holds parameters in a list.
Sequential
Sequential container for stacking layers in a linear pipeline.
Module
Base class for all neural network modules.
Trainer
High-level training loop for neural network modules.
EarlyStopping
Early stopping callback to terminate training when a monitored metric stops improving.
GradientAccumulator
Gradient accumulation helper for training with effective batch sizes larger than what fits in memory.
ModelCheckpoint
Model checkpoint helper to track and save the best model state during training.
clip_grad_norm_
export declare function clip_grad_norm_(parameters: Iterable<GradTensor>, maxNorm: number, normType?: number): number;
Clip the total norm of gradients of an iterable of parameters.
clip_grad_value_
export declare function clip_grad_value_(parameters: Iterable<GradTensor>, clipValue: number): void;
Clip the gradients of an iterable of parameters at specified value.
nn-module.ts
import { Linear, ReLU, Sequential, Trainer, crossEntropyLoss } from "deepbox/nn";import { GradTensor, sum, tensor } from "deepbox/ndarray";import { SGD } from "deepbox/optim";const model = new Sequential(new Linear(4, 8), new ReLU(), new Linear(8, 2));const trainer = new Trainer( model, new SGD(model.parameters(), { lr: 0.01 }), (output, target) => (output instanceof GradTensor ? crossEntropyLoss(output, target) : sum(output)), { epochs: 1 });const trainData = [ [tensor([[1, 0, 0, 0], [0, 1, 0, 0]]), tensor([0, 1], { dtype: "int32" })],] as const;console.log(Array.from(model.parameters()).length);console.log(trainer.fit(trainData));