tensorflow: Importing submodules from tensorflow.keras fails with No module named 'tensorflow.keras'
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 16.04
- TensorFlow installed from (source or binary): binary (pip)
- TensorFlow version (use command below): v1.4.0-19-ga52c8d9 1.4.1
- Python version: 3.6
- Exact command to reproduce: from tensorflow.keras import datasets # fails but should work import tensorflow as tf #succeeds tf.keras.datasets #succeeds from tensorflow.python.keras import datasetst # succeeds
Describe the problem
Importing submodules from tensorflow.keras fails with error: ModuleNotFoundError: No module named 'tensorflow.keras'. but import tensorflow as tf and then doing tf.keras.datasets works. This is a big inconsistency, also it means that every time an element from within the tensforlow.keras module you need to write the complete path (which is very annoying) this removes the simplicity and readability of the keras API. A work around is to import submodules from tensorflow.python.keras, which again is inconsistent.
In my opinion, since the documentation states that keras is availabe at tf.keras that should be the access path to the submodules and not tensorflow.python.keras. I’ll try to make a pull request for this.
Source code / logs
# fails but should work
from tensorflow.keras import datasets
# succeeds
import tensorflow as tf
tf.keras.datasets
# succeeds
from tensorflow.python.keras import datasetst
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 16
- Comments: 24 (8 by maintainers)
@rragundez and @drpngx thanks for the quick fix and response, especially on New Years!
@drpngx Please explain the philosophy. Why have you made that decision and what do you propose is a good way of importing Keras from TensorFlow?
Perhaps @fchollet has a comment on the best way of importing Keras from TensorFlow?
Currently I am writing:
But this is a bit clumsy compared to just writing the following, which unfortunately does not work:
It would be even more clumsy to write e.g.:
One workaround would seem to be:
But I am not really a Python expert so I don’t know if this is considered bad style for some reason?
Happy New Years! 😃
I imagine most users of TensorFlow need to use a builder API. It has been a never-ending source of frustration for myself and the community that you have had numerous different and unfinished builder API’s, instead of consolidating their best features into a single default API. You never even finished trivial features of the Layers API before you abandoned it.
To my knowledge Keras is the best builder API for TensorFlow. I would therefore encourage you to consider Keras to be the default builder API that many people will be using with TensorFlow. I think it would be wise to give it special treatment in the code-base if that is necessary.
Similarly goes for Keras’ author @fchollet whose philosophy on API design is wonderfully user-centric so I think his opinion should weigh heavily in your discussions on the rest of TensorFlow’s API as well.
from tensorflow.python.keras.layers.core import Reshape,Dense,Dropout,Activation,Flattenworks for me
Can someone clearly explain this:
“Works” import tensorflow from tensorflow.keras import _______
Doesnot work: import tensorflow as tf from tf.keras import ________
The standard python methods for importing modules and symbols from modules do not appear to work for parts of tensorflow that are described as modules. That includes tensorflow.keras, tensorflow.keras.layers, and tensorflow.keras.models. The result is that tensorflow comes across as a second class python package written by programmers who do not know what they are doing. It also results in ugly code when the full path has to be written for every symbol in a tensorflow script that uses the keras object model. If Google wants to project tensorflow as high quality software, this second class implementation needs to be fixed.
@dnjake
“second class python package written by programmers who do not know what they are doing” seems to be a bold statement. Google open-sourced tensorflow and Facebook open-sourced pytorch. tensorflow and pytorch are not perfect, but they are definitely valuable. Let us thank Google and Facebook for open-sourcing these valuable software. (I am a novice for tensorflow and pytorch.)
Pull request submitted https://github.com/tensorflow/tensorflow/pull/15757 This only solves the import problem with
tensorflow.python.keras, can be easily modified to do the same for any submodule inside thetensorflow.pythonmodule.I can confirm this problem exists with my system settings that are similar.
@rragundez Thanks for reposting this issue here and taking care of it!
I can call keras.something when I installed keras on NVIDIA Jetson TX2 with
sudo apt-get install python3-keras