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

tf.keras works, tf.python.keras doesn't #33075

Closed
OverLordGoldDragon opened this issue Oct 6, 2019 · 10 comments
Closed

tf.keras works, tf.python.keras doesn't #33075

OverLordGoldDragon opened this issue Oct 6, 2019 · 10 comments
Assignees
Labels
comp:keras Keras related issues stat:awaiting response Status - Awaiting response from author TF 2.0 Issues relating to TensorFlow 2.0 type:support Support issues

Comments

@OverLordGoldDragon
Copy link
Contributor

OverLordGoldDragon commented Oct 6, 2019

DOESN'T WORK:

from tensorflow.python.keras.layers import Input, Dense
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.optimizers import Nadam

ipt = Input(shape=(4,))
out = Dense(1, activation='sigmoid')(ipt)

model = Model(ipt, out)
model.compile(optimizer=Nadam(lr=1e-4), loss='binary_crossentropy')

X = np.random.randn(32,4)
Y = np.random.randint(0,2,(32,1))
model.train_on_batch(X,Y)

WORKS: remove .python from above's imports. Above's error trace below.

Keras 2.3.0 and TensorFlow 2.0.0 freshly-installed via Anaconda, older versions uninstalled. Why the difference?


  File "<ipython-input-7-1e86d21d8fc4>", line 13, in <module>
    model.train_on_batch(X,Y)

  File "D:\Anaconda\envs\tf2_env\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1017, in train_on_batch
    self._make_train_function()

  File "D:\Anaconda\envs\tf2_env\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2116, in _make_train_function
    params=self._collected_trainable_weights, loss=self.total_loss)

  File "D:\Anaconda\envs\tf2_env\lib\site-packages\tensorflow_core\python\keras\optimizers.py", line 653, in get_updates
    grads = self.get_gradients(loss, params)

  File "D:\Anaconda\envs\tf2_env\lib\site-packages\tensorflow_core\python\keras\optimizers.py", line 92, in get_gradients
    if None in grads:

  File "D:\Anaconda\envs\tf2_env\lib\site-packages\tensorflow_core\python\ops\math_ops.py", line 1336, in tensor_equals
    return gen_math_ops.equal(self, other)

  File "D:\Anaconda\envs\tf2_env\lib\site-packages\tensorflow_core\python\ops\gen_math_ops.py", line 3627, in equal
    name=name)

  File "D:\Anaconda\envs\tf2_env\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 545, in _apply_op_helper
    (input_name, err))

ValueError: Tried to convert 'y' to a tensor and failed. Error: None values not supported.

UPDATE: Debugging the two side-by-side, while both use the same files, execution diverges fairly quickly:

# .\tensorflow_core\python\keras\engine\training.py

### TF.KERAS
    if self._experimental_run_tf_function: #  TRUE
	
### TF.PYTHON.KERAS
    if self._experimental_run_tf_function: #  FALSE

Former proceeds to call training_v2_utils.train_on_batch(...) and returns thereafter, latter self._standardize_user_data(...) and others before ultimately failing.

One difference I noted between linked file and mine is, latter's short by ~100 lines - though I installed TF 2 via pip after the file's last update 12 days ago according to Github.

@OverLordGoldDragon
Copy link
Contributor Author

The "one difference" was the key; pip failed - downloaded manually. Looking forward to Anaconda support.

@OverLordGoldDragon
Copy link
Contributor Author

OverLordGoldDragon commented Oct 6, 2019

Nope, wasn't it - somehow made it temporarily work, then something else broke, then couldn't get it to work again with several fresh re-installs. Pip also fails to fetch Github's TensorFlow master branch. Likely a bug. Related SO

@pandrey-fr
Copy link
Contributor

Keras 2.3.0 and TensorFlow 2.0.0 freshly-installed

This is a side note to your issue, but with tensorflow 2.0 you are not using the separate keras package (instead, you are using the keras implementation embarked within tensorflow).

That being said, the imports should work, both with and without specifying .python.. In general, if you place yourself as a user, I think you had better import from tensorflow.keras, which is the intended public display of tensorflow's submodules (if you simply import tensorflow, you have access to tensorflow.keras, but not to tensorflow.python). The python term in the imports refers to the source code organization, and is therefore preferably used when writing source code (which then uses utility functions to reformat its public naming at import time), although you may sometimes need to use it to access specific back-end functionalities which are normally not made public.

@gadagashwini-zz gadagashwini-zz self-assigned this Oct 7, 2019
@gadagashwini-zz gadagashwini-zz added TF 2.0 Issues relating to TensorFlow 2.0 comp:keras Keras related issues labels Oct 7, 2019
@gadagashwini-zz
Copy link
Contributor

@OverLordGoldDragon,
Please use tf.keras instead of tf.python.keras. Please see the gist here. Thanks!

@gadagashwini-zz gadagashwini-zz added the stat:awaiting response Status - Awaiting response from author label Oct 7, 2019
@OverLordGoldDragon
Copy link
Contributor Author

OverLordGoldDragon commented Oct 7, 2019

@gadagashwini Your gist is already in my original post - my question concerns why tf.python.keras fails (it shouldn't)

@mihaimaruseac
Copy link
Collaborator

TL;DR: The API import is in the root of the package. Any other import is just Python allowing you to access privates with no consideration for good coding practices.

Long story short:

The only way that imports should be are

import tensorflow as tf

tf.keras....

We also provide support for

from tensorflow.keras import ...

though this is brittle and can break as we keep refactoring.

Importing from tensorflow.python or any other modules (including import tensorflow_core...) is not supported, and can break unannounced.

@OverLordGoldDragon
Copy link
Contributor Author

OverLordGoldDragon commented Oct 7, 2019

@mihaimaruseac That's fair, except tf.keras itself is full of imports from tf.python.keras - and making custom optimizers often requires imports from either of the two, which still ultimately import from tf.python.keras. It occurred with me while building a custom optimizer - making it compatible with optimizer_v2 thus implies dealing with tf.python.keras and all its music. So even if it's dev-only, dev's still bug-prone - and I've opened a PR per this thread's bug.

Regardless, your answer may be the real one here; so to confirm, tf.python.keras is private, intended for development, rather than public use? Thanks

@mihaimaruseac
Copy link
Collaborator

Regardless, your answer may be the real one here; so to confirm, tf.python.keras is private, intended for development, rather than public use? Thanks

Yes, that's exactly the case. Anything under tf.python is private

@ADH-LukeBollam
Copy link

ADH-LukeBollam commented Aug 17, 2020

Surely something which can silently do different behavior needs warning signs all over it

import tensorflow.keras.backend as K

Imports keras\api\_v2

vs

import tensorflow as tf
K = tf.keras.backend

Imports keras\api\_v1

This is in V1.15.3

@OverLordGoldDragon
Copy link
Contributor Author

OverLordGoldDragon commented Aug 17, 2020

@LukeBolly Fair suggestion; this can be done by overriding __getattr__:

# insert at bottom of `tensorflow/__init__.py`
_keras = keras
del keras

def __getattr__(name):
    if name == 'keras':
        import warnings
        warnings.warn("Using `tf.keras` is not recommended; use "
                      "`from tensorflow.keras import` instead.")
        return _keras
    raise AttributeError("module `tensorflow` has no attribute %s" % name)

(Note: del keras is to bypass Python's special lookup)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:keras Keras related issues stat:awaiting response Status - Awaiting response from author TF 2.0 Issues relating to TensorFlow 2.0 type:support Support issues
Projects
None yet
Development

No branches or pull requests

5 participants