Encode ordered feature sequences
Sequence classification or regression
PyTorch · Class
Applies a multi-layer long short-term memory recurrent network to an input sequence. It supports stacked, bidirectional, dropout, and projected configurations.
An LSTM consumes a sequence and maintains hidden and cell states while applying its recurrent transformation at each time step. Multiple recurrent layers can be stacked, dropout can be applied between layers, and bidirectional or projection variants can alter the output representation.
torch.nn.modules.rnn.LSTM(input_size, hidden_size, num_layers=1, bias=True, batch_first=False, dropout=0.0, bidirectional=False, proj_size=0, device=None, dtype=None)| Name | Type | Description |
|---|---|---|
input_size | int | Number of expected features in each input element. |
hidden_size | int | Number of features in the hidden state. |
num_layers | int | Number of recurrent layers; default 1. |
bias | bool | Whether to use bias weights; default True. |
batch_first | bool | If True, inputs and outputs use batch-first layout; default False. |
dropout | float | Dropout probability between recurrent layers; default 0. |
bidirectional | bool | If True, use a bidirectional LSTM; default False. |
proj_size | int | Projection size; positive values enable projections; default 0. |
device | torch.device | Optional device for created parameters. |
dtype | torch.dtype | Optional dtype for created parameters. |
Returns output for the sequence and a tuple containing the final hidden state and final cell state.
import torch
from torch import nn
lstm = nn.LSTM(input_size=4, hidden_size=8, batch_first=True)
x = torch.randn(2, 5, 4)
y, (h_n, c_n) = lstm(x)
print(y.shape, h_n.shape, c_n.shape)Creates a batch-first LSTM, processes two sequences of length five, and prints the sequence output plus final hidden and cell states.
Sequence classification or regression
Deep recurrent architectures
Context-sensitive sequence modeling
Compact or specialized recurrent representations
The constructor requires input_size and hidden_size; num_layers, bias, batch_first, dropout, bidirectional, and proj_size configure the module. It returns a sequence output together with the final hidden and cell states.
Follow-up: How does batch_first affect tensor shapes? It selects (batch, sequence, feature) for batched input and output instead of (sequence, batch, feature), while hidden and cell states keep their documented layout.
input_size is the number of features in each input element, while hidden_size is the number of features in the hidden state. num_layers controls how many recurrent LSTM layers are stacked.
Follow-up: What happens with num_layers=2? The second recurrent layer receives the outputs produced by the first layer.
dropout adds dropout to outputs between LSTM layers when its value is nonzero. bidirectional=True makes the module process the sequence in both directions, and proj_size enables an LSTM with projections.
Follow-up: Where is dropout absent? The documented dropout placement excludes the last LSTM layer.
torch.nn.RNNBasetorch.nn.RNNtorch.nn.GRUtorch.nn.LSTMCell