Implementation:Sktime Pytorch forecasting mLSTMNetwork
| Knowledge Sources | |
|---|---|
| Domains | Time_Series, Forecasting, Deep_Learning |
| Last Updated | 2026-02-08 08:00 GMT |
Overview
mLSTMNetwork is a complete model combining stacked mLSTM layers with a fully connected output layer for sequence prediction.
Description
mLSTMNetwork extends nn.Module and composes an mLSTMLayer (stacked matrix LSTM layers) with a final nn.Linear fully connected layer. The mLSTM layers process the input sequence recurrently, and the FC layer maps the last time step's hidden output to the desired output dimension. This provides an end-to-end trainable network architecture based on the mLSTM cell.
Usage
Use mLSTMNetwork as a standalone recurrent forecasting model when you want a complete mLSTM-based architecture with configurable depth, hidden size, dropout, layer normalization, and residual connections.
Code Reference
Source Location
- Repository: Sktime_Pytorch_forecasting
- File: pytorch_forecasting/layers/_recurrent/_mlstm/network.py
- Lines: 1-99
Signature
class mLSTMNetwork(nn.Module):
def __init__(
self,
input_size,
hidden_size,
num_layers,
output_size,
dropout=0.0,
use_layer_norm=True,
use_residual=True,
):
def forward(self, x, h=None, c=None, n=None):
def init_hidden(self, batch_size, device=None):
Import
from pytorch_forecasting.layers._recurrent._mlstm.network import mLSTMNetwork
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 mLSTM layer. |
| num_layers | int | Yes | Number of mLSTM layers to stack. |
| output_size | int | Yes | Number of features in the output. |
| dropout | float | No | Dropout probability for the mLSTM layers. Defaults to 0.0. |
| use_layer_norm | bool | No | Whether to use layer normalization in the mLSTM layers. Defaults to True. |
| use_residual | bool | No | Whether to use residual connections in the mLSTM layers. Defaults to True. |
forward
| Name | Type | Required | Description |
|---|---|---|---|
| x | torch.Tensor | Yes | Input sequence tensor. |
| h | torch.Tensor or None | No | Initial hidden states for all layers. If None, initialized to zeros. |
| c | torch.Tensor or None | No | Initial cell states for all layers. If None, initialized to zeros. |
| n | torch.Tensor or None | No | Initial normalized states for all layers. If None, initialized to zeros. |
Outputs
forward
| Name | Type | Description |
|---|---|---|
| output | torch.Tensor | Final output tensor from the fully connected layer, produced from the last time step. |
| (h, c, n) | tuple of torch.Tensor | Final hidden, cell, and normalized states for all layers. |
Usage Examples
import torch
from pytorch_forecasting.layers._recurrent._mlstm.network import mLSTMNetwork
model = mLSTMNetwork(
input_size=32,
hidden_size=64,
num_layers=2,
output_size=10,
dropout=0.1,
use_layer_norm=True,
use_residual=True,
)
seq_len, batch_size = 20, 16
x = torch.randn(seq_len, batch_size, 32)
output, (h, c, n) = model(x)
# output shape: (16, 10) -- last time step projected to output_size