我想从谷歌新闻中加载预先训练好的单词嵌入
model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
print (model.wv.vocab)但是错误显示如下:
UnicodeEncodeError: 'ascii' codec can't encode character '\u2022' in position 62425: ordinal not in range(128)我该如何解决这个问题?因为我想列出单词嵌入中的所有单词,并计算句子嵌入的平均值。
发布于 2018-02-01 08:19:28
我以同样的方式加载它们,并且没有这个问题--我怀疑是print语句。可能你的stdout是只为ascii设置的,不管它是在jupyter中还是在终端上。为了避免这个问题,我建议使用如下编码打开一个文件
with open("vocab.txt", "w", encoding="utf8") as vocab_out:
for word in model.wv.vocab:
vocab_out.write(word + "\n")https://stackoverflow.com/questions/46960119
复制相似问题