【问题标题】:TensorFlow:How to calculate the Euclidean distance between two tensor?TensorFlow:如何计算两个张量之间的欧几里得距离?
【发布时间】:2019-06-06 10:48:06
【问题描述】:

enter image description here

如何从 A 和 B 到 C? A 和 B 是张量,属于网络的输出。现在我需要从两个网络的输出运算中得到C,然后用C来计算损失函数。

关键问题是后期通过损失函数优化A和B,所以必须以计算图的形式完成。 TensorFlow计算张量的公式好像不能解决这个问题。

【问题讨论】:

    标签: tensorflow tensor


    【解决方案1】:
    import tensorflow as tf
    
    #Create The Tensors
    pt_a = tf.constant([5., 10.,11.])
    pt_b = tf.constant([8., 3.,12.])
    
    #Print Euclidean Distance
    print("Euclidean Distance:",tf.norm(pt_a-pt_b,ord='euclidean'))
    

    【讨论】:

    • 嗨,欢迎来到 SO。考虑添加对您的代码如何回答问题/问题的解释。对于刚接触该语言或主题的人来说,纯代码答案通常不太有用。
    【解决方案2】:

    使用tf.norm:

    C = tf.norm(B - A, axis=-1)
    

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 2018-06-14
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 2020-06-10
      • 2018-03-14
      • 2016-05-11
      • 1970-01-01
      相关资源
      最近更新 更多