我对 Keras 中的 model.evaluate() 和 model.predict() 函数有疑问。我在 Keras 中构建了一个简单的 LSTM 模型,想在测试数据集上测试模型性能。我考虑了以下两种方法来计算测试数据集上的指标:
然而,我得到了不同的结果。此外,model.evaluate() 方法的结果还取决于 batch_size 参数的值。根据我的理解和这个post,他们应该有相同的结果。这是可以复制结果的代码:
import tensorflow as tf
from keras.models import Model
from keras.layers import Dense, LSTM, Activation, Input
import numpy as np
from tqdm.notebook import tqdm
import keras.backend as K
from keras.callbacks import ModelCheckpoint, EarlyStopping
class VLSTM:
def __init__(self, input_shape=(6, 1), nb_output_units=1, nb_hidden_units=128, dropout=0.0,
recurrent_dropout=0.0, nb_layers=1):
self.input_shape = input_shape
self.nb_output_units = nb_output_units
self.nb_hidden_units = nb_hidden_units
self.nb_layers = nb_layers
self.dropout = dropout
self.recurrent_dropout = recurrent_dropout
def build(self):
inputs = Input(shape=self.input_shape)
outputs = LSTM(self.nb_hidden_units)(inputs)
outputs = Dense(1, activation=None)(outputs)
return Model(inputs=[inputs], outputs=[outputs])
def RMSE(output, target):
return K.sqrt(K.mean((output - target) ** 2))
n_train = 500
n_val = 100
n_test = 250
X_train = np.random.rand(n_train, 6, 1)
Y_train = np.random.rand(n_train, 1)
X_val = np.random.rand(n_val, 6, 1)
Y_val = np.random.rand(n_val, 1)
X_test = np.random.rand(n_test, 6, 1)
Y_test = np.random.rand(n_test, 1)
input_shape = (X_train.shape[1], X_train.shape[2])
model = VLSTM(input_shape=input_shape)
m = model.build()
m.compile(loss=RMSE,
optimizer='adam',
metrics=[RMSE])
callbacks = []
callbacks.append(EarlyStopping(patience=30))
# train model
hist = m.fit(X_train, Y_train, \
batch_size=32, epochs=10, shuffle=True, \
validation_data=(X_val, Y_val), callbacks=callbacks)
# Use evaluate method with default batch size
test_mse = m.evaluate(X_test, Y_test)[1]
print("Mse is {} using evaluate method with default batch size".format(test_mse))
# Use evaluate method with batch size 1
test_mse = m.evaluate(X_test, Y_test, batch_size=1)[1]
print("Mse is {} using evaluate method with batch size = 1".format(test_mse))
# Use evaluate method with batch size = n_test
test_mse = m.evaluate(X_test, Y_test, batch_size=n_test)[1]
print("Mse is {} using evaluate method with batch size = n_test".format(test_mse))
# Use pred method and compute RMSE mannually
Y_test_pred = m.predict(X_test)
test_mse = np.sqrt( ((Y_test_pred - Y_test) ** 2).mean())
print("Mse is {} using evaluate method with batch size = 1".format(test_mse))
运行代码后,结果如下:
Mse 是 0.3068242073059082 使用默认批量大小的评估方法
Mse 为 0.26647186279296875,使用批量大小 = 1 的评估方法
Mse 是 0.30763307213783264,使用批量大小 = n_test 的评估方法
Mse 是 0.3076330596820157 使用预测方法
看起来使用批量大小 = n_test 的 mode.predict() 和 model.evaluate() 给出了相同的结果。谁能解释一下?提前致谢!