OverLordGoldDragon
OverLordGoldDragon

Reputation: 19776

What is the difference between tf.keras and tf.python.keras?

I've ran into serious incompatibility problems for the same code ran with one vs. the other; e.g.:

Looking into the Github source, the modules and their imports look fairly identical, and tf.keras even imports from tf.python.keras. In tutorials, I see both being used time to time. As an example, code below will fail with tf.python.keras.

What's the deal? What is the difference, and when should I use one or the other?


from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Nadam
import numpy as np

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)

ADDITIONAL INFO:

Upvotes: 14

Views: 4800

Answers (1)

OverLordGoldDragon
OverLordGoldDragon

Reputation: 19776

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 from tensorflow.python or any other modules (including import 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.

Upvotes: 15

Related Questions