Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Class

nn.LSTM

Applies a multi-layer long short-term memory recurrent network to an input sequence. It supports stacked, bidirectional, dropout, and projected configurations.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
input_sizeintNumber of expected features in each input element.
hidden_sizeintNumber of features in the hidden state.
num_layersintNumber of recurrent layers; default 1.
biasboolWhether to use bias weights; default True.
batch_firstboolIf True, inputs and outputs use batch-first layout; default False.
dropoutfloatDropout probability between recurrent layers; default 0.
bidirectionalboolIf True, use a bidirectional LSTM; default False.
proj_sizeintProjection size; positive values enable projections; default 0.
devicetorch.deviceOptional device for created parameters.
dtypetorch.dtypeOptional dtype for created parameters.

Returns

Returns output for the sequence and a tuple containing the final hidden state and final cell state.

Key points

  • nn.LSTM is a recurrent module for processing ordered feature sequences, optionally using several stacked layers.
  • input_size specifies the feature width of each input element.
  • num_layers determines the depth of the recurrent stack.
  • bidirectional=True runs the recurrent computation in both directions.
  • A nonzero dropout value inserts dropout between recurrent layers.
  • proj_size selects a projected LSTM representation when positive.

Common mistakes

  • Configured dropout affects outputs between recurrent layers, not the output of the final layer.
  • batch_first changes input and output layout but does not change the layout of the hidden or cell states.
  • A positive projection size changes the recurrent representation and should be accounted for when sizing downstream layers.

Example: Run a batch-first LSTM

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.

When to use it

Encode ordered feature sequences

Sequence classification or regression

Stack recurrent layers

Deep recurrent architectures

Use bidirectional processing

Context-sensitive sequence modeling

Use projection LSTMs

Compact or specialized recurrent representations

Interview questions

QWhat does nn.LSTM return?

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.

QWhat are the main dimensional parameters?

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.

QHow do dropout, bidirectionality, and projections change the module?

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.RNNBase
  • torch.nn.RNN
  • torch.nn.GRU
  • torch.nn.LSTMCell

Source: LSTM — PyTorch 2.11 documentation. Checked 2026-10-16.