title: TensorFlow RuntimeError: "Attempting to capture an EagerTensor without building a function"
body: <p>I am trying to build a neural network in Python for solving PDEs, and, as such, I have had to write custom training steps. My training function looks like this:</p>
  <pre class="lang-py prettyprint-override"><code>...
  tf.enable_eager_execution()
  
  class PDENet:
  
       ...
    
       def train_step():
            input = self.input
            
            with tf.GradientTape() as tape, tf.Session() as sess:
                 tape.watch(input)
                 
                 output = self.model(input)
                 self.loss = self.pde_loss(output) # (network does not use training data)
                 
            grad = tape.gradient(self.loss, self.model.trainable_weights)
            self.optimizer.apply_gradients([(grad, self.model)])
  
       ...
  
  </code></pre>
  <p>Due to my hardware, I have no choice but to use tensorflow==1.12.0 and keras==2.2.4.<br>
  When I run this code, I get &quot;RuntimeError: Attempting to capture an EagerTensor without building a function&quot;. I have seen other posts about this, but all of the answers say to update tensorflow/keras, which I can't, use &quot;tf.enable_eager_execution()&quot;, which I've already done, and &quot;tf.disable_v2_behavior()&quot;, which is nonexistent on older versions of tensorflow. Is there anything else I can do to solve this problem? The error makes me think tensorflow wants me to add <code>@tf.function</code>, but that feature also doesn't seem to exist in tensorflow 1.</p>