jean - 11 months ago 81

Python Question

I have a cost, which depends on two variables

`a`

`b`

I want to :

- calculate both gradients of the cost at the current point,
- update the loss w.r.t. the first variable ()
`a`

- 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 Source

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]`