Why read letters instead of words?
Most text classifiers start by building a vocabulary of whole words, but surnames break that approach almost immediately. There are effectively unlimited possible names, and any fixed word list will constantly run into names it has never seen, forcing the model to fall back on a generic 'unknown word' token that throws away all useful information. A character-level model sidesteps this entirely: instead of a vocabulary of hundreds of thousands of words, it only needs a vocabulary of a few dozen letters, and every name, no matter how rare or newly invented, can still be spelled out using that small fixed alphabet. This makes the model robust to novel names, typos, and transliterations, and it forces the network to learn something more fundamental than memorized whole-word patterns: the statistical texture of spelling itself.
One character at a time: how the RNN cell updates its hidden state
A simple recurrent neural network keeps a running summary of everything it has read so far, stored as a vector called the hidden state. At each timestep it takes two inputs: the encoding of the current character (often a one-hot vector marking which letter of the alphabet it is) and the hidden state left over from the previous character. These are combined through a small set of learned weights and squashed through a nonlinearity such as tanh, producing a brand-new hidden state that replaces the old one. Critically, the very same weights are reused at every position in the name, so the network applies an identical update rule whether it is looking at the first letter or the fifteenth. Feeding a name like 'Kowalski' into this cell means running this update six, seven, or eight times in a row, each time folding one more letter's worth of information into the accumulating hidden state.
The final hidden state as a fingerprint of the whole name
After the last character has been processed, the hidden state is no longer about any single letter; it is a compressed summary of the entire sequence the network has read, shaped by training to retain whatever details are useful for the task at hand. To turn this into a language prediction, that final vector is passed through one more small layer, typically a linear transformation followed by a softmax, which converts it into a probability distribution over candidate origins such as Russian, Italian, Japanese, or Scottish. In effect, the recurrent cell acts as a feature extractor that boils an arbitrary-length string down to a fixed-size vector, and the final layer acts as the classifier that reads that vector and makes a decision. Training adjusts both parts jointly, using many labeled examples of names and their true origins, so that whatever information the hidden state ends up encoding is exactly the information the classifier needs.
Why this works so well for names specifically
Different languages leave distinctive statistical traces in how they spell things, and much of that signal lives in short character sequences rather than whole-word meaning. Polish surnames frequently end in '-ski' or '-cki', Russian ones in '-ov' or '-ova', Vietnamese names favor short syllables with particular diacritics, and Irish names often begin with 'Mc' or apostrophe-O. A character-level RNN does not need to be handed these rules explicitly: because it processes letters in order and accumulates state, it naturally becomes sensitive to recurring letter combinations and their positions, especially near the end of a name, which is often where the most identifying suffixes live. This is precisely the kind of task where character n-gram statistics do most of the work, and it is why even a fairly small, simple recurrent network can reach respectable accuracy on this classic classification problem.
A stepping stone toward LSTMs and GRUs
The plain RNN cell described here is elegant but has a well-known weakness: for longer sequences, gradients used during training tend to shrink or blow up as they are backpropagated through many repeated timesteps, making it hard for the network to retain information from early characters by the time it reaches the end of a long name. For short surnames this rarely causes serious trouble, which is part of why name classification is such a popular introductory example. But the same limitation becomes a real obstacle on longer sequences like full sentences or paragraphs. That is exactly the problem that gated architectures such as the LSTM and GRU were designed to solve, by adding learned gates that let the network selectively preserve or discard information over much longer spans. Understanding the plain character-level RNN first makes it far easier to appreciate what those extra gates are actually fixing.
Frequently asked questions
Why not just use a word-based model instead of reading letter by letter?
Word-based models need a vocabulary built in advance, and surnames are essentially an open-ended set: new names, spelling variants, and transliterations appear constantly, so a word list will inevitably encounter names it has never indexed. Reading characters instead means the model only needs a small, fixed alphabet, so it can process any name it is given, seen before or not, and it also picks up on sub-word patterns like common prefixes and suffixes that are strong signals of linguistic origin.
What does the hidden state actually contain?
There is no single interpretable meaning for any one dimension of the hidden state; it is a learned numerical vector shaped entirely by training. Informally, it functions as a compressed, running summary of the characters seen so far, with the network free to encode whatever combination of letter identity, position, and recent context turns out to be useful for predicting the correct origin at the end of the sequence.
How does the network decide the final classification?
Once the last character of the name has been processed, the resulting hidden state is passed through a small additional layer, typically a linear transformation followed by a softmax function, which converts the vector into a probability for each candidate language or culture of origin. The predicted origin is simply whichever class receives the highest probability.
Can this simple RNN actually beat random guessing by a lot?
Yes. Because many languages have strongly distinctive character patterns in surnames, particularly in endings, even a small single-layer RNN trained on a modest labeled dataset typically achieves noticeably higher accuracy than chance across a dozen or more origin categories. It will not match a large modern language model, but it demonstrates that useful linguistic signal can be extracted from raw character sequences with a very simple architecture.
Why do people describe this as a stepping stone to LSTMs?
The plain RNN illustrates the core recurrent idea, sequentially updating one hidden state, in its simplest possible form, which makes the training process transparent and easy to reason about. But it also makes the vanishing gradient problem easy to observe on longer inputs, motivating the gated memory mechanisms that LSTMs and GRUs introduce. Learning the plain RNN first gives a clear baseline for understanding exactly what those more complex, gated architectures improve upon.
Try it live
Everything above runs in your browser — open Character-Level RNN for Name Origin Classification and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.
▶ Open Character-Level RNN for Name Origin Classification simulation