After import tensorflow.kera.backend as K
what is the difference between tf.multiply and *?
Similarly, What is the difference between K.pow(x, -1) and 1/x??
I write the following codes of a customized metrics function based on some other's codes.
def dice_coef_weight_sub(y_true, y_pred):
"""
Returns the product of dice coefficient for each class
"""
y_true_f = (Lambda(lambda y_true: y_true[:, :, :, :, 0:])(y_true))
y_pred_f = (Lambda(lambda y_pred: y_pred[:, :, :, :, 0:])(y_pred))
product = tf.multiply([y_true_f, y_pred_f]) # multiply should be import from tf or tf.math
red_y_true = K.sum(y_true_f, axis=[0, 1, 2, 3]) # shape [None, nb_class]
red_y_pred = K.sum(y_pred_f, axis=[0, 1, 2, 3])
red_product = K.sum(product, axis=[0, 1, 2, 3])
smooth = 0.001
dices = (2. * red_product + smooth) / (red_y_true + red_y_pred + smooth)
ratio = red_y_true / (K.sum(red_y_true) + smooth)
ratio = 1.0 - ratio
# ratio = K.pow(ratio + smooth, -1.0) # different method to get ratio
return K.sum(multiply([dices, ratio]))
In the codes, can I replace tf.multiply by *? Can I replace K.pow(x,-1) by 1/x??
(From tensorflow's document, I know the difference between tf.pow and K.pow: tf.pow(x,y) receives 2 tensors to compute x^y for corresponding elements in x and y, while K.pow(x,a) receives a tensor x and a integer a to compute x^a. But I do not know why in the above code K.pow receives a float number 1.0 and it still works norally)
Assuming the two operands of * are both tf.Tensors and not tf.sparse.SparseTensors , the * operator is the same as tf.multiply, i.e., elementwise multiplication with broadcasting support.
If you are interested in studying the source code that performs the operator overloading, the key parts are:
For tf.sparse.SparseTensors, * is overloaded with sparse tensor-specific multiplication ops.
Assuming you're using Python3, the / operator is overloaded to the tf.math.truediv (i.e., floating-point division, which corresponds to the RealDiv op of TensorFlow).
In Python2, the / operator may be doing integer division, in which case it's overloaded in a dtype-dependent way. For floating dtypes, it's tf.math.truediv, for integer dtypes, it's tf.math.floordiv (integer floor division).
tf.pow() uses a different operator (i.e., the Pow) operator. But assuming all your dtypes are floating-point, 1 / x and tf.pow(x, -1.0) should be equivalent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With