问题
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?
回答1:
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()
回答2:
I think you are using old version tensorflow
Try to update it like
! pip install tensorflow --upgrade
来源:https://stackoverflow.com/questions/54847380/importerror-cannot-import-name-keras