pyshred.models.sequence_models package#
Submodules#
pyshred.models.sequence_models.abstract_sequence module#
pyshred.models.sequence_models.gru_model module#
- class pyshred.models.sequence_models.gru_model.GRU(hidden_size: int = 3, num_layers: int = 1, layer_norm: bool = False)[source]#
Bases:
AbstractSequence
GRU (Gated Recurrent Unit) sequence model for encoding temporal sensor dynamics.
A recurrent neural network that processes sensor measurement sequences to learn latent representations of the underlying dynamics. Supports optional layer normalization for improved training stability.
- Parameters:
hidden_size (int, optional) – Size of the GRU hidden state. Defaults to 3.
num_layers (int, optional) – Number of GRU layers. Defaults to 1.
layer_norm (bool, optional) – Whether to apply layer normalization. Defaults to False.
- Variables:
hidden_size (int) – Size of the GRU hidden state.
num_layers (int) – Number of GRU layers.
use_layer_norm (bool) – Whether layer normalization is applied.
output_size (int) – Size of the output (equals hidden_size).
- forward(x)[source]#
Forward pass through the GRU model.
- Parameters:
x (torch.Tensor) – Input tensor of shape (batch_size, sequence_length, input_size).
- Returns:
Output tensor with latent representations. Shape depends on decoder type: - MLP decoder: (batch_size, hidden_size) - UNET decoder: (batch_size, hidden_size, sequence_length)
- Return type:
torch.Tensor
- Raises:
TypeError – If decoder type is not supported.
- initialize(input_size: int, decoder_type, **kwargs)[source]#
Initialize the GRU with input size and decoder.
- Parameters:
input_size (int) – Number of input features (sensor measurements).
decoder_type (str) – Decoder model type.
**kwargs – Additional keyword arguments.
- property model_name#
Name of the sequence model.
- Returns:
Returns “GRU”.
- Return type:
str
pyshred.models.sequence_models.lstm_model module#
- class pyshred.models.sequence_models.lstm_model.LSTM(hidden_size: int = 64, num_layers: int = 2, layer_norm: bool = False)[source]#
Bases:
AbstractSequence
LSTM sequence model for encoding temporal sensor dynamics.
- Parameters:
hidden_size (int, optional) – Size of the hidden state. Defaults to 64.
num_layers (int, optional) – Number of LSTM layers. Defaults to 2.
layer_norm (bool, optional) – Whether to apply layer normalization. Defaults to False.
- Variables:
hidden_size (int) – Size of the hidden state.
num_layers (int) – Number of LSTM layers.
use_layer_norm (bool) – Whether layer normalization is applied.
- forward(x)[source]#
Forward pass through the LSTM model.
- Parameters:
x (torch.Tensor) – Input tensor of shape (batch_size, sequence_length, input_size).
- Returns:
Output tensor with latent representations.
- Return type:
torch.Tensor
- initialize(input_size: int, decoder_type, **kwargs)[source]#
Initialize the LSTM with input size and decoder.
- Parameters:
input_size (int) – Number of input features.
decoder_type (str) – Decoder model type.
**kwargs – Additional keyword arguments.
- property model_name#
Name of the sequence model.
- Returns:
Returns “LSTM”.
- Return type:
str
pyshred.models.sequence_models.transformer_model module#
- class pyshred.models.sequence_models.transformer_model.PositionalEncoding(d_model, max_sequence_length=5000, dropout=0.1)[source]#
Bases:
Module
Sinusoidal positional encoding for Transformer models.
Adds learnable positional information to input embeddings to help the model understand sequence order.
- Parameters:
d_model (int) – Dimensionality of the model embeddings.
max_sequence_length (int, optional) – Maximum sequence length to precompute encodings for. Defaults to 5000.
dropout (float, optional) – Dropout probability. Defaults to 0.1.
- Variables:
pe (torch.Tensor) – Precomputed positional encoding tensor.
- class pyshred.models.sequence_models.transformer_model.TRANSFORMER(d_model: int = 128, nhead: int = 16, dropout: float = 0.2, layer_norm: bool = False)[source]#
Bases:
AbstractSequence
Transformer-based sequence model for encoding temporal sensor dynamics.
Uses self-attention mechanisms to capture long-range dependencies in sensor measurement sequences. Includes positional encoding and supports optional layer normalization.
- Parameters:
d_model (int, optional) – Dimensionality of the model (embedding size). Defaults to 128.
nhead (int, optional) – Number of attention heads. Defaults to 16.
dropout (float, optional) – Dropout probability. Defaults to 0.2.
layer_norm (bool, optional) – Whether to apply layer normalization. Defaults to False.
- Variables:
d_model (int) – Model dimensionality.
hidden_size (int) – Hidden size (same as d_model).
output_size (int) – Output size (same as d_model).
use_layer_norm (bool) – Whether layer normalization is applied.
- forward(x: Tensor) Tensor [source]#
Forward pass through the Transformer model.
- Parameters:
x (torch.Tensor) – Input tensor of shape (batch_size, sequence_length, input_size).
- Returns:
Output tensor with latent representations. Shape depends on decoder type: - MLP decoder: (batch_size, d_model) - UNET decoder: (batch_size, d_model, sequence_length)
- Return type:
torch.Tensor
- Raises:
TypeError – If decoder type is not supported.
- initialize(input_size: int, lags: int, decoder_type, **kwargs)[source]#
Initialize the Transformer with input size, sequence length, and decoder.
- Parameters:
input_size (int) – Number of input features (sensor measurements).
lags (int) – Length of input sequences.
decoder_type (str) – Decoder model type.
**kwargs – Additional keyword arguments.
- property model_name#
Name of the sequence model.
- Returns:
Returns “Transformer”.
- Return type:
str