0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

can't pickle annoy.Annoy objectsの対処法

0
Posted at

機械学習系のmodelは基本pickleで管理していたんですが,Annoyについてはpickleでの保存時に以下のエラーがでます.

can't pickle annoy.Annoy objects

とりあえずググってHitしたissueをみてみます.

image.png

とのことです..

ちゃんとSampleコードをみたら,loadメソッドとsaveメソッドが用意されていたんですね.

ということで,このサンプルコードの通り

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') #保存

# ...

u = AnnoyIndex(f, 'angular')
u.load('test.ann') # super fast, will just mmap the file #読込
print(u.get_nns_by_item(0, 1000)) # will find the 1000 nearest neighbors

にすれば大丈夫そうです.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?