【发布时间】:2018-07-30 16:02:04
【问题描述】:
我已经训练并测试了一个用于情绪分析的 CNN。训练和测试数据的准备方式相同,对句子进行标记并给出唯一的整数:
tokenizer = Tokenizer(filters='$%&()*/:;<=>@[\\]^`{|}~\t\n')
tokenizer.fit_on_texts(text)
vocab_size = len(tokenizer.word_index) + 1
sequences = tokenizer.texts_to_sequences(text)
然后预训练手套模型为 CNN 创建嵌入矩阵:
filepath_glove = 'glove.twitter.27B.200d.txt'
glove_vocab = []
glove_embd=[]
embedding_dict = {}
file = open(filepath_glove,'r',encoding='UTF-8')
for line in file.readlines():
row = line.strip().split(' ')
vocab_word = row[0]
glove_vocab.append(vocab_word)
embed_vector = [float(i) for i in row[1:]] # convert to list of float
embedding_dict[vocab_word]=embed_vector
file.close()
for word, index in tokenizer.word_index.items():
`embedding_matrix[index] = embedding_dict[word]`
此时我还使用测试语句创建了这个矩阵,该矩阵后来作为权重传递到嵌入层:
e= Embedding(vocab_size, 200, input_length=maxSeqLength, weights=[embedding_matrix], trainable=False)(inp)
现在我想重新加载我的模型并使用一些新数据进行测试,但这意味着嵌入矩阵不会包含新数据中的一些单词。这让我想知道,如果在创建嵌入矩阵时我不应该包含测试数据?如果没有,嵌入层如何处理这些新词?这部分类似于这个问题,但我找不到答案: How does the Keras Embedding Layer work if word is not found? 谢谢
【问题讨论】:
-
能否请您集中讨论一个问题并提供MVCE?
-
已编辑。我希望现在清楚
标签: python keras conv-neural-network prediction sentiment-analysis