【问题标题】:what does `control_flow_ops.with_dependencies` mean for tensoflow?`control_flow_ops.with dependencies` 对张量流意味着什么?
【发布时间】:2017-08-20 23:24:18
【问题描述】:

我正在阅读tensorflow模型的代码: https://github.com/tensorflow/models/blob/master/slim/train_image_classifier.py

我对这段代码很困惑:

train_tensor = control_flow_ops.with_dependencies([update_op], total_loss,
                                                  name='train_op')

control_flow_ops.with_dependencies 是什么意思?

【问题讨论】:

    标签: python tensorflow computer-vision deep-learning conv-neural-network


    【解决方案1】:

    该函数有两个参数control_flow_ops.with_dependencies(dependencies, output_tensor)。第二个参数output_tensor,在您的情况下是total_loss,仅在评估dependencies 中的所有操作之后评估。顾名思义,output_tensor 依赖于正确评估的依赖关系。此函数强制执行此行为。

    Dependencies 是一个可迭代的操作,在你的例子中是一个列表中的单个 update_op

    【讨论】:

    • 谢谢@vega。另一个问题:train_tensor total_loss 是同一个张量吗?
    • 是的,该函数指定:“仅在dependencies 之后生成output_tensor 的内容......返回与output_tensor 相同。”
    【解决方案2】:

    首先with_dependenciesdeprecated,改用tf.control_dependencies

    至于它的作用,它只有在计算依赖关系之后才会产生一些值的输出。我通常用它来断言一些值。例如:

    assert_op = tf.Assert(tf.less_equal(tf.reduce_max(x), 1.), [x]) # max(x) <= 1
    with tf.control_dependencies([assert_op]):
        x= tf.identity(x)
    

    【讨论】:

      猜你喜欢
      • 2018-09-20
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 2020-10-12
      相关资源
      最近更新 更多