0

谁能建议我如何解决此错误。我只是在加载手套向量,在尝试迭代时,它显示了这个错误

embeddings_index = dict()
f = open('/content/drive/My Drive/lstm donor/lstm_glove_vectors')
for line in f:
    values = line.split()
    word = values[0]
    coefs = asarray(values[1:], dtype='float32')
    embeddings_index[word] = coefs
f.close()


---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-79-3373015fdc0b> in <module>()
      1 embeddings_index = dict()
      2 with open('/content/drive/My Drive/lstm donor/lstm_glove_vectors','r',encoding='utf-8') as f:
----> 3   for line in f:
      4           values = line.split()
      5           word = values[0]

/usr/lib/python3.6/codecs.py in decode(self, input, final)
    319         # decode input (taking the buffer into account)
    320         data = self.buffer + input
--> 321         (result, consumed) = self._buffer_decode(data, self.errors, final)
    322         # keep undecoded input until the next call
    323         self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
4

2 回答 2

0

看起来文件的第一个字节(位置 0)是 0x80,除非这意味着在单个字符解码期间的某个时刻位置 0。无论如何,这意味着它不是一个有效的 utf-8 文件。我不认识“lstm_glove_vectors”这个名字,所以有人训练了他们自己的向量或对原始分布式向量做了一些事情(至少重命名,也许更多处理)。这个文件很可能不是纯文本文件。它可能是 gzip 或 zip 文件?或者二进制编码为数字的向量?

我只是尝试使用moreorless命令之类的内容查看内容,然后查看似乎存在的内容。

最后一种可能性:Common Crawl 派生的 GloVe 向量的第一个版本确实存在一些 Unicode 错误,因此如果您使用的是非常旧的数据文件,则可能会发生这种情况。但这个问题在 2015 年得到了解决。

于 2020-05-25T18:14:11.547 回答
0

这是一个解码问题,即它无法解码您要读取的数据。如果您尝试读取 csv,请添加csv到您的文件名。

于 2020-05-25T17:42:26.883 回答