tf.multiply和*之间有什么区别? [英] what is the difference between `tf.multiply` and `*`?

查看:581
本文介绍了tf.multiply和*之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import tensorflow.kera.backend as K

tf.multiply*有什么区别?

what is the difference between tf.multiply and *?

同样,K.pow(x, -1)1/x有什么区别??

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]))

在代码中,能否将tf.multiply替换为*?我可以将K.pow(x,-1)替换为1/x ??

In the codes, can I replace tf.multiply by *? Can I replace K.pow(x,-1) by 1/x??

(从tensorflow的文档中,我知道tf.powK.pow之间的区别:tf.pow(x,y)接收2个张量以计算xy中对应元素的x ^ y,而K.pow(x,a)接收一个张量x和一个整数a来计算x ^ a.但我不知道为什么在上面的代码K.pow中收到一个浮点数1.0,并且仍然可以正常工作)

(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)

推荐答案

假定*的两个操作数都是tf.Tensor而不是tf.sparse.SparseTensor,则*运算符与tf.multiply相同,即具有广播支持的元素乘法.

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:

  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
  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

对于tf.sparse.SparseTensor s,*会因稀疏特定于张量的乘法运算而超载.

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

假设您使用的是Python3,/运算符将重载到tf.math.truediv(即浮点除法,它对应于TensorFlow的RealDiv op).

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).

在Python2中,/运算符可能正在执行整数除法,在这种情况下,它会以依赖dtype的方式重载.对于浮动dtype,它是tf.math.truediv,对于整数dtype,它是tf.math.floordiv(整数下位除法).

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()使用不同的运算符(即Pow)运算符.但是假设所有dtypes是浮点数,则1 / xtf.pow(x, -1.0)应该等效.

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.

这篇关于tf.multiply和*之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆