VarEmbed Tutorial

Varembed is a word embedding model incorporating morphological information, capturing shared sub-word features. Unlike previous work that constructs word embeddings directly from morphemes, varembed combines morphological and distributional information in a unified probabilistic framework. Varembed thus yields improvements on intrinsic word similarity evaluations. Check out the original paper, arXiv:1608.01056 accepted in EMNLP 2016.

Varembed is now integrated into Gensim providing ability to load already trained varembed models into gensim with additional functionalities over word vectors already present in gensim.

This Tutorial

In this tutorial you will learn how to train, load and evaluate varembed model on your data.

Train Model

The authors provide their code to train a varembed model. Checkout the repository MorphologicalPriorsForWordEmbeddings for to train a varembed model. You'll need to use that code if you want to train a model.

Load Varembed Model

Now that you have an already trained varembed model, you can easily load the varembed word vectors directly into Gensim.
For that, you need to provide the path to the word vectors pickle file generated after you train the model and run the script to package varembed embeddings provided in the varembed source code repository.

We'll use a varembed model trained on Lee Corpus as the vocabulary, which is already available in gensim.


In [2]:
from gensim.models.wrappers import varembed

vector_file = '../../gensim/test/test_data/varembed_leecorpus_vectors.pkl'
model = varembed.VarEmbed.load_varembed_format(vectors=vector_file)


/home/misha/git/gensim/docs/notebooks
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-2-3653006df438> in <module>
      4 
      5 vector_file = '../../gensim/test/test_data/varembed_leecorpus_vectors.pkl'
----> 6 model = varembed.VarEmbed.load_varembed_format(vectors=vector_file)

~/git/gensim/gensim/models/wrappers/varembed.py in load_varembed_format(cls, vectors, morfessor_model)
     58         if vectors is None:
     59             raise Exception("Please provide vectors binary to load varembed model")
---> 60         d = utils.unpickle(vectors)
     61         word_to_ix = d['word_to_ix']
     62         morpho_to_ix = d['morpho_to_ix']

~/git/gensim/gensim/utils.py in unpickle(fname)
   1379 
   1380     """
-> 1381     with smart_open(fname, 'rb') as f:
   1382         # Because of loading from S3 load can't be used (missing readline in smart_open)
   1383         if sys.version_info > (3, 0):

~/envs/gensim/lib/python3.7/site-packages/smart_open/smart_open_lib.py in smart_open(uri, mode, **kw)
    437             transport_params[key] = value
    438 
--> 439     return open(uri, mode, ignore_ext=ignore_extension, transport_params=transport_params, **scrubbed_kwargs)
    440 
    441 

~/envs/gensim/lib/python3.7/site-packages/smart_open/smart_open_lib.py in open(uri, mode, buffering, encoding, errors, newline, closefd, opener, ignore_ext, transport_params)
    305         buffering=buffering,
    306         encoding=encoding,
--> 307         errors=errors,
    308     )
    309     if fobj is not None:

~/envs/gensim/lib/python3.7/site-packages/smart_open/smart_open_lib.py in _shortcut_open(uri, mode, ignore_ext, buffering, encoding, errors)
    496     #
    497     if six.PY3:
--> 498         return _builtin_open(parsed_uri.uri_path, mode, buffering=buffering, **open_kwargs)
    499     elif not open_kwargs:
    500         return _builtin_open(parsed_uri.uri_path, mode, buffering=buffering)

FileNotFoundError: [Errno 2] No such file or directory: '../../gensim/test/test_data/varembed_leecorpus_vectors.pkl'

This loads a varembed model into Gensim. Also if you want to load with morphemes added into the varembed vectors, you just need to also provide the path to the trained morfessor model binary as an argument. This works as an optional parameter, if not provided, it would just load the varembed vectors without morphemes.


In [ ]:
morfessor_file = '../../gensim/test/test_data/varembed_leecorpus_morfessor.bin'
model_with_morphemes = varembed.VarEmbed.load_varembed_format(vectors=vector_file, morfessor_model=morfessor_file)

This helps load trained varembed models into Gensim. Now you can use this for any of the Keyed Vector functionalities, like 'most_similar', 'similarity' and so on, already provided in gensim.


In [ ]:
model.most_similar('government')

In [ ]:
model.similarity('peace', 'grim')

Conclusion

In this tutorial, we learnt how to load already trained varembed models vectors into gensim and easily use and evaluate it. That's it!

Resources