Python 100%的训练和评估准确率,也尝试过梯度剪裁
我总是得到100%的培训和验证准确率。下面是它的外观:Python 100%的训练和评估准确率,也尝试过梯度剪裁,python,machine-learning,keras,deep-learning,neural-network,Python,Machine Learning,Keras,Deep Learning,Neural Network,我总是得到100%的培训和验证准确率。下面是它的外观: Epoch 17/20 27738/27738 [==============================] - 228s 8ms/step - loss: 4.1600e-05 - accuracy: 1.0000 - val_loss: 4.6773e-05 - val_accuracy: 1.0000 Epoch 18/20 27738/27738 [==============================] - 229s 8
Epoch 17/20
27738/27738 [==============================] - 228s 8ms/step - loss: 4.1600e-05 - accuracy: 1.0000 - val_loss: 4.6773e-05 - val_accuracy: 1.0000
Epoch 18/20
27738/27738 [==============================] - 229s 8ms/step - loss: 3.6246e-05 - accuracy: 1.0000 - val_loss: 4.0900e-05 - val_accuracy: 1.0000
Epoch 19/20
27738/27738 [==============================] - 221s 8ms/step - loss: 3.1839e-05 - accuracy: 1.0000 - val_loss: 3.6044e-05 - val_accuracy: 1.0000
Epoch 20/20
27738/27738 [==============================] - 7616s 275ms/step - loss: 2.8176e-05 - accuracy: 1.0000 - val_loss: 3.1987e-05 - val_accuracy: 1.0000
以下是整个流程的代码:
encoder_input_sequences = pad_sequences(input_integer_seq, maxlen=max_input_len)
decoder_input_sequences = pad_sequences(output_input_integer_seq, maxlen=max_out_len, padding='post')
import numpy as np
read_dictionary = np.load('/Users/Downloads/wordvectors-master/hinvec.npy',allow_pickle='TRUE').item()
num_words = min(MAX_NUM_WORDS, len(word2idx_inputs) + 1)
embedding_matrix = np.zeros((num_words, EMBEDDING_SIZE))
for word, index in word2idx_inputs.items():
embedding_vector = read_dictionary.get(word)
if embedding_vector is not None:
embedding_matrix[index] = embedding_vector
embedding_layer = Embedding(num_words, EMBEDDING_SIZE, weights=[embedding_matrix], input_length=max_input_len)
decoder_targets_one_hot = np.zeros((
len(input_sentences),
max_out_len,
num_words_output
),
dtype='float32'
)
decoder_targets_one_hot.shape
encoder_inputs_placeholder = Input(shape=(max_input_len,))
x = embedding_layer(encoder_inputs_placeholder)
encoder = LSTM(LSTM_NODES, return_state=True)
encoder_outputs, h, c = encoder(x)
encoder_states = [h, c]
decoder_inputs_placeholder = Input(shape=(max_out_len,))
decoder_embedding = Embedding(num_words_output, LSTM_NODES)
decoder_inputs_x = decoder_embedding(decoder_inputs_placeholder)decoder_lstm = LSTM(LSTM_NODES, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs_x, initial_state=encoder_states)
###########################from here I add activation function and apply some parameters:
decoder_dense = Dense(num_words_output, activation='sigmoid')
decoder_outputs = decoder_dense(decoder_outputs)
opt = keras.optimizers.Adam(learning_rate=0.0001, clipvalue=1.0)
model = Model([encoder_inputs_placeholder,
decoder_inputs_placeholder], decoder_outputs)
model.compile(
optimizer=opt,
loss='binary_crossentropy',
metrics=['accuracy']
)
history = model.fit(
[encoder_input_sequences, decoder_input_sequences],
decoder_targets_one_hot,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
validation_split=0.1,
)
plt.plot(history.history['accuracy'])
plt.show()
编辑:
我更改了以下代码:
decoder_targets_one_hot.shape
############################ Added this
decoder_output_sequences = pad_sequences(output_integer_seq, maxlen=max_out_len, padding='post')
for i, d in enumerate(decoder_output_sequences):
for t, word in enumerate(d):
decoder_targets_one_hot[i, t, word] = 1
#############################
encoder_inputs_placeholder = Input(shape=(max_input_len,))
我认为这是正确的方法,但我仍然得到100%的准确率。这是正确的实施方式吗?顺便说一句,如果你想了解下面的输出,这里有一个到教程的链接,唯一的区别是我的数据集是eng hin而不是eng fra:你可以将解码器目标作为零向量初始化,但不要在任何地方将真类的索引设置为1。所以,基本上目标向量不是一个热向量。该模型试图为所有输入学习相同的目标,即零向量。我如何修改代码?你的意思是我需要添加这样的内容`对于枚举解码器中的i,d输出序列:对于t,枚举中的词:解码器目标\u one\u hot[i,t,word]=1`?我不知道你使用的数据以及目标应该是什么样子,但是,应该将正确的类设置为1。假设有三个类,第三个类是给定输入x的真实标签。那么目标向量必须等于我添加的[0 0 1],但可能我没有正确实现它。我编辑了这篇文章,你能看一下这个链接来了解我可能做错了什么吗?