29

I want to import keras.engine.topology in Tensorflow. I used to add the word tensorflow at the beginning of every Keras import if I want to use the Tensorflow version of Keras.

For example: instead of writing:

from keras.layers import Dense, Dropout, Input

I just write the following code and it works fine :

from tensorflow.keras.layers import Dense, Dropout, Input

But that's not the case for this specific import:

from tensorflow.keras.engine.topology import Layer, InputSpec

And I m getting the following error message:

No module named 'tensorflow.keras.engine'
nairouz mrabah
  • 1,177
  • 2
  • 13
  • 26

5 Answers5

51

You can import Layer and InputSpec from TensorFlow as follows:

from tensorflow.python.keras.layers import Layer, InputSpec

UPDATE: 30/10/2019

from tensorflow.keras.layers import Layer, InputSpec
rvinas
  • 11,824
  • 36
  • 58
6

In the keras_vggface/models.py file, change the import from:

from keras.engine.topology import get_source_inputs

to:

from keras.utils.layer_utils import get_source_inputs
Paul Benn
  • 1,911
  • 11
  • 26
user1210
  • 61
  • 1
  • 4
2

In order to import keras.engine you may try using:

import tensorflow.python.keras.engine

Note: But from tensorflow.python.keras.engine you cannot import topology

0

I solved this issue by changing the import from from keras.engine.topology import get_source_inputs to from keras.utils.layer_utils import get_source_inputs

0

In the /usr/local/lib/python3.10/dist-packages/keras_vggface/models.py in <module> file...

Change this:

keras.engine.topology import get_source_inputs

To:

keras.utils.layer_utils import get_source_inputs

Reason:

If you use the old module, you may encounter an error like module 'keras.engine' has no attribute 'Layer'

ABabin
  • 2,724
  • 24
  • 30