TensorFlow RuntimeError: "Attempting to capture an EagerTensor without building a function"

939 views Asked by At

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:

...
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)])

     ...

Due to my hardware, I have no choice but to use tensorflow==1.12.0 and keras==2.2.4.
When I run this code, I get "RuntimeError: Attempting to capture an EagerTensor without building a function". I have seen other posts about this, but all of the answers say to update tensorflow/keras, which I can't, use "tf.enable_eager_execution()", which I've already done, and "tf.disable_v2_behavior()", 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 @tf.function, but that feature also doesn't seem to exist in tensorflow 1.

0

There are 0 answers