Back to models
Classifier

SimpleRNNClassifierTorch

Simple recurrent neural network in PyTorch for time series classification.

Quickstart

python
from sktime.classification.deep_learning.rnn import SimpleRNNClassifierTorch

estimator = SimpleRNNClassifierTorch(hidden_dim: int=6, n_layers: int=1, activation: str | Callable | None=None, activation_hidden: str | Callable='ReLU', bias: bool=True, init_weights: bool=True, dropout: float=0.0, fc_dropout: float=0.0, bidirectional: bool=False, num_epochs: int=100, batch_size: int=1, optimizer: str | None | Callable='RMSprop', criterion: str | None | Callable='CrossEntropyLoss', callbacks: None | str | tuple [str, ... ]='ReduceLROnPlateau', optimizer_kwargs: dict | None=None, criterion_kwargs: dict | None=None, callback_kwargs: dict | None=None, metrics: None | str | Callable | tuple [str | Callable, ... ]=None, lr: float=0.001, verbose: bool=False, random_state: int=0)

Parameters(21)

hidden_dimint, default = 6
Number of features in the hidden state.
n_layersint, default = 1
Number of recurrent layers. Setting n_layers=2 would mean stacking two RNNs together to form a stacked RNN, with the second RNN taking in outputs of the first RNN and computing the final results.
activationstr or None or an instance of activation functions defined in
torch.nn, default = None Activation function used in the fully connected output layer.
activation_hiddenstr or Callable, default = “ReLU”

The activation function applied inside the RNN. Recommended callable instance of ‘ReLU’, ‘Tanh’. Because currently PyTorch only supports these two activations inside the RNN. https://docs.pytorch.org/docs/stable/generated/torch.nn.RNN.html#torch.nn.RNN

biasbool, default = True
If False, then the layer does not use bias weights.
init_weightsbool, default = True
If True, then the weights are initialized.
dropoutfloat, default = 0.0
If non-zero, introduces a Dropout layer on the outputs of each RNN layer except the fully connected output layer, with dropout probability equal to dropout.
fc_dropoutfloat, default = 0.0
If non-zero, introduces a Dropout layer on the outputs of the fully connected output layer, with dropout probability equal to fc_dropout.
bidirectionalbool, default = False
If True, then the RNN is bidirectional.
num_epochsint, default = 100
The number of epochs to train the model.
optimizercase insensitive str or None or an instance of optimizers

defined in torch.optim, default = “RMSprop” 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.
batch_sizeint, default = 1
The size of each mini-batch during training.
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.
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, default = 0
Seed to ensure reproducibility.

Examples

>>> from sktime.classification.deep_learning.rnn import SimpleRNNClassifierTorch
>>> 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 = SimpleRNNClassifierTorch (n_epochs = 50, batch_size = 2)
>>> clf. fit (X_train, y_train) SimpleRNNClassifierTorch(
... )