0

I have already seen how to define the weighted loss function from Own Loss Function in KERAS. But I want to know how to define loss function when there is interaction part, like this:

absolute value of (y_pred[i] - y_pred[j] - y_true[i] + y_true[j])*W[i,j]

where y_pred[i] means the i-th term of prediction, y_true[i] means the i-th term of true value and W[i,j] is the weight on i and j.

I want to make my own loss function is just the summation of above one for all i and j. And do the stochastic gradient descent to find the solution.

For example, if there are no weight terms, I can easily write my own codes with batch_size = 2 like this:

def Loss(y_true, y_pred):
p = K.maximum(0., y_true[0]-y_true[1] -y_pred[0]+ y_pred[1])
return p

But there are some weights here, if I still define the loss function similar as above one:

def Loss(y_true, y_pred):
p = K.maximum(0., (y_true[0]-y_true[1] -y_pred[0]+ y_pred[1])*W[0][1])
return p

it cannot be true. You know when we choose batch_size = 2. y_true[0] and y_true[1] are randomly chosen when we do the gradient descent. But W[0][1] is not the weight for y_true[0] and y_true[1] that randomly chosen.

If there are not intersection part, it will be very easy from Own Loss Function in KERAS.

I still have no idea how to solve my question. I am looking forward to your answer. Thank you very much!

Ruijian
  • 11
  • 3
  • Possible duplicate of [How to create custom objective function in Keras?](https://stackoverflow.com/questions/33859864/how-to-create-custom-objective-function-in-keras) – Tushar Gupta Sep 08 '17 at 09:44
  • I do not think it can solve my problem since I still cannot handle with the intersection part. – Ruijian Sep 08 '17 at 12:10

0 Answers0