optrade.models.utils


optrade.models.utils.patcher

class Patcher(patch_dim=16, stride=8)[source]

Bases: Module

Splits the input time series into patches.

Parameters:
  • patch_dim (int)

  • stride (int)

__init__(patch_dim=16, stride=8)[source]

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

Parameters:
  • patch_dim (int)

  • stride (int)

forward(x)[source]
Parameters:

x – input tensor of shape (B, M, L). B: batch_size, M: channels, L: sequence_length.

Returns:

patches – tensor of shape (B, M, N, P). N: number of patches, P: patch_dim. patches_combined: tensor of shape (B * M, N, P). N: number of patches, P: patch_dim. This is more efficient

to input into the Transformer encoder, as we are applying it to channels independently, thus, we can combine the batch and channel dimensions and then reshape it afterwards.

class VerticalPatcher(patch_dim=16, stride=8)[source]

Bases: Module

Splits the input time series into patches, vertically stacking channels within each patch.

Parameters:
  • patch_dim (int)

  • stride (int)

__init__(patch_dim=16, stride=8)[source]

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

Parameters:
  • patch_dim (int)

  • stride (int)

forward(x)[source]
Parameters:

x – input tensor of shape (B, M, L). B: batch_size, M: channels, L: sequence_length.

Returns:

patches – tensor of shape (B, N, M*P). N: number of patches, M*P: channels * patch_dim

optrade.models.utils.pos_enc

class PositionalEncoding(patch_dim=16, d_model=128, num_patches=64)[source]

Bases: Module

Parameters:
  • patch_dim (int)

  • d_model (int)

  • num_patches (int)

__init__(patch_dim=16, d_model=128, num_patches=64)[source]

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

Parameters:
  • patch_dim (int)

  • d_model (int)

  • num_patches (int)

forward(x)[source]
Parameters:

x – input tensor of shape (B, M, N, P) where B = batch_size, M = num_channels, N = num_patches, P = patch_dim.

Returns:

x – tensor of shape (B, M, N, D) where D = d_model.

optrade.models.utils.revin

class RevIN(num_channels, eps=1e-05, affine=True, target_channels=None)[source]

Bases: Module

Parameters:
  • num_channels (int)

  • eps (float)

  • affine (bool)

  • target_channels (List[int] | None)

__init__(num_channels, eps=1e-05, affine=True, target_channels=None)[source]

Kim et al. (2022): Reversible instance normalization for accurate time-series forecasting against distribution shift. Provides a learnable instance normalization layer that is reversible. Code is from https://github.com/yuqinie98/PatchTST/blob/main/PatchTST_supervised/layers/RevIN.py with modifications, which was originally taken from https://github.com/ts-kim/RevIN.

Parameters:
  • num_channels (int) – The number of features or channels.

  • eps (float) – A value added for numerical stability.

  • affine (bool) – If True, RevIN has learnable affine parameters (e.g., like LayerNorm).

  • target_channels (List[int] | None) – List of target channels for the head layer. It not None, it will denormalize only the target channels corresponding to an input shape of (batch_size, len(target_channels), pred_len) for the model output.

Return type:

None

forward(x, mode)[source]

Forward pass for normalization or denormalizating the time series with learnable affine transformations.

Parameters:
  • x – Input tensor of shape (batch_size, num_channels, seq_len).

  • mode (str) – ‘norm’ for normalization and ‘denorm’ for denormalization.

Returns:

Normalized or denormalized tensor of same shape as input tensor.

optrade.models.utils.utils

get_num_examples(data_loader)[source]
class Transpose(dim1, dim2)[source]

Bases: Module

__init__(dim1, dim2)[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 Unsqueeze(dim)[source]

Bases: Module

__init__(dim)[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 Reshape(*args)[source]

Bases: Module

__init__(*args)[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 Norm(norm_mode, num_channels, seq_len, d_model)[source]

Bases: Module

__init__(norm_mode, num_channels, seq_len, d_model)[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.utils.weight_init

set_seed(seed)[source]
trunc_normal_(tensor, mean=0.0, std=1.0, a=-2.0, b=2.0)[source]
xavier_init(m, seed=None)[source]

Module contents