Tensor Comparison and Manipulation

I’m trying to write a custom loss function in Keras. I have y_pred and y_true. Both are tensors and the values can be 0 or 1. So, for example, y_true is be:

[0,1,0,1]

and y_pred is:

[1,1,0,0].

I’m trying to compare these two tensors to output the following:

[-0.7, 2.5, 0.025, -0.1]

The logic is this:

y_true y_pred result
0 1 -0.7
1 1 2.5
0 0 0.025
1 0 -0.1

I’m not sure the best way of using various tensorflow operations to achieve this. My loss function will take logits, so I’m trying to do this:

def roi_loss_fn(y_true, y_pred):
  print("Y_TRUE-----")
  print(y_true)
  print("Y_PRED-----")
  print(y_pred)

  softmax = tf.nn.softmax(y_pred)
  print("SOFTMAX-----")
  print(softmax)

  #iterate or manipulate tensors to compare boolean values according to the table

Source: Python Questions

LEAVE A COMMENT