来自 word2vec网站我可以下载 GoogleNews-vectors-negative300.bin.gz。 .bin 文件(大约 3.4GB)是一种对我没用的二进制格式。托马斯·米科洛夫 assures us “将二进制格式转换为文本格式应该相当简单(尽管这会占用更多磁盘空间)。检查距离工具中的代码,读取二进制文件相当简单。”不幸的是,我对 C 的了解不够多,无法理解 http://word2vec.googlecode.com/svn/trunk/distance.c .
据说是 gensim也可以这样做,但我发现的所有教程似乎都是关于转换 from 文本,而不是其他方式。
有人可以建议修改 C 代码或 gensim 发出文本的指令吗?
最佳答案
我使用此代码加载二进制模型,然后将模型保存到文本文件,
from gensim.models.keyedvectors import KeyedVectors
model = KeyedVectors.load_word2vec_format('path/to/GoogleNews-vectors-negative300.bin', binary=True)
model.save_word2vec_format('path/to/GoogleNews-vectors-negative300.txt', binary=False)
注意:
以上代码适用于 新 版本的 gensim。对于以前的版本,我使用了这个代码:
from gensim.models import word2vec
model = word2vec.Word2Vec.load_word2vec_format('path/to/GoogleNews-vectors-negative300.bin', binary=True)
model.save_word2vec_format('path/to/GoogleNews-vectors-negative300.txt', binary=False)
关于python - 将 word2vec bin 文件转换为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27324292/