forked from babaktr/async-deep-flappybird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.py
146 lines (113 loc) · 4.62 KB
/
display.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
import random
import time
import matplotlib.pyplot as plt
np.set_printoptions(threshold='nan')
from game_state import GameState
from game_ac_network import GameACFFNetwork, GameACLSTMNetwork
from training_thread import A3CTrainingThread
from rmsprop_applier import RMSPropApplier
import random
def choose_action(pi_values):
return np.random.choice(range(len(pi_values)), p=pi_values)
def display(experiment_name,
rmsp_alpha,
rmsp_epsilon,
grad_norm_clip,
agent_type,
action_size,
rand_seed,
checkpoint_dir,
display_time_sleep,
display_episodes,
display_log_level,
display_save_log,
show_max):
# use CPU for display tool
device = "/cpu:0"
LOG_FILE = 'log_{}-{}.txt'.format(experiment_name, agent_type)
if agent_type == 'LSTM':
global_network = GameACLSTMNetwork(action_size, -1, device)
else:
global_network = GameACFFNetwork(action_size, -1, device)
learning_rate_input = tf.placeholder("float")
grad_applier = RMSPropApplier(learning_rate = learning_rate_input,
decay = rmsp_alpha,
momentum = 0.0,
epsilon = rmsp_epsilon,
clip_norm = grad_norm_clip,
device = device)
sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)
saver = tf.train.Saver()
checkpoint = tf.train.get_checkpoint_state(checkpoint_dir)
if checkpoint and checkpoint.model_checkpoint_path:
saver.restore(sess, checkpoint.model_checkpoint_path)
print("checkpoint loaded:", checkpoint.model_checkpoint_path)
else:
print("Could not find old checkpoint")
episode = 0
terminal = False
episode_rewards = []
episode_steps = []
episode_passed_obsts = []
print ' '
print 'DISPLAYING {} EPISODES'.format(display_episodes)
print '--------------------------------------------------- '
while not episode == display_episodes:
episode_reward = 0
episode_passed_obst = 0
game_state = GameState(rand_seed, action_size, show_score=True)
if display_log_level == 'FULL':
print 'EPISODE {}'.format(episode)
full_frame = None
while True:
pi_values, value = global_network.run_policy_and_value(sess, game_state.s_t)
action = choose_action(pi_values)
game_state.process(action)
terminal = game_state.terminal
episode_step = game_state.steps
reward = game_state.reward
passed_obst = game_state.passed_obst
if len(episode_passed_obsts) == 0 and show_max:
if passed_obst > 0:
full_frame = game_state.full_frame
elif episode_passed_obst > np.max(episode_passed_obsts) and show_max:
full_frame = game_state.full_frame
episode_reward += reward
episode_passed_obst = passed_obst
if display_log_level == 'FULL':
print 'step / pi_values: {} / value: {} / action: {} / reward: {} / passed_obst: {}'.format(pi_values, value, action, reward, passed_obst)
time.sleep(display_time_sleep)
if not terminal:
game_state.update()
else:
break
episode_rewards.append(episode_reward)
episode_steps.append(episode_step)
episode_passed_obsts.append(episode_passed_obst)
if not display_log_level == 'NONE':
reward_steps = format(float(episode_reward)/float(episode_step), '.4f')
print "EPISODE: {} / STEPS: {} / PASSED OBST: {} / REWARD: {} / REWARD/STEP: {}".format(episode, episode_step, passed_obst, episode_reward, reward_steps)
if display_save_log:
with open(LOG_FILE, "a") as text_file:
text_file.write('{},{},{},{},{}\n'.format(episode, episode_step, passed_obst, episode_reward, reward_steps))
episode += 1
print '--------------------------------------------------- '
print 'DISPLAY SESSION FINISHED'
print 'TOTAL EPISODES: {}'.format(display_episodes)
print ' '
print 'MIN'
print 'REWARD: {} / STEPS: {} / PASSED OBST: {}'.format(np.min(episode_rewards), np.min(episode_steps), np.min(episode_passed_obsts))
print ' '
print 'AVERAGE'
print 'REWARD: {} / STEPS: {} / PASSED OBST: {}'.format(np.average(episode_rewards), np.average(episode_steps), np.average(episode_passed_obsts))
print ' '
print 'MAX'
print 'REWARD: {} / STEPS: {} / PASSED OBST: {}'.format(np.max(episode_rewards), np.max(episode_steps), np.max(episode_passed_obsts))
if show_max and not full_frame == None:
plt.imshow(full_frame, origin='lower')
plt.show()