Package: lmdb-embeddings
LMDB EmbeddingsQuery word vectors (embeddings) very quickly with very little querying time overhead and far less memory usage than gensim or other equivalent solutions. This is made possible byLightning Memory-Mapped Database.Inspired byDelft. As explained in their readme, this approach permits us to have the pre-trained embeddings immediately "warm" (no load time), to free memory and to use any number of embeddings similtaneously with a very negligible impact on runtime when using SSD.For instance, in a traditional approachglove-840Btakes around 2 minutes to load and 4GB in memory. Managed with LMDB,glove-840Bcan be accessed immediately and takes only a couple MB in memory, for a negligible impact on runtime (around 1% slower).Installationpipinstalllmdb-embeddingsReading vectorsfromlmdb_embeddings.readerimportLmdbEmbeddingsReaderfromlmdb_embeddings.exceptionsimportMissingWordErrorembeddings=LmdbEmbeddingsReader('/path/to/word/vectors/eg/GoogleNews-vectors-negative300')try:vector=embeddings.get_word_vector('google')exceptMissingWordError:# 'google' is not in the database.passWriting vectorsAn example to write an LMDB vector file from a gensim model. As any iterator that yields word and vector pairs is supported, if you have the vectors in an alternative format then it is just a matter of altering theiter_embeddingsmethod below appropriately.I will be writing a CLI interface to convert standard formats soon.fromgensim.models.keyedvectorsimportKeyedVectorsfromlmdb_embeddings.writerimportLmdbEmbeddingsWriterGOOGLE_NEWS_PATH='GoogleNews-vectors-negative300.bin.gz'OUTPUT_DATABASE_FOLDER='GoogleNews-vectors-negative300'print('Loading gensim model...')gensim_model=KeyedVectors.load_word2vec_format(GOOGLE_NEWS_PATH,binary=True)defiter_embeddings():forwordingensim_model.vocab.keys():yieldword,gensim_model[word]print('Writing vectors to a LMDB database...')writer=LmdbEmbeddingsWriter(iter_embeddings()).write(OUTPUT_DATABASE_FOLDER)# These vectors can now be loaded with the LmdbEmbeddingsReader.LRU CacheA reader with an LRU (Least Recently Used) cache is included. This will save the embeddings for the 50,000 most recently queried words and return the same object instead of querying the database each time. Its interface is the same as the standard reader.
Seefunctools.lru_cachein the standard library.fromlmdb_embeddings.readerimportLruCachedLmdbEmbeddingsReaderfromlmdb_embeddings.exceptionsimportMissingWordErrorembeddings=LruCachedLmdbEmbeddingsReader('/path/to/word/vectors/eg/GoogleNews-vectors-negative300')try:vector=embeddings.get_word_vector('google')exceptMissingWordError:# 'google' is not in the database.passCustomisationBy default, LMDB Embeddings uses pickle to serialize the vectors to bytes (optimized and pickled with the highest available protocol). However, it is very easy to use an alternative approach - simply inject the serializer and unserializer as callables into theLmdbEmbeddingsWriterandLmdbEmbeddingsReader.Amsgpackserializer is included and can be used in the same way.fromlmdb_embeddings.writerimportLmdbEmbeddingsWriterfromlmdb_embeddings.serializersimportMsgpackSerializerwriter=LmdbEmbeddingsWriter(iter_embeddings(),serializer=MsgpackSerializer().serialize).write(OUTPUT_DATABASE_FOLDER)fromlmdb_embeddings.readerimportLmdbEmbeddingsReaderfromlmdb_embeddings.serializersimportMsgpackSerializerreader=LmdbEmbeddingsReader(OUTPUT_DATABASE_FOLDER,unserializer=MsgpackSerializer().unserialize)Running testspytestAuthorGithub:DomHudsonContributingContributions, issues and feature requests are welcome!Show your supportGive a ⭐️ if this project helped you!LicenseCopyright © 2019ThoughtRiver.This project isGPL-3.0licensed.