我正在训练用于预测的神经网络模型。损失函数是均方误差(MSE)。但是,我发现Keras计算出的MSE与Scikit-learn计算出的MSE有很大不同。
时代1/10 162315/162315 [==============================]-14秒 87us / step-损失:111.8723-均方误差:111.8723 - val_loss:9.5308-val_mean_squared_error:9.5308
Epoch 00001:损失从inf降低至111.87234,将模型保存为 / home /型号/2019.04.26.10.55 Scikit Learn MSE = 208.811126
Epoch 2/10 162315/162315 [==============================]-14秒 89us / step-损失: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了解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代码,它们非常相似。