Files
pytorch/docs/cpp/source/api/nn/recurrent.md
T
Svetlana Karslioglu 19da3da3d3 Restructure C++ docs (#174096)
The fix-cpp-build branch is a major restructuring that:

- Drops Exhale (which auto-generated API docs but caused OOM in CI) in favor of hand-written RST pages using Breathe directives
- Reorganizes notes into a hierarchical API reference with proper categories
- Adds new content: XPU docs, stable ABI docs, DeviceGuard/Stream docs
- Always runs Doxygen instead of gating on an env var

Pull Request resolved: https://github.com/pytorch/pytorch/pull/174096
Approved by: https://github.com/janeyx99, https://github.com/seemethere, https://github.com/albanD
2026-04-07 17:24:38 +00:00

2.5 KiB

myst
myst
html_meta
description keywords
Recurrent layers in PyTorch C++ — RNN, LSTM, and GRU modules for sequence modeling. PyTorch, C++, RNN, LSTM, GRU, recurrent, sequence, hidden state

Recurrent Layers

Recurrent layers process sequential data by maintaining hidden state across time steps. They are essential for tasks involving sequences: language modeling, speech recognition, time series prediction, and more.

  • RNN: Basic recurrent layer (simple but prone to vanishing gradients)
  • LSTM: Long Short-Term Memory (gated architecture, handles long-range dependencies)
  • GRU: Gated Recurrent Unit (simpler than LSTM, often similar performance)
  • Cell variants: Single-step versions for custom loop implementations

Key parameters:

  • input_size: Number of features in input
  • hidden_size: Number of features in hidden state
  • num_layers: Number of stacked recurrent layers
  • batch_first: If true, input shape is [batch, seq, features]
  • bidirectional: Process sequence in both directions

RNN

:members:
:undoc-members:
:members:
:undoc-members:

Example:

auto rnn = torch::nn::RNN(
    torch::nn::RNNOptions(128, 256)  // input_size, hidden_size
        .num_layers(2)
        .batch_first(true)
        .bidirectional(false));

auto input = torch::randn({32, 10, 128});  // [batch, seq_len, input_size]
auto [output, hidden] = rnn->forward(input);

LSTM

:members:
:undoc-members:
:members:
:undoc-members:

Example:

auto lstm = torch::nn::LSTM(
    torch::nn::LSTMOptions(128, 256)
        .num_layers(2)
        .batch_first(true)
        .dropout(0.1)
        .bidirectional(true));

auto input = torch::randn({32, 10, 128});
auto [output, state] = lstm->forward(input);
auto [h_n, c_n] = state;  // hidden state, cell state

GRU

:members:
:undoc-members:
:members:
:undoc-members:

RNNCell

:members:
:undoc-members:
:members:
:undoc-members:

LSTMCell

:members:
:undoc-members:
:members:
:undoc-members:

GRUCell

:members:
:undoc-members:
:members:
:undoc-members: