{"action":"opened","issue":{"url":"https://api.github.com/repos/tensorflow/tensorflow/issues/3114","repository_url":"https://api.github.com/repos/tensorflow/tensorflow","labels_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/3114/labels{/name}","comments_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/3114/comments","events_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/3114/events","html_url":"https://github.com/tensorflow/tensorflow/issues/3114","id":163042317,"number":3114,"title":"Backpropagation through the while-loop doesn't work if external tensors are used inside","user":{"login":"rizar","id":654434,"avatar_url":"https://avatars.githubusercontent.com/u/654434?v=3","gravatar_id":"","url":"https://api.github.com/users/rizar","html_url":"https://github.com/rizar","followers_url":"https://api.github.com/users/rizar/followers","following_url":"https://api.github.com/users/rizar/following{/other_user}","gists_url":"https://api.github.com/users/rizar/gists{/gist_id}","starred_url":"https://api.github.com/users/rizar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rizar/subscriptions","organizations_url":"https://api.github.com/users/rizar/orgs","repos_url":"https://api.github.com/users/rizar/repos","events_url":"https://api.github.com/users/rizar/events{/privacy}","received_events_url":"https://api.github.com/users/rizar/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-06-29T22:32:29Z","updated_at":"2016-06-29T22:32:29Z","closed_at":null,"body":"In the simple example below I copy `inputs` to `outputs` in a while loop. When I try to take gradient of the `outputs` w.r. to `inputs`, I get an error. It's worth noting that 1) the forward pass works 2) the gradient computation works if I made `inputs` a `TensorArray`, like it is done in the [scan implementation](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/functional_ops.py#L289)\r\n\r\nThe code:\r\n\r\n```python\r\nwith tf.Graph().as_default(), tf.Session() as sess:\r\n  num_steps = 9\r\n\r\n  inputs = tf.placeholder(dtype='float32', shape=(num_steps))\r\n  initial_outputs = tf.TensorArray(dtype=tf.float32, size=num_steps)\r\n  initial_i = tf.constant(0, dtype='int32')\r\n    \r\n  def should_continue(i, *args):\r\n    return i < num_steps\r\n  \r\n  def iteration(i, outputs_):\r\n    outputs_ = outputs_.write(i, tf.gather(inputs, i))\r\n    return i + 1, outputs_\r\n  \r\n  i, outputs = tf.while_loop(\r\n    should_continue, iteration,\r\n    [initial_i, initial_outputs])\r\n  \r\n  outputs = outputs.pack()\r\n  grad_wr_inputs = tf.convert_to_tensor(tf.gradients([tf.reduce_sum(outputs)], [inputs])[0])\r\n  print sess.run([outputs, grad_wr_inputs],\r\n                 feed_dict={inputs: [4, 6, 0, 7, 0, 0, 1, 2, 0]})\r\n```\r\n\r\nThe stack trace:\r\n\r\n```\r\n---------------------------------------------------------------------------\r\nInvalidArgumentError                      Traceback (most recent call last)\r\n<ipython-input-354-e1d117e742f9> in <module>()\r\n     20   grad_wr_inputs = tf.convert_to_tensor(tf.gradients([tf.reduce_sum(outputs)], [inputs])[0])\r\n     21   print sess.run([outputs, grad_wr_inputs],\r\n---> 22                  feed_dict={inputs: [4, 6, 0, 7, 0, 0, 1, 2, 0]})\r\n\r\n/google/data/ro/teams/colab/tensorflow/google3/third_party/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)\r\n    366     try:\r\n    367       result = self._run(None, fetches, feed_dict, options_ptr,\r\n--> 368                          run_metadata_ptr)\r\n    369       if run_metadata:\r\n    370         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)\r\n\r\n/google/data/ro/teams/colab/tensorflow/google3/third_party/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)\r\n    639     movers = self._update_with_movers(feed_dict_string, feed_map)\r\n    640     results = self._do_run(handle, target_list, unique_fetches,\r\n--> 641                            feed_dict_string, options, run_metadata)\r\n    642 \r\n    643     # User may have fetched the same tensor multiple times, but we\r\n\r\n/google/data/ro/teams/colab/tensorflow/google3/third_party/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)\r\n    707     if handle is None:\r\n    708       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,\r\n--> 709                            target_list, options, run_metadata)\r\n    710     else:\r\n    711       return self._do_call(_prun_fn, self._session, handle, feed_dict,\r\n\r\n/google/data/ro/teams/colab/tensorflow/google3/third_party/tensorflow/python/client/session.py in _do_call(self, fn, *args)\r\n    727         except KeyError:\r\n    728           pass\r\n--> 729       raise type(e)(node_def, op, message)\r\n    730 \r\n    731   def _extend_graph(self):\r\n\r\nInvalidArgumentError: All inputs to node Slice must be from the same frame.\r\n```\r\n\r\n"}}
{"actor":{"display_login":"rizar"}}