Python Keras中的RMSE/RMSLE损失函数

Python Keras中的RMSE/RMSLE损失函数,python,keras,custom-function,loss-function,Python,Keras,Custom Function,Loss Function,我试着参加我的第一次卡格尔比赛,RMSLE作为所需的损失函数。因为我找不到如何实现这个丢失函数我试着满足于RMSE。我知道这是过去Keras的一部分,有没有办法在最新版本中使用它,或者通过后端定制功能 这是我设计的NN: from keras.models import Sequential from keras.layers.core import Dense , Dropout from keras import regularizers model = Sequential() mode

我试着参加我的第一次卡格尔比赛,
RMSLE
作为所需的损失函数。因为我找不到如何实现这个
丢失函数
我试着满足于
RMSE
。我知道这是过去
Keras
的一部分,有没有办法在最新版本中使用它,或者通过
后端
定制功能

这是我设计的NN:

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)
我在GitHub上找到了一个自定义的
root\u mean\u squared\u error
函数,但据我所知,语法不是必需的。我认为
y_true
y_pred
必须在传递到返回之前进行定义,但我不知道具体如何,我刚开始用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')

谢谢你的想法,我感谢你的帮助

使用自定义丢失时,在传递函数对象而不是字符串时,需要将其不带引号:

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

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

正确的定义应该是

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

如果您每晚都在使用最新的tensorflow,尽管文档中没有RMSE,但是在中有一个
tf.keras.metrics.RootMeanSquaredError()

示例用法:

model.compile(tf.compat.v1.train.GradientDescentOptimizer(学习率),
损失=tf.keras.metrics.mean_squared_误差,
metrics=[tf.keras.metrics.RootMeanSquaredError(name='rmse')])

我更喜欢重用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"])

您可以使用与其他答案中显示的RMSE相同的方式执行RMSLE,您只需合并日志功能:

from tensorflow.keras import backend as K

def root_mean_squared_log_error(y_true, y_pred):
    return K.sqrt(K.mean(K.square(K.log(1+y_pred) - K.log(1+y_true))))

RMSLE的更简化版本:

import tensorflow as tf
import tensorflow.keras.backend as K

def rmsle_custom(y_true, y_pred):
    msle = tf.keras.losses.MeanSquaredLogarithmicError()
    return K.sqrt(msle(y_true, y_pred)) 

很好,非常感谢你指出了那个错误。我真的没有这样想,因为我是一个编程新手。你可能不知道如何编辑这个自定义函数,以便它计算均方根对数误差,是吗?它给了我未知的损失函数:均方根_error@Jitesh请不要发表这样的评论,用源代码提出您自己的问题。@Jitesh您可能在函数名周围加了引号。您需要将函数对象传递给编译函数,而不是它的名称。这段代码给出了与MAE相同的值,而不是RMSE(请参见下面的答案)。您定义的均方根误差似乎相当于keras中的“mse”(均方误差)。仅供参考。需要注意的一点是,这个损失函数的流形可能会变为无穷大(因为平方根),训练可能会失败。我刚刚尝试了这个函数,得到了这个无穷大的损失^ ^ ^ ^ lol,是的,如果在训练的某个点上,平方根返回无穷大,那么你所有的训练失败都会失败。非常感谢你的评论!我花了很多时间试图弄清楚为什么我的RMSE结果(使用上面的代码)与MAE相同。当我尝试将其用作损失函数时,我得到了一个错误:
AttributeError:'RootMeanSquaredError'对象没有属性'\uu name'
,即使我使用了name参数。您可能需要添加更多解释。