Python Keras 中的 RMSE/RMSLE 损失函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43855162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:26:46  来源:igfitidea点击:

RMSE/ RMSLE loss function in Keras

pythonkerascustom-functionloss-function

提问by dennis

I try to participate in my first Kaggle competition where RMSLEis given as the required loss function. For I have found nothing how to implement this loss functionI tried to settle for RMSE. I know this was part of Kerasin the past, is there any way to use it in the latest version, maybe with a customized function via backend?

我尝试参加我的第一次 Kaggle 比赛,其中RMSLE给出了所需的损失函数。因为我没有发现如何实现这一点,loss function我试图解决这个问题RMSE。我知道这是Keras过去的一部分,有没有办法在最新版本中使用它,也许通过自定义功能backend

This is the NN I designed:

这是我设计的神经网络:

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")#, metrics =["accuracy"])

model.fit(train_set, label_log, batch_size = 32, epochs = 50, validation_split = 0.15)

I tried a customized root_mean_squared_errorfunction I found on GitHub but for all I know the syntax is not what is required. I think the y_trueand the y_predwould have to be defined before passed to the return but I have no idea how exactly, I just started with programming in python and I am really not that good in math...

我尝试了root_mean_squared_error在 GitHub 上找到的自定义函数,但我知道语法不是必需的。我认为 they_true和 they_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)) 

I receive the following error with this function:

我收到此功能的以下错误:

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

Thanks for your ideas, I appreciate every help!

感谢您的想法,我感谢每一个帮助!

回答by Dr. Snoopy

When you use a custom loss, you need to put it without quotes, as you pass the function object, not a string:

当您使用自定义损失时,您需要将其不带引号,因为您传递的是函数对象,而不是字符串:

def root_mean_squared_error(y_true, y_pred):
        return K.sqrt(K.mean(K.square(y_pred - y_true))) 

model.compile(optimizer = "rmsprop", loss = root_mean_squared_error, 
              metrics =["accuracy"])

回答by Germán Sanchis

The accepted answer contains an error, which leads to that RMSE being actually MAE, as per the following issue:

接受的答案包含一个错误,导致 RMSE 实际上是 MAE,根据以下问题:

https://github.com/keras-team/keras/issues/10706

https://github.com/keras-team/keras/issues/10706

The correct definition should be

正确的定义应该是

def root_mean_squared_error(y_true, y_pred):
        return K.sqrt(K.mean(K.square(y_pred - y_true)))

回答by Richard Xue

If you are using latest tensorflow nightly, although there is no RMSE in the documentation, there is a tf.keras.metrics.RootMeanSquaredError()in the source code.

如果你每晚使用最新的 tensorflow,虽然文档中没有 RMSE,但tf.keras.metrics.RootMeanSquaredError()源代码中有一个。

sample usage:

示例用法:

model.compile(tf.compat.v1.train.GradientDescentOptimizer(learning_rate),
              loss=tf.keras.metrics.mean_squared_error,
              metrics=[tf.keras.metrics.RootMeanSquaredError(name='rmse')])

回答by George C

I prefer reusing part of the Keras work

我更喜欢重用部分 Keras 工作

from keras.losses import mean_squared_error

def root_mean_squared_error(y_true, y_pred):
    return K.sqrt(mean_squared_error(y_true, y_pred))

model.compile(optimizer = "rmsprop", loss = root_mean_squared_error, 
          metrics =["accuracy"])