0

Currently in tensorflow 2.0, Keras can be imported by both tensorflow.keras and tensorflow.python.keras. What's the difference and how should I choose from these two imports?

from tensorflow.keras import *
from tensorflow.python.keras import *

From my understanding, tensorflow.python.keras is pretty much the same as the python package Keras, while tensorflow.keras is implemented by TensorFlow which might be more compatible with TF, but do not have all packages as in Keras such as Layer, InputSpec, etc.

One other issue is that for some IDEs like PyCharm, they cannot find or auto-complete packages from tensorflow.keras, and the temporary solution is to use tensorflow.python.keras instead. (from tensorflow issue)

junhuizh
  • 402
  • 5
  • 11

3 Answers3

1

In TensorFlow 2.0, tf.keras is recommended. This version is stable now.

junhuizh
  • 402
  • 5
  • 11
0

tensorflow.keras and tensorflow.python.keras are exactly the same package, what tells you that they are different?

You are not supposed to use tensorflow.python imports directly, as per this answer: https://stackoverflow.com/a/47306203/6108843

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
  • Sorry I forgot to mention that I am using tensorflow 2.0. In 2.0, some packages in keras are missing such as `tensorflow.keras.layers.InputSpec`. And if they are exactly the same, why does tensorflow keep both of them? – junhuizh Sep 10 '19 at 09:28
  • @junhuizh The answer to that is available in the other answer I linked: https://stackoverflow.com/a/47306203/6108843 – Dr. Snoopy Sep 10 '19 at 09:31
0

As of the latest version of Tensorflow, it is recommended to use tensorflow.keras This is likely due to depreciation since TF is evolving rapidly.

Renart
  • 23
  • 3