Implement the Skip-gram with Negative Sampling loss for one positive center-context pair and a set of sampled negative words.
L=−logσ(uoTvc)−i=1∑Klogσ(−uiTvc)Here, vc is center_vec, uo is pos_vec, ui are the rows of neg_vecs, K is the number of negatives, and σ is the logistic sigmoid. Return the loss as a scalar float64 PyTorch tensor and keep it finite for large finite dot products.
Examples
Input: center_vec = [0.5,-0.2], pos_vec = [0.3,0.4], neg_vecs = [[-0.1,0.2],[0.4,-0.3]]
Output: 2.139492
Explanation: The loss combines one positive softplus term with the sum of the two negative terms.
Input: center_vec = [0.2,0.1,-0.4], pos_vec = [0.5,-0.2,0.3], neg_vecs = [[0.1,0.1,0.1]]
Output: 1.401507
Input: center_vec = [0.1,-0.2,0.3,-0.4], pos_vec = [0.2,0.2,-0.1,0.5], neg_vecs = [[0.1,0,-0.2,0.3],[-0.4,0.1,0.2,-0.1],[0,0.3,-0.3,0.2]]
Output: 2.735787
Hint 1
Use torch.dot for the positive pair and a matrix-vector product for all negatives.
Hint 2
The stable loss is F.softplus(-positive_score) plus F.softplus(negative_scores).sum().
Requirements
- Use PyTorch.
- Compute one positive score and all supplied negative scores.
- Sum the negative terms rather than averaging them.
- Use a numerically stable log-sigmoid or softplus formulation.
- Return a scalar float64 tensor.
Constraints
- center_vec and pos_vec have shape (D) and dtype torch.float64.
- neg_vecs has shape (K, D) and dtype torch.float64.
- D and K are positive.
- All inputs contain finite values.
All steps in Word2Vec
Accepts: array
Accepts: array
Accepts: array
SGNS Loss
Implement the Skip-gram with Negative Sampling loss for one positive center-context pair and a set of sampled negative words.
L=−logσ(uoTvc)−i=1∑Klogσ(−uiTvc)Here, vc is center_vec, uo is pos_vec, ui are the rows of neg_vecs, K is the number of negatives, and σ is the logistic sigmoid. Return the loss as a scalar float64 PyTorch tensor and keep it finite for large finite dot products.
Examples
Input: center_vec = [0.5,-0.2], pos_vec = [0.3,0.4], neg_vecs = [[-0.1,0.2],[0.4,-0.3]]
Output: 2.139492
Explanation: The loss combines one positive softplus term with the sum of the two negative terms.
Input: center_vec = [0.2,0.1,-0.4], pos_vec = [0.5,-0.2,0.3], neg_vecs = [[0.1,0.1,0.1]]
Output: 1.401507
Input: center_vec = [0.1,-0.2,0.3,-0.4], pos_vec = [0.2,0.2,-0.1,0.5], neg_vecs = [[0.1,0,-0.2,0.3],[-0.4,0.1,0.2,-0.1],[0,0.3,-0.3,0.2]]
Output: 2.735787
Hint 1
Use torch.dot for the positive pair and a matrix-vector product for all negatives.
Hint 2
The stable loss is F.softplus(-positive_score) plus F.softplus(negative_scores).sum().
Requirements
- Use PyTorch.
- Compute one positive score and all supplied negative scores.
- Sum the negative terms rather than averaging them.
- Use a numerically stable log-sigmoid or softplus formulation.
- Return a scalar float64 tensor.
Constraints
- center_vec and pos_vec have shape (D) and dtype torch.float64.
- neg_vecs has shape (K, D) and dtype torch.float64.
- D and K are positive.
- All inputs contain finite values.
All steps in Word2Vec
Accepts: array
Accepts: array
Accepts: array