Sequence representation learning
Encode ordered feature vectors such as sensor readings or event embeddings.
PyTorch · Class
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.
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.
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)| Name | Type | Description |
|---|---|---|
input_size | int | Number of expected features in each input timestep. |
hidden_size | int | Number of features in each hidden state. |
num_layers | int | Number of recurrent layers; default 1. |
bias | bool | Whether to use input-hidden and hidden-hidden bias weights; default True. |
batch_first | bool | If True, input and output use (batch, sequence, feature) rather than (sequence, batch, feature); default False. |
dropout | float | Dropout probability between recurrent layers except the last; default 0. |
bidirectional | bool | Whether to use a bidirectional GRU; default False. |
device | torch.device | Optional tensor device. |
dtype | torch.dtype | Optional tensor data type. |
Returns a tuple containing the output sequence and the final hidden state.
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.
Encode ordered feature vectors such as sensor readings or event embeddings.
Model temporal dependencies for forecasting or classification.
Process token or embedding sequences with recurrent state.
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.
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.
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.RNNtorch.nn.LSTMtorch.nn.GRUCell