I'm using Keras with Tensorflow backend. I want to create a custom loss function, which would get the Euclidean distance between the two histograms, but I get this error:
TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int32 of argument 'x'.
Therefore, I dont know how to define my loss function.
def myloss(y_true, y_pred):
h_true = tf.histogram_fixed_width( y_true, value_range=(0,1), nbins=20)
h_pred = tf.histogram_fixed_width( y_pred, value_range=(0,1), nbins=20)
return K.mean(K.square(h_true - h_pred))
I try to modify codes, but I get another error."h_true = tf.cast(h_true, dtype=tf.dtypes.float32) AttributeError: module 'tensorflow._api.v1.dtypes' has no attribute 'float32'"
def myloss(y_true, y_pred):
h_true = tf.histogram_fixed_width( y_true, value_range=(0,1), nbins=20)
h_pred = tf.histogram_fixed_width( y_pred, value_range=(0,1), nbins=20)
h_true = tf.cast(h_true, dtype=tf.dtypes.float32)
h_pred = tf.cast(h_pred, dtype=tf.dtypes.float32)
return K.mean(K.square(h_true - h_pred))
Last, this problem is solved by the Jakub. The solution is:
def myloss(y_true, y_pred):
h_true = tf.histogram_fixed_width( y_true, value_range=(0,1), nbins=20)
h_pred = tf.histogram_fixed_width( y_pred, value_range=(0,1), nbins=20)
h_true = tf.cast(h_true, dtype="float32")
h_pred = tf.cast(h_pred, dtype="float32")
return K.mean(K.square(h_true - h_pred))
y_trueandy_predboth to float32? - jakubdtype=tf.dtypes.float32, you can trydtype="float32". Note that it's a string. - jakub