Negative Sampling › From Softmax to Sigmoid
Language AI HandbookAll chapters Negative Sampling › From Softmax to SigmoidOne section · Sections in this chapter3 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The mathematical reformulation is straightforward. Instead of softmax, we use the sigmoid function to output a probability for each pair independently.
For a positive (center, context) pair, we want the model to output a high probability that this pair comes from the corpus. The sigmoid function maps the dot product of the embeddings to a probability:
where:
- : probability that the word pair is a observed context pair from the data
- : binary indicator variable (1 = observed pair from data, 0 = fake pair)
- : sigmoid function, mapping any real number to the range
- : context embedding vector for word
- : center embedding vector for word
- : dot product between the context and center embeddings, measuring their similarity
When the embeddings are similar (large positive dot product), the sigmoid output approaches 1, indicating high confidence that the pair comes from the corpus.
For negative pairs, we want , or equivalently . Using the property that , we write:
where:
- : a negative (randomly sampled) word that did not appear in the context
- : context embedding vector for the negative word
When the embeddings are dissimilar (negative dot product), approaches 1, correctly indicating that the pair is likely fake.
def sigmoid(x):
"""Compute sigmoid function with numerical stability."""
x = np.clip(x, -500, 500)
return 1 / (1 + np.exp(-x))
# Example: probability computation for word pairs
dot_product_positive = 2.5 # High similarity (learned)
dot_product_negative = -1.0 # Low similarity (learned)
prob_positive = sigmoid(dot_product_positive)
prob_negative = sigmoid(dot_product_negative)Sigmoid Probabilities for Word Pairs: -------------------------------------------------- Positive pair dot product: 2.5 P(real pair) = sigma(2.5) = 0.9241 Negative pair dot product: -1.0 P(real pair) = sigma(-1.0) = 0.2689 A well-trained model assigns high probability to observed pairs and low probability to fake pairs.
The positive pair with dot product 2.5 receives probability 0.92, indicating high confidence that this is a observed context pair. The negative pair with dot product -1.0 receives only 0.27 probability, correctly reflecting low confidence.
