Sub topic 10
Hierarchical Softmax
This chapter assumes you know vectors, dot products, the sigmoid function, conditional probability, and gradient descent. It also assumes you understand that word2vec learns word vectors by predicting nearby words.
Replacing the full softmax
In skip-gram, the model must score every word in the vocabulary when predicting a context word. With vocabulary size \(V\), a full softmax can cost \(O(V)\) for each training pair. This is expensive when \(V\) is large.
Hierarchical softmax changes the prediction task. Instead of choosing one word from \(V\) possibilities directly, it chooses a path through a binary tree. Each internal tree node contains a binary classifier.
A vocabulary word appears at a leaf. To predict that word, the model predicts each left-or-right decision on the path from the root to that leaf.
Computing a path probability
Each internal node has a vector \(un\). Given an input word vector \(vw\), the node computes:
\sigma(u_n^\mathsf{T}v_w)where \(\sigma\) is the sigmoid function. This value represents the probability of taking the branch assigned the positive direction. If the desired branch has the opposite direction, its probability is:
1-\sigma(u_n^\mathsf{T}v_w)Using a sign of \(+1\) for one branch and \(-1\) for the other gives one compact expression:
P(\text{branch})=\sigma(s \, u_n^\mathsf{T}v_w)The probability of the target word is the product of the probabilities for all decisions along its path.
import numpy as np
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
def path_probability(input_vector, path):
"""path contains (node_vector, branch_sign) pairs."""
probability = 1.0
for node_vector, sign in path:
score = sign * np.dot(node_vector, input_vector)
probability *= sigmoid(score)
return probability
v_word = np.array([0.8, -0.2])
root = np.array([0.5, 0.1])
node_a = np.array([0.2, 0.7])
# The target takes the positive branch at the root,
# then the negative branch at node_a.
path = [(root, +1), (node_a, -1)]
print(path_probability(v_word, path))Why it is faster
A balanced binary tree has a path of about \(\log_2 V\) nodes. Hierarchical softmax therefore needs about \(O(\log V)\) binary decisions instead of \(O(V)\) word scores.
The tree is often built with a Huffman coding algorithm. Huffman coding gives frequent words shorter paths and rare words longer paths. This puts more training work on common words, which can improve efficiency.
Trade-offs and selection
Hierarchical softmax has these properties:
- It uses a global tree structure rather than sampled negative examples.
- It gives every training example a path to its target word.
- Its cost is predictable, approximately \(O(\log V)\).
- Results depend on the tree, so the tree structure can affect learned vectors.
- Negative sampling is often preferred when the goal is high-quality similarity features, while hierarchical softmax can be useful for representing the full vocabulary efficiently.
Exercise
Why does hierarchical softmax reduce the cost of predicting a word from O(V) to approximately O(log V)?
Show answer
It replaces one decision among V words with binary decisions along a tree path, and a balanced tree has about log2(V) decisions.