2021年5月5日星期三

DQNAgent Throwing ValueError('Model "{}" has more than one output. DQN expects a model that has a single output.'.format(model))

I am new to reinforcement learning with keras-rl2 and I am not sure why I am getting this error. This is just some example code that I got that should just work but instead I am getting this error and I am very confused. Here is the code below, is it a problem with the versions of tf and keras that I have?

I am currently running this in Spyder through anaconda

import gym    from tensorflow.keras.models import Sequential  from tensorflow.keras.layers import Dense, Flatten, Convolution2D  from tensorflow.keras.optimizers import Adam  from rl.agents import DQNAgent  from rl.memory import SequentialMemory  from rl.policy import LinearAnnealedPolicy, EpsGreedyQPolicy    import random  import numpy as np  import matplotlib.pyplot as plt  from tensorflow.python.client import device_lib    #Make the Deep Q Network  def DQN(h, w, c, actions):      model= Sequential()      model.add(Convolution2D(32,(8,8), strides=(4,4), batch_size=3, activation='relu', input_shape=(h,w,c)))      model.add(Convolution2D(64,(4,4), strides=(4,4),batch_size=3, activation='relu', input_shape=(h,w,c)))      model.add(Convolution2D(64,(3,3), strides=(4,4),batch_size=3, activation='relu', input_shape=(h,w,c)))      model.add(Flatten())      model.add(Dense(512, activation='relu'))      model.add(Dense(256, activation='relu'))      model.add(Dense(actions,activation='linear'))      return model     #Make the agent and pass in the Deep Q network  def agentDqNN(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                print(device_lib.list_local_devices())      env = gym.make('Pong-v0')  height, width, channels = env.observation_space.shape  actions = 4  env.unwrapped.get_action_meanings()    episodes=5  for episode in range(episodes):      state = env.reset()      done = False      score = 0            while not done:          env.render()          action=random.choice([0,2,3])          n_state, reward, done, info=env.step(action) # take a random action          score+=reward      print("Episode:" + str(episode) + " Score:" + str(score))  env.close()    model = DQN(height, width, channels, actions)  model.summary()              dqn=agentDqNN(model, actions)  dqn.compile(optimizer=Adam(lr=1e-4))    dqn.fit(env, nb_steps=10000, visualize=False,verbose=3)           

And I get this output.

    Model "<tensorflow.python.keras.engine.sequential.Sequential object at 0x000001EE1E3C2388>" has more than one output. DQN expects a model that has a single output.  
https://stackoverflow.com/questions/67410917/dqnagent-throwing-valueerrormodel-has-more-than-one-output-dqn-expects-a May 06, 2021 at 10:05AM

没有评论:

发表评论