def f(process_id): print('Process Id: {}'.format(os.getpid())) process = psutil.Process(os.getpid()) new_model = Word2Vec.load('/tmp/mymodel.pkl') vector = new_model.wv["science"] annoy_index = AnnoyIndexer() annoy_index.load('/tmp/mymodel.index') annoy_index.model = new_model approximate_neighbors = new_model.wv.most_similar([vector], topn=5, indexer=annoy_index) print('\nMemory used by process {}: {}\n---'.format( os.getpid(), process.memory_info()))
# You can save and load your indexes from/to disk to prevent having to # construct them each time. This will create two files on disk, *fname* and # *fname.d*. Both files are needed to correctly restore all attributes. Before # loading an index, you will have to create an empty AnnoyIndexer object. # fname = '/tmp/mymodel.index' # Persist index to disk annoy_index.save(fname) # Load index back import os.path if os.path.exists(fname): annoy_index2 = AnnoyIndexer() annoy_index2.load(fname) annoy_index2.model = model # Results should be identical to above vector = wv["science"] approximate_neighbors2 = wv.most_similar([vector], topn=11, indexer=annoy_index2) for neighbor in approximate_neighbors2: print(neighbor) assert approximate_neighbors == approximate_neighbors2 ############################################################################### # Be sure to use the same model at load that was used originally, otherwise you # will get unexpected behaviors. #