为什么赋值操作不能作为参数添加到tensorflow中的控制依赖?
Why Assignment Operation cannot be added as a parameter to the control depedency in tensorflow?
我有一个简单的模型。但是我需要在执行更新之前修改一个变量。因此,我有以下内容:
l = tf.Variable(tf.constant(0.01), trainable=False, name="l")
baseline = tf.Variable(tf.constant(0.0), dtype=tf.float32, name="baseline")
# Actor optimizer
# Note that our target is: e^{-L} where L is the loss on the validation dataset.
# So we pass to the target mae_loss in the code below
def optimize_actor(scores_a, scores_v, target):
with tf.name_scope('Actor_Optimizer'):
update_moving_loss = tf.assign(baseline, l * baseline + (1 - l) * target, name="update_baseline")
dec_l = l.assign_add(0.001)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies([update_ops, update_moving_loss, dec_l]):
loss_a = scores_a * tf.exp(target - baseline)
loss_v = scores_v * tf.exp(target - baseline)
actor_a_optimizer = tf.train.AdamOptimizer(0.0001).minimize(loss_a)
actor_v_optimizer = tf.train.AdamOptimizer(0.0001).minimize(loss_v)
return actor_a_optimizer, actor_v_optimizer
因此,当运行上述脚本时,我得到了以下错误:
Can not convert a list into a Tensor or Operation.
导致此问题的原因是 update_moving_loss
和 dec_l
。当我删除它们时,代码运行正常。
请注意,我使用的是tensorflow 1.7
非常感谢任何帮助!!!
我无法重现您的问题,但我猜问题出在使用 tf.get_collection
上。正如在 documentation 中指定的那样,它将 return 集合中的一个 list 值。为了简洁起见,我们将此列表称为 L
。
然后你做这样的事情:
L = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies([L, tensor1, tensor2]):
...
参数是另一个列表,包含三个元素:您之前的列表 L
和两个张量。这就是问题:第一个元素是一个列表,因此你看到
Can not convert a list into a Tensor or Operation.
您可能可以通过 将 附加到 L 而不是创建另一个 list-with-a-list-inside:
来解决问题
L = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies( L + [tensor1, tensor2]):
...
+
运算符将新列表元素添加到原始列表 L
。
我有一个简单的模型。但是我需要在执行更新之前修改一个变量。因此,我有以下内容:
l = tf.Variable(tf.constant(0.01), trainable=False, name="l")
baseline = tf.Variable(tf.constant(0.0), dtype=tf.float32, name="baseline")
# Actor optimizer
# Note that our target is: e^{-L} where L is the loss on the validation dataset.
# So we pass to the target mae_loss in the code below
def optimize_actor(scores_a, scores_v, target):
with tf.name_scope('Actor_Optimizer'):
update_moving_loss = tf.assign(baseline, l * baseline + (1 - l) * target, name="update_baseline")
dec_l = l.assign_add(0.001)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies([update_ops, update_moving_loss, dec_l]):
loss_a = scores_a * tf.exp(target - baseline)
loss_v = scores_v * tf.exp(target - baseline)
actor_a_optimizer = tf.train.AdamOptimizer(0.0001).minimize(loss_a)
actor_v_optimizer = tf.train.AdamOptimizer(0.0001).minimize(loss_v)
return actor_a_optimizer, actor_v_optimizer
因此,当运行上述脚本时,我得到了以下错误:
Can not convert a list into a Tensor or Operation.
导致此问题的原因是 update_moving_loss
和 dec_l
。当我删除它们时,代码运行正常。
请注意,我使用的是tensorflow 1.7
非常感谢任何帮助!!!
我无法重现您的问题,但我猜问题出在使用 tf.get_collection
上。正如在 documentation 中指定的那样,它将 return 集合中的一个 list 值。为了简洁起见,我们将此列表称为 L
。
然后你做这样的事情:
L = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies([L, tensor1, tensor2]):
...
参数是另一个列表,包含三个元素:您之前的列表 L
和两个张量。这就是问题:第一个元素是一个列表,因此你看到
Can not convert a list into a Tensor or Operation.
您可能可以通过 将 附加到 L 而不是创建另一个 list-with-a-list-inside:
来解决问题L = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies( L + [tensor1, tensor2]):
...
+
运算符将新列表元素添加到原始列表 L
。