【发布时间】:2018-11-07 10:36:23
【问题描述】:
完整说明
我开始使用词嵌入,并找到了大量有关它的信息。我知道,到目前为止,我可以训练自己的词向量或使用以前训练过的词向量,例如 Google 或 Wikipedia 的,它们可用于英语,但对我没有用,因为我正在处理 巴西葡萄牙语。因此,我开始寻找预先训练的葡萄牙语词向量,最终找到了Hirosan's List of Pretrained Word Embeddings,这使我找到了Kyubyong 的WordVectors,从中我了解到了Rami Al-Rfou 的Polyglot。下载后,我一直尝试简单地加载词向量,但没有成功。
简短说明
我无法加载预训练的词向量;我正在尝试WordVectors 和Polyglot。
下载
- Kyubyong's pre-trained word2vector format word vectors for Portuguese;
- Polyglot's pre-trained word vectors for Portuguese;
加载尝试
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 第一次尝试:关注Al-Rfous's instructions;
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's word embedding load function;
首先,我们必须通过 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 的解决方案,但多语言似乎是使用葡萄牙语的更好模式。关于那个有什么想法吗?
【问题讨论】:
标签: python word2vec gensim python-unicode polyglot