3

I have some working Python3 sources gotten from the internet where initial Keras imports are direct like this:

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
...

In TensorFlow documentation instead I see the following indirect form:

import tensorflow as tf
from tensorflow.keras import layers
...

To me they seem to mean respectively, that Keras can be used without knowing that TensorFlow is behind, and, that Keras is provided (again?) as a part of TensorFlow. (I kind of expect that also Keras similarly provides references to TensorFlow in the former case)

What is the difference? Does it depend on how Keras and TensorFlow are installed, or rather on the way they are used? Is it a potential source of confusion that I have to get rid of? In other words, should I fix my installation, and how? Or should I just accept that there are two, and manage their respective usages to live with them safely?

Background: my installation is under Ubuntu Linux, with Python3.5.2, where pip3 list shows the following packages:

Keras (2.2.4)
Keras-Applications (1.0.6)
Keras-Preprocessing (1.0.5)
tensorboard (1.12.0)
tensorflow (1.12.0)

BTW, I have checked that they are really different:

import keras as keras
import tensorflow.keras as tf_keras
print( keras is tf_keras )
---> False
print( [keras.__version__ , tf_keras.__version__] )
---> ['2.2.4', '2.1.6-tf']
print( [len(dir(keras)) , len(dir(tf_keras)) ] )
---> [32, 30]
print( [ len(dir(keras.models)) , len(dir(tf_keras.models)) ] )
---> [27, 17]
print( [ len(dir(keras.layers)) , len(dir(tf_keras.layers)) ] )
---> [167, 117]

and indeed it seems that I have two different Keras and that the former is higher versioned and richer.

Related readings, useful but not enough to solve the "does it need a fix?" question:

Thanks!

lurix66
  • 502
  • 1
  • 5
  • 14
  • I agree it is confusing to have the option to import keras or tf.keras. For my part, I only installed tensorflow and it seems to have come with both as part of the install (via conda). I am also unclear on which I should go with. – Mastiff Mar 17 '19 at 03:51

3 Answers3

1

Rather than posting my own answer, I'll point you to a very exhaustive answer, much better than I would be able to write: it is here (thanks to Adrian Rosebrock).

Disclaimer: I have no link with Adrian or his activity. I have greatly appreciated his explanation though.

lurix66
  • 502
  • 1
  • 5
  • 14
0

There is no fix needed. They're two different packages and you just manage their respective usages.

Manoj Mohan
  • 5,654
  • 1
  • 17
  • 21
0

Official word as of September 2021:

User should always use from tensorflow import keras which will give them the public API.

import keras will directly access the keras PIP package, which is not 100% same as the public API namespace. It will probably give you keras.Model/layers.* etc, but not all the APIs. Under the hood, we populate all the APIs under keras/api/* and tensorflow __init__ files will pick them up from there.

https://discuss.tensorflow.org/t/keras-project-moved-to-new-repository-in-https-github-com-keras-team-keras/1999/10

bers
  • 4,817
  • 2
  • 40
  • 59