我正在尝试导入预训练的Wiki词嵌入。我正在尝试读取此文件,因此遇到以下错误

import gensim
from gensim.models import KeyedVectors
model = gensim.models.KeyedVectors.load_word2vec_format('C:\Users\PHQ-Admin\Downloads\enwiki_20180420_100d.txt')

错误:
model = gensim.models.KeyedVectors.load_word2vec_format('C:\Users\PHQ-Admin\Downloads\enwiki_20180420_100d.txt')
                                                           ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

最佳答案

您正在使用带有反斜杠(\)的路径,并且它试图转义产生错误的U,P等。您可以使用以下解决方案之一:

load_word2vec_format("C:/Users/PHQ-Admin/Downloads/enwiki_20180420_100d.txt")

要么

用反斜杠转义反斜杠。
load_word2vec_format("C:\\Users\\PHQ-Admin\\Downloads\\enwiki_20180420_100d.txt")

要么

只需在字符串前面加上r即可,因为它将普通字符串转换为原始字符串:
load_word2vec_format(r"C:\Users\PHQ-Admin\Downloads\enwiki_20180420_100d.txt")

关于python - 用于机器学习的python编程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58002791/

10-11 04:19