deepbox/nn
Recurrent (RNN/LSTM/GRU)
Recurrent layers for processing sequential data. Maintain hidden state across time steps.
RNN
extends Module
Vanilla recurrent neural network. Applies hₜ = tanh(Wₓₕx + Wₕₕhₜ₋₁ + b) at each time step. Simple but prone to vanishing/exploding gradients for long sequences.
LSTM
extends Module
Long Short-Term Memory. Uses input, forget, and output gates to control information flow. Effective for long sequences. The standard choice for most sequential tasks.
GRU
extends Module
Gated Recurrent Unit. Simplified version of LSTM with reset and update gates. Fewer parameters than LSTM with comparable performance.
Constructor Parameters
- inputSize: Number of features in the input
- hiddenSize: Number of features in the hidden state
- numLayers: Number of stacked recurrent layers (default: 1)
- bidirectional: If true, becomes a bidirectional RNN (default: false)
- dropout: Dropout rate between layers (default: 0)
recurrent.ts
import { LSTM, GRU, RNN } from "deepbox/nn";import { tensor } from "deepbox/ndarray";// LSTM for sequence processingconst lstm = new LSTM(10, 20, { numLayers: 2 });// Input: (batch, seqLen, inputSize)const input = tensor([[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]]); // [1, 1, 10]const output = lstm.forward(input); // Returns output Tensor only// Use forwardWithState to also get hidden/cell stateconst [seqOut, [h, c]] = lstm.forwardWithState(input);// GRU (fewer parameters than LSTM)const gru = new GRU(10, 20);const gruOut = gru.forward(input); // Returns output Tensor only