Module
Base class for all neural network modules in Deepbox. All models should subclass this class. Modules can contain other modules, allowing you to nest them in a tree structure. You can register submodules, parameters (trainable tensors), and buffers (non-trainable tensors).Constructor
Properties
training: boolean- Whether the module is in training mode (affects Dropout, BatchNorm, etc.)
Methods
forward
inputs- Input tensors (Tensor or GradTensor)
call
module(x) instead of module.forward(x).
Executes forward pre-hooks, then forward pass, then forward hooks.
train
mode- Training mode (true) or evaluation mode (false)
eval
train(false).
Returns: this (for method chaining)
parameters
recurse- Whether to include parameters of child modules (default: true)
namedParameters
prefix- Prefix for parameter namesrecurse- Whether to include parameters of child modules
modules
recurse- Whether to include nested child modules
namedModules
zeroGrad
freezeParameters
requiresGrad=false.
Parameters:
names- Array of parameter names to freeze. If undefined, freezes all parameters.recurse- Whether to include parameters from child modules
unfreezeParameters
requiresGrad=true.
Parameters:
names- Array of parameter names to unfreezerecurse- Whether to include parameters from child modules
to
device- Target device (‘cpu’, ‘webgpu’, ‘wasm’)
stateDict
loadStateDict
stateDict- State dictionary to load
Protected Methods
registerModule
registerParameter
registerBuffer
Example
Sequential
Sequential container for stacking layers in a linear pipeline. Simplifies model construction by automatically chaining layers.Constructor
layers- Variable number of Module instances to stack sequentially
InvalidParameterError- If no layers are providedDeepboxError- If a layer is undefined
Properties
length: number- Number of layers in the container
Methods
forward
inputs- Single input tensor
InvalidParameterError- If input count is invalid or a layer returns multiple outputs
getLayer
index- Zero-based index of the layer
IndexError- If index is out of bounds
Iteration
Sequential is iterable and supports the iterator protocol:Example
Building Complex Networks
See Also
- Linear Layer - Fully connected layers
- Activation Functions - ReLU, Sigmoid, etc.
- Normalization - BatchNorm, LayerNorm, Dropout