Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ns_exponent parameter to control the negative sampling distribution for *2vec models. Fix #2090 #2093

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion gensim/models/base_any2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def _set_train_params(self, **kwargs):
raise NotImplementedError()

def __init__(self, sentences=None, workers=3, vector_size=100, epochs=5, callbacks=(), batch_words=10000,
trim_rule=None, sg=0, alpha=0.025, window=5, seed=1, hs=0, negative=5, cbow_mean=1,
trim_rule=None, sg=0, alpha=0.025, window=5, seed=1, hs=0, negative=5, ns_exponent=0.75, cbow_mean=1,
min_alpha=0.0001, compute_loss=False, fast_version=0, **kwargs):
self.sg = int(sg)
if vector_size % 4 != 0:
Expand All @@ -309,6 +309,7 @@ def __init__(self, sentences=None, workers=3, vector_size=100, epochs=5, callbac
self.min_alpha = float(min_alpha)
self.hs = int(hs)
self.negative = int(negative)
self.ns_exponent = ns_exponent
self.cbow_mean = int(cbow_mean)
self.compute_loss = bool(compute_loss)
self.running_training_loss = 0
Expand Down Expand Up @@ -627,6 +628,10 @@ def _check_training_sanity(self, epochs=None, total_examples=None, total_words=N
@classmethod
def load(cls, *args, **kwargs):
model = super(BaseWordEmbeddingsModel, cls).load(*args, **kwargs)
if not hasattr(model, 'ns_exponent'):
model.ns_exponent = 0.75
if not hasattr(model.vocabulary, 'ns_exponent'):
model.vocabulary.ns_exponent = 0.75
if model.negative and hasattr(model.wv, 'index2word'):
model.vocabulary.make_cum_table(model.wv) # rebuild cum_table from vocabulary
if not hasattr(model, 'corpus_count'):
Expand Down
11 changes: 8 additions & 3 deletions gensim/models/doc2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ def __init__(self, documents=None, dm_mean=None, dm=1, dbow_words=0, dm_concat=0
If > 0, negative sampling will be used, the int for negative specifies how many "noise words"
should be drawn (usually between 5-20).
If set to 0, no negative sampling is used.
ns_exponent : float
The exponent used to smooth the cumulative distribution used for negative sampling.
1.0 leads to a sampling based on the frequency distribution, 0.0 makes items beings sampled equally,
while a negative value makes unpopular items being sampled more often than popular onces. The default value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, grammar, and to give a hint of when this could be beneficially tuned, I'd reword as:

"The exponent used to shape the negative sampling distribution. A value of 1.0 samples exactly in proportion to the frequencies, 0.0 samples all words equally, while a negative value samples low-frequency words more than high-frequency words. The popular default value of 0.75 was chosen by the original Word2Vec paper. More recently, in https://arxiv.org/abs/1804.04212, Caselles-Dupré, Lesaint, & Royo-Letelier suggest that other values may perform better for recommendation applications."

is empirically set to 0.75 following the original paper of Word2Vec.
dm_mean : int {1,0}
If 0 , use the sum of the context word vectors. If 1, use the mean.
Only applies when `dm` is used in non-concatenative mode.
Expand Down Expand Up @@ -383,7 +388,7 @@ def __init__(self, documents=None, dm_mean=None, dm=1, dbow_words=0, dm_concat=0
self.dm_tag_count = int(dm_tag_count)

kwargs['null_word'] = dm_concat
vocabulary_keys = ['max_vocab_size', 'min_count', 'sample', 'sorted_vocab', 'null_word']
vocabulary_keys = ['max_vocab_size', 'min_count', 'sample', 'sorted_vocab', 'null_word', 'ns_exponent']
vocabulary_kwargs = dict((k, kwargs[k]) for k in vocabulary_keys if k in kwargs)
self.vocabulary = Doc2VecVocab(**vocabulary_kwargs)

Expand Down Expand Up @@ -790,10 +795,10 @@ def build_vocab_from_freq(self, word_freq, keep_raw_vocab=False, corpus_count=No


class Doc2VecVocab(Word2VecVocab):
def __init__(self, max_vocab_size=None, min_count=5, sample=1e-3, sorted_vocab=True, null_word=0):
def __init__(self, max_vocab_size=None, min_count=5, sample=1e-3, sorted_vocab=True, null_word=0, ns_exponent=0.75):
super(Doc2VecVocab, self).__init__(
max_vocab_size=max_vocab_size, min_count=min_count, sample=sample,
sorted_vocab=sorted_vocab, null_word=null_word)
sorted_vocab=sorted_vocab, null_word=null_word, ns_exponent=ns_exponent)

def scan_vocab(self, documents, docvecs, progress_per=10000, trim_rule=None):
logger.info("collecting all words and their counts")
Expand Down
15 changes: 10 additions & 5 deletions gensim/models/fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ class FastText(BaseWordEmbeddingsModel):
"""
def __init__(self, sentences=None, sg=0, hs=0, size=100, alpha=0.025, window=5, min_count=5,
max_vocab_size=None, word_ngrams=1, sample=1e-3, seed=1, workers=3, min_alpha=0.0001,
negative=5, cbow_mean=1, hashfxn=hash, iter=5, null_word=0, min_n=3, max_n=6, sorted_vocab=1,
bucket=2000000, trim_rule=None, batch_words=MAX_WORDS_IN_BATCH, callbacks=()):
negative=5, ns_exponent=0.75, cbow_mean=1, hashfxn=hash, iter=5, null_word=0, min_n=3, max_n=6,
sorted_vocab=1, bucket=2000000, trim_rule=None, batch_words=MAX_WORDS_IN_BATCH, callbacks=()):
"""Initialize the model from an iterable of `sentences`. Each sentence is a
list of words (unicode strings) that will be used for training.

Expand Down Expand Up @@ -210,6 +210,11 @@ def __init__(self, sentences=None, sg=0, hs=0, size=100, alpha=0.025, window=5,
If > 0, negative sampling will be used, the int for negative specifies how many "noise words"
should be drawn (usually between 5-20).
If set to 0, no negative sampling is used.
ns_exponent : float
The exponent used to smooth the cumulative distribution used for negative sampling.
1.0 leads to a sampling based on the frequency distribution, 0.0 makes items beings sampled equally,
while a negative value makes unpopular items being sampled more often than popular onces. The default value
is empirically set to 0.75 following the original paper of Word2Vec.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

cbow_mean : int {1,0}
If 0, use the sum of the context word vectors. If 1, use the mean, only applies when cbow is used.
hashfxn : function
Expand Down Expand Up @@ -267,7 +272,7 @@ def __init__(self, sentences=None, sg=0, hs=0, size=100, alpha=0.025, window=5,
self.wv = FastTextKeyedVectors(size, min_n, max_n)
self.vocabulary = FastTextVocab(
max_vocab_size=max_vocab_size, min_count=min_count, sample=sample,
sorted_vocab=bool(sorted_vocab), null_word=null_word)
sorted_vocab=bool(sorted_vocab), null_word=null_word, ns_exponent=ns_exponent)
self.trainables = FastTextTrainables(
vector_size=size, seed=seed, bucket=bucket, hashfxn=hashfxn)
self.wv.bucket = self.bucket
Expand Down Expand Up @@ -731,10 +736,10 @@ def accuracy(self, questions, restrict_vocab=30000, most_similar=None, case_inse


class FastTextVocab(Word2VecVocab):
def __init__(self, max_vocab_size=None, min_count=5, sample=1e-3, sorted_vocab=True, null_word=0):
def __init__(self, max_vocab_size=None, min_count=5, sample=1e-3, sorted_vocab=True, null_word=0, ns_exponent=0.75):
super(FastTextVocab, self).__init__(
max_vocab_size=max_vocab_size, min_count=min_count, sample=sample,
sorted_vocab=sorted_vocab, null_word=null_word)
sorted_vocab=sorted_vocab, null_word=null_word, ns_exponent=ns_exponent)

def prepare_vocab(self, hs, negative, wv, update=False, keep_raw_vocab=False, trim_rule=None,
min_count=None, sample=None, dry_run=False):
Expand Down
20 changes: 13 additions & 7 deletions gensim/models/word2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ class Word2Vec(BaseWordEmbeddingsModel):

def __init__(self, sentences=None, size=100, alpha=0.025, window=5, min_count=5,
max_vocab_size=None, sample=1e-3, seed=1, workers=3, min_alpha=0.0001,
sg=0, hs=0, negative=5, cbow_mean=1, hashfxn=hash, iter=5, null_word=0,
sg=0, hs=0, negative=5, ns_exponent=0.75, cbow_mean=1, hashfxn=hash, iter=5, null_word=0,
trim_rule=None, sorted_vocab=1, batch_words=MAX_WORDS_IN_BATCH, compute_loss=False, callbacks=(),
max_final_vocab=None):
"""
Expand Down Expand Up @@ -480,6 +480,11 @@ def __init__(self, sentences=None, size=100, alpha=0.025, window=5, min_count=5,
If > 0, negative sampling will be used, the int for negative specifies how many "noise words"
should be drawn (usually between 5-20).
If set to 0, no negative sampling is used.
ns_exponent : float
The exponent used to smooth the cumulative distribution used for negative sampling.
1.0 leads to a sampling based on the frequency distribution, 0.0 makes items beings sampled equally,
while a negative value makes unpopular items being sampled more often than popular onces. The default value
is empirically set to 0.75 following the original paper of Word2Vec.
cbow_mean : int {1,0}
If 0, use the sum of the context word vectors. If 1, use the mean, only applies when cbow is used.
hashfxn : function
Expand Down Expand Up @@ -523,8 +528,8 @@ def __init__(self, sentences=None, size=100, alpha=0.025, window=5, min_count=5,

self.wv = Word2VecKeyedVectors(size)
self.vocabulary = Word2VecVocab(
max_vocab_size=max_vocab_size, min_count=min_count, sample=sample,
sorted_vocab=bool(sorted_vocab), null_word=null_word, max_final_vocab=max_final_vocab)
max_vocab_size=max_vocab_size, min_count=min_count, sample=sample, sorted_vocab=bool(sorted_vocab),
null_word=null_word, max_final_vocab=max_final_vocab, ns_exponent=ns_exponent)
self.trainables = Word2VecTrainables(seed=seed, vector_size=size, hashfxn=hashfxn)

super(Word2Vec, self).__init__(
Expand Down Expand Up @@ -1146,7 +1151,7 @@ def __iter__(self):

class Word2VecVocab(utils.SaveLoad):
def __init__(self, max_vocab_size=None, min_count=5, sample=1e-3, sorted_vocab=True, null_word=0,
max_final_vocab=None):
max_final_vocab=None, ns_exponent=0.75):
self.max_vocab_size = max_vocab_size
self.min_count = min_count
self.sample = sample
Expand All @@ -1155,6 +1160,7 @@ def __init__(self, max_vocab_size=None, min_count=5, sample=1e-3, sorted_vocab=T
self.cum_table = None # for negative sampling
self.raw_vocab = None
self.max_final_vocab = max_final_vocab
self.ns_exponent = ns_exponent

def scan_vocab(self, sentences, progress_per=10000, trim_rule=None):
"""Do an initial scan of all words appearing in sentences."""
Expand Down Expand Up @@ -1397,7 +1403,7 @@ def create_binary_tree(self, wv):

logger.info("built huffman tree with maximum node depth %i", max_depth)

def make_cum_table(self, wv, power=0.75, domain=2**31 - 1):
def make_cum_table(self, wv, domain=2**31 - 1):
"""Create a cumulative-distribution table using stored vocabulary word counts for
drawing random words in the negative-sampling training routines.

Expand All @@ -1413,10 +1419,10 @@ def make_cum_table(self, wv, power=0.75, domain=2**31 - 1):
# compute sum of all power (Z in paper)
train_words_pow = 0.0
for word_index in xrange(vocab_size):
train_words_pow += wv.vocab[wv.index2word[word_index]].count**power
train_words_pow += wv.vocab[wv.index2word[word_index]].count**self.ns_exponent
cumulative = 0.0
for word_index in xrange(vocab_size):
cumulative += wv.vocab[wv.index2word[word_index]].count**power
cumulative += wv.vocab[wv.index2word[word_index]].count**self.ns_exponent
self.cum_table[word_index] = round(cumulative / train_words_pow * domain)
if len(self.cum_table) > 0:
assert self.cum_table[-1] == domain
Expand Down