0

I want to make a custom MSE loss function that uses only one item from the output layer.

What I have now is this:

def new_loss(y_true, y_pred):
   index_pred = K.argmax(K.abs(y_pred), axis = -1)
   pred = tf.gather(y_true, index_pred, axis = 1)

   index_true = K.argmin(K.abs(y_true), axis = -1)
   true = tf.gather(y_true, index_true, axis = 1)

   return K.mean(K.sqrt(K.square(K.log(pred)-K.log(true))))

But it gives the error,

An operation has `None` for gradient.

I have been looking and can't find anything to work in my scenario.

I am working with Keras and Tensorflow as back-end.

Thank you in advance.

EDIT:

I tried

def new_loss(y_true, y_pred):
   index_pred = K.argmax(K.abs(y_pred), axis = -1)
   pred = tf.gather(y_pred, index_pred, axis = 1)

   index_true = K.argmin(K.abs(y_true), axis = -1)
   true = tf.gather(y_true, index_true, axis = 1)

   return K.mean(K.sqrt(K.square(K.log(pred)-K.log(true))))

And it doesn't give the error. So it is not K.argmax/K.argmin that is the problem.

Jamidd
  • 41
  • 1
  • 6
  • what do you mean by `one item from the output layer`? Explain it – Vlad May 03 '19 at 18:32
  • My last layer has 17 neurons with softmax. So I want to know the index of the selected one and compare the value in y_true in that index with the smallest value of y_true, so I can use those two numbers to compute my loss function. Lets imagine my last layer has three neurons, if I recieve [0.7, 0.2, 0.1] as my output layer and my y_true is [23, 12, 100], I want to return MSE(23, 12). – Jamidd May 03 '19 at 18:37
  • `argmin`, `argmax` do not have grads. Define mathematically what you want to achieve. – Vlad May 03 '19 at 18:45
  • I am not using `argmin`, `argmax` for the actual function, just to get the index's for the values. – Jamidd May 03 '19 at 18:47
  • Have you tried with eager enabled in compile. Other wise shapes wont be read well. Also argmin and argmax are not differentiable. Soft-argmax is the workaround. – sreagm Nov 22 '20 at 17:45

0 Answers0