Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom RMSPE loss function in keras

Tags:

python

keras

I am trying to define my own loss function in keras which is Root Mean Squared Percentage Error. RMSPE is defined as :
equation
I have defined my loss function as:

from keras import backend as K
def rmspe(y_true, y_pred):
    sum = K.sqrt(K.mean(K.square( (y_true - y_pred) /
          K.clip(K.abs(y_true),K.epsilon(),None) ), axis=-1) )
    return sum*100.

But after a few iterations it is giving me loss value as nan. Can someone point out what am i doing wrong. Thanks

like image 630
Sargam Modak Avatar asked Feb 21 '17 10:02

Sargam Modak


People also ask

How do I customize a loss function in keras?

Creating custom loss functions in Keras A custom loss function can be created by defining a function that takes the true values and predicted values as required parameters. The function should return an array of losses. The function can then be passed at the compile stage.

What is keras loss function?

The purpose of loss functions is to compute the quantity that a model should seek to minimize during training.

How do you find the root mean square percentage error in Python?

Try: rmspe = (np. sqrt(np. mean(np. square((y_true - y_pred) / y_true)))) * 100 Except ZeroDivisionError: print("Oh, no!

What is Tensorflow loss?

We use a loss function to determine how far the predicted values deviate from the actual values in the training data. We change the model weights to make the loss minimum, and that is what training is all about.


1 Answers

It's good that you're clipping the denominator. But epsilon in the tensorflow backend is 1e-7 when I check on my machine. So you can still blow up your gradient by ten million when you divide by the clipped value. What you want to do is clip your gradient which you can do with either the clipvalue or clipnorm arguments to your optimizer:

optimizer = SGD(clipvalue=10.0)

or

optimizer = SGD(clipnorm=2.0)

You have to play with the value a bit depending on how many output variables you have and how much noise is in your data.

like image 88
CognizantApe Avatar answered Oct 06 '22 00:10

CognizantApe