TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

171. Word2Vec Skip-gram with Negative Sampling

Hard

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 word
  • pos_emb: shape (d,) — embedding of a positive context word
  • neg_embs: shape (k, d) — embeddings of k negative samples

Loss:

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

L=−logσ(vc⊤​vp​)−i=1∑k​logσ(−vc⊤​vni​​)

Asked at

NumPy

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.

Upgrade to PremiumBack to problems

Already premium?