How to create a loss function that changes in the era in Keras

I would like to create a custom loss function that has a weight coefficient that is updated depending on which era I am in.

For example: Let's say I have a loss function that has a beta weight, where beta increases during the first 20 eras ...

 def custom_loss(x, x_pred): loss1 = objectives.binary_crossentropy(x, x_pred) loss2 = objectives.mse(x, x_pred) return (beta*current_epoch/20) * loss1 + loss2 

How could I implement something like this in the keras loss function?

+5
source share
1 answer

In their documentation, they mention that you can use the anano / Tf symbolic functions that return a scalar for each data point. So you can do something like this

 loss = tf.contrib.losses.softmax_cross_entropy(x, x_pred) * (beta * current_epoch / 20 ) + tf.contrib.losses.mean_squared_error 

You will need to pass x and x_pred as x and x_pred as tf.placeholders. I think you could use keras to create the model, but then again you would need to run the computational graph using sess.run ()

References: https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html#using-keras-models-with-tensorflow

0
source

Source: https://habr.com/ru/post/1258241/


All Articles