首页 > 解决方案 > 无法使用 tf.control_dependencies() 创建控制依赖项

问题描述

我试图理解tf.control_dependencies(),并想验证它确实创建了控制依赖项。这是代码

import tensorflow as tf

a = tf.get_variable('a', shape = [2, 3])
b = tf.get_variable('b', shape = [2, 3])
c = tf.scalar_mul(2, a)
d = tf.scalar_mul(3, b)

with tf.control_dependencies([d, c]):
  f = d-c

print (f.op.control_inputs)

它返回[]了,这不是我所期望的。如果我按以下方式添加控制依赖项

f = d-c
f.op._add_control_inputs([c.op, d.op])
print (f.op.control_inputs)

它回馈了我的预期[<tf.Operation 'Mul' type=Mul>, <tf.Operation 'Mul_1' type=Mul>]

所以我的问题是,tf.control_dependencies()真的添加控制依赖项吗?还是f.op.control_inputs返回所有控制输入?

标签: pythontensorflow

解决方案


在这里解决了。只是更改tf.get_variable()tf.Varibble().


推荐阅读