From de26d3e7d5cda6c3c43f644f77d935a0e3db3d6d Mon Sep 17 00:00:00 2001 From: Asim Shankar Date: Fri, 27 Apr 2018 12:59:20 -0700 Subject: [PATCH] eager: Improve error message when GradientTape is used incorrectly. PiperOrigin-RevId: 194580654 --- tensorflow/python/eager/pywrap_tfe_src.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tensorflow/python/eager/pywrap_tfe_src.cc b/tensorflow/python/eager/pywrap_tfe_src.cc index 2bfa1f0..4ecba1a 100644 --- a/tensorflow/python/eager/pywrap_tfe_src.cc +++ b/tensorflow/python/eager/pywrap_tfe_src.cc @@ -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; } } -- 2.7.4