{"action":"created","issue":{"url":"https://api.github.com/repos/tensorflow/tensorflow/issues/593","labels_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/593/labels{/name}","comments_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/593/comments","events_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/593/events","html_url":"https://github.com/tensorflow/tensorflow/issues/593","id":123597444,"number":593,"title":"automatic gradient computation for nested While (in python interface) fails","user":{"login":"trubin","id":890936,"avatar_url":"https://avatars.githubusercontent.com/u/890936?v=3","gravatar_id":"","url":"https://api.github.com/users/trubin","html_url":"https://github.com/trubin","followers_url":"https://api.github.com/users/trubin/followers","following_url":"https://api.github.com/users/trubin/following{/other_user}","gists_url":"https://api.github.com/users/trubin/gists{/gist_id}","starred_url":"https://api.github.com/users/trubin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trubin/subscriptions","organizations_url":"https://api.github.com/users/trubin/orgs","repos_url":"https://api.github.com/users/trubin/repos","events_url":"https://api.github.com/users/trubin/events{/privacy}","received_events_url":"https://api.github.com/users/trubin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-12-23T03:41:18Z","updated_at":"2015-12-24T03:26:46Z","closed_at":null,"body":"I drew up this simple example to show that nested While gradient computation fails in the python interface (as the gradContext for the forwardContext of the second input of the Add op is None). Toggling the comment demonstrates the issue is not with all control flow graphs.\r\n\r\n```python\r\nimport tensorflow as tf\r\nfrom tensorflow.python.ops import control_flow_ops\r\n\r\nOUTER_LOOP_MAX = 5\r\nINNER_LOOP_MAX = 3\r\n\r\ninner = tf.placeholder(\"float32\", [1, INNER_LOOP_MAX])\r\nouter = tf.placeholder(\"float32\", [1, OUTER_LOOP_MAX])\r\n\r\nX = tf.Variable(tf.zeros([1], dtype=\"float32\"))\r\n\r\nmax = tf.constant(1)\r\n\r\ndef outer_cond_func(c1, outer_acc, outer_array):\r\n    return tf.less(c1, OUTER_LOOP_MAX)\r\n\r\n\r\ndef outer_body_func(c1, outer_acc, outer_array):\r\n    concat = tf.concat(0, [[0], tf.expand_dims(c1, 0)])\r\n    slice = tf.slice(outer_array, concat, [1, 1])\r\n    outer_num = tf.reduce_sum(slice)\r\n\r\n    def inner_cond_func(c2, inner_acc, inner_array):\r\n        return tf.less(c2, INNER_LOOP_MAX)\r\n\r\n    def inner_body_func(c2, inner_acc, inner_array):\r\n        concat2 = tf.concat(0, [[0], tf.expand_dims(c2, 0)])\r\n        inner_num = tf.reduce_sum(tf.slice(inner_array, concat2, [1,1]))\r\n        inner_acc += inner_num * outer_num * X\r\n\r\n        c2 += 1\r\n        return c2, inner_acc, inner_array\r\n\r\n    _, inside_summed_products, _ = control_flow_ops.While(inner_cond_func, inner_body_func, [tf.constant(0), tf.constant(0.0), inner])\r\n\r\n    def true_func():\r\n        return 2*outer_num\r\n\r\n    def false_func():\r\n        return 3*outer_num\r\n\r\n    cond_num = control_flow_ops.cond(tf.less(c1,max),true_func, false_func)\r\n    outer_acc = tf.add(outer_acc, inside_summed_products)\r\n    # outer_acc = tf.add(outer_acc, cond_num)\r\n    c1 += 1\r\n    return c1, outer_acc, outer_array\r\n\r\n_, value, _ = control_flow_ops.While(outer_cond_func, outer_body_func, [tf.constant(0), tf.constant(0.0), outer])\r\ncontrol_flow_ops.switch()\r\n\r\nloss = value * X\r\ntrain_op = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss)\r\n\r\nsess = tf.Session()\r\ninit = tf.initialize_all_variables()\r\nsess.run(init)\r\n\r\nfor i in xrange(1, 10):\r\n    feed_dict = {inner: [[1.0, 2.0, 3.0]], outer: [[4.0, 5.0, 6.0, 7.0, 8.0]]}\r\n    print sess.run([train_op, loss], feed_dict=feed_dict)\r\n```"},"comment":{"url":"https://api.github.com/repos/tensorflow/tensorflow/issues/comments/167035599","html_url":"https://github.com/tensorflow/tensorflow/issues/593#issuecomment-167035599","issue_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/593","id":167035599,"user":{"login":"ebrevdo","id":1794715,"avatar_url":"https://avatars.githubusercontent.com/u/1794715?v=3","gravatar_id":"","url":"https://api.github.com/users/ebrevdo","html_url":"https://github.com/ebrevdo","followers_url":"https://api.github.com/users/ebrevdo/followers","following_url":"https://api.github.com/users/ebrevdo/following{/other_user}","gists_url":"https://api.github.com/users/ebrevdo/gists{/gist_id}","starred_url":"https://api.github.com/users/ebrevdo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ebrevdo/subscriptions","organizations_url":"https://api.github.com/users/ebrevdo/orgs","repos_url":"https://api.github.com/users/ebrevdo/repos","events_url":"https://api.github.com/users/ebrevdo/events{/privacy}","received_events_url":"https://api.github.com/users/ebrevdo/received_events","type":"User","site_admin":false},"created_at":"2015-12-24T03:26:46Z","updated_at":"2015-12-24T03:26:46Z","body":"Thank you for reporting this issue.\n\nWhile is not a documented feature, partly because of issues like this.  We\nwill work on it but make no promises as to when it'll be ready.\n\nOn Wed, Dec 23, 2015 at 4:15 PM, Angel Darquea <notifications@github.com>\nwrote:\n\n> +1\n>\n> —\n> Reply to this email directly or view it on GitHub\n> <https://github.com/tensorflow/tensorflow/issues/593#issuecomment-167012425>\n> .\n>\n"}}