optrade.models.pytorch


optrade.models.pytorch.dlinear

class Model(task='forecasting', seq_len=512, pred_len=96, num_channels=7, num_classes=2, moving_avg=25, individual=False, return_head=True, revin=True, revout=False, revin_affine=False, eps_revin=1e-05, target_channels=None)[source]

Bases: Module

Paper link: https://arxiv.org/pdf/2205.13504.pdf Taken from: https://github.com/thuml/Time-Series-Library/blob/main/models/DLinear.py

Parameters:
  • task (str)

  • seq_len (int)

  • pred_len (int)

  • num_channels (int)

  • num_classes (int)

  • moving_avg (int)

  • individual (bool)

  • return_head (bool)

  • revin (bool)

  • revout (bool)

  • revin_affine (bool)

  • eps_revin (float)

  • target_channels (list | None)

__init__(task='forecasting', seq_len=512, pred_len=96, num_channels=7, num_classes=2, moving_avg=25, individual=False, return_head=True, revin=True, revout=False, revin_affine=False, eps_revin=1e-05, target_channels=None)[source]
Parameters:
  • task (str) – Task name among ‘classification’, ‘anomaly_detection’, ‘imputation’, or ‘forecasting’.

  • seq_len (int) – Length of input sequence.

  • pred_len (int) – Length of output forecasting.

  • num_channels (int) – Number of input channels (features).

  • num_classes (int) – Number of classes for classification task.

  • moving_avg (int) – Window size of moving average.

  • individual (bool) – Whether shared model among different variates.

  • return_head (bool)

  • revin (bool)

  • revout (bool)

  • revin_affine (bool)

  • eps_revin (float)

  • target_channels (list | None)

Return type:

None

encoder(x)[source]
forecast(x_enc)[source]
classification(x_enc)[source]
forward(x_enc)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class moving_avg(kernel_size, stride)[source]

Bases: Module

Moving average block to highlight the trend of time series

__init__(kernel_size, stride)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class series_decomp(kernel_size)[source]

Bases: Module

Series decomposition block

__init__(kernel_size)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

optrade.models.pytorch.emforecaster

class Model(args, seed=42, seq_len=336, pred_len=96, num_channels=1, revin=True, revout=True, revin_affine=True, eps_revin=1e-05, patch_model_id='TSMixer', patch_norm='none', patch_act='GeLU', patch_dim=24, patch_stride=12, patch_embed_dim=128, pos_enc='none', return_head=True, target_channels=None)[source]

Bases: Module

Parameters:
  • args (BaseModel)

  • seed (int)

  • seq_len (int)

  • pred_len (int)

  • num_channels (int)

  • revin (bool)

  • revout (bool)

  • revin_affine (bool)

  • eps_revin (float)

  • patch_model_id (str)

  • patch_norm (str)

  • patch_act (str)

  • patch_dim (int)

  • patch_stride (int)

  • patch_embed_dim (int)

  • pos_enc (str)

  • return_head (bool)

  • target_channels (list | None)

__init__(args, seed=42, seq_len=336, pred_len=96, num_channels=1, revin=True, revout=True, revin_affine=True, eps_revin=1e-05, patch_model_id='TSMixer', patch_norm='none', patch_act='GeLU', patch_dim=24, patch_stride=12, patch_embed_dim=128, pos_enc='none', return_head=True, target_channels=None)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • args (BaseModel)

  • seed (int)

  • seq_len (int)

  • pred_len (int)

  • num_channels (int)

  • revin (bool)

  • revout (bool)

  • revin_affine (bool)

  • eps_revin (float)

  • patch_model_id (str)

  • patch_norm (str)

  • patch_act (str)

  • patch_dim (int)

  • patch_stride (int)

  • patch_embed_dim (int)

  • pos_enc (str)

  • return_head (bool)

  • target_channels (list | None)

Return type:

None

forward_patch_model(x)[source]
forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_patch_model()[source]
Return type:

Module

optrade.models.pytorch.linear

class Model(seq_len, pred_len, num_channels, norm_mode='layer', revin=True, revout=False, revin_affine=False, eps_revin=1e-05, channel_independent=False, target_channels=None)[source]

Bases: Module

Parameters:
  • channel_independent (bool)

  • target_channels (list | None)

__init__(seq_len, pred_len, num_channels, norm_mode='layer', revin=True, revout=False, revin_affine=False, eps_revin=1e-05, channel_independent=False, target_channels=None)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • channel_independent (bool)

  • target_channels (list | None)

Return type:

None

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

optrade.models.pytorch.mamba

class Mamba(d_model, num_enc_layers, pred_len, num_channels=1, revin=False, revout=False, revin_affine=False, eps_revin=1e-05, head_type='linear', norm_mode='layer', patching=False, patch_dim=16, patch_stride=8, seq_len=512, last_state=True, dropout=0.0, channel_independent=False, target_channels=None)[source]

Bases: Module

Parameters:
  • num_channels (int)

  • revin (bool)

  • revout (bool)

  • revin_affine (bool)

  • eps_revin (float)

  • head_type (str)

  • norm_mode (str)

  • patching (bool)

  • patch_dim (int)

  • patch_stride (int)

  • seq_len (int)

  • last_state (bool)

  • dropout (float)

  • channel_independent (bool)

  • target_channels (list | None)

__init__(d_model, num_enc_layers, pred_len, num_channels=1, revin=False, revout=False, revin_affine=False, eps_revin=1e-05, head_type='linear', norm_mode='layer', patching=False, patch_dim=16, patch_stride=8, seq_len=512, last_state=True, dropout=0.0, channel_independent=False, target_channels=None)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • num_channels (int)

  • revin (bool)

  • revout (bool)

  • revin_affine (bool)

  • eps_revin (float)

  • head_type (str)

  • norm_mode (str)

  • patching (bool)

  • patch_dim (int)

  • patch_stride (int)

  • seq_len (int)

  • last_state (bool)

  • dropout (float)

  • channel_independent (bool)

  • target_channels (list | None)

Return type:

None

forward(x)[source]

Computes the forward pass of the Mamba model. There are two possible modes:

Patched Version: This is meant for univariate or multivariate time series forecasting, which applies a patching mechanism to the input sequence. The input tensor should have shape (B, M, L), where B is the batch size, M is the number of channels, and L is the sequence length. The output tensor will have shape (B, pred_len), where pred_len is the prediction length.

Non-Patched Version: This is meant for univariate variable-length time series classification (SOZ localization), where the input tensor should have shape (B, L, 1), where B is the batch size, and L is the sequence length which can change from batch to batch, and is padded accordingly. The output tensor will have shape (B, pred_len), where pred_len is the prediction length (usually set to pred_len=1 for binary classification).

Parameters:

x (torch.Tensor) – Input tensor of shape (batch_size, seq_len, num_channels)

Legend:

B: batch_size, M: num_channels, L: seq_len, N: num_patches, P: patch_dim, D: d_model.

optrade.models.pytorch.patchtst

class Model(num_enc_layers, d_model, d_ff, num_heads, input_channels, seq_len, pred_len, attn_dropout=0.0, ff_dropout=0.0, pred_dropout=0.0, batch_first=True, norm_mode='batch1d', revin=True, revout=True, revin_affine=True, eps_revin=1e-05, patch_dim=16, stride=1, return_head=True, head_type='linear', channel_independent=False, target_channels=None)[source]

Bases: Module

Parameters:
  • num_enc_layers (int)

  • d_model (int)

  • d_ff (int)

  • num_heads (int)

  • input_channels (List[str])

  • seq_len (int)

  • pred_len (int)

  • attn_dropout (float)

  • ff_dropout (float)

  • pred_dropout (float)

  • batch_first (bool)

  • norm_mode (str)

  • revin (bool)

  • revout (bool)

  • revin_affine (bool)

  • eps_revin (float)

  • patch_dim (int)

  • stride (int)

  • return_head (bool)

  • head_type (str)

  • channel_independent (bool)

  • target_channels (list | None)

__init__(num_enc_layers, d_model, d_ff, num_heads, input_channels, seq_len, pred_len, attn_dropout=0.0, ff_dropout=0.0, pred_dropout=0.0, batch_first=True, norm_mode='batch1d', revin=True, revout=True, revin_affine=True, eps_revin=1e-05, patch_dim=16, stride=1, return_head=True, head_type='linear', channel_independent=False, target_channels=None)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • num_enc_layers (int)

  • d_model (int)

  • d_ff (int)

  • num_heads (int)

  • input_channels (List[str])

  • seq_len (int)

  • pred_len (int)

  • attn_dropout (float)

  • ff_dropout (float)

  • pred_dropout (float)

  • batch_first (bool)

  • norm_mode (str)

  • revin (bool)

  • revout (bool)

  • revin_affine (bool)

  • eps_revin (float)

  • patch_dim (int)

  • stride (int)

  • return_head (bool)

  • head_type (str)

  • channel_independent (bool)

  • target_channels (list | None)

Return type:

None

forward(x, y=None, ch_ids=None)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class PatchTSTBackbone(num_enc_layers, d_model, d_ff, num_heads, num_channels, num_patches, pred_len, input_channels, attn_dropout=0.0, ff_dropout=0.0, pred_dropout=0.0, batch_first=True, norm_mode='batch1d', return_head=True, head_type='linear', channel_independent=False, target_channels=None)[source]

Bases: Module

Parameters:

target_channels (list | None)

__init__(num_enc_layers, d_model, d_ff, num_heads, num_channels, num_patches, pred_len, input_channels, attn_dropout=0.0, ff_dropout=0.0, pred_dropout=0.0, batch_first=True, norm_mode='batch1d', return_head=True, head_type='linear', channel_independent=False, target_channels=None)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:

target_channels (list | None)

forward(x, y=None, ch_ids=None)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Parameters:
  • x (Tensor)

  • y (Tensor | None)

  • ch_ids (Tensor | None)

Return type:

Tensor

class SupervisedHead(linear_dim, pred_len, dropout=0.0)[source]

Bases: Module

__init__(linear_dim, pred_len, dropout=0.0)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Applies a linear layer to each channel independently to form a prediction, optional dropout. :param x: The input of shape (batch_size, num_channels, num_patches, d_model) :type x: torch.Tensor

Returns:

x (torch.Tensor) – The output of shape (batch_size, num_channels, pred_len).

Return type:

Tensor

class EncoderBlock(d_model, d_ff, num_heads, num_channels, num_patches, attn_dropout=0.0, ff_dropout=0.0, batch_first=True, norm_mode='batch1d')[source]

Bases: Module

Parameters:
  • d_model – The embedding dimension.

  • num_heads – The number of heads in the multi-head attention models.

  • dropout – The dropout value.

  • batch_first – If True, then the input and output tensors are provided as (batch, seq, feature).

  • norm – The type of normalization to use. Either “batch1d”, “batch2d”, or “layer”.

__init__(d_model, d_ff, num_heads, num_channels, num_patches, attn_dropout=0.0, ff_dropout=0.0, batch_first=True, norm_mode='batch1d')[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]
Parameters:

x (Tensor) – Input tensor of shape (batch_size, num_patches, d_model).

Returns:

fc_out – Output of the transformer block, a tensor of shape (batch_size, num_patches, d_model).

Return type:

Tensor

optrade.models.pytorch.recurrent

class Model(d_model, num_enc_layers, pred_len, backbone_id, bidirectional=False, dropout=0.0, seq_len=512, patching=False, patch_dim=16, patch_stride=8, num_channels=1, head_type='linear', norm_mode='layer', revin=False, revout=False, revin_affine=False, eps_revin=1e-05, last_state=True, avg_state=False, return_head=True, channel_independent=False, target_channels=None)[source]

Bases: Module

Parameters:

target_channels (list | None)

__init__(d_model, num_enc_layers, pred_len, backbone_id, bidirectional=False, dropout=0.0, seq_len=512, patching=False, patch_dim=16, patch_stride=8, num_channels=1, head_type='linear', norm_mode='layer', revin=False, revout=False, revin_affine=False, eps_revin=1e-05, last_state=True, avg_state=False, return_head=True, channel_independent=False, target_channels=None)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:

target_channels (list | None)

Return type:

None

compute_backbone(x)[source]
forward(x)[source]

Forward pass of the model.

Parameters:

x (torch.Tensor) – The input data. Shape: (batch_size, num_channels, seq_len) = (B, M, L).

optrade.models.pytorch.tsmixer

class Model(seq_len, pred_len, num_enc_layers, d_model, num_channels, dropout=0.0, revin=True, revin_affine=True, revout=False, eps_revin=1e-05, return_head=True, target_channels=None, channel_independent=False)[source]

Bases: Module

Parameters:
  • seq_len (int)

  • pred_len (int)

  • num_enc_layers (int)

  • d_model (int)

  • num_channels (int)

  • dropout (float)

  • revin (bool)

  • revin_affine (bool)

  • revout (bool)

  • eps_revin (float)

  • return_head (bool)

  • target_channels (list | None)

  • channel_independent (bool)

__init__(seq_len, pred_len, num_enc_layers, d_model, num_channels, dropout=0.0, revin=True, revin_affine=True, revout=False, eps_revin=1e-05, return_head=True, target_channels=None, channel_independent=False)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • seq_len (int)

  • pred_len (int)

  • num_enc_layers (int)

  • d_model (int)

  • num_channels (int)

  • dropout (float)

  • revin (bool)

  • revin_affine (bool)

  • revout (bool)

  • eps_revin (float)

  • return_head (bool)

  • target_channels (list | None)

  • channel_independent (bool)

Return type:

None

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class ResBlock(seq_len, d_model, dropout, num_channels)[source]

Bases: Module

__init__(seq_len, d_model, dropout, num_channels)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]
Parameters:

x (torch.Tensor) – Shape (batch_size, seq_len, num_vars)

Returns:

torch.Tensor – Shape (batch_size, seq_len, num_vars)

Module contents