【问题标题】:Need Code Explanation in Basic Tensorflow需要基本 TensorFlow 中的代码说明
【发布时间】:2019-11-28 15:37:37
【问题描述】:

下面是我试图理解的代码。我的困惑是 basic_loss 是一个 !D 数组或具有 1 个元素的张量。所以 loss1 在执行 tf.reduce.sum 时也应该具有相同的值。类似地,loss2 也应该与 basic_loss 具有相同的值。但是当我尝试执行时,它们都没有相同的值。这可能是什么原因?

import tensorflow as tf
    anchor = tf.random_uniform([1 , 128], dtype=tf.float32)
    positive = tf.random_uniform([1 , 128], dtype=tf.float32)
    negative = tf.random_uniform([1 , 128], dtype=tf.float32)
    pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), axis=-1)
    neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), axis=-1)
    basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), 0.3)
    loss1 = tf.reduce_sum(basic_loss)
    loss2 = tf.maximum(basic_loss, 0)
    print(basic_loss.eval())
    print(loss1.eval())
    print(loss2.eval())
    sess.close()

输出:[-1.5216954] -2.0545285 [0.72764665]

【问题讨论】:

    标签: python-3.x tensorflow2.0


    【解决方案1】:

    您是否在tf.Graphtf.Session 下运行代码?在 TF 2.0 中,不鼓励使用它们,因为它们不能以直观的方式工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 2016-04-17
      • 2020-11-07
      • 2021-09-24
      • 2017-12-24
      • 1970-01-01
      相关资源
      最近更新 更多