LSTM Output Shape Calculator

Calculate the output shape of a PyTorch LSTM layer. Enter input size, hidden size, number of layers, and bidirectional flag to get exact output dimensions.

Built by Michael Lip

PyTorch LSTM Code Example

import torch
import torch.nn as nn

# Define LSTM
lstm = nn.LSTM(
  input_size=128,
  hidden_size=256,
  num_layers=2,
  batch_first=True,
  bidirectional=True,
  dropout=0.1
)

# Input: (batch, seq_len, input_size)
x = torch.randn(32, 50, 128)
output, (h_n, c_n) = lstm(x)

# output: (batch, seq_len, num_directions * hidden_size)
print(output.shape) # torch.Size([32, 50, 512])

# h_n: (num_layers * num_directions, batch, hidden_size)
print(h_n.shape)   # torch.Size([4, 32, 256])

# c_n: same shape as h_n
print(c_n.shape)   # torch.Size([4, 32, 256])

# Total parameters for bidirectional 2-layer LSTM:
print(sum(p.numel() for p in lstm.parameters())) # 1,839,104

Frequently Asked Questions

What is the LSTM output shape in PyTorch?

For input [batch, seq_len, input_size], the LSTM output shape is [batch, seq_len, hidden_size * num_directions] where num_directions is 2 if bidirectional=True, else 1. The hidden state h_n has shape [num_layers * num_directions, batch, hidden_size].

How does bidirectional affect LSTM output?

Setting bidirectional=True doubles the last dimension of the output. A bidirectional LSTM with hidden_size=256 outputs features of size 512 (256*2). The hidden state also doubles in the first dimension.

How many parameters does an LSTM have?

An LSTM layer has 4 * ((input_size * hidden_size) + (hidden_size * hidden_size) + hidden_size * 2) parameters per layer. For additional layers, input_size becomes hidden_size * num_directions. Multiply by num_layers and num_directions.

Is this tool free?

Yes. All HeyTensor tools are free, run in your browser, and require no signup.

Does this work offline?

Once loaded, the tool runs entirely in your browser. No internet needed after the initial page load.

About This Tool

This tool is part of HeyTensor, a free suite of PyTorch and deep learning utilities. All calculations run entirely in your browser, no calculator inputs or results are sent to any server. The source code is open on GitHub.

Contact

HeyTensor is built and maintained by Michael Lip. For questions or feedback, email [email protected].

TENSOR PREFLIGHT · DOWNLOADABLE PYTORCH TOOLKIT

Take the next step in your own PyTorch model.

Trace a local forward pass, inspect tensor shapes in an HTML report, and turn a repair into a regression check. Includes eight broken-and-fixed workflows, source code and setup instructions.

Is this toolkit right for my problem?

Use the free calculator for a layer formula or a quick shape check.

Consider Tensor Preflight when you have a local PyTorch model and want a forward-pass report plus repeatable checks. Python 3.9+ and PyTorch 2.8+ are required. It does not run your model in this website or automatically repair it.

Open the sample report before buying to check the output format.

Python 3.9+ · PyTorch 2.8+ · Runs locally

Get Tensor Preflight — $29 One-time payment · ZIP download See a sample report
📊 Based on real data from our PyTorch Error Database — 52 errors analyzed from Stack Overflow
By the same builder: GitHub, theluckystrike BeLikeNative, Grammar AI EarlyThunder, Dev Blog Bug Bounty Reality Zovo, AI Dev Tools