Recurrent Neural Network

Machine Learning
Introduced by
Rumelhart, D. E.; Elman, J. L.
Year
1986–1990
Type
Sequential neural network
Family
Deep learning / NLP / CV

What does it do?

A Recurrent Neural Network (RNN) is a class of neural network that processes sequential data by maintaining a hidden state that is carried across time steps.

The simple recurrent form was popularised for modelling temporal structure by Elman (1990), while the backpropagation method on which RNN training relies is described by Rumelhart, Hinton and Williams (1986). RNNs were a leading architecture for sequence modelling in natural language processing, speech and time-series analysis before attention-based models became dominant.

The intuition behind the method

Unlike feedforward networks that treat each input independently, an RNN processes a sequence one element at a time and passes a summary of what it has seen so far — its hidden state — to the next step. Think of it like reading a sentence word by word: at each word, you update your understanding in light of all previous words. This recurrent connection allows the network to capture temporal dependencies and context, making it naturally suited to tasks where order matters and past context shapes the meaning of the current element.

History

Elman's 1990 paper popularised the simple recurrent network for cognitive modelling. Its recurrent links feed hidden-unit patterns back to the network as a dynamic memory; the approach builds on a proposal first described by Jordan (1986), whose network instead fed back its outputs. Training such networks relies on the backpropagation algorithm of Rumelhart, Hinton and Williams (1986), which is applied to recurrent networks by unrolling them over time. Through the 1990s and 2000s RNNs were applied to speech recognition and language modelling, but the vanishing gradient problem limited how much long-range context they could learn, which motivated gated architectures such as LSTM and GRU.

When to use it

Choose an RNN when the input is a sequence in which order carries meaning, such as text, speech, sensor streams, time series or biological sequences, and when those sequences are not very long. It is a reasonable choice when a compact recurrent model is sufficient, or when you plan to compare against gated variants such as LSTM and GRU. Avoid a vanilla RNN when the task depends on long-range context, because the vanishing gradient problem makes early information hard to learn; gated variants are usually the better starting point there. For most natural language understanding tasks, pretrained transformer models are generally stronger, so RNNs are more often used as components in larger systems or as baselines. A simple decision rule is to benchmark a vanilla RNN against a GRU or LSTM with a matched parameter count on validation sequences of the length your application needs, and to adopt the gated model if it improves validation performance at acceptable cost. Treat the hidden size as a capacity setting tuned on validation data rather than a fixed default.

Limitations and considerations

  • Vanishing and exploding gradients make it difficult to learn dependencies across long sequences; gated variants such as LSTM or GRU are usually preferred in that regime.
  • Sequential computation cannot be parallelised across time steps, making training slow on long sequences compared to convolutional or transformer architectures.
  • On many NLP benchmarks, pretrained transformer models such as BERT and RoBERTa are generally stronger than RNN-based models.
  • Limited memory capacity: the fixed-dimensional hidden state struggles to compress long-range context.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Accepts sequences of any length, since the same transition is applied at every step; batched training usually still requires padding or length bucketing.
  • Parameter sharing across time steps keeps model size manageable relative to input length.
  • Forms the conceptual and practical foundation for LSTM, GRU, and many modern sequence models.
  • Suitable as a lightweight baseline for sequence classification and regression before more expensive architectures.
  • Can produce outputs at every time step, enabling many-to-many tasks such as sequence tagging and translation.

Applications

  • Text classification and sentiment analysis on short-to-medium length documents.
  • Language modelling and next-word prediction at character or word level.
  • Time-series forecasting for sensor, financial, or physiological data of moderate length.
  • Sequence tagging tasks such as part-of-speech tagging when used as an encoder.
  • Speech-to-text and audio feature extraction pipelines.
  • Baseline sequence modelling before upgrading to LSTM, GRU, or transformer architectures.

How it works

How it works

1

The input is a sequence of vectors (words, time steps, frames) fed to the network one element at a time. Each element x_t is paired with the hidden state h_{t-1} carried from the previous step, allowing context to accumulate across the entire sequence. The initial hidden state h_0 is commonly set with all entries equal to zero.

2

At each step t, the hidden state is updated as h_t = tanh(W_h * h_{t-1} + W_x * x_t + b). The weight matrices W_h and W_x are shared across all time steps, so the model learns a single transition rule that is applied repeatedly, regardless of sequence length.

Formula
h_t = \tanh(W_h h_{t-1} + W_x x_t + b)
3

An output y_t is computed from the hidden state at each time step (many-to-many) or only at the final step (many-to-one). For classification tasks the final hidden state is passed to a softmax layer; for sequence-to-sequence tasks outputs are produced at every step.

Formula
y_t = W_y h_t + b_y
4

Training uses backpropagation through time (BPTT): the network is unrolled over the sequence, and gradients flow backward through the unrolled steps to update all shared weights. Full BPTT stores activations for every time step, so memory and computation grow with sequence length. For long series, truncated BPTT is the usual practical approach: the sequence is processed in chunks and gradients are propagated back only over a fixed window, which limits the length of dependencies the model can learn directly. Gradients that pass back through many steps can vanish or explode, so early time steps are not reliably updated for long sequences.

5

Shared weights W_h, W_x, and W_y are updated via gradient descent or an adaptive optimizer (Adam, RMSProp). Gradient clipping is commonly applied to prevent exploding gradients. The same weights govern every step, so the model generalises across varying sequence lengths.

Common pitfalls

  • Applying a vanilla RNN to long sequences without acknowledging the vanishing gradient limitation and benchmarking against LSTM/GRU.
  • Failing to clip gradients, leading to NaN losses or unstable training on longer inputs.
  • Treating the final hidden state as a rich summary without checking whether early-sequence information is actually retained.
  • Skipping a proper train/validation/test split or cross-validation, especially given RNNs' tendency to overfit small datasets.
  • Not normalising or scaling input features, which amplifies gradient instability.

Frequently asked questions

What is the difference between an RNN, an LSTM, and a GRU?

An RNN is the simplest recurrent architecture: a single hidden state is updated at each step. LSTM adds gates that regulate what information is kept or discarded, which helps preserve long-range information and mitigates vanishing gradients. GRU is a streamlined gated variant with fewer parameters. Gated models generally perform better than a vanilla RNN on tasks that require memory over long spans, though the comparison should be checked on your own data.

Why does training an RNN sometimes produce NaN losses?

NaN losses are often associated with exploding gradients, so gradient clipping and a lower learning rate are the usual first remedies. Normalising input features also helps stabilise training. If instability persists, a gated architecture such as LSTM or GRU is a common next step, since its gating tends to make optimisation more stable.

Can I still use an RNN for NLP tasks today?

For production NLP, pretrained transformer models such as BERT or RoBERTa are generally the stronger choice. Vanilla RNNs are most useful as lightweight baselines, as components inside larger architectures, or in settings where model size or compute is tightly constrained.

How do I choose the hidden state dimension?

There is no universal value. Begin with a modest size, monitor validation loss, and increase capacity only if the model shows underfitting. Larger hidden states raise the risk of overfitting when the dataset is small.

What loss function should I use for sequence classification?

For binary classification use binary cross-entropy; for multi-class use categorical cross-entropy with softmax on the final hidden state. For sequence labelling (one output per step), apply the loss at every time step and average or sum across steps.

Related methods

Sources

  1. Elman, J. L. (1990). Finding structure in time. Cognitive Science, 14(2), 179–211.

    Open DOI reference
  2. Rumelhart, D. E., Hinton, G. E., & Williams, R. J. (1986). Learning representations by back-propagating errors. Nature, 323(6088), 533–536.

    Open DOI reference
  3. 2018_Book_IntroductionToDeepLearning, p. 149