Now that we have learned how to calculate gradients w.r.t. vectors in the
last blog post,
let’s see whether we can apply this technique to another model, the famous
Word2vec.
Basically what Word2vec is trying to do is to predict every word around each word.
It does this by calculating the probability of every possible choice in the
vocabulary to be the context word for each center word.
Concretely, if uo is the vector representing the context word,
and vc is the vector representing the center word, then the probability
of uo being the correct context word is:
y^o=p(o∣c)=∑w=1VeuwTvceuoTvc.
Now we want this to be as high as possible, which means the cost function
is defined as below:
J=CE(y,y^)=−∑i=1Vyilogy^i
Let’s define θo=uoTvc. Then we already know that
∂θ∂J=y^−y.
Now we go back.
∂vci∂J=w=1∑V∂θw∂J∂vci∂θw=w=1∑V(y^w−yw)uwiT=Ui(y^−y)
∂vc∂J=U(y^−y)
∂uki∂J=w=1∑V∂θw∂J∂uki∂θw=(y^k−yk)∂uki∂θk=(y^k−yk)vci
∂uk∂J=(y^k−yk)vc
∂U∂J=vc(y^−y)T