Implement the skip-gram loss with negative sampling used in Word2Vec.
Signature: def skipgram_loss(center_emb: np.ndarray, pos_emb: np.ndarray, neg_embs: np.ndarray) -> float
Inputs:
center_emb: shape (d,) — embedding of the center wordpos_emb: shape (d,) — embedding of a positive context wordneg_embs: shape (k, d) — embeddings of k negative samplesLoss:
L = -log(sigmoid(center . pos)) - sum_i log(sigmoid(-center . neg_i))
Use a numerically stable log-sigmoid (do not call np.log(sigmoid(x)) directly).
Math
Asked at
import numpy as np
def skipgram_loss(...):
pass
Premium problem
Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.
Already premium?