简体   繁体   中英

what is the difference between `tf.multiply` and `*`?

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.Tensor s and not tf.sparse.SparseTensor s , the * operator is the same as tf.multiply , ie, elementwise multiplication with broadcasting support.

If you are interested in studying the source code that performs the operator overloading, the key parts are:

  1. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/math_ops.py#L891
  2. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/math_ops.py#L1225
  3. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/math_ops.py#L1201

For tf.sparse.SparseTensor s, * is overloaded with sparse tensor-specific multiplication ops.

Assuming you're using Python3, the / operator is overloaded to the tf.math.truediv (ie, 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 (ie, the Pow ) operator. But assuming all your dtypes are floating-point, 1 / x and tf.pow(x, -1.0) should be equivalent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM