Search code examples
pythontensorflowkerasdeep-learningtf.keras

How to import keras from tf.keras in Tensorflow?


import tensorflow as tf
import tensorflow 

from tensorflow import keras
from keras.layers import Dense

I am getting the below error

from keras.layers import Input, Dense
Traceback (most recent call last):

  File "<ipython-input-6-b5da44e251a5>", line 1, in <module>
    from keras.layers import Input, Dense

ModuleNotFoundError: No module named 'keras'

How do I solve this?

Note: I am using Tensorflow version 1.4


Solution

  • Use the keras module from tensorflow like this:

    import tensorflow as tf

    Import classes

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

    or use directly

    dense = tf.keras.layers.Dense(...)

    EDIT Tensorflow 2

    from tensorflow.keras.layers import Input, Dense

    and the rest stays the same.