Negative Sampling › The Negative Sampling Objective
Language AI HandbookAll chapters Negative Sampling › The Negative Sampling ObjectiveOne section · Sections in this chapter4 / 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
Having established that we can use sigmoid to score individual word pairs, we now face a key question: what exactly should the model optimize? The answer lies in constructing a clever objective function that captures our core intuition: observed context pairs should score high, while fake pairs should score low.
For each positive pair from the corpus, we sample negative words from a noise distribution . The objective for this training example combines both goals into a single expression:
where:
- : objective function to maximize (higher is better)
- : center word embedding vector for word
- : context embedding vector for the positive context word
- : context embedding vector for the -th negative sample word
- : number of negative samples per positive pair
- : sigmoid function
- : positive term, maximized when the positive pair has high similarity
- : negative term, maximized when the negative pair has low similarity (negative dot product)
The positive term rewards the model for assigning high probability to observed pairs. When the dot product is large and positive, the sigmoid approaches 1, and . When the dot product is small or negative, the sigmoid drops toward 0, and is a large negative penalty.
The negative terms reward the model for correctly identifying fake pairs. Notice the negative sign inside the sigmoid. For a negative pair to score well, we need , which happens when the dot product itself is negative (dissimilar embeddings). If the model mistakenly places a negative word close to the center word, the dot product becomes positive, the sigmoid drops, and the log becomes a large penalty.
Together, these terms create opposing forces: the first pulls observed context words toward the center word, while the second pushes random words away. Through millions of such updates, the embedding space self-organizes so that semantically related words cluster together.
def negative_sampling_loss(center_vec, context_vec, negative_vecs):
"""
Compute the negative sampling loss for one training example.
Args:
center_vec: Embedding of the center word
context_vec: Embedding of the positive context word
negative_vecs: List of embeddings for negative samples
Returns:
loss: The negative of the objective (to be minimized)
"""
# Positive term: log sigma(context . center)
pos_dot = np.dot(context_vec, center_vec)
pos_loss = np.log(sigmoid(pos_dot) + 1e-10)
# Negative terms: sum of log sigma(-negative . center)
neg_loss = 0
for neg_vec in negative_vecs:
neg_dot = np.dot(neg_vec, center_vec)
neg_loss += np.log(sigmoid(-neg_dot) + 1e-10)
# We maximize the objective, so return negative for minimization
return -(pos_loss + neg_loss)
# Example with random vectors
np.random.seed(42)
embedding_dim = 50
center = np.random.randn(embedding_dim) * 0.5
context = np.random.randn(embedding_dim) * 0.5
negatives = [np.random.randn(embedding_dim) * 0.5 for _ in range(5)]
loss = negative_sampling_loss(center, context, negatives)Negative Sampling Loss Example: -------------------------------------------------- Embedding dimension: 50 Number of negative samples: 5 Center . Context (positive): 1.0506 sigma(dot) = 0.7409 Center . Negatives: Negative 1: dot = -1.3478, sigma(-dot) = 0.7938 Negative 2: dot = 0.8562, sigma(-dot) = 0.2981 Negative 3: dot = 3.7310, sigma(-dot) = 0.0234 Negative 4: dot = 2.8612, sigma(-dot) = 0.0541 Negative 5: dot = -1.9121, sigma(-dot) = 0.8713 Total loss (to minimize): 8.5505
With random initial embeddings, the dot products are near zero, producing sigmoid values around 0.5. The model is uncertain about all pairs. Training pushes these probabilities toward 1 for positive pairs and toward 0 for negatives.


The loss landscape reveals the asymmetric pressure applied by each term. For positive pairs (left), the loss drops sharply as the dot product increases, strongly encouraging the model to push observed context words closer. For negative pairs (right), the loss drops as the dot product becomes negative. The steepest gradients occur near zero, where the model is most uncertain.
Gradient Computation
The objective function creates a tug-of-war in embedding space. Each training step applies forces that reshape the geometry of word representations. Understanding these gradients reveals how semantic structure emerges from local updates.
During backpropagation, two types of forces act on each embedding. An attractive force from positive pairs pulls the center word embedding toward its true context word. A repulsive force from negative pairs pushes the center word away from each sampled negative. The magnitude of each force depends on the current prediction confidence. If the model already correctly predicts a positive pair, the gradient is small. If it is uncertain or wrong, the gradient is large, focusing learning where it matters most.
Starting from the objective function , we compute gradients using the chain rule and the sigmoid derivative .
Gradient for the center word embedding :
where:
- : always negative since , so this term pulls the center embedding toward the positive context
- : the model's (mistaken) confidence that the -th negative pair comes from the corpus; during gradient ascent, this pushes the center embedding away from each negative
Gradient for the positive context word embedding :
Since the coefficient is negative, this gradient points toward the center word during gradient ascent, pulling the context embedding closer.
Gradient for each negative word embedding :
The positive coefficient means we subtract this during gradient ascent, pushing each negative embedding away from the center word. The key observation is that this gradient is proportional to how much the model currently confuses the negative for a real pair: if the model already assigns low probability to this negative pair, the gradient is small and the update is minor.

The gradient curves cross at zero, where the model is maximally uncertain. For positive pairs, the model learns strongly when embeddings are dissimilar but relaxes when they are already similar. For negative pairs, the opposite holds. This elegant self-regulation emerges naturally from the sigmoid function.