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

[MRG] Fix FastText word-vectors w/ ngrams/buckets off #2891

Merged
merged 6 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions gensim/models/fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,7 @@ def adjust_vectors(self):

"""
if self.bucket == 0:
self.vectors = self.vectors_vocab # no ngrams influence
piskvorky marked this conversation as resolved.
Show resolved Hide resolved
return

self.vectors = self.vectors_vocab[:].copy()
Expand Down
11 changes: 11 additions & 0 deletions gensim/test/test_fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from gensim.models.fasttext import FastText as FT_gensim, FastTextKeyedVectors, _unpack
from gensim.models.keyedvectors import KeyedVectors
from gensim.test.utils import datapath, get_tmpfile, temporary_file, common_texts as sentences
from gensim.test.test_word2vec import TestWord2VecModel
import gensim.models._fasttext_bin
from gensim.models.fasttext_inner import compute_ngrams, compute_ngrams_bytes, ft_hash_bytes

Expand Down Expand Up @@ -1371,6 +1372,7 @@ def _read_fb(fin):


class ZeroBucketTest(unittest.TestCase):
"""Tests FastText with no buckets/no-ngrams (essentially FastText-as-Word2Vec"""
piskvorky marked this conversation as resolved.
Show resolved Hide resolved
def test_in_vocab(self):
model = train_gensim(bucket=0)
self.assertIsNotNone(model.wv['anarchist'])
Expand All @@ -1379,6 +1381,15 @@ def test_out_of_vocab(self):
model = train_gensim(bucket=0)
self.assertRaises(KeyError, model.wv.word_vec, 'streamtrain')

def test_cbow_neg(self):
"""See gensim.test.test_word2vec.TestWord2VecModel.test_cbow_neg"""
piskvorky marked this conversation as resolved.
Show resolved Hide resolved
model = FT_gensim(
sg=0, cbow_mean=1, alpha=0.05, window=5, hs=0, negative=15,
min_count=5, epochs=10, workers=2, sample=0,
max_n=0 # force no char-ngram buckets
)
TestWord2VecModel.model_sanity(self, model)


class UnicodeVocabTest(unittest.TestCase):
def test_ascii(self):
Expand Down