2

我正在训练一个用于预测的神经网络模型。损失函数是均方误差 (MSE)。但是,我发现 Keras 计算的 MSE 与 Scikit-learn 计算的 MSE 有很大不同。

纪元 1/10 162315/162315 [==============================] - 14s 87us/步 - 损失:111.8723 - mean_squared_error :111.8723 - val_loss:9.5308 - val_mean_squared_error:9.5308

Epoch 00001:损失从 inf 提高到 111.87234,将模型保存到 /home/Model/2019.04.26.10.55 Scikit Learn MSE = 208.811126

纪元 2/10 162315/162315 [===============================] - 14s 89us/步 - 损失:4.5191 - mean_squared_error :4.5191 - val_loss:3.7627 - val_mean_squared_error:3.7627

……

Epoch 00010:损失从 0.05314 提高到 0.05057,将模型保存到 /home/Model/2019.04.26.10.55 Scikit Learn MSE = 0.484048

MSE 的计算公式为Keras

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

MSE 的计算公式为Scikit-Learn

class my_callback(Callback): 
def __init__(self, trainX, trainY, model_name):
    self.trainset_X = trainX
    self.trainset_Y = trainY
    self.model_name = model_name
    self.previous_mse = 10000000

def on_train_begin(self, logs={}):
    return 

def on_train_end(self, logs={}):
    return

def on_epoch_begin(self, epoch, logs={}):
    return

def on_epoch_end(self, epoch, logs={}):        
    # ----- Train -----
    y_pred = self.model.predict(self.trainset_X, batch_size=64)
    curr_mse = mean_squared_error(self.trainset_Y, y_pred)
    print('Scikit Learn MSE = %f' % curr_mse)

    if curr_mse < self.previous_mse:
        print('Save the best model to %s' % self.model_name)
        self.model.save(self.model_name)
        self.previous_mse = curr_mse
    return

def on_batch_begin(self, batch, logs={}):
    return

def on_batch_end(self, batch, logs={}):
    return

你知道为什么会有如此不同吗?我还检查了它们的python代码,它们非常相似。

4

0 回答 0