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

PyTorch · Class

nn.GRU

torch.nn.GRU applies one or more gated recurrent-unit layers to an input sequence. It supports configurable hidden size, stacking, dropout between layers, batch-first layouts, and bidirectional processing.

How it works

GRU consumes a sequence one timestep at a time and maintains hidden features through recurrent layers. It can stack layers, optionally apply dropout between them, and process sequences in both directions; the returned sequence output contains timestep results while the final hidden tensor summarizes the last state for each layer and direction.

Syntax

torch.nn.modules.rnn.GRU(input_size, hidden_size, num_layers=1, bias=True, batch_first=False, dropout=0.0, bidirectional=False, device=None, dtype=None)

Parameters

NameTypeDescription
input_sizeintNumber of expected features in each input timestep.
hidden_sizeintNumber of features in each hidden state.
num_layersintNumber of recurrent layers; default 1.
biasboolWhether to use input-hidden and hidden-hidden bias weights; default True.
batch_firstboolIf True, input and output use (batch, sequence, feature) rather than (sequence, batch, feature); default False.
dropoutfloatDropout probability between recurrent layers except the last; default 0.
bidirectionalboolWhether to use a bidirectional GRU; default False.
devicetorch.deviceOptional tensor device.
dtypetorch.dtypeOptional tensor data type.

Returns

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

Key points

  • nn.GRU is a recurrent module for applying one or more gated recurrent layers to sequence data.
  • input_size must match the number of features in each input timestep.
  • hidden_size controls the feature width of each recurrent hidden state.
  • bidirectional=True adds a reverse-direction recurrent pass alongside the forward pass.
  • Setting bias=False removes the input-hidden and hidden-hidden bias parameters.

Common mistakes

  • batch_first changes the input and output layout but does not change the layout of hidden states.
  • The dropout setting has an effect between recurrent layers, not after the final layer; with one layer it does not provide an inter-layer dropout stage.

Example: Process a batch of sequences

import torch
from torch import nn

gru = nn.GRU(input_size=4, hidden_size=8, batch_first=True)
x = torch.randn(2, 5, 4)
y, h_n = gru(x)
print(y.shape, h_n.shape)  # torch.Size([2, 5, 8]), torch.Size([1, 2, 8])

Creates a batch-first GRU, processes two sequences of length five, and returns outputs for every timestep plus the final hidden state.

When to use it

Sequence representation learning

Encode ordered feature vectors such as sensor readings or event embeddings.

Time-series modeling

Model temporal dependencies for forecasting or classification.

Sequence-to-sequence models

Process token or embedding sequences with recurrent state.

Interview questions

QWhat are the main constructor arguments of nn.GRU?

The constructor takes input_size, hidden_size, num_layers, bias, batch_first, dropout, and bidirectional, with optional device and dtype. input_size describes features per timestep, while hidden_size describes features in each hidden state.

Follow-up: How does bidirectionality affect the output width? A bidirectional GRU has separate forward and reverse directions, so its per-timestep output width is twice hidden_size.

QWhat does batch_first change?

With batch_first=True, sequence tensors use (batch, sequence, feature); otherwise they use (sequence, batch, feature). This option applies to input and output tensors, not hidden states.

Follow-up: What hidden-state layout should be used for a batched one-layer unidirectional GRU? Use (1, batch, hidden_size), independently of batch_first.

QWhat does nn.GRU return?

The module applies a multi-layer gated recurrent unit RNN to an input sequence and returns both the output sequence and the final hidden state. The output contains results across timesteps, while the hidden result contains the final state for every layer and direction.

Follow-up: Why might a model use the final hidden state instead of the whole output sequence? It provides a compact sequence summary when downstream processing needs one representation per sequence.

  • torch.nn.RNN
  • torch.nn.LSTM
  • torch.nn.GRUCell

Source: GRU. Checked 2026-10-16.