在Keras中的RMSE/RMSLE损失函数

7 浏览
0 Comments

在Keras中的RMSE/RMSLE损失函数

我尝试参加我的第一个Kaggle竞赛,其中要求使用RMSLE作为损失函数。因为我找不到如何实现这个损失函数,所以我试图使用RMSE。我知道这在过去是Keras的一部分,现在有没有办法在最新版本中使用它,也许可以通过backend来自定义函数?

这是我设计的神经网络:

from keras.models import Sequential
from keras.layers.core import Dense , Dropout
from keras import regularizers
model = Sequential()
model.add(Dense(units = 128, kernel_initializer = "uniform", activation = "relu", input_dim = 28,activity_regularizer = regularizers.l2(0.01)))
model.add(Dropout(rate = 0.2))
model.add(Dense(units = 128, kernel_initializer = "uniform", activation = "relu"))
model.add(Dropout(rate = 0.2))
model.add(Dense(units = 1, kernel_initializer = "uniform", activation = "relu"))
model.compile(optimizer = "rmsprop", loss = "root_mean_squared_error")
model.fit(train_set, label_log, batch_size = 32, epochs = 50, validation_split = 0.15)

我尝试了在GitHub上找到的一个自定义的`root_mean_squared_error`函数,但据我所知,语法不符合要求。我认为`y_true`和`y_pred`在传递给return之前需要定义,但我不知道具体如何定义,我刚刚开始学习Python编程,对数学不是很擅长...

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), axis=-1)) 

我使用这个函数收到以下错误:

`ValueError: ('Unknown loss function', ':root_mean_squared_error')`

谢谢你的建议,我非常感谢每一次帮助!

0