没有属性“ get_default_graph”

时间:2019-04-02 18:30:26

标签: python tensorflow keras

我有一个python代码,如下:

from keras.models import Sequential
....
gc.collect()
model = Sequential()
model.add(Dense(32,input_shape = (6,),activation = 'relu'))
...

但是它在第model=Sequential()行给了我一个错误。是:

AttributeError: 'module' object has no attribute 'get_default_graph'

或以完整的形式:

File "neuralnetwork.py", line 38, in <module> model = Sequential()
File "/usr/local/lib/python2.7/dist-packages/keras/engine/sequential.py", line 87, in __init__
super(Sequential, self).__init__(name=name)
File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/network.py", line 96, in __init__
self._init_subclassed_network(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/network.py", line 294, in _init_subclassed_network
self._base_init(name=name)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/network.py", line 109, in _base_init
name = prefix + '_' + str(K.get_uid(prefix))
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 74, in get_uid
graph = tf.get_default_graph()
AttributeError: 'module' object has no attribute 'get_default_graph'

我确实了解神经网络,它们的工作原理以及所有知识。我从头开始实现了一个。但我是第一次使用tensorflow,不知道此错误可能意味着什么。请帮我解决这个问题。谢谢!

2 个答案:

答案 0 :(得分:0)

由于您正在使用tensorflow 2.0,因此应在tensorflow中使用keras API。将导入语句更改为:

FormBuilder

答案 1 :(得分:-1)

未安装的Tenserflow版本= 2.o。使用pip install tenserflow将安装1.13,这解决了我的问题希望对您有所帮助