title: Using tf.custom_gradient in tensorflow r1.8
body: <h3>System information</h3>
  
  <ul>
  <li><strong>Have I written custom code (as opposed to using a stock example script provided in TensorFlow)</strong>: Y</li>
  <li><strong>OS Platform and Distribution (e.g., Linux Ubuntu 16.04)</strong>: Ubuntu 16.04</li>
  <li><strong>TensorFlow installed from (source or binary)</strong>: binary</li>
  <li><strong>TensorFlow version (use command below)</strong>: r1.8</li>
  <li><strong>Python version</strong>: 2.7.14</li>
  <li><strong>GCC/Compiler version (if compiling from source)</strong>: 5.4</li>
  <li><strong>CUDA/cuDNN version</strong>: 8.0/7.0</li>
  <li><strong>GPU model and memory</strong>: GTX1080, 8G</li>
  <li><strong>Bazel version</strong>: N/A</li>
  <li><strong>Exact command to reproduce</strong>: python test_script.py</li>
  </ul>
  
  <h3>Describe the problem</h3>
  
  <p>Hello, I'm trying to make a custom_gradient op using the function of <a href="https://www.tensorflow.org/api_docs/python/tf/custom_gradient" rel="nofollow noreferrer">tf.custom_gradient</a>. I made my test code based on the API explanation online. However, it seems there is a problem in the custom_gradient function. Thanks!</p>
  
  <h3>Source code / logs</h3>
  
  <pre><code>import tensorflow as tf
  import numpy as np
  
  @tf.custom_gradient
  def log1pexp(x):
    e = tf.exp(x)
    def grad(dy):
      return dy * (1 - 1 / (1 + e))
    return tf.log(1 + e), grad
  
  x = tf.constant(100.)
  f = tf.custom_gradient(log1pexp)
  
  y, dy = f(x)
  
  sess = tf.Session()
  print (y.eval(session=sess), y.eval(session=sess).shape)
  
  File "/home/local/home/research/DL/unit_tests/tf_test_custom_grad.py", line 14, in &lt;module&gt;
      y, dy = f(x)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/custom_gradient.py", line 111, in decorated
      return _graph_mode_decorator(f, *args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/custom_gradient.py", line 132, in _graph_mode_decorator
      result, grad_fn = f(*args)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 439, in __iter__
  "Tensor objects are not iterable when eager execution is not "
  TypeError: Tensor objects are not iterable when eager execution is not enabled. To iterate over this tensor use tf.map_fn.
  </code></pre>