【问题标题】:KerasRL : Value Error: Tensor must be from same graph as TensorKeras:Valueerror:张量必须来自与张量相同的图
【发布时间】:2022-07-20 03:08:53
【问题描述】:

我正在尝试构建一个 RL 模型来玩 Atari Pinball 游戏,同时关注 Nicholas Renotte 的 video。但是,当我尝试构建最终的 KerasRL 模型时,出现以下错误:

ValueError: Tensor("dense/kernel/Read/ReadVariableOp:0", shape=(256, 9), dtype=float32) must be from the same graph as Tensor("dense_4/Relu:0", shape=(None, 256), dtype=float32) (graphs are <tensorflow.python.framework.ops.Graph object at 0x000001DA9F3E0A90> and FuncGraph(name=keras_graph, id=2038356824176)).

代码:

def build_model(height, width, channels, actions):
    model = Sequential()
    model.add(Convolution2D(32, (8,8), strides=(4,4), activation='relu', input_shape=(3,height, width, channels)))
    model.add(Convolution2D(64, (4,4), strides=(2,2), activation='relu'))
    model.add(Convolution2D(64, (3,3), activation='relu'))
    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dense(256, activation='relu'))
    model.add(Dense(actions, activation='linear'))
    return model

height, width, channels = env.observation_space.shape
actions = env.action_space.n
model = build_model(height, width, channels, actions)

from rl.agents import DQNAgent
from rl.memory import SequentialMemory
from rl.policy import LinearAnnealedPolicy, EpsGreedyQPolicy

def build_agent(model, actions):
    policy = LinearAnnealedPolicy(EpsGreedyQPolicy(), attr='eps', value_max=1., value_min=.1, value_test=.2, nb_steps=10000)
    memory = SequentialMemory(limit=1000, window_length=3)
    dqn = DQNAgent(model=model, memory=memory, policy=policy,
                  enable_dueling_network=True, dueling_type='avg', 
                   nb_actions=actions, nb_steps_warmup=1000
                  )
    return dqn

dqn = build_agent(model, actions)
dqn.compile(Adam(lr=1e-4))

调用build_agent函数时弹出错误。

我尝试使用tf.keras.backend.clear_session(),但没有帮助。

【问题讨论】:

    标签: tensorflow keras deep-learning reinforcement-learning keras-rl


    【解决方案1】:

    您或许可以尝试在build_model 之前添加此内容:

    import tensorflow as tf
    tf.compat.v1.disable_eager_execution()
    
    def build_model(height, width, channels, actions):
    

    我还尝试关注 Nicholas Renotte 的视频。如需更深入的答案,请查看this link。会有一个警告,但至少它会运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 2018-09-20
      • 2018-08-26
      相关资源
      最近更新 更多