@@ -83,11 +83,11 @@ def __init__(self, env, scope="estimator", summaries_dir=None):
8383 with tf .variable_scope (scope ):
8484 # Build the graph
8585 self .build_model ()
86- if summaries_dir :
87- summary_dir = os .path .join (summaries_dir , "summaries_%s" % scope )
88- if not os .path .exists (summary_dir ):
89- os .makedirs (summary_dir )
90- self .summary_writer = tf .summary .FileWriter (summary_dir )
86+ if summaries_dir :
87+ summary_dir = os .path .join (summaries_dir , "summaries_%s" % scope )
88+ if not os .path .exists (summary_dir ):
89+ os .makedirs (summary_dir )
90+ self .summary_writer = tf .summary .FileWriter (summary_dir )
9191
9292 def build_model (self ):
9393 """
@@ -199,18 +199,19 @@ def create_memory(env):
199199
200200
201201def setup_summary ():
202- episode_total_reward = tf .Variable (0. , name = "EpisodeTotalReward" )
203- tf .summary .scalar (env_name + '/Total Reward/Episode' , episode_total_reward )
204- episode_avg_max_q = tf .Variable (0. , name = "EpisodeAvgMaxQ" )
205- tf .summary .scalar (env_name + '/Average Max Q/Episode' , episode_avg_max_q )
206- episode_duration = tf .Variable (0. , name = "EpisodeDuration" )
207- tf .summary .scalar (env_name + '/Duration/Episode' , episode_duration )
208- episode_avg_loss = tf .Variable (0. , name = "EpisodeAverageLoss" )
209- tf .summary .scalar (env_name + '/Average Loss/Episode' , episode_avg_loss )
210- summary_vars = [episode_total_reward , episode_avg_max_q , episode_duration , episode_avg_loss ]
211- summary_placeholders = [tf .placeholder (tf .float32 ) for _ in range (len (summary_vars ))]
212- update_ops = [summary_vars [i ].assign (summary_placeholders [i ]) for i in range (len (summary_vars ))]
213- summary_op = tf .summary .merge_all ()
202+ with tf .variable_scope ("episode" ):
203+ episode_total_reward = tf .Variable (0. , name = "EpisodeTotalReward" )
204+ tf .summary .scalar (env_name + '/Total Reward/Episode' , episode_total_reward )
205+ episode_avg_max_q = tf .Variable (0. , name = "EpisodeAvgMaxQ" )
206+ tf .summary .scalar (env_name + '/Average Max Q/Episode' , episode_avg_max_q )
207+ episode_duration = tf .Variable (0. , name = "EpisodeDuration" )
208+ tf .summary .scalar (env_name + '/Duration/Episode' , episode_duration )
209+ episode_avg_loss = tf .Variable (0. , name = "EpisodeAverageLoss" )
210+ tf .summary .scalar (env_name + '/Average Loss/Episode' , episode_avg_loss )
211+ summary_vars = [episode_total_reward , episode_avg_max_q , episode_duration , episode_avg_loss ]
212+ summary_placeholders = [tf .placeholder (tf .float32 ) for _ in range (len (summary_vars ))]
213+ update_ops = [summary_vars [i ].assign (summary_placeholders [i ]) for i in range (len (summary_vars ))]
214+ summary_op = tf .summary .merge_all (scope = "episode" )
214215 return summary_placeholders , update_ops , summary_op
215216
216217
@@ -229,6 +230,8 @@ def setup_summary():
229230
230231 copy_model = copy_model_parameters (q_estimator , target_estimator )
231232
233+ summary_placeholders , update_ops , summary_op = setup_summary ()
234+
232235 with tf .Session () as sess :
233236 sess .run (tf .global_variables_initializer ())
234237
@@ -244,14 +247,18 @@ def setup_summary():
244247
245248 total_t = sess .run (tf .train .get_global_step ())
246249
247- for i in tqdm (range (n_episodes )):
250+ for episode in tqdm (range (n_episodes )):
248251 # Save the current checkpoint
249252 saver .save (tf .get_default_session (), network_path )
250253
251254 frame = env .reset ()
252255 state = get_initial_state (frame )
256+
257+ total_reward = 0
258+ total_loss = 0
259+ total_q_max = 0
253260
254- for t in itertools .count ():
261+ for duration in itertools .count ():
255262 # Maybe update the target estimator
256263 if total_t % network_update_interval == 0 :
257264 sess .run (copy_model )
@@ -281,9 +288,20 @@ def setup_summary():
281288 # Perform gradient descent update
282289 states_batch = adapt_batch_state (states_batch )
283290 loss = q_estimator .update (sess , states_batch , action_batch , targets_batch )
284-
291+
292+ total_q_max += np .max (q_values_next )
293+ total_loss += loss
285294 total_t += 1
295+ total_reward += reward
286296 if terminal :
287297 break
298+
299+ stats = [total_reward , total_q_max / duration , duration , total_loss / duration ]
300+ for i in range (len (stats )):
301+ sess .run (update_ops [i ], feed_dict = {
302+ summary_placeholders [i ]: float (stats [i ])
303+ })
304+ summary_str = sess .run (summary_op , )
305+ q_estimator .summary_writer .add_summary (summary_str , episode )
288306
289307 env .env .ale .saveScreenPNG (six .b ('%s/test_image_%05i.png' % (CHART_DIR , i )))
0 commit comments