The machine learning model was managed by the basic pickle, but for Annoy, the following error occurs when saving with pickle.
can't pickle annoy.Annoy objects
For the time being, let's google and see the hit issue.
https://github.com/spotify/annoy/issues/367
apparently. ..
If you look at the Sample code properly, you can see that the load method and save method were prepared.
So, according to this sample code
from annoy import AnnoyIndex
import random
f = 40
t = AnnoyIndex(f, 'angular') # Length of item vector that will be indexed
for i in range(1000):
v = [random.gauss(0, 1) for z in range(f)]
t.add_item(i, v)
t.build(10) # 10 trees
t.save('test.ann') #Save
# ...
u = AnnoyIndex(f, 'angular')
u.load('test.ann') # super fast, will just mmap the file #Read
print(u.get_nns_by_item(0, 1000)) # will find the 1000 nearest neighbors
It seems to be okay if you set it to.
Recommended Posts