Book a free call

Transformer vs RNN: How They Differ and When to Use Each

Transformers and recurrent neural networks both handle sequences, but they work in very different ways. Here is how each one reads text and which to pick.

Hertiv engineering teamReviewed by a senior engineer
7 min read
Transformer vs RNN: How They Differ and When to Use Each
On this page

Short answer

A transformer is a neural network that reads a whole sequence at once using attention, while an RNN, or recurrent neural network, reads it one step at a time and carries a hidden state forward. Transformers train faster on long text and power most modern language models, while RNNs are simpler and lighter for short sequences and small devices. The choice depends on sequence length, data size and hardware.

What is the difference between a transformer and an RNN?

A transformer is a neural network that reads a whole sequence at once using attention, while an RNN, or recurrent neural network, reads a sequence one step at a time and carries a hidden state forward. That one difference in how they read explains almost everything else about them.

Both models handle sequences: text, speech, sensor readings and any data where order matters. An RNN processes the first word, updates its memory, then moves to the second word, and so on. A transformer looks at all the words together and works out which ones matter to each other, in parallel.

Transformers power most modern language models, while RNNs are still used for short sequences, small devices and simple forecasting. The choice is not about which model is better in general, but about which one fits the length of your sequences, the size of your data and the hardware you have.

How does an RNN work?

An RNN works by looping through a sequence one step at a time, keeping a hidden state that summarises everything it has seen so far. At each step it takes the next input and the previous hidden state, and produces a new hidden state and an output.

The hidden state is the model's memory. For a sentence, it carries the meaning of the words so far; for a sensor reading, it carries the trend of the last few values. The model learns what to keep in that state and what to forget, which is why it can handle sequences of different lengths.

The weakness is that the steps are sequential, so the model cannot process step ten until it has finished step nine. Long sequences also weaken the memory: by the time the model reaches the fiftieth word, the first word's influence has faded. Variants such as LSTMs and GRUs add gates that help the memory last longer, but they do not remove the sequential bottleneck.

RNNs are still a good fit when sequences are short and the hardware is limited. A small forecasting model on a sensor, or a simple text classifier on a phone, often runs faster and cheaper as an RNN than as a transformer.

How does a transformer work?

A transformer works by using attention to weigh every part of a sequence against every other part, all at once. Instead of reading word by word, it computes a score for how much each word should attend to each other word, then combines the words using those scores.

Attention is the key idea. In the sentence "the bank by the river", attention lets the word "bank" pick up meaning from "river" rather than from a financial context. The model learns these weights from data, and it does so for many different kinds of relationships at the same time, using several attention heads in parallel.

Because nothing is sequential, the whole sequence can be processed in parallel on a GPU, which makes training far faster than an RNN for long text. The trade-off is memory: attention compares every position with every other position, so the cost grows with the square of the sequence length. A sequence twice as long needs roughly four times the attention memory.

Transformers also need position information, since they read everything at once and would otherwise not know the order of the words. A positional encoding is added to each input, giving the model a sense of where each word sits in the sequence.

Transformer vs RNN: side by side

The two models differ in how they read, how they train, how they scale and what they cost to run. The table below puts the main differences in one place.

FeatureRNNTransformer
Reading orderOne step at a timeWhole sequence at once
MemoryHidden state, fades over timeAttention over all positions
Training speedSlow, steps cannot run in parallelFast, parallel on GPUs
Long sequencesStruggles past dozens of stepsHandles thousands of tokens
Memory costGrows with sequence lengthGrows with the square of length
Model sizeUsually smallOften very large
Best forShort sequences, small devicesLanguage, long documents, large data

The table makes the trade-off clear: transformers win on long sequences and large data, while RNNs win on simplicity and low cost. Neither model is right for every job, and the table is the fastest way to see which one fits a given project.

One row deserves extra attention: the memory cost. For a sequence of 100 tokens, attention needs about 10,000 comparisons; for 1,000 tokens, it needs about 1,000,000. That square growth is why very long documents are split into chunks before a transformer reads them.

When should you use each one?

Use a transformer when the task involves language, long sequences or large amounts of training data. Translation, summarising, question answering and text generation are all transformer territory, and the pretrained models available today make starting easy.

Use an RNN when the sequences are short, the data is small and the model must run on limited hardware. A sensor that reports every minute, a short forecasting window or a simple classifier on a phone are all jobs where an RNN is often the simpler and cheaper choice.

Data size matters as much as sequence length. Transformers have many parameters and need a lot of data to train well, while a small RNN can learn from a few thousand examples. If your dataset is small and you cannot use a pretrained model, an RNN or a simpler model may give better results than a transformer trained from scratch.

Many teams start by checking what already exists: a pretrained transformer for the task, or a simple baseline model. Our guide to machine learning algorithms covers the simpler baselines, and comparing against one of them before building a transformer often saves weeks of work.

Example (hypothetical): forecasting 30 days of sales

Example (hypothetical): a shop wants to forecast daily sales for the next 30 days, using the last 90 days of sales as input. The team compares an RNN and a small transformer for the job.

The RNN reads the 90 days one step at a time, and each step takes about 2 milliseconds, so one forecast takes 180 milliseconds. The transformer reads all 90 days in parallel across 6 layers, and each layer takes about 1.5 milliseconds, so one forecast takes about 9 milliseconds.

At 1,000 forecasts a day, the RNN needs 180 seconds of compute and the transformer needs 9 seconds. The transformer is faster here, but it needs more memory and a GPU, while the RNN runs on a small server. For a 90-day window, the team picks the RNN, because the forecast quality is the same and the running cost is lower.

The example shows the real decision: speed is not the only factor. When the window grows to 900 days, the RNN's memory fades and its sequential reading becomes a bottleneck, and that is the point where the transformer earns its extra cost.

What are the common mistakes?

The most common mistake is picking a transformer because it is the newer model, even when the task is simple. A 30-day forecast or a short text classifier does not need attention over thousands of tokens, and a transformer adds cost, memory and complexity for no gain.

The second mistake is the opposite: using an RNN for a long document and wondering why the model forgets the beginning. RNN memory fades over dozens of steps, so a document of several hundred words needs a transformer, or the document needs splitting into short chunks first.

The third mistake is ignoring the square memory cost of attention. A transformer that works fine on 500-token inputs can run out of memory on 4,000-token inputs, because the attention cost grows sixteen-fold. Long inputs should be chunked or summarised before they reach the model.

Finally, many teams skip the simple baseline. A moving average, a small decision tree or a basic RNN often beats a hastily built transformer, and the baseline takes a day to build. If the transformer cannot beat the baseline, the problem is usually the data, not the model.

Key takeaways

  • Transformers read a whole sequence at once with attention; RNNs read one step at a time.
  • Transformers train faster and handle long text; RNNs are simpler and cheaper for short sequences.
  • Attention memory grows with the square of sequence length, so long inputs need chunking.
  • Use a transformer for language and long sequences; use an RNN for short data on small hardware.
  • Always compare against a simple baseline before building a bigger model.

Frequently asked questions

Is a transformer always better than an RNN?

No. Transformers are better for language and long sequences, but RNNs are simpler, cheaper and often better for short sequences and small devices. The right model depends on sequence length, data size and hardware, not on which model is newer.

What is attention in a transformer?

Attention is a mechanism that scores how much each part of a sequence should matter to each other part, then combines them using those scores. It lets the word bank pick up meaning from river instead of from a financial context.

Why are RNNs slow to train?

RNNs process sequences one step at a time, and step ten cannot start until step nine has finished. That sequential chain cannot be split across a GPU, so training a long sequence takes far longer than a transformer that reads everything in parallel.

What is the difference between an LSTM and an RNN?

An LSTM is a type of RNN with gates that control what the hidden state keeps and forgets. The gates help the memory last longer over dozens of steps, but the model is still sequential and still reads one step at a time.

Can I use a transformer for time series forecasting?

Yes, for long windows with complex patterns, a transformer can work well. For short windows and small datasets, an RNN or a simple statistical model is often just as accurate and much cheaper to run.

How long a sequence can a transformer handle?

It depends on memory, because attention cost grows with the square of the length. A sequence of 1,000 tokens needs about a million attention comparisons, so very long documents are usually split into chunks first.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: AI and agent systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Choosing between a transformer and an RNN for your model?

A senior engineer replies within one business day. Or see our Machine Learning Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call