我正在阅读tensorflow模型的代码:classifier.py
我对这个代码部分非常困惑:
train_tensor = control_flow_ops.with_dependencies([update_op], total_loss,
name='train_op')control_flow_ops.with_dependencies是什么意思?
发布于 2017-03-28 15:11:48
该函数有两个参数control_flow_ops.with_dependencies(dependencies, output_tensor)。第二个参数output_tensor (在您的例子中是total_loss )只有在计算了dependencies中的所有操作之后才计算。顾名思义,output_tensor依赖于依赖项来进行适当的计算。这一职能加强了这种行为。
依赖项是操作的可迭代性,在您的示例中是列表中的单个update_op。
发布于 2018-10-10 06:25:50
首先,with_dependencies是deprecated,使用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)https://stackoverflow.com/questions/43060206
复制相似问题