Overview
sLSTMLayer stacks multiple sLSTM cells into a deep recurrent layer with optional residual connections and layer normalization.
Description
sLSTMLayer extends nn.Module and wraps multiple sLSTMCell instances into a multi-layer architecture for sequence modeling. It processes input sequences time-step by time-step, passing data through each stacked cell. Residual connections are supported with automatic input projection when input and hidden sizes differ. Per-layer layer normalization is applied after each cell's output. Hidden and cell states are detached at the end of the forward pass to prevent backpropagation through time across sequence boundaries.
Usage
Use sLSTMLayer when you need a multi-layer stabilized LSTM recurrent block. It is the intermediate building block between individual sLSTMCell instances and the complete sLSTMNetwork.
Code Reference
Source Location
Signature
class sLSTMLayer(nn.Module):
def __init__(
self,
input_size,
hidden_size,
num_layers=1,
dropout=0.0,
use_layer_norm=True,
use_residual=True,
):
def forward(self, x, h=None, c=None):
def init_hidden(self, batch_size, device=None):
Import
from pytorch_forecasting.layers._recurrent._slstm.layer import sLSTMLayer
I/O Contract
Inputs
__init__
| Name |
Type |
Required |
Description
|
| input_size |
int |
Yes |
Number of features in the input.
|
| hidden_size |
int |
Yes |
Number of features in the hidden state of each sLSTM cell.
|
| num_layers |
int |
No |
Number of stacked sLSTM layers. Defaults to 1.
|
| dropout |
float |
No |
Dropout probability for the input of each sLSTM cell. Defaults to 0.0.
|
| use_layer_norm |
bool |
No |
Whether to use layer normalization for each sLSTM cell. Defaults to True.
|
| use_residual |
bool |
No |
Whether to use residual connections in each sLSTM layer. Defaults to True.
|
forward
| Name |
Type |
Required |
Description
|
| x |
torch.Tensor |
Yes |
Input tensor of shape (seq_len, batch_size, input_size).
|
| h |
list of torch.Tensor or None |
No |
Initial hidden states for each layer. If None, initialized to zeros.
|
| c |
list of torch.Tensor or None |
No |
Initial cell states for each layer. If None, initialized to zeros.
|
Outputs
forward
| Name |
Type |
Description
|
| output |
torch.Tensor |
Tensor containing hidden states for each time step, of shape (seq_len, batch_size, hidden_size).
|
| (h, c) |
tuple of lists |
Final hidden and cell states for each layer, detached from the computation graph. Each list contains num_layers tensors of shape (batch_size, hidden_size).
|
init_hidden
| Name |
Type |
Description
|
| (h, c) |
tuple of lists |
Lists of zero-initialized hidden and cell states for each layer.
|
Usage Examples
import torch
from pytorch_forecasting.layers._recurrent._slstm.layer import sLSTMLayer
layer = sLSTMLayer(
input_size=32,
hidden_size=64,
num_layers=3,
dropout=0.1,
use_layer_norm=True,
use_residual=True,
)
seq_len, batch_size = 10, 16
x = torch.randn(seq_len, batch_size, 32)
output, (h, c) = layer(x)
# output shape: (10, 16, 64)
# h: list of 3 tensors, each (16, 64)
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.