To derive the gradients of the negative sampling loss function ( L ) with respect to the target word embedding ( w ), the positive context word embedding ( c_{\text{pos}} ), and the negative context word embeddings ( c_{\text{neg}_i} ), we'll follow these steps:
Given:
The loss function:
[
L = - \left[ \log \sigma\left( w^\top c_{\text{pos}} \right) + \sum_{i=1}^K \log \sigma\left( - w^\top c_{\text{neg}_i} \right) \right]
]
where ( \sigma(x) = \frac{1}{1 + e^{-x}} ) is the sigmoid function.
First, let’s compute the derivative of the sigmoid function:
[
\frac{d\sigma(x)}{dx} = \sigma(x)(1 - \sigma(x))
]
Next, we'll compute the gradients step by step.
Gradient with respect to the target word embedding ( w ):
Step 1: Compute the derivative of the first term with respect to ( w ):
Let ( z_{\text{pos}} = w^\top c_{\text{pos}} ).
[
\frac{\partial}{\partial w} \left( -\log \sigma\left( z_{\text{pos}} \right) \right) = - \frac{1}{\sigma\left( z_{\text{pos}} \right)} \cdot \sigma\left( z_{\text{pos}} \right)(1 - \sigma\left( z_{\text{pos}} \right)) \cdot c_{\text{pos}} = - (1 - \sigma\left( z_{\text{pos}} \right)) c_{\text{pos}}
]
Step 2: Compute the derivative of each negative term with respect to ( w ):
For each ( i \in {1, \dots, K} ), let ( z_{\text{neg}i} = - w^\top c{\text{neg}_i} ).
[
\frac{\partial}{\partial w} \left( -\log \sigma\left( z_{\text{neg}i} \right) \right) = - \frac{1}{\sigma\left( z{\text{neg}i} \right)} \cdot \sigma\left( z{\text{neg}i} \right)(1 - \sigma\left( z{\text{neg}i} \right)) \cdot (-c{\text{neg}i}) = - \sigma\left( - z{\text{neg}i} \right) c{\text{neg}_i}
]
But since ( \sigma(-x) = 1 - \sigma(x) ), we have:
[
\sigma\left( - z_{\text{neg}i} \right) = \sigma\left( w^\top c{\text{neg}_i} \right)
]
So the derivative becomes:
[
- \sigma\left( w^\top c_{\text{neg}i} \right) c{\text{neg}_i}
]
Step 3: Combine the derivatives:
[
\frac{\partial L}{\partial w} = - \left( -(1 - \sigma\left( w^\top c_{\text{pos}} \right)) c_{\text{pos}} + \sum_{i=1}^K \left( - \sigma\left( w^\top c_{\text{neg}i} \right) c{\text{neg}_i} \right) \right)
]
Simplify:
[
\frac{\partial L}{\partial w} = (1 - \sigma\left( w^\top c_{\text{pos}} \right)) c_{\text{pos}} + \sum_{i=1}^K \sigma\left( w^\top c_{\text{neg}i} \right) c{\text{neg}_i}
]
Gradient with respect to the positive context word embedding ( c_{\text{pos}} ):
[
\frac{\partial L}{\partial c_{\text{pos}}} = - \frac{\partial}{\partial c_{\text{pos}}} \left( \log \sigma\left( w^\top c_{\text{pos}} \right) \right) = - (1 - \sigma\left( w^\top c_{\text{pos}} \right)) w
]
Gradient with respect to each negative context word embedding ( c_{\text{neg}_i} ):
For each ( i \in {1, \dots, K} ):
[
\frac{\partial L}{\partial c_{\text{neg}i}} = - \frac{\partial}{\partial c{\text{neg}i}} \left( \log \sigma\left( - w^\top c{\text{neg}i} \right) \right) = - \sigma\left( w^\top c{\text{neg}_i} \right) w
]
Summary of the gradients:
-
Gradient with respect to ( w ):
[
\boxed{\frac{\partial L}{\partial w} = \left( 1 - \sigma\left( w^\top c_{\text{pos}} \right) \right) c_{\text{pos}} + \sum_{i=1}^K \sigma\left( w^\top c_{\text{neg}i} \right) c{\text{neg}_i}}
]
-
Gradient with respect to ( c_{\text{pos}} ):
[
\boxed{\frac{\partial L}{\partial c_{\text{pos}}} = - \left( 1 - \sigma\left( w^\top c_{\text{pos}} \right) \right) w}
]
-
Gradient with respect to each ( c_{\text{neg}_i} ):
[
\boxed{\frac{\partial L}{\partial c_{\text{neg}i}} = - \sigma\left( w^\top c{\text{neg}_i} \right) w}
]
These gradients can now be used to update the embeddings during training using optimization algorithms like stochastic gradient descent.