Back to models
Classifier

LSTMFCNClassifierTorch

LSTM-FCN classifier for time series classification in PyTorch.

Combines an LSTM arm with a CNN arm. Optionally uses an attention mechanism in the LSTM which the author indicates provides improved performance.

Quickstart

python
from sktime.classification.deep_learning.lstmfcn import LSTMFCNClassifierTorch

estimator = LSTMFCNClassifierTorch(kernel_sizes: tuple=(8, 5, 3), filter_sizes: tuple=(128, 256, 128), lstm_size: int=8, dropout: float=0.8, attention: bool=False, activation: str | Callable | None=None, activation_hidden: str | Callable | None='ReLU', num_epochs: int=2000, batch_size: int=128, optimizer: str | None | Callable='Adam', optimizer_kwargs: dict | None=None, criterion: str | None | Callable='CrossEntropyLoss', criterion_kwargs: dict | None=None, callbacks: None | str | tuple [str, ... ]='ReduceLROnPlateau', callback_kwargs: dict | None=None, init_weights: str | None='kaiming_uniform', metrics: None | str | Callable | tuple [str | Callable, ... ]=None, lr: float=0.001, verbose: bool=False, random_state: int | None=None)

Parameters(20)

kernel_sizestuple of int, default=(8, 5, 3)
Specifying the length of the 1D convolution windows for each conv layer
filter_sizestuple of int, default=(128, 256, 128)
Size of filter for each conv layer
lstm_sizeint, default=8
Output dimension for LSTM layer (hidden state size)
dropoutfloat, default=0.8
Controls dropout rate of LSTM layer
attentionbool, default=False
If True, uses attention mechanism before LSTM layer
activationstr, Callable, or None, default=None

Activation applied to the output layer.

Permitted values:

  • None: no activation is applied to the output layer and the network returns raw outputs (logits). This is typically required when using CrossEntropyLoss, which expects logits as input.

  • str: name of a class in torch.nn. Case-sensitive names are recommended and must match PyTorch (e.g., "ReLU", "LeakyReLU"). Lowercase aliases for common activations are also accepted (e.g., "relu" is resolved to "ReLU"). The class is instantiated with default constructor arguments. Must be a valid torch.nn activation; see https://pytorch.org/docs/stable/nn.html#non-linear-activations-weighted-sum-nonlinearity

  • torch.nn.Module: an instance of a torch.nn.Module subclass, for example torch.nn.ReLU(). Arbitrary callables are not supported.

Recommended activations: ReLU, Tanh, Sigmoid, LeakyReLU, ELU, SELU, GELU.

activation_hiddenstr, Callable, or None, default=”ReLU”

Activation applied to the hidden layers.

Permitted values:

  • None: no activation is applied to the hidden layers.

  • str: name of a class in torch.nn. Case-sensitive names are recommended and must match PyTorch (e.g., "ReLU", "LeakyReLU"). Lowercase aliases for common activations are also accepted (e.g., "relu" is resolved to "ReLU"). The class is instantiated with default constructor arguments. Must be a valid torch.nn activation; see https://pytorch.org/docs/stable/nn.html#non-linear-activations-weighted-sum-nonlinearity

  • torch.nn.Module: an instance of a torch.nn.Module subclass, for example torch.nn.ReLU(). Arbitrary callables are not supported.

Recommended activations: ReLU, Tanh, Sigmoid, LeakyReLU, ELU, SELU, GELU.

num_epochsint, default=2000
The number of epochs to train the model.
batch_sizeint, default=128
The size of each mini-batch during training.
optimizercase insensitive str or None or an instance of optimizers

defined in torch.optim, default = “Adam” The optimizer to use for training the model. List of available optimizers: https://pytorch.org/docs/stable/optim.html#algorithms

optimizer_kwargsdict or None, default = None
Additional keyword arguments to pass to the optimizer.
criterioncase insensitive str or None or an instance of a loss function

defined in PyTorch, default = “CrossEntropyLoss” The loss function to be used in training the neural network. List of available loss functions: https://pytorch.org/docs/stable/nn.html#loss-functions

criterion_kwargsdict or None, default = None
Additional keyword arguments to pass to the loss function.
callbacksNone or str or a tuple of str, default = “ReduceLROnPlateau”

Currently only learning rate schedulers are supported as callbacks. If more than one scheduler is passed, they are applied sequentially in the order they are passed. If None, then no learning rate scheduler is used. Note: Since PyTorch learning rate schedulers need to be initialized with the optimizer object, we only accept the class name (str) of the scheduler here and do not accept an instance of the scheduler. As that can lead to errors and unexpected behavior. List of available learning rate schedulers: https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate

callback_kwargsdict or None, default = None
The keyword arguments to be passed to the callbacks.
init_weights: str or None, default = ‘kaiming_uniform’
The method to initialize the weights of the conv layers. Supported values are ‘kaiming_uniform’, ‘kaiming_normal’, ‘xavier_uniform’, ‘xavier_normal’, or None for default PyTorch initialization.
metricsNone or str or Callable or tuple of str and/or Callable, default = None

Metrics to compute during training. If None, no metrics are computed beyond the loss. Metrics are computed from torchmetrics library. If a string/Callable is passed, it must be one of the metrics defined in https://lightning.ai/docs/torchmetrics/stable/ Examples: “Accuracy”, “F1Score”, “Precision”, “Recall”

lrfloat, default = 0.001
The learning rate to use for the optimizer.
verbosebool, default = False
Whether to print progress information during training.
random_stateint | None, default = None
Seed to ensure reproducibility.

Examples

>>> from sktime.classification.deep_learning.lstmfcn import LSTMFCNClassifierTorch
>>> from sktime.datasets import load_unit_test
>>> X_train, y_train = load_unit_test (split = "train")
>>> X_test, y_test = load_unit_test (split = "test")
>>> clf = LSTMFCNClassifierTorch (num_epochs = 50, batch_size = 128)
>>> clf. fit (X_train, y_train) LSTMFCNClassifierTorch(
... )

References

[1]

Karim et al. Multivariate LSTM-FCNs for Time Series Classification, 2019

https://arxiv.org/pdf/1801.04503.pdf