Tensorflow:AttributeError:'函数'对象没有属性'graph'

时间:2019-01-14 10:55:28

标签: python tensorflow keras

我正在使用Keras == 1.1.0tensorflow-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

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

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。这是我为解决该问题所做的事情:

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

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

    对于Windows,请删除以下位置的文件:%LocalAppData%\pip\Cache

我希望这对您有帮助

答案 1 :(得分:0)

这是我解决问题的方法。

1.使用keras卸载pip uninstall keras
2.检查是否未安装其他版本(例如,通过conda
3.删除%LocalAppData%\pip\Cache中的缓存
4.重新安装keras