import tensorflow.kera.backend as K
之后
tf.multiply
和*
之间有什么区别?
同样, 我基于其他代码编写了以下自定义指标功能的代码。K.pow(x, -1)
和1/x
??有什么区别?] >>
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
吗?(从张量流的文档中,我知道
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,并且仍然可以正常工作)
将tensorflow.kera.backend导入为K后,tf.multiply和*有什么区别?同样,K.pow(x,-1)和1 / x有什么区别?我写了以下代码...
假设*
的两个操作数都是tf.Tensor
而不是tf.sparse.SparseTensor
,*
运算符与tf.multiply
相同,即,具有广播支持的元素乘法。
如果您有兴趣研究执行运算符重载的源代码,那么关键部分是: