def game_solved_basic(self,trainer: Engine):
     passed = trainer.state.metrics['time_passed']
     print("Game solved in %s, after %d episodes "
           "and %d iterations!" % (
               timedelta(seconds=int(passed)),
               trainer.state.episode, trainer.state.iteration))
     trainer.should_terminate = True
    def game_solved(trainer: Engine):
        print('Game solved in %s, after %d episode and %d iterations' % (
            timedelta(seconds=trainer.state.metrics['time_passed']),
            trainer.state.episode, trainer.state.iteration
        ))

        trainer.should_terminate = True
Example #3
0
 def game_solved(trainer: Engine):
     print("Game solved in %s, after %d episodes and %d iterations!" % (
         timedelta(seconds=trainer.state.metrics["time_passed"]),
         trainer.state.episode,
         trainer.state.iteration,
     ))
     trainer.should_terminate = True
    def episode_completed(trainer: Engine):
        passed = trainer.state.metrics.get('time_passed', 0)
        avg_steps = trainer.state.metrics.get('avg_steps', 50)
        avg_reward = trainer.state.metrics.get('avg_reward', 0.0)
        print("Episode %d: reward=%.0f (avg %.2f), "
              "steps=%s (avg %.2f), speed=%.1f f/s, "
              "elapsed=%s" %
              (trainer.state.episode, trainer.state.episode_reward, avg_reward,
               trainer.state.episode_steps, avg_steps,
               trainer.state.metrics.get('avg_fps',
                                         0), timedelta(seconds=int(passed))))

        if avg_steps < 15 and trainer.state.episode > 100:
            print("Average steps has fallen below 10, stop training")
            trainer.should_terminate = True
Example #5
0
def test_terminate_on_nan_and_inf():

    torch.manual_seed(12)

    def update_fn(engine, batch):
        pass

    trainer = Engine(update_fn)
    trainer.state = State()
    h = TerminateOnNan()

    trainer.state.output = 1.0
    h(trainer)
    assert not trainer.should_terminate

    trainer.state.output = torch.tensor(123.45)
    h(trainer)
    assert not trainer.should_terminate

    trainer.state.output = torch.asin(torch.randn(10, ))
    h(trainer)
    assert trainer.should_terminate
    trainer.should_terminate = False

    trainer.state.output = np.array([1.0, 2.0])
    h._output_transform = lambda x: x.tolist()
    h(trainer)
    assert not trainer.should_terminate
    h._output_transform = lambda x: x

    trainer.state.output = torch.asin(torch.randn(4, 4))
    h(trainer)
    assert trainer.should_terminate
    trainer.should_terminate = False

    trainer.state.output = (10.0, 1.0 /
                            torch.randint(0, 2, size=(4, )).type(torch.float),
                            1.0)
    h(trainer)
    assert trainer.should_terminate
    trainer.should_terminate = False

    trainer.state.output = (1.0, torch.tensor(1.0), "abc")
    h(trainer)
    assert not trainer.should_terminate

    trainer.state.output = 1.0 / torch.randint(0, 2, size=(4, 4)).type(
        torch.float)
    h(trainer)
    assert trainer.should_terminate
    trainer.should_terminate = False

    trainer.state.output = (float("nan"), 10.0)
    h(trainer)
    assert trainer.should_terminate
    trainer.should_terminate = False

    trainer.state.output = float("inf")
    h(trainer)
    assert trainer.should_terminate
    trainer.should_terminate = False

    trainer.state.output = [float("nan"), 10.0]
    h(trainer)
    assert trainer.should_terminate
    trainer.should_terminate = False
 def game_solved(trainer: Engine):
     passed = trainer.state.metrics["time_passed"]
     print(
         f"Game solved in {timedelta(seconds=int(passed))} after {trainer.state.episode}"
         f" episodes and {trainer.state.iteration} iterations!")
     trainer.should_terminate = True