在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
吗?我能用1/x
替换K.pow(x,-1)
吗??你知道吗
(从tensorflow的文档中,我知道tf.pow
和K.pow
之间的区别:tf.pow(x,y)
接收2个张量来计算^y
中相应元素的x^y,而K.pow(x,a)
接收张量x
和整数a
来计算x^a。但我不知道为什么在上面的代码中K.pow
接收浮点数1.0它仍然有效)
假设
*
的两个操作数都是tf.Tensor
且不是tf.sparse.SparseTensor
,则*
运算符与tf.multiply
运算符相同,即支持广播的元素乘法。你知道吗如果您对研究执行运算符重载的源代码感兴趣,关键部分包括:
对于
tf.sparse.SparseTensor
s,*
重载稀疏张量特定的乘法运算。你知道吗假设您使用的是Python3,
/
运算符重载为tf.math.truediv
(即,浮点除法,它对应于TensorFlow的RealDiv
运算)。你知道吗在Python2中,
/
操作符可能正在执行整数除法,在这种情况下,它以依赖于数据类型的方式重载。{对于整型,它是浮动的。你知道吗tf.pow()
使用不同的运算符(即Pow
)运算符。但是假设所有的数据类型都是浮点型,1 / x
和tf.pow(x, -1.0)
应该是等价的。你知道吗相关问题 更多 >
编程相关推荐