studylog/北の雲

chainer/python/nlp

gemsimのWord2vecファイルをpy2系と3系でやり取りしたい

以前python3系のgensimのword2vecで作ったデータを2系で読もうとしたらエラー

raise ValueError, "unsupported pickle protocol: %d" % proto
ValueError: unsupported pickle protocol: 3

pickleのプロトコルが違うらしい


stackoverflow.com

>In python 2 there are 3 different protocols (0, 1, 2) and the default is 0.
>In python 3 there are 5 different protocols (0, 1, 2, 3, 4) and the default is 3.

だそうだ。

なので一度python3系でloadしてから、save_word2vec_formatで保存し直す。
これは元のword2vecでも使えるようのモデルファイル形式らしい。

gensim: models.word2vec – Deep learning with word2vec

save_word2vec_format(fname, fvocab=None, binary=False)
Store the input-hidden weight matrix in the same format used by the original C word2vec-tool, for compatibility.

3系で
load(filename)
save_word2vec_format(new_filename)

2系で
load_word2vec_format(new_filename)

これでおk。