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