Page 13 of 14 · Neural networks & security
Recurrent networks and attention: models for sequences
Some data only makes sense in order: words in a sentence, events in a log, packets in a session. "Five failed logins, then a success, then sudo" tells a very different story than the same events shuffled. This page follows how networks learned to handle order, from recurrent networks to the attention mechanism behind today's large language models.
A network with memory
A recurrent neural network (RNN) reads a sequence one item at a time. At every step it combines the new input with its own hidden state from the previous step, then passes the updated state forward. The hidden state is the network's memory of everything it has seen so far.
The new memory ht depends on the old memory ht−1 and the current input xt. The same weights W and U are reused at every step, just as a CNN reuses one filter at every position.
Step through an authentication log
A toy RNN with three hand-set memory units, so you can see what each one tracks. A trained RNN learns its own units, which are rarely this easy to read.
The problem: memories fade
To learn from an event 50 steps back, backpropagation has to pass the gradient through all 50 steps, multiplying by roughly the same factor each time. If that factor is below 1, the signal shrinks toward zero (vanishing gradient). Above 1, it blows up (exploding gradient).
LSTM (long short-term memory, 1997) and GRU networks fixed much of this with gates: small learned switches that decide what to write into memory, what to keep, and what to forget. A gate set to "keep" lets information and gradients pass through many steps nearly unchanged. LSTMs dominated speech recognition and translation through the mid-2010s.
Attention: look at everything at once
RNNs still read one word at a time and squeeze everything into one hidden state. Attention lets each word look directly at every other word and decide how much each one matters to its meaning. Select a word to see where it attends; the shading and percentages show the attention weights.
Weights here are hand-set to illustrate the idea. In a real transformer they are computed from learned query and key vectors, and there are many attention "heads," each looking for different relationships.
Each word produces a query (what am I looking for?), a key (what do I contain?), and a value (what do I pass along?). Matching queries against keys gives the weights; softmax (page 08) makes them sum to 1.
Transformers and LLMs
The 2017 paper "Attention Is All You Need" dropped recurrence entirely and built a model from stacked attention layers: the transformer. Because every word is processed at the same time instead of one after another, transformers train efficiently on GPUs and scale to enormous datasets. Large language models such as GPT, Claude, and Gemini are transformers trained to predict the next token across huge amounts of text.
Reads in order, one step at a time. Memory fades over long sequences. Still used for small, streaming, or low-power tasks.
Sees the whole sequence at once through attention. Parallel and scalable. The basis of modern language, vision, and code models.
import torch.nn as nn lstm = nn.LSTM(input_size=32, hidden_size=64, batch_first=True) # reads sequences of 32-number events attn = nn.MultiheadAttention(embed_dim=64, num_heads=4, batch_first=True)
- RNNs carry a hidden state forward through a sequence, reusing the same weights at every step.
- Plain RNNs struggle with long-range memory because gradients vanish or explode; LSTMs and GRUs use gates to fix this.
- Attention lets every position weigh every other position directly; transformers are built from it.