完整描述
我开始使用词嵌入技术,并找到了大量相关信息。我理解,到目前为止,我可以训练自己的词向量或使用之前训练好的向量,例如Google或Wikipedia提供的英语向量,但对我没有用处,因为我正在处理的是巴西葡萄牙语文本。因此,我寻找了预训练的葡萄牙语词向量,最终找到了Hirosan's List of Pretrained Word Embeddings,这使我了解了Kyubyong的WordVectors,从中我了解了Rami Al-Rfou的Polyglot。在下载后,我一直无法成功地加载这些词向量。
简短描述
我无法加载预训练的词向量;我正在尝试WordVectors和Polyglot。 下载 加载尝试Kyubyong的WordVectors 第一次尝试:使用Hirosan建议的Gensim;
from gensim.models import KeyedVectors
kyu_path = '.../pre-trained_word_vectors/kyubyong_pt/pt.bin'
word_vectors = KeyedVectors.load_word2vec_format(kyu_path, binary=True)
并返回错误:
[...]
File "/Users/luisflavio/anaconda3/lib/python3.6/site-packages/gensim/utils.py", line 359, in any2unicode
return unicode(text, encoding, errors=errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
下载的zip文件中还包含其他文件,但它们都返回类似的错误。 Polyglot">Polyglot 第一次尝试:按照Al-Rfous's instructions">Al-Rfous的说明操作;
import pickle
import numpy
pol_path = '.../pre-trained_word_vectors/polyglot/polyglot-pt.pkl'
words, embeddings = pickle.load(open(pol_path, 'rb'))
并返回错误:
File "/Users/luisflavio/Desktop/Python/w2v_loading_tries.py", line 14, in <module>
words, embeddings = pickle.load(open(polyglot_path, "rb"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd4 in position 1: ordinal not in range(128)
第二次尝试:使用Polyglot的词嵌入加载函数;
首先,我们需要通过pip安装polyglot:
pip install polyglot
现在我们可以导入它:
from polyglot.mapping import Embedding
pol_path = '.../pre-trained_word_vectors/polyglot/polyglot-pt.pkl'
embeddings = Embedding.load(polyglot_path)
并返回错误:
File "/Users/luisflavio/anaconda3/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
额外信息
我在MacOS High Sierra上使用Python 3。
解决方案
Kyubyong的WordVectors 正如Aneesh Joshi所指出的那样,加载Kyubyong的模型的正确方法是调用Word2Vec的本机加载函数。
from gensim.models import Word2Vec
kyu_path = '.../pre-trained_word_vectors/kyubyong_pt/pt.bin'
model = Word2Vec.load(kyu_path)
尽管我非常感激Aneesh Joshi的解决方案,但多语言似乎是与葡萄牙语一起工作的更好模型。有任何想法吗?
load_word2vec_format()
和load()
之间的区别,谢谢您。正如我在更新帖子中提到的,Polyglot更可靠,您知道它发生了什么事吗? - Luís Flávio