简体   繁体   English

“tensorflow.math.multiply”和“tensorflow.keras.layers.multiply”有什么区别?

[英]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.multiplytensorflow.keras.layers.multiply什么tensorflow.keras.layers.multiply

Similarly, What is the difference between tensorflow.math.add and tensorflow.keras.layers.add ?同样, tensorflow.math.addtensorflow.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 multiplyfrom 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.powK.powtf.pow(x,y)接收2张量将计算的x ^ y表示相应的元素xy ,而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.

相关问题 乘法(例如 tf.math.multiply())和乘法层(例如 tf.keras.layers.multiply())有什么区别? - What is the difference between a multiplication (eg. tf.math.multiply()) and a multiplication layer (eg. tf.keras.layers.multiply())? tensorflow.keras.layers 和 keras.layers 有什么区别? - What is the difference between tensorflow.keras.layers and keras.layers? Tensorflow 和 Keras 有什么区别? - What is the difference between Tensorflow and Keras? 将标量乘以带有 tensorflow keras 后端的张量 - Multiply a scalar to a tensor with tensorflow keras backend 如何在 tensorflow/keras 中将三个矩阵相乘 - How to multiply three matrices in tensorflow/keras 使用 CPU(在 Tensorflow 2.x 中)“Keras 后端 + Tensorflow”和“来自 Tensorflow 的 Keras”有什么区别 - What is difference between “Keras backend + Tensorflow” and “Keras from Tensorflow” using CPU(in Tensorflow 2.x) 将 TensorFlow 2+ 中两个 Conv2D 层的输出相乘 - Multiply outputs from two Conv2D layers in TensorFlow 2+ Tensorflow.js Layers 模型和 Graph 模型有什么区别? - What is the difference between Tensorflow.js Layers model and Graph model? 具有单矩阵乘法的简单张量流keras模型不起作用 - Simple tensorflow keras model with single matrix multiply not working TensorFlow - tf.keras.layers.Layer 与 tf.keras.Model 之间的区别 - TensorFlow - Difference between tf.keras.layers.Layer vs tf.keras.Model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM