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

标签 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

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

最佳答案

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

  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

希望对你有帮助

关于python - Tensorflow:AttributeError: 'function' 对象没有属性 'graph',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54180124/

相关文章:

Python git 模块无效的 git 存储库错误

python - 使用 pip 安装特定的 tensorflow 分支

python - 为什么当作为参数传递给 `input_shape` 层时, `Dense` 不包括批处理维度?

python-3.x - 加载预先训练的 Keras 模型并进行预测

python - SKlearn Tfidfvectorizer 与 Keras : expected dense_input_1 to have shape

python - 从 vim 发送代码到 stata

python - Raspberry Pi 的轻量级音频播放和图像显示

python - SQLAlchemy 设置默认 nullable=False

python - 你能将 opencv SIFT 与 tensorflow 模型集成吗?

python - keras中的加权mse自定义损失函数