Search code examples
pythontensorflowkeras

ImportError: cannot import name 'keras'


When running this in Jupyter notebooks (python):

import tensorflow as tf
from tensorflow import keras

I get this error:

ImportError: cannot import name 'keras'

I've tried other commands in place of the second one, such as (but not limited to)

from tensorflow.keras import layers

But it always returns some error. I'm using the online version of Jupyter, and running print(tf.VERSION) returns 1.1.0. I'm not sure if the problem is just that I have the wrong version, or if it's something else. How do I fix this?


Solution

  • You have an old version of Tensorflow; to access Keras from Tensorflow 1.1, you should use

    import tensorflow.contrib.keras as keras
    

    For Sequential, use

    from tensorflow.contrib.keras.python.keras.models import Sequential
    model = Sequential()