[英]What is the difference between "tensorflow.math.multiply" and "tensorflow.keras.layers.multiply"?
What is the difference between tensorflow.math.multiply
and tensorflow.keras.layers.multiply
? tensorflow.math.multiply
和tensorflow.keras.layers.multiply
什么tensorflow.keras.layers.multiply
?
Similarly, What is the difference between tensorflow.math.add
and tensorflow.keras.layers.add
?同样,
tensorflow.math.add
和tensorflow.keras.layers.add
什么tensorflow.keras.layers.add
?
The reason why I have this question is that when I build my own customized loss function and metrics product = multiply([y_true_f, y_pred_f])
, if I use from tensorflow.keras.layers import multiply
, errors occured:我有这个问题的原因是,当我构建自己的自定义损失函数和指标
product = multiply([y_true_f, y_pred_f])
,如果我使用from tensorflow.keras.layers import multiply
, from tensorflow.keras.layers import multiply
发生错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'main_target' with dtype float and shape [?,?,?]
tensorflow.python.framework.errors_impl.InvalidArgumentError:您必须为占位符张量“main_target”提供一个值,其数据类型为浮点型和形状 [?,?,?]
But if I use from tensorflow.math import multiply
, it works normally.但是如果我使用
from tensorflow.math import multiply
,它可以正常工作。
I want to know why.我想知道为什么。 Thanks.
谢谢。 (I use tensorflow 1.15, ubuntu18)
(我使用 tensorflow 1.15,ubuntu18)
Update : After import tensorflow.kera.backend as K
更新:
import tensorflow.kera.backend as K
what is the difference between tf.multiply
and .*
? tf.multiply
和.*
什么区别?
Similarly, What is the difference between K.pow
and /
??同样,
K.pow
和/
什么区别??
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 .*
?在代码中,我可以用
.*
替换tf.multiply
吗? Can I replace K.pow
by /
??我可以用
/
替换K.pow
吗??
(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) (从tensorflow的文档,我知道之间的差
tf.pow
和K.pow
: tf.pow(x,y)
接收2张量将计算的x ^ y表示相应的元素x
和y
,而K.pow(x,a)
接收一个张量x
和一个整数a
来计算 x^a。但我不知道为什么在上面的代码中K.pow
收到一个浮点数 1.0 并且它仍然正常工作)
All classes in tensorflow.keras.layers
are Keras layers, meaning that they take as input Keras tensors produced by other layers (in the functional API), or they can be arranged to make sequential models using the Sequential API. tensorflow.keras.layers
中的所有类都是tensorflow.keras.layers
层,这意味着它们将其他层(在功能 API 中)生成的 Keras 张量作为输入,或者可以将它们安排为使用 Sequential API 来制作顺序模型。
Other tensorflow functions like the ones in tensorflow.math
are meant to operate in tensorflow (not keras) tensors.像那些其他tensorflow功能
tensorflow.math
意味着在tensorflow操作(不keras)张量。 For a custom loss, the inputs and outputs are tensorflow tensors, so you should use tensorflow functions and not Keras layers.对于自定义损失,输入和输出是 tensorflow 张量,因此您应该使用 tensorflow 函数而不是 Keras 层。
Keras layer operations are used when you want to perform such operation as part of a neural network architecture, for example the add
layer is used to implement residual connections in a ResNet architecture.当您希望将此类操作作为神经网络架构的一部分执行时,会使用 Keras 层操作,例如,
add
层用于在 ResNet 架构中实现残差连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.