Skip-gram with negative sampling uses the loss J=−log⁡σ(uo⊤vc)−∑k=1Klog⁡σ(−uk⊤vc), where vc is the centre-word vector, uo the true context vector and uk the vectors of K sampled negative words. Derive ∂J/∂vc. Then, for vc=(1,0), uo=(0.5,0.5) and one negative u1=(1,−1), what are the gradient and the result of one SGD step with learning rate 0.1?

Sequence models and transformers, Word embeddings
Level L3 Intermediate. Length Long. Nature Mat Mathematical.

What's being asked

Whether you can differentiate log-sigmoid terms and see why the update pulls vc towards uo and away from the negatives.

Key idea

Using σ′(x)=σ(x)(1−σ(x)): ∂J∂vc=(σ(uo⊤vc)−1)uo+∑kσ(uk⊤vc) uk.

Steps

  1. For the positive term: ddx[−log⁡σ(x)]=−(1−σ(x))=σ(x)−1, with x=uo⊤vc. By the chain rule, the gradient with respect to vc is (σ(x)−1) uo.
  2. For each negative term: ddx[−log⁡σ(−x)]=σ(x), with x=uk⊤vc, giving σ(uk⊤vc) uk.
  3. Add them to get the formula in the key idea. The first term has a negative coefficient, so a gradient step moves vc towards uo. The negative terms have positive coefficients, so a gradient step moves vc away from the uk.
  4. Numbers: uo⊤vc=0.5, so σ(0.5)=0.6225. u1⊤vc=1, so σ(1)=0.7311.
  5. Gradient =(0.6225−1)(0.5,0.5)+0.7311 (1,−1)=(−0.1888,−0.1888)+(0.7311,−0.7311)=(0.5423, −0.9198).
  6. SGD step: vc←vc−0.1 ∇=(1−0.0542,  0+0.0920)=(0.9458, 0.0920).
  7. Cost: only K+1 output vectors are touched, not the whole vocabulary as in the full softmax.

Common mistakes

How long your answer should be

Half a page; 5 minutes.

Last checked on 4 December 2026.

Read this

Previous in Sequence models and transformers: What is the KV cache in autoregressive transformer decoding? How does it change the cost of generating each token, and how much memory does it need for a 32-layer model with dmodel=4096, a context of 4096 tokens, and fp16 values?
Next in Sequence models and transformers: In skip-gram with a full softmax, P(o∣c)=exp⁡(uo⊤vc)∑wexp⁡(uw⊤vc) and the loss is J=−log⁡P(o∣c). Show that ∂J/∂vc=∑wP(w∣c) uw−uo. For a three-word vocabulary with u1=(1,0), u2=(0,1), u3=(−1,0), centre vector vc=(1,1) and true context word o=1, what is the gradient?