ImportError: cannot import name 'keras'

允我心安 提交于 2021-02-07 18:47:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!