Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Even in eager mode, Keras is passing custom models non-eager tensors in 'fit' #26268

Closed
malmaud opened this issue Mar 1, 2019 · 19 comments
Closed
Assignees
Labels
comp:keras Keras related issues TF 1.14 for issues seen with TF 1.14 type:bug Bug

Comments

@malmaud
Copy link
Contributor

malmaud commented Mar 1, 2019

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): 10.14
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): '1.14.1-dev20190301'
  • Python version: 3.7

Describe the current behavior
When calling keras.model.fit on a custom model, it seems the model is passed a graph-mode tensor instead of an eager tenser, even when in eager mode.

Describe the expected behavior
If in eager mode, the tensors passed to the call method of a custom model should be eager tensors. Otherwise, the advantages of eager mode, like the ability to use native control flow, are lost.

Code to reproduce the issue

import tensorflow as tf
tf.enable_v2_behavior()
from tensorflow import keras

class MyModel(keras.Model):
    def call(self, x):
        if x > 0:
            return x + 1
        else:
            return x - 1
            
m = MyModel()
m(tf.constant(0))  # This works, returns -1 as expected
m.compile(loss='mse', optimizer='sgd')
m.fit(tf.constant(0), tf.constant(1)) # This fails
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-55-2c7beacc3d4f> in <module>
----> 1 m.fit(tf.constant(0), tf.constant(1))

/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    804         steps=steps_per_epoch,
    805         validation_split=validation_split,
--> 806         shuffle=shuffle)
    807 
    808     # Prepare validation data.

/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset)
   2501       else:
   2502         cast_inputs = x_input
-> 2503       self._set_inputs(cast_inputs)
   2504     else:
   2505       y_input = y

/anaconda3/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    454     self._setattr_tracking = False  # pylint: disable=protected-access
    455     try:
--> 456       result = method(self, *args, **kwargs)
    457     finally:
    458       self._setattr_tracking = previous_value  # pylint: disable=protected-access

/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _set_inputs(self, inputs, outputs, training)
   2773             outputs = self.call(inputs, training=training)
   2774           else:
-> 2775             outputs = self.call(inputs)
   2776           # Reset to the previously saved value. If `call()` had `add_metric`
   2777           # or `add_loss`, then `_contains_symbolic_tensors` will have been set

<ipython-input-52-94b7a4f52815> in call(self, x)
      1 class MyModel(keras.Model):
      2     def call(self, x):
----> 3         if x > 0:
      4             return x+1
      5         else:

/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in __bool__(self)
    658       `TypeError`.
    659     """
--> 660     raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
    661                     "Use `if t is not None:` instead of `if t:` to test if a "
    662                     "tensor is defined, and use TensorFlow ops such as "

TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.
@ymodak ymodak self-assigned this Mar 6, 2019
@ymodak ymodak added comp:keras Keras related issues type:bug Bug labels Mar 6, 2019
@ymodak ymodak added the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Mar 6, 2019
@ymodak ymodak removed their assignment Mar 6, 2019
@malmaud
Copy link
Contributor Author

malmaud commented Mar 6, 2019

Note that even adding run_eagerly=True to the compile call doesn't resolve this.

@jekbradbury
Copy link
Contributor

I believe you need to pass dynamic=True to the model constructor:

m = MyModel(dynamic=True)

Otherwise Keras assumes that your model is able to take symbolic tensors (and uses this for shape inference) even if you've asked it to compile in eager mode. This was added in d0373dc, which has a little more context.

@jekbradbury
Copy link
Contributor

There is a bug here, though, in that Keras should probably tell you why this is failing.

@malmaud
Copy link
Contributor Author

malmaud commented Mar 7, 2019

Thanks, that does the trick!

There also might be a bit a documentation issue here - I don't recall this being prominently highlighted in the new tutorials and overviews of eager mode/keras.

@tensorflowbutler tensorflowbutler removed the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Mar 7, 2019
@pavithrasv
Copy link
Member

Thank you @jekbradbury for the quick response on this! We will definitely need to throw an error explaining the issue here. I will look into that.

@malmaud I agree that this is not documented well. This concept is very new and we are working to see if we can remove the need for this hence the lack of documentation.

@jvishnuvardhan jvishnuvardhan added the TF 1.14 for issues seen with TF 1.14 label Sep 30, 2019
@wevonosky
Copy link

Can we get some more explanation for how to add dynamic=True and what that does?

@brett-daley
Copy link

brett-daley commented Feb 7, 2020

I found a solution that works for me and posted it in this StackOverflow answer.

tl;dr set the run_eagerly attribute manually after compiling your model:

model.compile()
model.run_eagerly = True

@yin1999
Copy link

yin1999 commented Jun 26, 2020

@brett-daley It works! Thank you.

@surGeonGG
Copy link

I am having the exact same problem with keras version 2.3.0-tf and tensorflow version 2.2.0. Adding dynamic=True gives me this error for a functional model:

TypeError: ('Functional models may only specify 'name' and 'trainable' keyword arguments during initialization. Got an unexpected argument:', 'dynamic')

@surGeonGG
Copy link

I was able to fix my issue by subclassing tf.Keras.Model. Also, there was no need to pass dynamic=True to the model constructor; run_eagerly=True when compiling works.

@talpay
Copy link

talpay commented Sep 8, 2020

@surGeonGG Can you be more specific please? I assume you did more than just simply subclassing because propagating the init via a super-call shouldn't make any difference.

@PrattJena
Copy link

I am having the same issue when running this on 2.3.0. Doing this also doesnt resolve the issue

model.compile()
model.run_eagerly = True

@dannyng
Copy link

dannyng commented Nov 23, 2020

Same with PrattJena, running on 2.3.0 and have same issue even with run_eagerly = True

@Carterbouley
Copy link

I also have this same issue. Any pointers towards a solution?

@tensorflowbutler
Copy link
Member

Hi There,

We are checking to see if you still need help on this, as you are using an older version of tensorflow which is officially considered end of life . We recommend that you upgrade to the latest 2.x version and let us know if the issue still persists in newer versions. Please open a new issue for any help you need against 2.x, and we will get you the right help.

This issue will be closed automatically 7 days from now. If you still need help with this issue, please provide us with more information.

@Carterbouley
Copy link

Carterbouley commented Feb 21, 2021 via email

@ymodak
Copy link
Contributor

ymodak commented Mar 2, 2021

Closing this issue since it is resolved. Feel free to reopen if necessary. Thanks!

@ymodak ymodak closed this as completed Mar 2, 2021
@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@geetachavan1 geetachavan1 added this to Done in TensorFlow 2.5 Mar 10, 2021
@eyildiz-ugoe
Copy link

This isn't solved. I was able to reproduce the error even after passing the suggested line after the compile() function is called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:keras Keras related issues TF 1.14 for issues seen with TF 1.14 type:bug Bug
Projects
Development

No branches or pull requests