Docs /Templates/ sinapsis-sktime

CNTCClassifierTorchSKTimeWrapper

sinapsis-sktimeSKTimedynamicinferencemodelsprediction

Python template · sinapsis_sktime.templates.sktime_classifiers

Overview

Template for SkTime classification model training and inference for the deep_learning module.

CNTCClassifierTorch — inherited documentation

Contextual Time-series Neural Classifier for TSC, implemented in PyTorch.

CNTC combines a Contextual Convolutional Neural Network (CCNN) and a Contextual Long Short-Term Memory network (CLSTM) as parallel feature extractors, concatenates their outputs per time step, refines them with self-attention and classifies with a multilayer perceptron, following [1]_.

The four stages are:

- **Feature extraction.** The CCNN arm stacks contextual convolutional layers (recurrent convolutions, equation 1 of [1]_) followed by standard convolutional layers. The CLSTM arm stacks contextual LSTM layers whose gates receive sliding-window means of the input as contextual features (equation 4 of [1]_). Both arms see the same input. - **Concatenation.** The two arms are concatenated along the feature axis, per time step, giving ``c_k = concat(mu_k, h_k)`` (equation 5 of [1]_). - **Attention.** Pooling downsamples the merged sequence, then sequential self-attention reweights it (equations 6 to 8 of [1]_). - **Multilayer perceptron.** Fully connected layers with dropout, followed by the output layer.

Parameters ---------- context_filter_sizes : tuple of int, default = (16,) Number of filters in each contextual convolutional layer of the CCNN arm. The length of the tuple sets the number of such layers; any length >= 1 is allowed. [1]_ uses a single layer with 8, 16, 32 or 64 filters. context_kernel_sizes : tuple of int, default = (3,) Length of the 1D convolution window of each contextual convolutional layer. Must have the same length as ``context_filter_sizes``. Combined with ``context_steps``, a kernel of size ``n`` gives an effective receptive field of ``(n - 1) * context_steps + 1``. context_steps : int, default = 3 Number of recurrent iterations ``K`` performed inside every contextual convolutional layer. ``context_steps=1`` reduces the layer to an ordinary convolution. context_dropout : float or tuple of float, default = 0.8 Dropout rate applied after each contextual convolutional layer. A float applies the same rate to all of them, a tuple sets them individually and must have the same length as ``context_filter_sizes``. conv_filter_sizes : tuple of int, default = (8,) Number of filters in each standard convolutional layer, applied after the contextual convolutional layers. The length of the tuple sets the number of such layers; ``()`` disables them entirely. [1]_ uses a single layer with 8, 16 or 32 filters. conv_kernel_sizes : tuple of int, default = (3,) Length of the 1D convolution window of each standard convolutional layer. Must have the same length as ``conv_filter_sizes``. conv_dropout : float or tuple of float, default = 0.8 Dropout rate applied after each standard convolutional layer. A float applies the same rate to all of them, a tuple sets them individually and must have the same length as ``conv_filter_sizes``. lstm_units : tuple of int, default = (8,) Number of cells in each contextual LSTM layer of the CLSTM arm. The length of the tuple sets the number of stacked layers; any length >= 1 is allowed. [1]_ uses a single layer with 8, 16, 32 or 64 cells. context_window : int, default = 3 Size of the sliding window used to build the contextual features of the CLSTM arm. At each time step the context vector holds the ``context_window`` most recent window means, so it has dimension ``context_window * n_dims``. lstm_dropout : float or tuple of float, default = 0.8 Dropout rate applied after each contextual LSTM layer. A float applies the same rate to all of them, a tuple sets them individually and must have the same length as ``lstm_units``. [1]_ uses 0.8. pool_size : int, default = 2 Size and stride of the pooling window applied to the merged sequence before attention. Values above 1 downsample the time axis. Clipped to the series length. pool_type : str or None, default = "max" Pooling to apply before attention. One of ``"max"``, ``"avg"``, ``"both"`` (max and average pooling concatenated along the feature axis), or ``None`` to disable pooling. pool_dropout : float, default = 0.6 Dropout rate applied after pooling. attention_width : int or None, default = 10 Width of the local attention window. ``None`` lets every time step attend to every other time step. [1]_ uses 8 or 10. attention_units : int, default = 32 Hidden dimension of the additive attention scorer. Unused when ``attention_type="multiplicative"``. attention_type : str, default = "additive" Attention scoring function, either ``"additive"`` or ``"multiplicative"``. [1]_ scores alignments with a feedforward network, which corresponds to ``"additive"``. attention_activation : str, callable, torch.nn.Module or None, default = None Non-linearity applied to the attention logits before normalisation. ``None`` normalises the raw alignment scores with a softmax, as in equation (7) of [1]_. attention_dropout : float, default = 0.5 Dropout rate applied after attention. [1]_ uses 0.5. dense_layers : tuple of int, default = (64, 64) Number of units in each dense layer of the multilayer perceptron. The length of the tuple sets the number of such layers; ``()`` connects the attention output straight to the output layer. [1]_ uses two layers of 64 units. dense_dropout : float or tuple of float, default = (0.5, 0.8) Dropout rate applied after each dense layer. A float applies the same rate to all of them, a tuple sets them individually and must have the same length as ``dense_layers``. [1]_ uses 0.5 and 0.8. activation : str, 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.

activation_hidden : str, Callable, or None, default="ReLU" Activation applied to the hidden layers, that is the convolutional layers of the CCNN arm and the dense layers of the multilayer perceptron.

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.

init_weights : str or None, default = "xavier_uniform" The method used to initialize the weights of the convolutional and linear layers. Supported values are ``"kaiming_uniform"``, ``"kaiming_normal"``, ``"xavier_uniform"``, ``"xavier_normal"``, or ``None`` for the default PyTorch initialization. Biases are zeroed whenever a method is given. num_epochs : int, default = 150 The number of epochs to train the model. batch_size : int, default = 16 The size of each mini-batch during training. [1]_ uses 16, 32 or 64. optimizer : case insensitive str or None, or a class or 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 criterion : case 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 callbacks : None 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 optimizer_kwargs : dict or None, default = None Additional keyword arguments to pass to the optimizer. criterion_kwargs : dict or None, default = None Additional keyword arguments to pass to the loss function. callback_kwargs : dict or None, default = None The keyword arguments to be passed to the callbacks. metrics : None 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" lr : float, default = 0.001 The learning rate to use for the optimizer. verbose : bool, default = False Whether to print progress information during training. random_state : int, default = 0 Seed to ensure reproducibility.

References ---------- .. [1] Amadu Fullah Kamara, Enhong Chen, Qi Liu, Zhen Pan, Combin- ing Contextual Neural Networks for Time Series Classification, Neurocomputing (2019), doi: https://doi.org/10.1016/j.neucom.2019.10.113

Examples -------- >>> from sktime.classification.deep_learning.cntc import CNTCClassifierTorch >>> from sktime.datasets import load_unit_test >>> X_train, y_train = load_unit_test(split="train") >>> clf = CNTCClassifierTorch(num_epochs=5, batch_size=4) # doctest: +SKIP >>> clf.fit(X_train, y_train) # doctest: +SKIP CNTCClassifierTorch(...)

Capabilities

Capabilities: not set

Installation

Install the published package using pip or uv.

Terminalbash
pip install sinapsis-sktime==1.0.0
uv pip install sinapsis-sktime==1.0.0
uv add sinapsis-sktime==1.0.0

Configuration

Effective fields from the inspector schema, including nested Pydantic model fields.

metadata
sinapsis_core.template_base.base_models.TemplateMetadata | None
Optional
Default: null
root_dir
str
Optional
Default: "/sinapsis-hub/private/platform/sinapsis-hub/playground/publish"
model_save_pathRequired
Path where the trained model will be saved. cntcclassifiertorch_init context_filter_sizes typing.Optional[tuple[int, ...]]: default: (16,) context_kernel_sizes typing.Optional[tuple[int, ...]]: default: (3,)… Read full field description

Path where the trained model will be saved. cntcclassifiertorch_init context_filter_sizes typing.Optional[tuple[int, ...]]: default: (16,) context_kernel_sizes typing.Optional[tuple[int, ...]]: default: (3,) context_steps <class 'int'>: default: 3 context_dropout typing.Union[float, tuple[float, ...], NoneType]: default: 0.8 conv_filter_sizes typing.Optional[tuple[int, ...]]: default: (8,) conv_kernel_sizes typing.Optional[tuple[int, ...]]: default: (3,) conv_dropout typing.Union[float, tuple[float, ...], NoneType]: default: 0.8 lstm_units typing.Optional[tuple[int, ...]]: default: (8,) context_window <class 'int'>: default: 3 lstm_dropout typing.Union[float, tuple[float, ...], NoneType]: default: 0.8 pool_size <class 'int'>: default: 2 pool_type typing.Optional[str]: default: max pool_dropout <class 'float'>: default: 0.6 attention_width typing.Optional[int]: default: 10 attention_units <class 'int'>: default: 32 attention_type <class 'str'>: default: additive attention_activation typing.Union[str, collections.abc.Callable, NoneType]: default: None attention_dropout <class 'float'>: default: 0.5 dense_layers typing.Optional[tuple[int, ...]]: default: (64, 64) dense_dropout typing.Union[float, tuple[float, ...], NoneType]: default: (0.5, 0.8) activation typing.Union[str, collections.abc.Callable, NoneType]: default: None activation_hidden typing.Union[str, collections.abc.Callable, NoneType]: default: ReLU init_weights typing.Optional[str]: default: xavier_uniform num_epochs <class 'int'>: default: 150 batch_size <class 'int'>: default: 16 optimizer typing.Union[str, collections.abc.Callable, NoneType]: default: Adam criterion typing.Union[str, collections.abc.Callable, NoneType]: default: CrossEntropyLoss callbacks typing.Union[NoneType, str, tuple[str, ...]]: default: ReduceLROnPlateau optimizer_kwargs typing.Optional[dict]: default: None criterion_kwargs typing.Optional[dict]: default: None callback_kwargs typing.Optional[dict]: default: None metrics typing.Union[NoneType, str, collections.abc.Callable, tuple[str | collections.abc.Callable, ...]]: default: None lr <class 'float'>: default: 0.001 verbose <class 'bool'>: default: False random_state <class 'int'>: default: 0

str
Required
Default: null
cntcclassifiertorch_initRequired
CNTCClassifierTorchAttributes
Required
Default: null
lr
float
Optional
Default: 0.001
metrics
typing.Union[NoneType, str, collections.abc.Callable, tuple[str | collections.abc.Callable, ...]]
Optional
Default: null
verbose
bool
Optional
Default: false
callbacks
typing.Union[NoneType, str, tuple[str, ...]]
Optional
Default: "ReduceLROnPlateau"
criterion
typing.Union[str, collections.abc.Callable, NoneType]
Optional
Default: "CrossEntropyLoss"
optimizer
typing.Union[str, collections.abc.Callable, NoneType]
Optional
Default: "Adam"
pool_size
int
Optional
Default: 2
pool_type
typing.Optional[str]
Optional
Default: "max"
activation
typing.Union[str, collections.abc.Callable, NoneType]
Optional
Default: null
batch_size
int
Optional
Default: 16
lstm_units
typing.Optional[tuple[int, ...]]
Optional
Default: [8]
num_epochs
int
Optional
Default: 150
conv_dropout
typing.Union[float, tuple[float, ...], NoneType]
Optional
Default: 0.8
dense_layers
typing.Optional[tuple[int, ...]]
Optional
Default: [64,64]
init_weights
typing.Optional[str]
Optional
Default: "xavier_uniform"
lstm_dropout
typing.Union[float, tuple[float, ...], NoneType]
Optional
Default: 0.8
pool_dropout
float
Optional
Default: 0.6
random_state
int
Optional
Default: 0
context_steps
int
Optional
Default: 3
dense_dropout
typing.Union[float, tuple[float, ...], NoneType]
Optional
Default: [0.5,0.8]
attention_type
str
Optional
Default: "additive"
context_window
int
Optional
Default: 3
attention_units
int
Optional
Default: 32
attention_width
typing.Optional[int]
Optional
Default: 10
callback_kwargs
typing.Optional[dict]
Optional
Default: null
context_dropout
typing.Union[float, tuple[float, ...], NoneType]
Optional
Default: 0.8
criterion_kwargs
typing.Optional[dict]
Optional
Default: null
optimizer_kwargs
typing.Optional[dict]
Optional
Default: null
activation_hidden
typing.Union[str, collections.abc.Callable, NoneType]
Optional
Default: "ReLU"
attention_dropout
float
Optional
Default: 0.5
conv_filter_sizes
typing.Optional[tuple[int, ...]]
Optional
Default: [8]
conv_kernel_sizes
typing.Optional[tuple[int, ...]]
Optional
Default: [3]
attention_activation
typing.Union[str, collections.abc.Callable, NoneType]
Optional
Default: null
context_filter_sizes
typing.Optional[tuple[int, ...]]
Optional
Default: [16]
context_kernel_sizes
typing.Optional[tuple[int, ...]]
Optional
Default: [3]

Configuration example

Generated from documented defaults. Replace any placeholders and complete required nested settings before running.

yaml
templates:
  - template_name: "CNTCClassifierTorchSKTimeWrapper"
    class_name: "CNTCClassifierTorchSKTimeWrapper"
    attributes:
      root_dir: "/sinapsis-hub/private/platform/sinapsis-hub/playground/publish"
      model_save_path: "<required>"
      cntcclassifiertorch_init:
        lr: 0.001
        metrics: null
        verbose: false
        callbacks: "ReduceLROnPlateau"
        criterion: "CrossEntropyLoss"
        optimizer: "Adam"
        pool_size: 2
        pool_type: "max"
        activation: null
        batch_size: 16
        lstm_units: [8]
        num_epochs: 150
        conv_dropout: 0.8
        dense_layers: [64, 64]
        init_weights: "xavier_uniform"
        lstm_dropout: 0.8
        pool_dropout: 0.6
        random_state: 0
        context_steps: 3
        dense_dropout: [0.5, 0.8]
        attention_type: "additive"
        context_window: 3
        attention_units: 32
        attention_width: 10
        callback_kwargs: null
        context_dropout: 0.8
        criterion_kwargs: null
        optimizer_kwargs: null
        activation_hidden: "ReLU"
        attention_dropout: 0.5
        conv_filter_sizes: [8]
        conv_kernel_sizes: [3]
        attention_activation: null
        context_filter_sizes: [16]
        context_kernel_sizes: [3]

Inheritance

Immediate parent: sinapsis_sktime.templates.sktime_classifiers.SKTimeDeepLearningClassifiers

Environment variables

Environment definitions belong to their canonical Python modules.

SINAPSIS_BUILD_DOCS · defined elsewhere