Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.:

  • Getting value of tensor
  • Compiling model
  • Saving optimizer

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:

  • CUDA 10.0.130, cuDNN 7.4.2, Python 3.7.4, Windows 10
  • tensorflow, tensorflow-gpu v2.0.0, and Keras 2.3.0 via pip, all else via Anaconda 3
like image 659
OverLordGoldDragon Avatar asked Oct 08 '19 02:10

OverLordGoldDragon


People also ask

What is the difference between TensorFlow Python Keras and TensorFlow Keras?

TensorFlow is an open-sourced end-to-end platform, a library for multiple machine learning tasks, while Keras is a high-level neural network library that runs on top of TensorFlow. Both provide high-level APIs used for easily building and training models, but Keras is more user-friendly because it's built-in Python.

Is TF Keras the same as Keras?

The difference between tf. keras and keras is the Tensorflow specific enhancement to the framework. keras is an API specification that describes how a Deep Learning framework should implement certain part, related to the model definition and training.

What is TF Keras?

Keras is a deep learning API written in Python, running on top of the machine learning platform TensorFlow. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result as fast as possible is key to doing good research.

Do I need Keras for TensorFlow?

You can use TensorFlow without Keras and you can use Keras with CNTK, Theano, or other machine learning libraries. While you can use Keras without TensorFlow, Keras is always going to need a backend; it's simply an interface rather than a major processing utility.


1 Answers

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.

like image 56
OverLordGoldDragon Avatar answered Oct 17 '22 01:10

OverLordGoldDragon