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?

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

What's being asked

Whether you can derive the standard "observed minus expected" gradient of a softmax loss and evaluate it.

Key idea

J=−uo⊤vc+log⁡∑weuw⊤vc, so ∇vcJ=−uo+∑wP(w∣c)uw: the expected output vector minus the observed one.

Steps

  1. Write the loss as J=−uo⊤vc+log⁡∑wexp⁡(uw⊤vc).
  2. Differentiate the first term: −uo.
  3. Differentiate the log-sum-exp: 1∑weuw⊤vc∑weuw⊤vcuw=∑wP(w∣c)uw.
  4. Numbers: scores (uw⊤vc)=(1, 1, −1). The exponentials are (2.718, 2.718, 0.368) with sum 5.804, so P=(0.4683, 0.4683, 0.0634).
  5. Expected vector =0.4683(1,0)+0.4683(0,1)+0.0634(−1,0)=(0.4049, 0.4683).
  6. Gradient =(0.4049,0.4683)−(1,0)=(−0.5951, 0.4683). Moving against it increases the score of word 1 and decreases that of word 2, which was wrongly as likely as word 1.
  7. The sum over all ∣V∣ words is the reason the full softmax is expensive, which motivates negative sampling or hierarchical 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: 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?
Next in Sequence models and transformers: How does the GloVe objective J=∑i,jf(Xij)(wi⊤w~j+bi+b~j−log⁡Xij)2 arise from the idea that meaning is carried by ratios of co-occurrence probabilities? What is the role of f?