I'm using Keras with Tensorflow backend. I would like to create a new custom loss function where I pool the individual values of y_true and y_pred into bins (think of a histogram), and then calculate the chi2 between the two histograms.
I understand tensor object are not iterable, so I can't loop over the individual elements of y_true and y_pred to fill the histograms.
Update:
I tried to create a loss function like this:
def Chi2Loss(y_true, y_pred):
h_true = tf.histogram_fixed_width( y_true, value_range=(-1., 1.), nbins=20)
h_pred = tf.histogram_fixed_width( y_true, value_range=(-1., 1.), nbins=20)
return K.mean(K.square(h_true - h_pred))
But I get an error:
TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int32 of argument 'x'.