What is the difference between tf.keras and tf.python.keras?
Solution 1:
From an official TensorFlow dev, shortened (emphasis mine):
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.
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 fromtensorflow.python
or any other modules (includingimport tensorflow_core
) is not supported, and can break unannounced.
Me: To confirm, tf.python.keras
is private, intended for development, rather than public use?
Yes, that's exactly the case. Anything under
tf.python
is private
This, however, is not the full picture. tf.python
remains the only way to access certain functions / classes - e.g., tf.python.framework
and tf.python.ops
, both used in tf.keras.optimizers
. But as per above, this doesn't become a concern unless you're "developing" - i.e. writing custom functionality or classes. "Out of box" usage should be fine without ever touching tf.python
.
Note this isn't only a compatibility matter, and the two are not interchangeable "as long as nothing breaks"; for example, tf.keras
uses optimizer_v2, which differs substantially from tf.python.keras
Optimizer.
Lastly, note that both above links end up in tf.python.keras
-- not certain, but it appears that tf.keras
doesn't actually exist in TF Github (e.g. nothing references OptimizerV2
), but it does merge with TF in tensorflow_core/python/keras/api/_v2
folder when installed locally:
from tensorflow import keras
print(keras.__file__)
from tensorflow.python import keras
print(keras.__file__)
D:\Anaconda\lib\site-packages\tensorflow_core\python\keras\api\_v2\keras\__init__.py
D:\Anaconda\lib\site-packages\tensorflow_core\python\keras\__init__.py
Though both share the python/
folder, they're not both tf.python
- can be verified from their respective __init__.py
.
UPDATE: tf.python.keras.optimizers
used with tf.python.keras.layers
vs tf.keras.optimizers
used with tf.keras.layers
runs 11.5x slower for a mid-sized model (code). I continue to see former in user code - consider this a note of warning.