Skip to main content
 首页 » 编程设计

python之Tensorflow:AttributeError: 'function' 对象没有属性 'graph'

2024年12月31日23虾米哥

我正在使用 Keras == 1.1.0 和 tensorflow-gpu == 1.12.0。
错误调用之后:

    input_layer = Input(shape=(2, )) 
    layer = Dense(self._hidden[0], activation='relu')(input_layer) 

这是回溯

Traceback (most recent call last): 
  File "D:/Documents/PycharmProjects/DDPG-master-2/main.py", line 18, in <module> 
    main() 
  File "D:/Documents/PycharmProjects/DDPG-master-2/main.py", line 14, in main 
    agent = Agent(state_size=world.state_size, action_size=world.action_size) 
  File "D:\Documents\PycharmProjects\DDPG-master-2\ddpg.py", line 50, in __init__ 
    batch_size=batch_size, tau=tau) 
  File "D:\Documents\PycharmProjects\DDPG-master-2\networks\actor.py", line 68, in __init__ 
    self._generate_model() 
  File "D:\Documents\PycharmProjects\DDPG-master-2\networks\actor.py", line 132, in _generate_model 
    layer = Dense(self._hidden[0], activation='relu')(input_layer) 
  File "D:\Anaconda3\lib\site-packages\keras\engine\topology.py", line 487, in __call__ 
    self.build(input_shapes[0]) 
  File "D:\Anaconda3\lib\site-packages\keras\layers\core.py", line 695, in build 
    name='{}_W'.format(self.name)) 
  File "D:\Anaconda3\lib\site-packages\keras\initializations.py", line 59, in glorot_uniform 
    return uniform(shape, s, name=name) 
  File "D:\Anaconda3\lib\site-packages\keras\initializations.py", line 32, in uniform 
    return K.random_uniform_variable(shape, -scale, scale, name=name) 
  File "D:\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 282, in random_uniform_variable 
    return variable(value, dtype=dtype, name=name) 
  File "D:\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 152, in variable 
    if tf.get_default_graph() is get_session().graph: 
AttributeError: 'function' object has no attribute 'graph' 
 
Process finished with exit code 1 
 

我之前有 tensorflow-gpu == 1.9 并卸载它并升级到 1.12,因为我看到它是类似问题的常见解决方案。但是它没有用。

编辑(添加一些与回溯相关的相关代码):

   agent = DDPG(state_size=world.state_size, action_size=world.action_size) 
 
   self._actor = Actor(tensorflow_session=tensorflow_session, 
                            state_size=state_size, action_size=action_size, 
                            hidden_units=actor_hidden_units, 
                            learning_rate=actor_learning_rate, 
                            batch_size=batch_size, tau=tau) 
 
       def _generate_model(self): 
        """ 
        Generates the model based on the hyperparameters defined in the 
        constructor. 
 
        :return: at tuple containing references to the model, weights, 
            and input later 
        """ 
        input_layer = Input(shape=(self._state_size,)) 
        layer = Dense(self._hidden[0], activation='relu')(input_layer) 
        layer = Dense(self._hidden[1], activation='relu')(layer) 
        output_layer = Dense(self._action_size, activation='sigmoid')(layer) 
        model = Model(input=input_layer, output=output_layer) 
        return model, model.trainable_weights, input_layer 

代码与三个不同的类相关。

请您参考如下方法:

我遇到了同样的问题。以下是我为解决它所做的事情:

  1. 确保我的项目中没有名为tensorflow.py
  2. 的其他文件
  3. 使用 --no-cache-dir 参数重新安装 tensorflow pip --no-cache-dir install tensorflow 并删除 pip 缓存文件。

    对于Linux:
    rm -rf ~/.cache/pip/*

    对于 Windows,删除此位置中的文件:%LocalAppData%\pip\Cache

希望对你有帮助