【发布时间】:2021-12-15 14:47:48
【问题描述】:
我有自己的数据集,我想在其中使用 gensim word2vec 进行训练,但我不知道该怎么做。
from google.colab import files
import io
uploaded = files.upload()
data_path = 'chatbot_dataset.txt'
with open(data_path, 'r') as f:
lines = f.read().split('\n')
for line in lines:
input_text = line.split('\t')[0]
if len(input_text.split()) > MAX_SENTENCE_LENGTH:
break
target_text = '<START> ' + line.split('\t')[1] + " <END>"
input_texts.append(input_text)
target_texts.append(target_text)
model = Word2Vec(lines, min_count=1,workers=3,size=100,window=3,sg=1)
model.wv.get_vector('hello')
但我在执行此操作时遇到此错误,即使“你好”这个词已经在我的数据集中:
KeyError Traceback (most recent call last)
<ipython-input-15-b41c8cb17d3b> in <module>()
140 model.wv.vector_size
141 #check out how 'PEM' is represented in an array of 100 numbers
--> 142 model.wv.get_vector('hello')
143 #find words with similar meaning to 'PEN'
144 model.wv.most_similar('to')
1 frames
/usr/local/lib/python3.7/dist-packages/gensim/models/keyedvectors.py in word_vec(self, word, use_norm)
450 return result
451 else:
--> 452 raise KeyError("word '%s' not in vocabulary" % word)
453
454 def get_vector(self, word):
KeyError: "word 'hello' not in vocabulary"
【问题讨论】:
标签: python machine-learning nlp word2vec