eager: Improve error message when GradientTape is used incorrectly.
authorAsim Shankar <ashankar@google.com>
Fri, 27 Apr 2018 19:59:20 +0000 (12:59 -0700)
committerTensorFlower Gardener <gardener@tensorflow.org>
Fri, 27 Apr 2018 20:01:43 +0000 (13:01 -0700)
PiperOrigin-RevId: 194580654

tensorflow/python/eager/pywrap_tfe_src.cc

index 2bfa1f0..4ecba1a 100644 (file)
@@ -1402,8 +1402,12 @@ PyObject* TFE_Py_TapeGradient(PyObject* tape, PyObject* vspace,
     auto* tape_set = GetTapeSet();
     if (tape_set->find(tape_obj) != tape_set->end()) {
       PyErr_SetString(PyExc_RuntimeError,
-                      "Trying to call tape.gradient on a non-persistent tape "
-                      "while it is still active.");
+                      "gradient() cannot be invoked within the "
+                      "GradientTape context (i.e., while operations are being "
+                      "recorded). Either move the call to gradient() to be "
+                      "outside the 'with tf.GradientTape' block, or "
+                      "use a persistent tape: "
+                      "'with tf.GradientTape(persistent=true)'");
       return nullptr;
     }
   }