Tensorflow Keras模型具有不同的损失

Tensorflow Keras模型具有不同的损失,tensorflow,machine-learning,keras,artificial-intelligence,keras-layer,Tensorflow,Machine Learning,Keras,Artificial Intelligence,Keras Layer,我目前正在研究Keras模型。我的目标是尽可能降低RMSE,因此我将损失和度量设置为RMSE。在模型训练之后,当我根据训练数据计算模型的RMSE时,我得到了更高的结果。为什么呢 我对该模型的RMSE功能如下: from keras import backend as K def root_mean_squared_error(y_true, y_pred): return K.sqrt(K.mean(K.square(y_pred - y_true))) 模型训练完成后,我使用

我目前正在研究Keras模型。我的目标是尽可能降低RMSE,因此我将损失和度量设置为RMSE。在模型训练之后,当我根据训练数据计算模型的RMSE时,我得到了更高的结果。为什么呢

我对该模型的RMSE功能如下:

from keras import backend as K
def root_mean_squared_error(y_true, y_pred):
        return K.sqrt(K.mean(K.square(y_pred - y_true)))
模型训练完成后,我使用此函数计算rmse

from sklearn.metrics import mean_squared_error
from math import sqrt
def measure_mse(actual, predicted):
    return  sqrt(mean_squared_error(actual, predicted))

measure_mse(train_y, model.predict(train_x))
==================================================================== ==================================================================== 结果:

keras模型:val_根_均方_误差:1.8079

我计算:2.1155


val_root_mean_squared_error
是验证RMSE(即根据验证数据计算)。报告的数字,即2.1155,是否也根据验证数据计算?是的,它是。我说了验证错误,但在开始时,我介绍了列车集而不是测试集的代码,对此表示抱歉。我试着运行了几次,Keras计算的数字与我计算的数字之间的差值在0.2-0.5之间,这对于验证集和火车集都是如此。这是一幅图像
val_root_mean_squared_error
是验证RMSE(即根据验证数据计算)。报告的数字,即2.1155,是否也根据验证数据计算?是的,它是。我说了验证错误,但在开始时,我介绍了列车集而不是测试集的代码,对此表示抱歉。我试着运行了几次,Keras计算的数字与我计算的数字之间的差值在0.2-0.5之间,这对于验证集和火车集都是如此。这是一张图片
layer_in = Input(shape=(16,1))

layer_regr = GRU(64, activation='relu', kernel_regularizer=regularizers.l2(0.01), kernel_initializer='truncated_normal', return_sequences=True)(layer_in)

layer_regr = Dropout(0.2)(layer_regr)

layer_regr = GRU(16, activation='relu', kernel_initializer='truncated_normal', return_sequences=True)(layer_regr)

layer_regr = GRU(64, activation='relu', kernel_initializer='truncated_normal')(layer_regr)

layer_regr = Dropout(0.2)(layer_regr)

layer_regr = Dense(16, activation='relu', kernel_initializer='truncated_normal')(layer_regr)

layer_out = Dense(1,)(layer_regr)

model = Model(inputs=layer_in, outputs=layer_out)

model.compile(loss=root_mean_squared_error, optimizer='adam', metrics=[root_mean_squared_error])

checkpointer = ModelCheckpoint(filepath="weights.hdf5", verbose=1, save_best_only=True)

model.fit(train_x, train_y, epochs=10, batch_size = 16, validation_data=(test_x, test_y), verbose=1, callbacks=[checkpointer])

model.load_weights('weights.hdf5')