Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

A recurrent neural network (RNN) is a neural network for ordered data. It reads one item at a time while carrying a learned, compressed hidden state from earlier steps to later ones. That recurrence lets the model use context in text, sensor readings, audio, events and time series instead of treating every observation as unrelated.

For a sequence x1, …, xT, a simple RNN computes ht = φ(Wxhxt + Whhht−1 + bh) and may produce ŷt = g(Whyht + by). The same weights are reused at every time step. This provides memory-like behavior, but the state is a finite representation, not a perfect record of the entire past.

Why ordinary neural networks struggle with sequences

A feed-forward network normally receives a fixed-size feature vector and computes an output without an inherent notion of “before” and “after.” Applying it independently to each time step loses relationships between observations. Padding every sequence to a common length can also waste computation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Consider “The keys to the cabinet …”. The likely verb depends on earlier words, not just the final token. An RNN processes the sentence in order and updates a state that summarizes preceding context. The same idea applies to a machine sensor whose current reading is meaningful only alongside recent readings. The summary can forget or distort details when sequences are long; it is learned compression, not literal storage.

RNNs are designed for variable-length sequences because the recurrent cell can be applied as many times as the input contains steps. TensorFlow describes this stepwise state processing for time series and language data in its RNN guide.

The basic vanilla RNN cell

One recurrent update

  1. Read the current input xt.
  2. Combine it with the previous state ht−1.
  3. Apply a nonlinear function, commonly tanh, to create ht.
  4. Map the new state to an output when the task requires one.

The compact equations are:

ht = tanh(Wxhxt + Whhht−1 + bh)

ŷt = softmax(Whyht + by)

Wxh maps input features to hidden units, Whh carries recurrent information, and Why produces task outputs. A regression head might be linear; a binary classifier might use a sigmoid. The hidden vector is a set of activations updated by learned weights, not a separate database.

Unrolling through time

x1 ──► [RNN cell] ──► h1 ──► y1
          ▲
          │
        h0

x2 ──► [RNN cell] ──► h2 ──► y2
          ▲
          │
        h1

x3 ──► [RNN cell] ──► h3 ──► y3
          ▲
          │
        h2

When unrolled, the cell appears once per step, but every copy shares parameters. Parameter sharing keeps the model compact and lets it process sequences of different lengths. Recurrence means ht depends on ht−1; it does not imply human-like or biological feedback.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Input and output arrangements

Pattern What it does Examples
One-to-one One vector produces one output Ordinary non-sequential prediction
Many-to-one One output summarizes a sequence Sentiment, activity or patient-level classification
One-to-many One input starts a generated sequence Caption, music or text generation
Many-to-many, aligned One output for each input step Part-of-speech tagging, frame labels, sensor anomalies
Many-to-many, encoder–decoder One sequence is encoded and another is generated Translation, sequence forecasting, dialogue responses

Encoder–decoder inputs and outputs do not need to have equal lengths.

How RNNs are trained

Forward pass and loss

  1. Turn raw records into ordered input and target sequences.
  2. Run the recurrent cell through each input step.
  3. Compute a loss for the selected output positions.
  4. Propagate gradients through the unrolled graph.
  5. Update the shared weights and repeat over batches and epochs.

For next-token prediction, an input such as “the cat sat” can have targets “cat sat down”; cross-entropy is calculated at each position and summed or averaged. For a regression sequence, mean squared error can be written as L = (1/T) Σt(yt − ŷt)².

Backpropagation through time

Backpropagation through time (BPTT) is ordinary backpropagation after the recurrence has been unrolled. Since one parameter is used at many steps, its total gradient is the sum of contributions from each use: ∂L/∂W = Σt(∂L/∂W)|t. The error signal must travel backward through the chain of state transitions. See the mathematical treatment in Dive into Deep Learning and its classic edition.

Truncated BPTT

For a long stream, training can backpropagate through only a fixed recent window. This lowers memory and computation costs, but dependencies outside that window receive no direct gradient path. Keep three concepts separate: how many steps are processed in the forward pass, how many steps gradients traverse, and whether the hidden state is carried between chunks. Detaching a carried state limits gradient history even when the forward state continues.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Vanishing and exploding gradients

During BPTT, derivatives contain repeated products of recurrent matrices and activation derivatives. A simplified term is:

∂hT/∂ht ≈ Πk=t+1T WhhT diag(φ′(ak)). If effective factors are usually below one, the product shrinks exponentially: the vanishing-gradient problem. If they exceed one, it can grow: exploding gradients. Early inputs then receive little learning signal, or updates become unstable and produce huge values or NaNs. Repeated-products analysis is discussed in Dive into Deep Learning, Deep Learning, and a modern survey at arXiv:2304.11461. Gradient clipping helps with large updates; it does not restore information already lost through vanishing gradients.

How LSTMs preserve information

An LSTM maintains both a hidden state ht and a cell state ct. A standard formulation is:

it = σ(Wiixt + Whiht−1 + bi)

ft = σ(Wifxt + Whfht−1 + bf)

gt = tanh(Wigxt + Whght−1 + bg)

ot = σ(Wioxt + Whoht−1 + bo)

ct = ft ⊙ ct−1 + it ⊙ gt; ht = ot ⊙ tanh(ct).

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Input gate: controls writing of candidate information.
  • Forget gate: controls retention of the old cell state.
  • Candidate: proposes new content.
  • Output gate: controls what becomes visible as the hidden state.

The additive cell update creates a more direct path for information and gradients than repeatedly replacing one state. It mitigates, rather than guarantees the elimination of, long-range optimization problems. The formulation is documented in PyTorch’s LSTM reference. Hochreiter and Schmidhuber introduced LSTM in 1997; the original paper is at bioinf.jku.at.

How GRUs differ from LSTMs

A GRU uses one hidden state and typically a reset gate, update gate and candidate state:

rt = σ(Wirxt + bir + Whrht−1 + bhr)

zt = σ(Wizxt + biz + Whzht−1 + bhz)

nt = tanh(Winxt + bin + rt ⊙ (Whnht−1 + bhn))

ht = (1 − zt) ⊙ nt + zt ⊙ ht−1.

The reset gate controls prior-state contribution to the candidate; the update gate chooses how much old state to retain. GRUs have no separate cell state and generally use a simpler gated structure, but actual speed and accuracy depend on implementation, hardware, sequence length and configuration. PyTorch notes that its GRU operation ordering can differ subtly from the original paper; consult the exact GRU documentation. GRUs were introduced in Cho and colleagues’ 2014 work at arXiv:1406.1078.

Model State design Strength Limitation Starting use
Vanilla RNN Hidden state Small and easy to explain Weak long-range memory Short sequences and baselines
LSTM Hidden plus cell state; three principal gates Well-established long-range retention More parameters and computation Delayed or irregular dependencies
GRU Hidden state; reset and update gates Compact gated baseline No separate cell-state pathway Resource-conscious experiments

Important RNN variants

Bidirectional RNNs

A bidirectional model runs one recurrent layer from first to last and another from last to first, then commonly concatenates their states: ht = [→ht; ←ht]. It is useful for offline tagging, speech labeling and document analysis when the complete sequence is available. It is unsuitable for strictly causal streaming because the backward pass uses future observations. TensorFlow’s guide describes this distinction at tensorflow.org.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Stacked RNNs

Stacking recurrent layers lets lower layers learn local temporal features and higher layers learn more abstract patterns. Capacity, memory use and optimization difficulty all increase, as can overfitting. In Keras, an intermediate recurrent layer needs return_sequences=True to pass every time step to the next recurrent layer; the time-series tutorial explains this at tensorflow.org.

Stateful and stateless processing

Stateless training initializes each independent sequence, often with zeros. Stateful processing carries a state between chunks of one deliberately ordered stream. Reset state at real sequence boundaries. Carrying state from one unrelated example to another leaks information and can corrupt evaluation; state persistence is a data-ordering decision, not merely a layer setting.

Teacher forcing

For autoregressive generation, teacher forcing feeds the true previous token during training. It speeds learning but creates exposure bias because inference feeds the model’s own prior prediction. Scheduled sampling, sequence-level objectives and decoding changes are possible mitigations, each with trade-offs.

Preparing data correctly

  • Split chronologically when observations are time-dependent; do not let future windows enter training.
  • Fit normalization statistics on the training portion only.
  • Define input offsets and forecast horizons explicitly.
  • Pad variable-length sequences with a distinguishable value and mask padded positions in the model and loss.
  • Use packing or length bucketing where the framework supports it.
  • Ensure sorting, packing and batch order agree with the framework API.
  • Reset state between unrelated examples and detach carried state between truncated-BPTT chunks.

Masking and recurrent-layer behavior vary by framework and version, so verify the installed release documentation at TensorFlow/Keras or the relevant PyTorch reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Minimal implementations

Keras

import keras
from keras import layers

model = keras.Sequential([
    layers.Input(shape=(None, 10)),
    layers.SimpleRNN(64),
    layers.Dense(1)
])
model.compile(optimizer="adam", loss="mse")

For one output per step, use layers.LSTM(64, return_sequences=True) before the final dense layer. With return_sequences=False (the usual default), the recurrent layer returns its final output rather than the whole sequence.

PyTorch

import torch
from torch import nn

class SequenceModel(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super().__init__()
        self.rnn = nn.RNN(input_size, hidden_size, batch_first=True)
        self.output = nn.Linear(hidden_size, output_size)

    def forward(self, x):
        sequence_output, final_hidden = self.rnn(x)
        return self.output(sequence_output[:, -1, :])

With batch_first=True, the usual input layout is (batch, sequence_length, features). Multilayer, bidirectional and batch settings change output and hidden-state shapes; check the exact installed PyTorch version, especially for masking, packed sequences, dropout and optimized kernels.

Implementation checklist

  • Normalize with training-set statistics.
  • Preserve chronology in validation and testing.
  • Choose many-to-one or many-to-many targets deliberately.
  • Mask padding and use a loss matching the target type.
  • Monitor gradient norms and clip gradients when they explode.
  • Overfit a tiny sample to verify alignment and model wiring.
  • Test the actual deployment horizon, latency and memory—not only one-step accuracy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Where RNNs are useful

Typical applications include forecasting, industrial telemetry, sensor anomaly detection, physiological signals, speech and audio classification, character or token language modeling, event streams, handwriting and gesture recognition, sequence labeling, translation and recurrent image-caption decoders. NVIDIA lists representative recurrent applications at developer.nvidia.com; TensorFlow discusses time series and natural language at tensorflow.org.

When another architecture is better

Requirement First candidates Reason
Short sequence, simple baseline or teaching Vanilla RNN Minimal state and computation
Long or delayed dependencies LSTM Gated cell-state pathway
Compact gated model GRU Fewer state components; validate empirically
Future context is available offline Bidirectional RNN Uses both directions
Large data, parallel training or broad context Transformer Attention accesses many positions and parallelizes training
Bounded receptive field and parallel temporal computation Temporal convolutional network Convolutional optimization with causal or non-causal options
Scarce data, strong seasonality or interpretability Classical time-series model Deep-learning overhead may not be justified

RNNs remain attractive for streaming, stateful, low-latency or memory-constrained systems. Transformers are not universally superior: their benefits depend on context length, data, compute and deployment constraints.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshooting common failures

Nearly constant predictions

  • Check target offsets and feature scaling.
  • Compare with a constant baseline and overfit a tiny batch.
  • Inspect whether padding dominates the loss.
  • Review hidden size, regularization, learning rate and class balance.

NaN loss

  • Search inputs, labels, activations and gradients for non-finite values.
  • Lower the learning rate and clip gradients.
  • Use numerically stable losses and temporarily disable mixed precision.
  • Check invalid logarithms, probabilities and malformed masks.

Suspiciously strong validation

Look for random temporal splits, future-derived features, normalization fitted on all data, duplicate windows, or hidden state carried from training into validation. These are leakage patterns, not evidence of a powerful model.

Long-generation drift

Autoregressive errors accumulate. Evaluate at the deployment horizon, verify inverse scaling, compare training and inference horizons, and inspect exposure bias rather than relying only on one-step metrics.

Shape or state errors

Check feature dimensions, sequence lengths, padding conventions, batch ordering, final partial batches, state resets and hidden-state detachment. A bidirectional model that succeeds offline but fails online is usually using unavailable future context.

Frequently Asked Questions

Does an RNN remember the whole sequence?

No. Its hidden state is a learned, finite-dimensional summary. It can retain useful context but may forget details, especially across long sequences.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do LSTMs eliminate vanishing gradients?

No. Their gated additive cell state makes long-range optimization easier and often mitigates vanishing gradients, but retention and training are not guaranteed.

Are GRUs always faster than LSTMs?

No. GRUs are structurally simpler, but measured speed depends on implementation, hardware, sequence length, batch size and configuration.

Can a bidirectional RNN be used for real-time prediction?

Only if future observations are available. A backward pass requires the complete sequence, so strict streaming systems should use a causal model.

The Bottom Line

Choose recurrence when ordered state, streaming behavior or a small deployment footprint matters. Start with a vanilla RNN for a short, transparent baseline; use an LSTM or GRU when dependencies span longer intervals; and compare against Transformers, temporal convolutions or classical models when parallel training, long context, limited data or interpretability changes the trade-off.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.