2017-05-08 571 views
6

我尝试参加我的第一个Kaggle比赛,其中RMSLE作为所需的损失函数给出。因为我没有发现如何实现这个loss function我试图解决RMSE。我知道这是过去的Keras的一部分,有没有什么方法可以在最新版本中使用它,也许有通过backend定制的功能?Keras的RMSE/RMSLE损失函数

这是我设计的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) 

我尝试定制root_mean_squared_error功能我在GitHub上找到,但就我所知的语法是不是需要什么。我认为y_truey_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') 

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

回答

13

当您使用自定义的损失,你需要把它放在不带引号,因为你传递给函数的对象,而不是字符串:

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

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

工作完全正常,非常感谢你对指出错误。我真的没有这么想过,因为我对编程很陌生。你根本不知道如何编辑这个自定义函数,以便计算均方根对数错误,对吗? – dennis

+0

它给了我未知的损失函数:root_mean_squared_error – Jitesh

+0

@Jitesh请不要做这样的评论,用源代码做出自己的问题。 –