我想从预先训练的手套嵌入中提取特征。但是我得到Keyerror的某些字眼。这是单词标记的列表。
words1=['nuclear','described', 'according', 'called','physics', 'account','interesting','holes','theoretical','like','space','radiation','property','impulsed','darkfield']
我从“冲动”,“暗场”这两个词中得到了Keyerror,因为这可能是看不见的词。如何避免此错误? 。
这是我的完整代码:
gloveFile = "glove.6B.50d.txt"
import numpy as np
def loadGloveModel(gloveFile):
print ("Loading Glove Model")
with open(gloveFile, encoding="utf8" ) as f:
content = f.readlines()
model = {}
for line in content:
splitLine = line.split()
word = splitLine[0]
embedding = np.array([float(val) for val in splitLine[1:]])
model[word] = embedding
print ("Done.",len(model)," words loaded!")
return model
model = loadGloveModel(gloveFile)
words1=['nuclear','described', 'according', 'called','physics', 'account','interesting','holes','theoretical','like','space','radiation','property','impulsed','darkfield']
import numpy as np
vector_2 = np.mean([model[word] for word in words1],axis=0) ## Got error message
“脉冲”字的错误消息
答案 0 :(得分:0)
我建议在下面