TensorFlow 2 and Keras:

432 views Asked by At

When I want to use Keras with TensorFlow 2 I got this error:

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

2

There are 2 answers

0
Ivan Lorusso On

Since TensorFlow 2 defaults to eager execution Keras will need some changes in order to be compatible with it, but until then a previous version of TensorFlow is required.

0
MiniQuark On

The Keras API (https://keras.io/) has multiple implementations, including the original and reference implementation (https://github.com/keras-team/keras), but also various other implementations, including tf.keras, which is part of TensorFlow.

So there are two ways you can use Keras with TensorFlow:

  1. Using the reference implementation with the TensorFlow backend. However, this implementation has not been updated to support TensorFlow 2 yet (as of June 2019).
  2. Using TensorFlow's implementation, tf.keras. This one works fine with TF 2.

To use tf.keras, you must make sure to use the correct imports:

from tensorflow import keras
# NOT: import keras

Similarly, use:

from tensorflow.keras.layers import Dense
# Not from keras.layers import Dense

Hope this helps.