gpt4 book ai didi

python - 无属性 'get_default_graph'

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:27 24 4
gpt4 key购买 nike

我有一个 python 代码:

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

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

AttributeError: 'module' 对象没有属性 '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,不知道这个错误可能意味着什么。请帮我解决这个问题。谢谢!

最佳答案

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

import tensorflow as tf
from tensorflow import keras
....
model = keras.Sequential()
model.add(keras.layers.Dense(32,input_shape = (6,),activation = 'relu'))
...

关于python - 无属性 'get_default_graph',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55481393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com