1
0
mirror of https://github.com/gryf/coach.git synced 2025-12-17 19:20:19 +01:00

working checkpoints

This commit is contained in:
Zach Dwiel
2018-09-14 20:59:29 +00:00
committed by zach dwiel
parent 433bc3e27b
commit 6541bc76b9
5 changed files with 69 additions and 32 deletions

View File

@@ -341,6 +341,16 @@ class GraphManager(object):
self.total_steps_counters[RunPhase.TRAIN][TrainingSteps] += 1
[manager.train() for manager in self.level_managers]
# # option 1
# for _ in StepsLoop(self.total_steps_counters, RunPhase.TRAIN, steps):
# [manager.train() for manager in self.level_managers]
#
# # option 2
# steps_loop = StepsLoop(self.total_steps_counters, RunPhase.TRAIN, steps)
# while steps_loop or other:
# [manager.train() for manager in self.level_managers]
def reset_internal_state(self, force_environment_reset=False) -> None:
"""
Reset an episode for all the levels
@@ -403,6 +413,7 @@ class GraphManager(object):
if result.game_over:
hold_until_a_full_episode = False
self.handle_episode_ended()
# TODO: why not just reset right now?
self.reset_required = True
if keep_networks_in_sync:
self.sync_graph()
@@ -426,16 +437,16 @@ class GraphManager(object):
# perform several steps of training interleaved with acting
if steps.num_steps > 0:
self.phase = RunPhase.TRAIN
count_end = self.total_steps_counters[self.phase][steps.__class__] + steps.num_steps
self.reset_internal_state(force_environment_reset=True)
#TODO - the below while loop should end with full episodes, so to avoid situations where we have partial
# episodes in memory
count_end = self.total_steps_counters[self.phase][steps.__class__] + steps.num_steps
while self.total_steps_counters[self.phase][steps.__class__] < count_end:
# The actual steps being done on the environment are decided by the agents themselves.
# This is just an high-level controller.
self.act(EnvironmentSteps(1))
self.train(TrainingSteps(1))
self.save_checkpoint()
self.occasionally_save_checkpoint()
self.phase = RunPhase.UNDEFINED
def sync_graph(self) -> None:
@@ -491,35 +502,40 @@ class GraphManager(object):
for v in self.variables_to_restore:
self.sess.run(v.assign(variables[v.name.split(':')[0]]))
def save_checkpoint(self):
def occasionally_save_checkpoint(self):
# only the chief process saves checkpoints
if self.task_parameters.save_checkpoint_secs \
and time.time() - self.last_checkpoint_saving_time >= self.task_parameters.save_checkpoint_secs \
and (self.task_parameters.task_index == 0 # distributed
or self.task_parameters.task_index is None # single-worker
):
self.save_checkpoint()
checkpoint_path = os.path.join(self.task_parameters.save_checkpoint_dir,
"{}_Step-{}.ckpt".format(
self.checkpoint_id,
self.total_steps_counters[RunPhase.TRAIN][EnvironmentSteps]))
if not isinstance(self.task_parameters, DistributedTaskParameters):
saved_checkpoint_path = self.checkpoint_saver.save(self.sess, checkpoint_path)
else:
saved_checkpoint_path = checkpoint_path
def _log_save_checkpoint(self):
checkpoint_path = os.path.join(self.task_parameters.save_checkpoint_dir,
"{}_Step-{}.ckpt".format(
self.checkpoint_id,
self.total_steps_counters[RunPhase.TRAIN][EnvironmentSteps]))
if not isinstance(self.task_parameters, DistributedTaskParameters):
saved_checkpoint_path = self.checkpoint_saver.save(self.sess, checkpoint_path)
else:
saved_checkpoint_path = checkpoint_path
# this is required in order for agents to save additional information like a DND for example
[manager.save_checkpoint(self.checkpoint_id) for manager in self.level_managers]
screen.log_dict(
OrderedDict([
("Saving in path", saved_checkpoint_path),
]),
prefix="Checkpoint"
)
screen.log_dict(
OrderedDict([
("Saving in path", saved_checkpoint_path),
]),
prefix="Checkpoint"
)
def save_checkpoint(self):
# this is required in order for agents to save additional information like a DND for example
[manager.save_checkpoint(self.checkpoint_id) for manager in self.level_managers]
self.checkpoint_id += 1
self.last_checkpoint_saving_time = time.time()
self._log_save_checkpoint()
self.checkpoint_id += 1
self.last_checkpoint_saving_time = time.time()
def improve(self):
"""