1

Given a custom loss function in keras:

def my_custom_loss_func(self, y_true, y_pred): #some code

Is it possible to get the values in y_pred to do some calculations for the loss function? I tried, but someone told me y_pred is just a place holder and the actual value of y_pred cannot be extracted. You can just use Keras backend functions to process y_pred, but you cannot actually access the values in it, such as y_pred[1], or something like that.

What I want to do is something like: if "the top 10 values in y_pred are negative and the bottom 10 are negative", then "return a very high cost because I do not want the network to be optimized this way".

Yes, no worries. Here is some sample code.

def my_custom_loss_func(self, y_true, y_pred):
    position_vector_initial = x[0, 0:3] #global variable
    position_vector_now = y_pred[0, 0:3]
    angle = angle_between(position_vector_initial, position_vector_now)
    if (angle < 0):
        return high_loss
    else:
        return kb.mean(kb.sum(kb.square(y_true - y_pred)))
Renzo
  • 11
  • 2
  • https://stackoverflow.com/questions/43818584/custom-loss-function-in-keras – DerekG Apr 27 '20 at 14:56
  • Yes, thank you, I saw that comment, but that one only used Keras backend functions to process yTrue, yPred: K.sum(K.log(yTrue) - K.log(yPred)). It is not explicitly accessing the data inside the yPred tensor, so that it can be processed with non-Keras functions. – Renzo Apr 27 '20 at 15:26
  • can you write pseudocode for your loss function? It isn't yet clear why actual values are necessary here – Marat Apr 27 '20 at 15:41
  • assuming `angle_between` can operate tensors, something like `condition = tf.cast(angle < 0, int); return condition * high_loss + (1-condition) * regular_loss` should work – Marat Apr 27 '20 at 15:58
  • As you said you need to use Keras backend functions to process y_pred and y_true data, because all the operations need to be tensor operations, so keras can do the math to train your network (all the backpropagation stuffs, etc). So, if you are trying to do a custom loss to train your model, it is the only way. But if not, you can use model.predict and do your operations with the returned value. – Augusto Maillo Apr 27 '20 at 18:57
  • Thank you, that's the answer I was looking for. – Renzo Apr 27 '20 at 19:18

0 Answers0