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

Loading fastText models using only bin file #1341

Merged
merged 34 commits into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7759a95
french wiki issue resolved
May 22, 2017
c12b4fa
Merge branch 'develop' into french
prakhar2b May 22, 2017
8025710
bin and vec mismatch handled
prakhar2b May 22, 2017
7ee83d9
updating with lastest codes and resolving conflicts
May 23, 2017
041a6e9
Merge branch 'develop' of https://github.com/RaRe-Technologies/gensim…
Jun 2, 2017
22c6710
added test from bin only loading
Jun 2, 2017
61be613
[WIP] loading bin only
Jun 2, 2017
e11ac44
word vec from its ngrams
Jun 6, 2017
a63a3bc
[WIP] word vec from ngrams
Jun 6, 2017
f80410f
Merge branch 'develop' of https://github.com/RaRe-Technologies/gensim…
Jun 7, 2017
454d74e
[WIP] getting syn0 from all n-grams
Jun 7, 2017
e6b0d8b
[TDD] test comparing word vector from bin_only and default loading
Jun 7, 2017
9b03ea3
cleaned up test code
Jun 8, 2017
c496be9
added docstring for bin_only
Jun 8, 2017
2c4a8dd
Merge branch 'ft_oov_fix' of https://github.com/jayantj/gensim into f…
Jun 12, 2017
d2ab903
resolved wiki.fr issue
Jun 12, 2017
82507d1
pep8 fixes
Jun 12, 2017
c44b958
Merge branch 'develop' of https://github.com/RaRe-Technologies/gensim…
Jun 16, 2017
0fc1159
default bin file loading only
Jun 16, 2017
f421b05
logging info modified plus changes a/c review
Jun 19, 2017
68ec73b
removed unused code in fasttext.py
Jun 19, 2017
f7b372e
removed unused codes and vec files from test
Jun 19, 2017
5f7fe02
added lee_fasttext vec files again
Jun 20, 2017
8bd56cf
re-added removed files and unused codes
Jun 21, 2017
b916187
added file name in logging info
Jun 21, 2017
1a0bfc0
removing unused load_word2vec_format code
Jun 22, 2017
98e0287
updated logging info and comments
Jun 22, 2017
f3d2032
input file name with or without .bin both accepted
Jun 22, 2017
bd7e7f6
resolved typo mistake
Jun 22, 2017
800cd01
test for file name
Jun 22, 2017
a15233a
minor change to input filename handling in ft wrapper
jayantj Jun 23, 2017
431aebf
changes to logging and assert messages, pep8 fixes
jayantj Jun 23, 2017
e52fee4
removes redundant .vec files
jayantj Jun 23, 2017
cebb3fc
fixes utf8 bug in flake8_diff.sh script
jayantj Jun 28, 2017
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
5 changes: 3 additions & 2 deletions continuous_integration/travis/flake8_diff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ echo -e '\nRunning flake8 on the diff in the range' "$COMMIT_RANGE" \
echo '--------------------------------------------------------------------------------'

# We ignore files from sklearn/externals.
# Excluding vec files since they contain non-utf8 content and flake8 raises exception for non-utf8 input
# We need the following command to exit with 0 hence the echo in case
# there is no match
MODIFIED_FILES="$(git diff --name-only $COMMIT_RANGE || echo "no_match")"
MODIFIED_FILES="$(git diff --name-only $COMMIT_RANGE -- . ':(exclude)*.vec' || echo "no_match")"

check_files() {
files="$1"
Expand All @@ -133,6 +134,6 @@ check_files() {
if [[ "$MODIFIED_FILES" == "no_match" ]]; then
echo "No file has been modified"
else
check_files "$(echo "$MODIFIED_FILES" )" "--ignore=E501,E731,E12,W503 --exclude=*.sh,*.md,*.yml,*.rst,*.ipynb,*.txt,*.csv,*.vec,Dockerfile*"
check_files "$(echo "$MODIFIED_FILES" )" "--ignore=E501,E731,E12,W503 --exclude=*.sh,*.md,*.yml,*.rst,*.ipynb,*.txt,*.csv,Dockerfile*"
fi
echo -e "No problem detected by flake8\n"
75 changes: 56 additions & 19 deletions gensim/models/wrappers/fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import numpy as np
from numpy import float32 as REAL, sqrt, newaxis
from gensim import utils
from gensim.models.keyedvectors import KeyedVectors
from gensim.models.keyedvectors import KeyedVectors, Vocab
from gensim.models.word2vec import Word2Vec

from six import string_types
Expand Down Expand Up @@ -219,10 +219,6 @@ def save(self, *args, **kwargs):
kwargs['ignore'] = kwargs.get('ignore', ['syn0norm', 'syn0_all_norm'])
super(FastText, self).save(*args, **kwargs)

@classmethod
def load_word2vec_format(cls, *args, **kwargs):
return FastTextKeyedVectors.load_word2vec_format(*args, **kwargs)

@classmethod
def load_fasttext_format(cls, model_file, encoding='utf8'):
"""
Expand All @@ -232,13 +228,17 @@ def load_fasttext_format(cls, model_file, encoding='utf8'):
with a model loaded this way, though you can query for word similarity etc.

`model_file` is the path to the FastText output files.
FastText outputs two training files - `/path/to/train.vec` and `/path/to/train.bin`
Expected value for this example: `/path/to/train`
FastText outputs two model files - `/path/to/model.vec` and `/path/to/model.bin`

Expected value for this example: `/path/to/model` or `/path/to/model.bin`,
as gensim requires only `.bin` file to load entire fastText model.

"""
model = cls()
model.wv = cls.load_word2vec_format('%s.vec' % model_file, encoding=encoding)
model.load_binary_data('%s.bin' % model_file, encoding=encoding)
if not model_file.endswith('.bin'):
model_file += '.bin'
model.file_name = model_file
model.load_binary_data(encoding=encoding)
return model

@classmethod
Expand All @@ -251,9 +251,9 @@ def delete_training_files(cls, model_file):
logger.debug('Training files %s not found when attempting to delete', model_file)
pass

def load_binary_data(self, model_binary_file, encoding='utf8'):
def load_binary_data(self, encoding='utf8'):
"""Loads data from the output binary file created by FastText training"""
with utils.smart_open(model_binary_file, 'rb') as f:
with utils.smart_open(self.file_name, 'rb') as f:
self.load_model_params(f)
self.load_dict(f, encoding=encoding)
self.load_vectors(f)
Expand Down Expand Up @@ -284,12 +284,12 @@ def load_model_params(self, file_handle):
def load_dict(self, file_handle, encoding='utf8'):
vocab_size, nwords, _ = self.struct_unpack(file_handle, '@3i')
# Vocab stored by [Dictionary::save](https://github.com/facebookresearch/fastText/blob/master/src/dictionary.cc)
assert len(self.wv.vocab) == nwords, 'mismatch between vocab sizes'
assert len(self.wv.vocab) == vocab_size, 'mismatch between vocab sizes'
logger.info("loading %s words for fastText model from %s", vocab_size, self.file_name)

self.struct_unpack(file_handle, '@1q') # number of tokens
if self.new_format:
pruneidx_size, = self.struct_unpack(file_handle, '@q')
for i in range(nwords):
for i in range(vocab_size):
word_bytes = b''
char_byte = file_handle.read(1)
# Read vocab word
Expand All @@ -298,8 +298,26 @@ def load_dict(self, file_handle, encoding='utf8'):
char_byte = file_handle.read(1)
word = word_bytes.decode(encoding)
count, _ = self.struct_unpack(file_handle, '@qb')
assert self.wv.vocab[word].index == i, 'mismatch between gensim word index and fastText word index'
self.wv.vocab[word].count = count

if i == nwords and i < vocab_size:
# To handle the error in pretrained vector wiki.fr (French).
# For more info : https://github.com/facebookresearch/fastText/issues/218

assert word == "__label__", (
'mismatched vocab_size ({}) and nwords ({}), extra word "{}"'.format(vocab_size, nwords, word))
continue # don't add word to vocab

self.wv.vocab[word] = Vocab(index=i, count=count)
self.wv.index2word.append(word)

assert len(self.wv.vocab) == nwords, (
'mismatch between final vocab size ({} words), '
'and expected number of words ({} words)'.format(len(self.wv.vocab), nwords))
if len(self.wv.vocab) != vocab_size:
# expecting to log this warning only for pretrained french vector, wiki.fr
logger.warning(
"mismatch between final vocab size (%s words), and expected vocab size (%s words)",
len(self.wv.vocab), vocab_size)

if self.new_format:
for j in range(pruneidx_size):
Expand All @@ -310,7 +328,8 @@ def load_vectors(self, file_handle):
self.struct_unpack(file_handle, '@?') # bool quant_input in fasttext.cc
num_vectors, dim = self.struct_unpack(file_handle, '@2q')
# Vectors stored by [Matrix::save](https://github.com/facebookresearch/fastText/blob/master/src/matrix.cc)
assert self.vector_size == dim, 'mismatch between model sizes'
assert self.vector_size == dim, (
'mismatch between vector size in model params ({}) and model vectors ({})'.format(self.vector_size, dim))
float_size = struct.calcsize('@f')
if float_size == 4:
dtype = np.dtype(np.float32)
Expand All @@ -321,7 +340,9 @@ def load_vectors(self, file_handle):
self.wv.syn0_all = np.fromfile(file_handle, dtype=dtype, count=num_vectors * dim)
self.wv.syn0_all = self.wv.syn0_all.reshape((num_vectors, dim))
assert self.wv.syn0_all.shape == (self.bucket + len(self.wv.vocab), self.vector_size), \
'mismatch between weight matrix shape and vocab/model size'
'mismatch between actual weight matrix shape {} and expected shape {}'.format(
self.wv.syn0_all.shape, (self.bucket + len(self.wv.vocab), self.vector_size))

self.init_ngrams()

def struct_unpack(self, file_handle, fmt):
Expand All @@ -337,8 +358,12 @@ def init_ngrams(self):
"""
self.wv.ngrams = {}
all_ngrams = []
for w, v in self.wv.vocab.items():
self.wv.syn0 = np.zeros((len(self.wv.vocab), self.vector_size), dtype=REAL)

for w, vocab in self.wv.vocab.items():
all_ngrams += self.compute_ngrams(w, self.wv.min_n, self.wv.max_n)
self.wv.syn0[vocab.index] += np.array(self.wv.syn0_all[vocab.index])

all_ngrams = set(all_ngrams)
self.num_ngram_vectors = len(all_ngrams)
ngram_indices = []
Expand All @@ -348,6 +373,18 @@ def init_ngrams(self):
self.wv.ngrams[ngram] = i
self.wv.syn0_all = self.wv.syn0_all.take(ngram_indices, axis=0)

ngram_weights = self.wv.syn0_all

logger.info("loading weights for %s words for fastText model from %s", len(self.wv.vocab), self.file_name)

for w, vocab in self.wv.vocab.items():
word_ngrams = self.compute_ngrams(w, self.wv.min_n, self.wv.max_n)
for word_ngram in word_ngrams:
self.wv.syn0[vocab.index] += np.array(ngram_weights[self.wv.ngrams[word_ngram]])

self.wv.syn0[vocab.index] /= (len(word_ngrams) + 1)
logger.info("loaded %s weight matrix for fastText model from %s", self.wv.syn0.shape, self.file_name)

@staticmethod
def compute_ngrams(word, min_n, max_n):
ngram_indices = []
Expand Down
172 changes: 0 additions & 172 deletions gensim/test/test_data/cp852_fasttext.vec

This file was deleted.

Loading