jean jean - 1 month ago 18
Python Question

Tensorflow control_dependencies not working with list

I have a cost, which depends on two variables

a
and
b
.

I want to :


  1. calculate both gradients of the cost at the current point,

  2. update the loss w.r.t. the first variable (
    a
    )

  3. update the loss w.r.t. the second variable (
    b
    ).



In that order.

To do this I tried something like this:

opt=tf.train.GradientDescentOptimizer(0.001)
grad_cost_wrt_a=opt.compute_gradients(cost,[a])
grad_cost_wrt_b=opt.compute_gradients(cost,[b])

with tf.control_dependencies([grad_cost_wrt_a,grad_cost_wrt_b]):
update_wrt_a=opt.apply_gradients(grad_cost_wrt_a)
with tf.control_dependencies([update_wrt_a]):
update_wrt_b=opt.apply_gradients(grad_cost_wrt_b)


To be able to then do:

sess.run([update_wrt_a,update_wrt_b],feed_dict={x: x_input, y: y_input})


First this does not work I get:
cannot convert a list into a Tensor or Operation but control_dependencies is supposed to recieve a list of tensors...

Then bonus question do I really need all those control_dependencies ?

Answer

Your grad_cost_wrt_a and grad_cost_wrt_b variables are lists, do something like grad_cost_wrt_a[0], grad_cost_wrt_b[0]

Comments