RNN Models¶
RNN-based models for time series with regularization and DenseNet variants.
RNNDropout ¶
WeightDropout ¶
Bases: Module
A module that wraps another layer in which some weights will be replaced by 0 during training.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
wrapped RNN module. |
required |
weight_p
|
float
|
weight dropout probability. |
required |
layer_names
|
str | list[str]
|
name(s) of the parameters to apply dropout to. |
'weight_hh_l0'
|
Source code in tsfast/models/rnn.py
RNN ¶
RNN(input_size: int, hidden_size: int, num_layers: int, hidden_p: float = 0.0, input_p: float = 0.0, weight_p: float = 0.0, rnn_type: str = 'gru', ret_full_hidden: bool = False, normalization: str = '', **kwargs)
Bases: Module
Multi-layer RNN with dropout and normalization, inspired by https://arxiv.org/abs/1708.02182.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
number of input features per timestep. |
required |
hidden_size
|
int
|
number of hidden units per layer. |
required |
num_layers
|
int
|
number of stacked RNN layers. |
required |
hidden_p
|
float
|
dropout probability applied between hidden layers. |
0.0
|
input_p
|
float
|
dropout probability applied to the input. |
0.0
|
weight_p
|
float
|
weight dropout probability applied within each RNN cell. |
0.0
|
rnn_type
|
str
|
recurrent cell type, one of |
'gru'
|
ret_full_hidden
|
bool
|
if True, return stacked hidden outputs from all layers. |
False
|
normalization
|
str
|
normalization between layers ( |
''
|
**kwargs
|
additional keyword arguments forwarded to the underlying |
{}
|
Source code in tsfast/models/rnn.py
unflatten_state ¶
Convert [batch, state_size] flat tensor to per-layer hidden state list.
Returns:
| Type | Description |
|---|---|
list
|
For GRU/RNN: |
list
|
For LSTM: |
Source code in tsfast/models/rnn.py
unflatten_sequence ¶
Convert [batch, seq_len, state_size] to [num_layers, batch, seq_len, hidden_size].
For LSTM, extracts only h-states (matching ret_full_hidden output format).
Source code in tsfast/models/rnn.py
Sequential_RNN ¶
Sequential_RNN(input_size: int, hidden_size: int, num_layers: int, hidden_p: float = 0.0, input_p: float = 0.0, weight_p: float = 0.0, rnn_type: str = 'gru', ret_full_hidden: bool = False, normalization: str = '', **kwargs)
Bases: RNN
RNN variant that returns only the output tensor, discarding hidden state.
Source code in tsfast/models/rnn.py
SimpleRNN ¶
SimpleRNN(input_size: int, output_size: int, num_layers: int = 1, hidden_size: int = 100, linear_layers: int = 0, return_state: bool = False, **kwargs)
Bases: Module
Simple RNN with a linear output head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
number of input features per timestep. |
required |
output_size
|
int
|
number of output features per timestep. |
required |
num_layers
|
int
|
number of stacked RNN layers. |
1
|
hidden_size
|
int
|
number of hidden units per RNN layer. |
100
|
linear_layers
|
int
|
number of hidden linear layers in the output head. |
0
|
return_state
|
bool
|
if True, return |
False
|
**kwargs
|
additional keyword arguments forwarded to |
{}
|
Source code in tsfast/models/rnn.py
ResidualBlock_RNN ¶
Bases: Module
Two-layer RNN block with a residual skip connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
number of input features per timestep. |
required |
hidden_size
|
int
|
number of hidden units in each RNN layer. |
required |
**kwargs
|
additional keyword arguments forwarded to |
{}
|
Source code in tsfast/models/rnn.py
SimpleResidualRNN ¶
SimpleResidualRNN(input_size: int, output_size: int, num_blocks: int = 1, hidden_size: int = 100, **kwargs)
Bases: Sequential
Sequential stack of residual RNN blocks with a linear output head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
number of input features per timestep. |
required |
output_size
|
int
|
number of output features per timestep. |
required |
num_blocks
|
int
|
number of stacked residual RNN blocks. |
1
|
hidden_size
|
int
|
number of hidden units per block. |
100
|
**kwargs
|
additional keyword arguments forwarded to |
{}
|
Source code in tsfast/models/rnn.py
DenseLayer_RNN ¶
Bases: Module
Two-layer RNN that concatenates its output with the input (DenseNet-style).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
number of input features per timestep. |
required |
hidden_size
|
int
|
growth rate (number of new features produced). |
required |
**kwargs
|
additional keyword arguments forwarded to |
{}
|
Source code in tsfast/models/rnn.py
DenseBlock_RNN ¶
Bases: Sequential
Sequential block of DenseNet-style RNN layers with feature concatenation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_layers
|
int
|
number of dense RNN layers in this block. |
required |
num_input_features
|
int
|
number of input features entering the block. |
required |
growth_rate
|
int
|
number of new features each dense layer adds. |
required |
**kwargs
|
additional keyword arguments forwarded to |
{}
|
Source code in tsfast/models/rnn.py
DenseNet_RNN ¶
DenseNet_RNN(input_size: int, output_size: int, growth_rate: int = 32, block_config: tuple = (3, 3), num_init_features: int = 32, **kwargs)
Bases: Sequential
DenseNet architecture using RNN layers with transition layers between blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
number of input features per timestep. |
required |
output_size
|
int
|
number of output features per timestep. |
required |
growth_rate
|
int
|
number of new features each dense layer adds. |
32
|
block_config
|
tuple
|
tuple specifying the number of layers in each dense block. |
(3, 3)
|
num_init_features
|
int
|
number of features produced by the initial RNN layer. |
32
|
**kwargs
|
additional keyword arguments forwarded to |
{}
|
Source code in tsfast/models/rnn.py
SeperateRNN ¶
SeperateRNN(input_list: list[list[int]], output_size: int, num_layers: int = 1, hidden_size: int = 100, linear_layers: int = 1, **kwargs)
Bases: Module
RNN that processes input channel groups separately before merging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_list
|
list[list[int]]
|
list of index lists, each defining a group of input channels. |
required |
output_size
|
int
|
number of output features per timestep. |
required |
num_layers
|
int
|
number of stacked RNN layers in the merging RNN. |
1
|
hidden_size
|
int
|
total hidden size (split evenly across per-group RNNs). |
100
|
linear_layers
|
int
|
number of hidden linear layers in the output head. |
1
|
**kwargs
|
additional keyword arguments forwarded to |
{}
|
Source code in tsfast/models/rnn.py
RNNLearner ¶
RNNLearner(dls, loss_func=nn.L1Loss(), metrics: list | None = None, n_skip: int = 0, num_layers: int = 1, hidden_size: int = 100, sub_seq_len: int | None = None, opt_func=torch.optim.Adam, input_norm: type | None = StandardScaler, output_norm: type | None = None, augmentations: list | None = None, transforms: list | None = None, aux_losses: list | None = None, grad_clip: float | None = None, cuda_graph: bool = False, **kwargs)
Create a Learner with a SimpleRNN model and standard training setup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dls
|
DataLoaders providing training and validation data. |
required | |
loss_func
|
loss function for training. |
L1Loss()
|
|
metrics
|
list | None
|
list of metric functions evaluated during validation. |
None
|
n_skip
|
int
|
number of initial timesteps to skip in loss and metric computation. |
0
|
num_layers
|
int
|
number of stacked RNN layers. |
1
|
hidden_size
|
int
|
number of hidden units per RNN layer. |
100
|
sub_seq_len
|
int | None
|
sub-sequence length for TBPTT; enables stateful training when set. |
None
|
opt_func
|
optimizer constructor. |
Adam
|
|
input_norm
|
type | None
|
scaler class for input normalization, or None to disable. |
StandardScaler
|
output_norm
|
type | None
|
scaler class for output denormalization, or None to disable. |
None
|
augmentations
|
list | None
|
list of augmentation transforms (train only). |
None
|
transforms
|
list | None
|
list of transforms (train + valid). |
None
|
aux_losses
|
list | None
|
list of auxiliary loss functions. |
None
|
grad_clip
|
float | None
|
max gradient norm for clipping, or None to disable. |
None
|
cuda_graph
|
bool
|
if True and sub_seq_len is set, use CudaGraphTbpttLearner for faster training. |
False
|
**kwargs
|
additional keyword arguments forwarded to |
{}
|
Source code in tsfast/models/rnn.py
AR_RNNLearner ¶
AR_RNNLearner(dls, alpha: float = 0, beta: float = 0, metrics: list | None = None, n_skip: int = 0, opt_func=torch.optim.Adam, input_norm: type | None = StandardScaler, **kwargs)
Create a Learner with an autoregressive RNN model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dls
|
DataLoaders providing training and validation data. |
required | |
alpha
|
float
|
activation regularization penalty weight. |
0
|
beta
|
float
|
temporal activation regularization penalty weight. |
0
|
metrics
|
list | None
|
metric functions for validation, or None for default RMSE. |
None
|
n_skip
|
int
|
number of initial timesteps to skip in metric computation. |
0
|
opt_func
|
optimizer constructor. |
Adam
|
|
input_norm
|
type | None
|
scaler class for input normalization, or None to disable. |
StandardScaler
|
**kwargs
|
additional keyword arguments forwarded to |
{}
|