首页 > 解决方案 > 在 Python 中使用预训练嵌入层时的高精度测量

问题描述

我正在尝试使用 GloVe 在我的生成模型中实现一个预训练的嵌入层。

在模型中,我输入从文本中提取的 50 (X) 个项目的序列,并预测文本中的 51. 单词 (y)。

当模型只训练了 1/100 次迭代时,我已经达到了 0.99 的准确度。可能是什么问题?

# create a weight matrix for words in training docs
embedding_matrix = zeros((vocab_size, 100))
for word, i in tokenizer.word_index.items():
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
    embedding_matrix[i] = embedding_vector

 # define model
 model = Sequential() #assigning the sequential function to a model
model.add(Embedding(vocab_size, 100, weights=[embedding_matrix], input_length=seq_length, trainable = False)) #defining embedding layer size
model.add(LSTM(100, return_sequences=True)) #adding layer of nodes
model.add(LSTM(100))  #adding layer of nodes
model.add(Dense(100, activation='relu')) #specifying the structure of the hidden layer, recu is an argument of a rectified linear unit. 
model.add(Dense(vocab_size, activation='softmax')) #using the softmax function to creating probabilities
print(model.summary())
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# fit the model
model.fit(X, y, batch_size=128, epochs=100, verbose=1)

链接到 github:https ://github.com/KiriKoppelgaard/Generative_model 从 2018 年 11 月 14 日提交

标签: pythonkerasnlp

解决方案


推荐阅读