英文:
what is the difference between `tf.multiply` and `*`?
问题
在代码中,可以用 *
替换 tf.multiply
吗?可以用 1/x
替换 K.pow(x, -1)
吗?(根据 TensorFlow 文档,我知道 tf.pow
和 K.pow
之间的区别:tf.pow(x, y)
接受两个张量来计算对应元素的 x^y,而 K.pow(x, a)
接受一个张量 x
和一个整数 a
来计算 x^a。但我不知道为什么在上面的代码中,K.pow
接受一个浮点数 1.0 仍然可以正常工作。)
在代码中,你可以用 *
替换 tf.multiply
,并且可以用 1/x
替换 K.pow(x, -1)
。在这种情况下,*
将执行元素级别的乘法操作,而 1/x
将执行元素级别的倒数操作,与 K.pow(x, -1)
的效果相同。
关于为什么在上面的代码中,K.pow
接受一个浮点数 1.0 仍然可以正常工作,这可能是因为 TensorFlow/Keras 允许在一定程度上自动广播操作,使其适用于不同的张量形状。在这种情况下,将浮点数 1.0 视为一个标量,会自动广播到与 x
具有相同形状的张量,以执行元素级别的求幂操作。但请注意,这种行为可能会因 TensorFlow/Keras 的版本和配置而有所不同,因此最好在实际使用时进行测试以确保兼容性。
英文:
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)
答案1
得分: 3
假设*
的两个操作数都是tf.Tensor
而不是tf.sparse.SparseTensor
,则*
运算符与tf.multiply
相同,即支持广播的逐元素乘法。
如果您有兴趣研究执行运算符重载的源代码,关键部分如下:
- https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/math_ops.py#L891
- https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/math_ops.py#L1225
- https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/math_ops.py#L1201
对于tf.sparse.SparseTensor
,*
被重载为稀疏张量特定的乘法操作。
假设您正在使用Python3,/
运算符被重载为tf.math.truediv
(即浮点除法,对应于TensorFlow的RealDiv
操作)。
在Python2中,/
运算符可能执行整数除法,在这种情况下,它会根据数据类型进行重载。对于浮点数据类型,它是tf.math.truediv
,对于整数数据类型,它是tf.math.floordiv
(整数地板除法)。
tf.pow()
使用不同的运算符(即Pow
运算符)。但假设您的所有数据类型都是浮点数,1 / x
和tf.pow(x, -1.0)
应该是等价的。
英文:
Assuming the two operands of *
are both tf.Tensor
s and not tf.sparse.SparseTensor
s , 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:
- https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/math_ops.py#L891
- https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/math_ops.py#L1225
- 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
(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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论