Keras中的RMSE / RMSLE损失函数 [英] RMSE/ RMSLE loss function in Keras

查看:1586
本文介绍了Keras中的RMSE / RMSLE损失函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试参加我的第一场Kaggle比赛,其中给出 RMSLE 作为必需的损失函数。因为我对如何实现此损失函数一无所知,所以我尝试满足 RMSE 的要求。我知道这过去是 Keras 的一部分,有没有办法在最新版本中使用它,也许可以通过 backend

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

这是我设计的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_mean_squared_error 函数,但就我所知,语法不是什么需要。我认为 y_true y_pred 必须在传递给返回值之前进行定义,但我不知道该如何精确,我刚开始使用python编程,而我的数学确实不是那么好...

I tried a customized root_mean_squared_error function I found on GitHub but for all I know the syntax is not what is required. I think the y_true and the y_pred would 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...

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')

谢谢对于您的想法,我非常感谢您的帮助!

Thanks for your ideas, I appreciate every help!

推荐答案

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

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

这篇关于Keras中的RMSE / RMSLE损失函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆