Testing the DQN model

Now, Lets test how our trained DQN model performs on new games. The function below uses the trained DQN model to play ten games and see if our average target of 200 points will be achieved.

def test(env, model, states, episodes=100, render=False):
"""Test the performance of the DQN agent."""
scores_test = []
for episode in range(1, (episodes+1)):
state = env.reset()
state = state.reshape(1, states)

done = False
time_step = 0

while not done:
if render:
env.render()
action = np.argmax(model.predict(state)[0])
new_state, reward, done, info = env.step(action)
new_state = new_state.reshape(1, states)
state = new_state
time_step += 1
scores_test.append(time_step)
if episode % 10 == 0:
print('episode {}, score {} '.format(episode, time_step))
print('Average score over 100 test games: {}'.format(np.mean(scores_test)))

test(env, model, states, render=False)
To view the Cart-Pole game on your screen when testing, set render argument to true inside the test function.
Figure 15.9:  Test scores with the trained Q agent

When the agent is tested on new 100 Cart-Pole games, it is averaging a score of 277.88.

Remove the threshold of 200 points and aim at training the agent to consistently score an average of 450 points or more.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.234.150