培训和评估的准确性在keras LSTM模型中不同:为什么评估产生的结果与培训期间不同?

时间:2019-03-14 17:18:31

标签: python validation keras lstm

我们正在构建用于对物理光学过程进行建模的LSTM。

到目前为止,我已经使用带有Tensorflow后端的Keras在python中生成了以下代码。

#Define model
model = Sequential()
model.add(LSTM(128, batch_size=BATCH_SIZE, input_shape=(train_x.shape[1],train_x.shape[2]), return_sequences=True, stateful=False ))#,,return_sequences=Tru# stateful=True 
model.add(Dense(2, activation='softmax'))
opt = tf.keras.optimizers.Adam(lr=0.01, decay=1e-6)

#Compile model
model.compile(
    loss='sparse_categorical_crossentropy',
    optimizer=opt,
    metrics=['accuracy']
)

model.fit(
    train_x, train_y,
    batch_size=BATCH_SIZE,
    epochs=EPOCHS,#,
    verbose=1)

#Now I want to make sure that the we can predict the training set (using evaluate) and that it is the same result as during training
score = model.evaluate(train_x, train_y, batch_size=BATCH_SIZE, verbose=0)
print(' Train accuracy:', score[1])

代码的输出是

Epoch 1/10 5872/5872 [==============================] - 0s 81us/sample - loss: 0.6954 - acc: 0.4997
Epoch 2/10 5872/5872 [==============================] - 0s 13us/sample - loss: 0.6924 - acc: 0.5229 
Epoch 3/10 5872/5872 [==============================] - 0s 14us/sample - loss: 0.6910 - acc: 0.5256
Epoch 4/10 5872/5872 [==============================] - 0s 13us/sample - loss: 0.6906 - acc: 0.5243 
Epoch 5/10 5872/5872 [==============================] - 0s 13us/sample - loss: 0.6908 - acc: 0.5238

Train accuracy: 0.52480716

因此,问题在于最终建模精度(0.5238)应该等于(评估)精度(0.52480716),而事实并非如此。我在这里做错了什么,对此给予了极大的帮助

2 个答案:

答案 0 :(得分:0)

“由于您的模型随时间而变化,因此前几个时期的损失通常要比最后一个时期的损失高。”

https://keras.io/getting-started/faq/#why-is-the-training-loss-much-higher-than-the-testing-loss

为了进行评估,由于采用了经过训练的模型,因此准确性更高。

答案 1 :(得分:0)

谢谢 如下所示,不是,我不理解

model = Sequential()
model.add(LSTM(32, batch_size=BATCH_SIZE, input_shape=(train_x.shape[1],train_x.shape[2]), return_sequences=True, stateful=False ))#,,return_sequences=Tru# stateful=True 
model.add(Dense(2, activation='softmax'))
opt = tf.keras.optimizers.Adam(lr=0.01, decay=1e-6)

#Compile model
model.compile(
    loss='sparse_categorical_crossentropy',
    optimizer=opt,
    metrics=['accuracy']
)

#Train model
model.fit(
    train_x, train_y,
    batch_size=BATCH_SIZE,
    epochs=EPOCHS,
    verbose=1,
    shuffle=False,
    validation_data=(validation_x, validation_y)]
)

score = model.evaluate(validation_x, validation_y, batch_size=BATCH_SIZE, verbose=0)

print(' Validation accuracy:', score[1])

输出

 Epoch 1/5 5872/5872 [==============================] - 3s 554us/sample - loss: 0.6923 - acc: 0.5154 - val_loss: 0.7149 - val_acc: 0.4668
 Epoch 2/5 5872/5872 [==============================] - 2s 406us/sample - loss: 0.6895 - acc: 0.4983 - val_loss: 0.7218 - val_acc: 0.4821
 Epoch 3/5 5872/5872 [==============================] - 2s 404us/sample - loss: 0.6890 - acc: 0.4940 - val_loss: 0.7230 - val_acc: 0.4821
 Epoch 4/5 5872/5872 [==============================] - 2s 406us/sample - loss: 0.6883 - acc: 0.4928 - val_loss: 0.7336 - val_acc: 0.4592
 Epoch 5/5 5872/5872 [==============================] - 2s 404us/sample - loss: 0.6881 - acc: 0.4934 - val_loss: 0.7278 - val_acc: 0.4745

 Validation accuracy: 0.45663264