I need to create a loss function for Keras that works with only binary values. In wanted for to transform all the values greater than 0.5 to 1.0, so I did that:
def MyLoss(y_true, y_pred:
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(K.cast(K.greater(y_pred, 0.5), 'float32'))
#y_pred_f = K.flatten(K.cast(y_pred > 0.5), 'float32')
#y_pred_f = K.flatten(y_pred > 0.5)
return K.sum(y_true_f * y_pred_f)
The code compiles, but later it generates the following error:
ValueError: None values not supported.
I also tried the commented lines, same error. If I don't try to modify the values using simply y_pred_f = K.flatten(y_pred)
, it runs.
What am I doing wrong?
How can I binarize a tensor?