CNN / TCN Models¶
Convolutional and temporal convolutional network architectures for time series.
CNN ¶
CNN(input_size: int, output_size: int, hl_depth: int = 1, hl_width: int = 10, act: type[Module] = Mish, bn: bool = False)
Bases: Module
Simple stacked 1D CNN with a pointwise output projection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
Number of input channels. |
required |
output_size
|
int
|
Number of output channels. |
required |
hl_depth
|
int
|
Number of hidden convolutional layers. |
1
|
hl_width
|
int
|
Number of channels in each hidden layer. |
10
|
act
|
type[Module]
|
Activation function class. |
Mish
|
bn
|
bool
|
Whether to apply batch normalization. |
False
|
Source code in tsfast/models/cnn.py
CausalConv1d ¶
CausalConv1d(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, dilation: int = 1, groups: int = 1, bias: bool = True)
Bases: Conv1d
Causal 1D convolution that pads only on the left to prevent future leakage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Number of input channels. |
required |
out_channels
|
int
|
Number of output channels. |
required |
kernel_size
|
int
|
Size of the convolving kernel. |
required |
stride
|
int
|
Stride of the convolution. |
1
|
dilation
|
int
|
Dilation factor for the kernel. |
1
|
groups
|
int
|
Number of blocked connections from input to output channels. |
1
|
bias
|
bool
|
Whether to add a learnable bias. |
True
|
Source code in tsfast/models/cnn.py
TCN_Block ¶
TCN_Block(input_size: int, output_size: int, num_layers: int = 1, activation: type[Module] | None = Mish, wn: bool = True, bn: bool = False, **kwargs)
Bases: Module
Single TCN residual block with stacked causal convolutions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
Number of input channels. |
required |
output_size
|
int
|
Number of output channels. |
required |
num_layers
|
int
|
Number of causal convolution layers in the block. |
1
|
activation
|
type[Module] | None
|
Activation function class, or None to disable. |
Mish
|
wn
|
bool
|
Whether to apply weight normalization. |
True
|
bn
|
bool
|
Whether to apply batch normalization. |
False
|
**kwargs
|
Additional arguments passed to |
{}
|
Source code in tsfast/models/cnn.py
TCN ¶
TCN(input_size: int, output_size: int, hl_depth: int = 1, hl_width: int = 10, act: type[Module] = Mish, bn: bool = False)
Bases: Module
Temporal Convolutional Network with exponentially increasing dilation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
Number of input channels. |
required |
output_size
|
int
|
Number of output channels. |
required |
hl_depth
|
int
|
Number of TCN blocks (each with doubled dilation). |
1
|
hl_width
|
int
|
Number of channels in each hidden TCN block. |
10
|
act
|
type[Module]
|
Activation function class. |
Mish
|
bn
|
bool
|
Whether to apply batch normalization. |
False
|
Source code in tsfast/models/cnn.py
SeperateTCN ¶
SeperateTCN(input_list: list[int], output_size: int, hl_depth: int = 1, hl_width: int = 10, act: type[Module] = Mish, bn: bool = False, final_layer: int = 3)
Bases: Module
TCN with separate convolutional branches per input group, merged by a linear head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_list
|
list[int]
|
List of channel counts, one per input group. |
required |
output_size
|
int
|
Number of output channels. |
required |
hl_depth
|
int
|
Number of TCN blocks per branch. |
1
|
hl_width
|
int
|
Total hidden width split evenly across branches. |
10
|
act
|
type[Module]
|
Activation function class. |
Mish
|
bn
|
bool
|
Whether to apply batch normalization. |
False
|
final_layer
|
int
|
Number of hidden layers in the final linear head. |
3
|
Source code in tsfast/models/cnn.py
CRNN ¶
CRNN(input_size: int, output_size: int, num_ft: int = 10, num_cnn_layers: int = 4, num_rnn_layers: int = 2, hs_cnn: int = 10, hs_rnn: int = 10, hidden_p: float = 0, input_p: float = 0, weight_p: float = 0, rnn_type: str = 'gru')
Bases: Module
Convolutional-Recurrent Neural Network combining a TCN front-end with an RNN back-end.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
Number of input channels. |
required |
output_size
|
int
|
Number of output channels. |
required |
num_ft
|
int
|
Number of intermediate features between the CNN and RNN stages. |
10
|
num_cnn_layers
|
int
|
Number of TCN blocks in the convolutional stage. |
4
|
num_rnn_layers
|
int
|
Number of stacked RNN layers. |
2
|
hs_cnn
|
int
|
Hidden channel width of the TCN stage. |
10
|
hs_rnn
|
int
|
Hidden size of the RNN stage. |
10
|
hidden_p
|
float
|
Dropout probability on RNN hidden-to-hidden connections. |
0
|
input_p
|
float
|
Dropout probability on RNN inputs. |
0
|
weight_p
|
float
|
Weight dropout probability for RNN parameters. |
0
|
rnn_type
|
str
|
RNN cell type ( |
'gru'
|
Source code in tsfast/models/cnn.py
SeperateCRNN ¶
SeperateCRNN(input_list: list[int], output_size: int, num_ft: int = 10, num_cnn_layers: int = 4, num_rnn_layers: int = 2, hs_cnn: int = 10, hs_rnn: int = 10, hidden_p: float = 0, input_p: float = 0, weight_p: float = 0, rnn_type: str = 'gru')
Bases: Module
CRNN with separate TCN branches per input group, merged before the RNN stage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_list
|
list[int]
|
List of channel counts, one per input group. |
required |
output_size
|
int
|
Number of output channels. |
required |
num_ft
|
int
|
Number of intermediate features between the CNN and RNN stages. |
10
|
num_cnn_layers
|
int
|
Number of TCN blocks per branch in the convolutional stage. |
4
|
num_rnn_layers
|
int
|
Number of stacked RNN layers. |
2
|
hs_cnn
|
int
|
Hidden channel width of the TCN branches. |
10
|
hs_rnn
|
int
|
Hidden size of the RNN stage. |
10
|
hidden_p
|
float
|
Dropout probability on RNN hidden-to-hidden connections. |
0
|
input_p
|
float
|
Dropout probability on RNN inputs. |
0
|
weight_p
|
float
|
Weight dropout probability for RNN parameters. |
0
|
rnn_type
|
str
|
RNN cell type ( |
'gru'
|
Source code in tsfast/models/cnn.py
Conv1D ¶
Conv1D(input_size: int, output_size: int, kernel_size: int = 3, activation: type[Module] | None = Mish, wn: bool = True, bn: bool = False, **kwargs) -> nn.Sequential
Create a 1D convolutional block with optional activation and batch norm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
Number of input channels. |
required |
output_size
|
int
|
Number of output channels. |
required |
kernel_size
|
int
|
Size of the convolving kernel. |
3
|
activation
|
type[Module] | None
|
Activation function class, or None to disable. |
Mish
|
wn
|
bool
|
Whether to apply weight normalization. |
True
|
bn
|
bool
|
Whether to apply batch normalization before the convolution. |
False
|
**kwargs
|
Additional arguments passed to |
{}
|
Source code in tsfast/models/cnn.py
CConv1D ¶
CConv1D(input_size: int, output_size: int, kernel_size: int = 2, activation: type[Module] | None = Mish, wn: bool = True, bn: bool = False, **kwargs) -> nn.Sequential
Create a causal 1D convolutional block with optional weight norm and batch norm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
Number of input channels. |
required |
output_size
|
int
|
Number of output channels. |
required |
kernel_size
|
int
|
Size of the convolving kernel. |
2
|
activation
|
type[Module] | None
|
Activation function class, or None to disable. |
Mish
|
wn
|
bool
|
Whether to apply weight normalization. |
True
|
bn
|
bool
|
Whether to apply batch normalization before the convolution. |
False
|
**kwargs
|
Additional arguments passed to |
{}
|
Source code in tsfast/models/cnn.py
TCNLearner ¶
TCNLearner(dls, num_layers: int = 3, hidden_size: int = 100, loss_func: Module = nn.L1Loss(), metrics: list | None = None, n_skip: int | None = None, opt_func: type = torch.optim.Adam, input_norm: type[Scaler] | None = StandardScaler, output_norm: type[Scaler] | None = None, **kwargs) -> Learner
Create a Learner with a TCN model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dls
|
DataLoaders providing training and validation data. |
required | |
num_layers
|
int
|
Number of TCN hidden layers (sets receptive field to 2**num_layers). |
3
|
hidden_size
|
int
|
Number of channels in hidden TCN layers. |
100
|
loss_func
|
Module
|
Loss function instance. |
L1Loss()
|
metrics
|
list | None
|
List of metric functions. |
None
|
n_skip
|
int | None
|
Number of initial time steps to skip in the loss (defaults to 2**num_layers). |
None
|
opt_func
|
type
|
Optimizer constructor. |
Adam
|
input_norm
|
type[Scaler] | None
|
Input normalization scaler class, or None to disable. |
StandardScaler
|
output_norm
|
type[Scaler] | None
|
Output denormalization scaler class, or None to disable. |
None
|
**kwargs
|
Additional arguments passed to |
{}
|
Source code in tsfast/models/cnn.py
CRNNLearner ¶
CRNNLearner(dls, loss_func: Module = nn.L1Loss(), metrics: list | None = None, n_skip: int = 0, opt_func: type = torch.optim.Adam, input_norm: type[Scaler] | None = StandardScaler, output_norm: type[Scaler] | None = None, **kwargs) -> Learner
Create a Learner with a CRNN model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dls
|
DataLoaders providing training and validation data. |
required | |
loss_func
|
Module
|
Loss function instance. |
L1Loss()
|
metrics
|
list | None
|
List of metric functions. |
None
|
n_skip
|
int
|
Number of initial time steps to skip in the loss. |
0
|
opt_func
|
type
|
Optimizer constructor. |
Adam
|
input_norm
|
type[Scaler] | None
|
Input normalization scaler class, or None to disable. |
StandardScaler
|
output_norm
|
type[Scaler] | None
|
Output denormalization scaler class, or None to disable. |
None
|
**kwargs
|
Additional arguments passed to |
{}
|
Source code in tsfast/models/cnn.py
AR_TCNLearner ¶
AR_TCNLearner(dls, hl_depth: int = 3, alpha: float = 1, beta: float = 1, metrics: list | None = None, n_skip: int | None = None, opt_func: type = torch.optim.Adam, input_norm: type[Scaler] | None = StandardScaler, **kwargs) -> Learner
Create a Learner with an autoregressive TCN model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dls
|
DataLoaders providing training and validation data. |
required | |
hl_depth
|
int
|
Number of TCN hidden layers. |
3
|
alpha
|
float
|
Regularization weight for smoothness penalty. |
1
|
beta
|
float
|
Regularization weight for sparsity penalty. |
1
|
metrics
|
list | None
|
Metric functions (defaults to RMSE). |
None
|
n_skip
|
int | None
|
Number of initial time steps to skip in the loss (defaults to 2**hl_depth). |
None
|
opt_func
|
type
|
Optimizer constructor. |
Adam
|
input_norm
|
type[Scaler] | None
|
Input normalization scaler class, or None to disable. |
StandardScaler
|
**kwargs
|
Additional arguments passed to |
{}
|