modularml.models.sequential

Classes

SequentialCNN(*args, **kwargs)

Initializes a sequential CNN with lazy layer building.

SequentialMLP(*args, **kwargs)

Initializes a sequential MLP model using PyTorch backend.

class modularml.models.sequential.SequentialCNN(*args: Any, **kwargs: Any)

Bases: BaseModel, Module

Initializes a sequential CNN with lazy layer building.

Parameters:
  • input_shape (Tuple[int], optional) – Shape (n_features, feature_len)

  • output_shape (Tuple[int], optional) – Desired final shape

  • n_layers (int) – Number of CNN layers

  • hidden_dim (int) – Output size of each layer

  • kernel_size (int) – Kernel size for convolution

  • padding (int) – Padding size

  • stride (int) – Stride length

  • activation (str) – Activation function

  • dropout (float) – Dropout probability

  • pooling (int) – Pooling kernel size (1 = no pooling)

  • flatten_output (bool) – Whether to flatten output and apply linear layer

  • backend (Backend) – Backend (default: torch)

__init__(input_shape: Tuple[int] | None = None, output_shape: Tuple[int] | None = None, n_layers: int = 2, hidden_dim: int = 16, kernel_size: int = 3, padding: int = 1, stride: int = 1, activation: str = 'relu', dropout: float = 0.0, pooling: int = 1, flatten_output: bool = True, backend: Backend = Backend.TORCH)

Initializes a sequential CNN with lazy layer building.

Parameters:
  • input_shape (Tuple[int], optional) – Shape (n_features, feature_len)

  • output_shape (Tuple[int], optional) – Desired final shape

  • n_layers (int) – Number of CNN layers

  • hidden_dim (int) – Output size of each layer

  • kernel_size (int) – Kernel size for convolution

  • padding (int) – Padding size

  • stride (int) – Stride length

  • activation (str) – Activation function

  • dropout (float) – Dropout probability

  • pooling (int) – Pooling kernel size (1 = no pooling)

  • flatten_output (bool) – Whether to flatten output and apply linear layer

  • backend (Backend) – Backend (default: torch)

build(input_shape: Tuple[int] | None = None, output_shape: Tuple[int] | None = None)

Builds convolutional layers and final FC layer if needed.

Parameters:
  • input_shape (Optional[Tuple[int]], optional) – Used if model not built (num_features, feature_len). Defaults to None.

  • output_shape (Optional[Tuple[int]], optional) – Used if model not built (num_targets, target_len). Defaults to None.

forward(x: torch.Tensor)

Forward pass

Parameters:

x (torch.Tensor) – Sample with shape (batch_size, n_features, feature_len)

Returns:

Output tensor of shape (batch_size, *output_shape)

Return type:

torch.Tensor

classmethod from_config(config: Dict[str, Any]) BaseModel

Dynamically reconstructs a model from config. Prioritizes _target_ path, falls back to ModelRegistry.

get_config() Dict[str, Any]

Return a serializable config dictionary, including _target_.

class modularml.models.sequential.SequentialMLP(*args: Any, **kwargs: Any)

Bases: BaseModel, Module

Initializes a sequential MLP model using PyTorch backend. Actual model layers are built in the build() method.

Parameters:
  • input_shape (Tuple[int], optional) – Shape of input sample (n_features, feature_len)

  • output_shape (Tuple[int], optional) – Desired shape of output

  • n_layers (int) – Number of fully-connected layers

  • hidden_dim (int) – Size of hidden units

  • activation (str) – Activation function name

  • dropout (float) – Dropout rate

  • backend (Backend) – ML backend (default is PyTorch)

__init__(input_shape: Tuple[int] | None = None, output_shape: Tuple[int] | None = None, n_layers: int = 2, hidden_dim: int = 32, activation: str = 'relu', dropout: float = 0.0, backend: Backend | None = Backend.TORCH)

Initializes a sequential MLP model using PyTorch backend. Actual model layers are built in the build() method.

Parameters:
  • input_shape (Tuple[int], optional) – Shape of input sample (n_features, feature_len)

  • output_shape (Tuple[int], optional) – Desired shape of output

  • n_layers (int) – Number of fully-connected layers

  • hidden_dim (int) – Size of hidden units

  • activation (str) – Activation function name

  • dropout (float) – Dropout rate

  • backend (Backend) – ML backend (default is PyTorch)

build(input_shape: Tuple[int] | None = None, output_shape: Tuple[int] | None = None)

Constructs the internal sequential layers. Called lazily when input/output shape is known.

Parameters:
  • input_shape (Optional[Tuple[int]], optional) – Used if model not built (num_features, feature_len). Defaults to None.

  • output_shape (Optional[Tuple[int]], optional) – Used if model not built (num_targets, target_len). Defaults to None.

forward(x: torch.Tensor)

Forward pass

Parameters:

x (torch.Tensor) – Input tensor with shape (batch_size, n_features, feature_len)

Returns:

Output tensor of shape (batch_size, *output_shape)

Return type:

torch.Tensor

classmethod from_config(config: Dict[str, Any]) BaseModel

Dynamically reconstructs a model from config. Prioritizes _target_ path, falls back to ModelRegistry.

get_config() Dict[str, Any]

Return a serializable config dictionary, including _target_.