Keras模型有不同的损失

时间:2020-04-13 08:36:59

标签: tensorflow machine-learning keras artificial-intelligence keras-layer

我目前正在研究Keras模型。我的目标是使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)))

训练模型后,我使用此函数计算均方根值

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))

================================================ ====================

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')

================================================ ====================

结果:

keras模型:val_root_mean_squared_error: 1.8079

我计算: 2.1155

0 个答案:

没有答案