deepbox/nn
Recurrent & Packed Sequences
Sequence layers and packed-sequence helpers for variable-length recurrent workloads.
Sequence modeling
type PackedSequence
export type PackedSequence = { /** Packed data tensor of shape (totalElements, features) */ readonly data: Tensor; /** Number of active sequences at each timestep */ readonly batchSizes: number[]; /** Original indices b…
A packed representation of variable-length sequences.
GRU
GRU (Gated Recurrent Unit) layer.
LSTM
LSTM (Long Short-Term Memory) layer.
RNN
Elman RNN layer with `tanh` or `relu` nonlinearity.
packPaddedSequence
export declare function packPaddedSequence(input: Tensor, lengths: readonly number[], enforcesSorted?: boolean): PackedSequence;
Pack a padded 3D tensor into a .
packSequence
export declare function packSequence(sequences: readonly Tensor[], enforcesSorted?: boolean): PackedSequence;
Pack a list of variable-length tensors into a .
padPackedSequence
export declare function padPackedSequence(packed: PackedSequence, totalLength?: number): [Tensor, number[]];
Pad a to a dense tensor.
unpackSequence
export declare function unpackSequence(packed: PackedSequence): [Tensor[], number[]];
Unpack a back to a list of tensors.
nn-recurrent.ts
import { GRU, LSTM, RNN, packSequence, unpackSequence,} from "deepbox/nn";import { tensor } from "deepbox/ndarray";const sequences = [tensor([[1], [2], [3]]), tensor([[1], [2]])];const packed = packSequence(sequences);console.log(new RNN(1, 4));console.log(new LSTM(1, 4));console.log(new GRU(1, 4));console.log(unpackSequence(packed));