I. A
I. A

Reputation: 2312

Why Assignment Operation cannot be added as a parameter to the control depedency in tensorflow?

I have a simple model. But I need to modify a variable before performing an update. Hence, I have the following:

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

Therefore, when running the above script, I got the following error:

Can not convert a list into a Tensor or Operation.

What is causing this problem is update_moving_loss and dec_l. When I remove them, the code runs fine.

Please note that I am using tensorflow 1.7

Any help is much appreciated!!!

Upvotes: 0

Views: 45

Answers (1)

mikkola
mikkola

Reputation: 3476

I cannot reproduce your problem, but my guess is the problem lies with using tf.get_collection. As specified in the documentation, it will return a list of values in the collection. Let's call this list L to make things concise.

You then do something like this:

L = tf.get_collection(tf.GraphKeys.UPDATE_OPS)    
with tf.control_dependencies([L, tensor1, tensor2]):
         ...

The argument there is another list, with three elements: your list L from before, and two tensors. This is the problem: the first element is a list, and thus you see

Can not convert a list into a Tensor or Operation.

You can probably resolve the issue by appending to L rather than making another list-with-a-list-inside:

L = tf.get_collection(tf.GraphKeys.UPDATE_OPS)    
with tf.control_dependencies( L + [tensor1, tensor2]):
         ...

The + operator adds the new list elements to the original list L.

Upvotes: 1

Related Questions