TL;DR
A softmax over classes needs scores and a sum over all of them, so every prediction costs time proportional to . With a vocabulary of hundreds of thousands of words, that is the bottleneck. Hierarchical softmax arranges the classes as the leaves of a binary tree. To predict a class, the model makes one yes-or-no decision at each internal node on the path from the root to that leaf. The probability of the class is the product of those decisions, and only the nodes on one path are ever computed.
Because a balanced tree over leaves has depth about , the cost drops from to about per prediction. The probabilities still sum to one over all classes, so it is a true distribution, not an approximation.
Where you'll see it: in word2vec-style training of word embeddings, and in older neural language models with very large vocabularies, where it made training practical before fast hardware made the full softmax cheap enough.
Intuition
Twenty questions instead of a long list
Imagine guessing a word from a dictionary by asking yes-or-no questions. You could check every entry one at a time, which takes as long as the dictionary is long. Or you could ask "is it in the first half of the alphabet?", then "in the first half of that?", and so on. Each answer halves the candidates, so about questions identify any entry. Hierarchical softmax does exactly that, except that each question is a learned probability rather than a fact.
The classes are the leaves of a binary tree. Each internal node holds a small learned classifier that decides between its left and right subtree. The model does not compute a score for every class. It computes one score per node on the path it cares about.
flowchart TD R["Root: animal or vehicle?"] -->|left| A["Node: mammal or bird?"] R -->|right| B["Node: car or boat?"] A -->|left| C["cat"] A -->|right| D["dog"] B -->|left| E["car"] B -->|right| F["boat"]
To find the probability of "cat", the model multiplies the probability of going left at the root by the probability of going left at the mammal node. It never touches the car, boat or bird parts of the tree.
Why the tree shape matters
The leaves can be arranged in any binary tree, and the arrangement changes the cost. A balanced tree gives every class the same path length. A Huffman tree, built by repeatedly merging the two least frequent items until one tree remains, gives frequent words short paths and rare words long ones. The average path length then stays within one step of the Entropy (in bits) of the word distribution, so the common words that dominate the training data are the cheapest to predict. Rare words still work, just at a higher cost.
What the tree does not change
The output is still a full probability distribution over all classes. Hierarchical softmax is an exact reparameterisation, not a sampling trick. It changes how the probabilities are factored, and it changes how many numbers are touched to compute one of them.
The math
Setup
Let be the hidden vector that the network produces for one prediction, such as the context around a word. Let the classes be the leaves of a binary tree . Each internal node has a learned vector . Put a score on each node:
Here is the dot product between the node's vector and the hidden vector, a single real number. There are internal nodes in a binary tree with leaves, so the number of output parameters is about the same as in a flat softmax, which has output vectors.
Decisions at a node
At each internal node, the probability of going left is the sigmoid of its score, and the probability of going right is one minus that. Writing for a left move and for a right move, both cases fit one formula:
Check the two cases: is the left probability and is the right probability. They add to 1, as they must.
Probability of a class
Let the path from the root to leaf pass through internal nodes , with moves along the way. The probability of class is
Here is the depth of leaf , the number of decisions on its path. Each factor is one decision.
The probabilities sum to one. Take any internal node. Its left and right children receive probabilities and , which add to 1. Apply this at the root: the probabilities of all leaves in the left subtree sum to , and those in the right subtree to . Recursively, each subtree's leaves sum to one. So .
Cost
For a flat softmax, computing one probability needs all scores for the normaliser, so the cost is . For hierarchical softmax, computing needs only the nodes on one path, each a dot product of length :
For a balanced tree, for every , so the cost is .
Training
Training maximises the log-likelihood of the observed class . This is a sum over the path:
The derivative of with respect to is . Each node on the path therefore receives an update of size proportional to how wrong its own decision was. Nodes off the path receive no gradient for this example, which is the source of the speed-up in training.
Worked examples
Take a balanced tree with four leaves . The root splits from . Node splits from , and node splits from . Suppose the hidden vector gives scores , and .
- Look up the sigmoids. , , , .
- Class is left at and left at : .
- Class is left at and right at : .
- Class is right at and left at : .
- Class is right at and right at : .
- Check the total: .
Interpretation: the root score favours the left pair, and the node score favours over . The four numbers form a proper distribution, computed with three sigmoids instead of four exponentials and a normaliser. For four classes the saving is small, but the same arithmetic shows why the method scales: a path of length two costs two sigmoids regardless of what the other branches contain.
A language model has a vocabulary of words and hidden size .
- Flat softmax per prediction: multiply-adds, just to compute the normaliser.
- Balanced hierarchical softmax: depth . Cost multiply-adds.
- Ratio: . The tree is about fifty thousand times cheaper per prediction.
- With a Huffman tree the average depth for natural text is much smaller than 20, because most tokens are common words near the root. The exact saving depends on the word frequencies.
Interpretation: at this vocabulary size the flat softmax dominates the training time, and the tree removes almost all of that cost. The price is that the path probabilities must be learned well for every internal node, which is harder to tune than a single output matrix.
Tradeoffs and failure modes
| Situation | What goes wrong | Better choice |
|---|---|---|
| Small vocabulary, a few thousand classes | The tree saves little, and the extra indirection can hurt accuracy | A plain softmax |
| You need the full distribution at inference time | Computing every class's probability still touches every node, so the saving disappears | A plain softmax, which is well optimised on GPUs |
| Very large output layer with a fast GPU | Matrix multiplication is cheap, so the saving matters less | Full softmax, or sampled softmax |
| Training with a very large vocabulary on modest hardware | Nothing: this is the favourable case, since gradients reach only the nodes on each path | Hierarchical softmax, often with a Huffman tree |
| Words are poorly grouped by the tree | A wrong decision near the root cannot be undone further down | A tree built from frequency or from clustering that reflects meaning |
Failure modes are mostly about the tree. A poor split near the root forces every related word to share an early mistake. Rare words sit deep in a Huffman tree, so they get fewer gradient updates and their vectors can stay noisy. Also, hierarchical softmax is a training speed-up that can give a different model than the full softmax, so comparing perplexities across the two needs care: both should be computed the same way at evaluation.
History
The idea of grouping classes into a hierarchy to speed up maximum-entropy models was studied by Joshua Goodman in 2001 under the name class-based models. Frederic Morin and Yoshua Bengio applied a binary-tree factorisation of the output layer to a neural language model at the 2005 AISTATS conference. Their goal was practical: a neural model over a large vocabulary was too slow to train with a full softmax.
Andriy Mnih and Geoffrey Hinton followed with a scalable hierarchical language model around 2008 and 2009, which learned the tree and the word vectors together. The method gained wide use when Tomas Mikolov and colleagues built word2vec in 2013. Their papers used a Huffman tree over word frequencies, so common words were predicted with short paths. That choice made training on billions of words feasible at the time, and it made hierarchical softmax a standard tool for word embeddings. Later work tended to use negative sampling for embeddings and full softmax for large language models on modern hardware, but the tree idea remains the clearest way to see why output-layer cost can be reduced.
Check yourself
QuestionRecall: what does the probability of one class equal in hierarchical softmax?Show answer
It is the product of the probabilities of the decisions on the path from the root to that class's leaf. Each decision is a sigmoid of the node's score, taken with the sign that matches the direction of the move. Nodes off the path do not appear in the product.
QuestionComputation: a class is reached through three decisions with probabilities 0.8, 0.5 and 0.9. What is its probability?Show answer
Multiply the three: . Each decision is a probability in , so the product is smaller than any single factor, which is why deep leaves get small probabilities unless their decisions are confident.
QuestionWhy: why do the probabilities of all leaves sum to exactly one?Show answer
At each internal node the two children receive and , which sum to one. Starting at the root and recursing down, the total mass of every subtree is split between its two children with nothing lost or created. So the leaves of the whole tree sum to one. The proof uses only the rule at each node, so it holds for any tree shape.
QuestionApplied: a model has a vocabulary of 50,000 words and hidden size 512. Roughly how much cheaper is a balanced hierarchical softmax per prediction than a flat softmax?Show answer
The flat softmax costs about million multiply-adds. A balanced tree has depth , costing . The ratio is , so roughly three thousand times cheaper per prediction. A Huffman tree would be cheaper again on average for natural text.
Sources
- Morin and Bengio, Hierarchical Probabilistic Neural Network Language Model (AISTATS, 2005), PMLR
- Mikolov et al., Efficient Estimation of Word Representations in Vector Space (2013), arXiv:1301.3781
- Mikolov et al., Distributed Representations of Words and Phrases and their Compositionality (2013), arXiv:1310.4546
- Softmax function (Wikipedia)