title: TensorFlow Eager Execution: AttributeError: 'NoneType' object has no attribute '_tape'
body: <p>I'm trying to run a custom-build model with TF in Eager Execution, but receive the error below, which I really don't know how to interpret: </p>
  
  
  
  <pre class="lang-python prettyprint-override"><code>AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-24-113069c95a32&gt; in &lt;module&gt;()
       17     answer=tf.convert_to_tensor(answers_train[i], dtype=tf.float32)
       18 
  ---&gt; 19     grads = grad(model, sent, quest, answer)
       20     optimizer.apply_gradients(zip(grads, model.variables),
       21                             global_step=tf.train.get_or_create_global_step())
  
  &lt;ipython-input-20-21f32f7e2b32&gt; in grad(model, sent, quest, targets)
        2       with tfe.GradientTape() as tape:
        3         loss_value = loss(model, sent, quest, targets)
  ----&gt; 4         return tape.gradient(loss_value, model.variables)
  
  /Users/sdoneva/anaconda/lib/python3.6/site-packages/tensorflow/python/eager/backprop.py in __exit__(self, typ, value, traceback)
      715 
      716   def __exit__(self, typ, value, traceback):
  --&gt; 717     tape.pop_tape(self._tape)
      718 
      719   def watch(self, tensor):
  
  /Users/sdoneva/anaconda/lib/python3.6/site-packages/tensorflow/python/eager/tape.py in pop_tape(tape)
       60 def pop_tape(tape):
       61   """Pops the top tape in the stack, if any."""
  ---&gt; 62   pywrap_tensorflow.TFE_Py_TapeSetRemove(tape._tape)  # pylint: disable=protected-access
       63 
       64 
  
  AttributeError: 'NoneType' object has no attribute '_tape'
  </code></pre>
  
  <p>My inputs are arrays converted to Tensors: </p>
  
  <pre class="lang-python prettyprint-override"><code>sent=tf.convert_to_tensor(contexts_train[i], dtype=tf.float32)
  quest=tf.convert_to_tensor(questions_train[i], dtype=tf.float32)
  answer=tf.convert_to_tensor(answers_train[i], dtype=tf.float32)
  </code></pre>
  
  <p>And this is how I defined my grad and loss function:</p>
  
  <pre class="lang-python prettyprint-override"><code>def grad(model, sent, quest, targets):
        with tfe.GradientTape() as tape:
          loss_value = loss(model, sent, quest, targets)
          return tape.gradient(loss_value, model.variables)
  def loss(model, sent, quest, y):
      prediction = model.predict(sent, quest)
      return tf.keras.losses.categorical_crossentropy(y, prediction)
  </code></pre>
  
  <p>And how I call the training:</p>
  
  <pre class="lang-python prettyprint-override"><code>grads = grad(model, sent, quest, answer)
  optimizer.apply_gradients(zip(grads, model.variables),
                          global_step=tf.train.get_or_create_global_step())
  </code></pre>
  
  <p>For the sake of completeness, here the Model:</p>
  
  <pre class="lang-python prettyprint-override"><code>class Model(tf.keras.Model):
  
      def __init__(self):
          super(Model, self).__init__()
          self.embed=tf.keras.layers.Embedding(42,50)
          self.grucell=tf.keras.layers.GRUCell(50)
          self.rnn=tf.keras.layers.RNN(self.grucell)
          self.dense=tf.keras.layers.Dense(42,activation=tf.nn.softmax)
          self.dropout=tf.keras.layers.Dropout(0.3)
  
      def predict(self, sentence, question):
          encoded_sentence=self.embed(sentence)
          encoded_sentence=tf.keras.backend.expand_dims(encoded_sentence, axis=-1)  
          encoded_sentence=self.rnn(encoded_sentence)
          encoded_sentence=self.dropout(encoded_sentence)
  
          encoded_question=self.embed(question)
          encoded_question=tf.keras.backend.expand_dims(encoded_question, axis=-1)
          encoded_question=self.rnn(encoded_question)
          encoded_question=self.dropout(encoded_question)
  
          merged= tf.keras.layers.concatenate([encoded_sentence, encoded_question])
          pred= self.dense(merged)
          pred= tf.keras.backend.expand_dims(pred, axis=1)
  
          return pred
  </code></pre>
  
  <p>It would be great to receive some help to understand what is happening in the background.</p>