I need help writing a custom loss/metric function for Keras. My categories are binary encoded (not one-hot). I would like to do a bitwise comparison between the real classes and the predicted classes.
For example, Real-label: 0x1111111111 Predicted-label: 0x1011101111
The predicted-label has 8 of 10 bits correct so the accuracy of this match should be 0.8 not 0.0. I have no idea how I am support to do this with Keras commands.
EDIT 1: Currently I am using something like this but it is not working yet:
def custom_binary_error(y_true, y_pred, n=11):
diff_dec = K.tf.bitwise.bitwise_xor(K.tf.cast(y_true, K.tf.int32), K.tf.cast(y_pred, K.tf.int32))
diff_bin = K.tf.mod(K.tf.bitwise.right_shift(K.tf.expand_dims(diff_dec,1), K.tf.range(n)), 2)
diff_sum = K.tf.math.reduce_sum(diff_bin, 1)
diff_percent = K.tf.math.divide(diff_sum, 11)
return K.tf.math.reduce_mean(diff_percent, 0)
I get this error:
ValueError: Dimensions must be equal, but are 2048 and 11 for 'loss/activation_1_loss/RightShift' (op: 'RightShift') with input shapes: [?,1,2048], [11].