Esempio n. 1
0
    def LogSetup(self):
        Logger().init(
            config_or_config_dict=GlobalConfig().DEFAULT_LOG_CONFIG_DICT,
            log_path=GlobalConfig().DEFAULT_LOG_PATH,
            log_level=GlobalConfig().DEFAULT_LOG_LEVEL)
        ConsoleLogger().init(logger_name='console_logger',
                             to_file_flag=True,
                             level=GlobalConfig().DEFAULT_LOG_LEVEL,
                             to_file_name=os.path.join(Logger().log_dir,
                                                       'console.log'))

        self.assertTrue(ConsoleLogger().inited_flag)
        self.assertTrue(Logger().inited_flag)
Esempio n. 2
0
 def save(self, save_path, global_step, name=None, default_save_param=None, *args, **kwargs):
     if default_save_param is None:
         default_save_param = dict(_parameters=self._parameters, _source_config=self._source_config.config_dict)
     if not name:
         name = self.name
     Logger().out_to_file(file_path=save_path,
                          file_name='{}-{}.json'.format(name, global_step),
                          content=default_save_param)
Esempio n. 3
0
 def setUp(self):
     BaseTestCase.setUp(self)
     try:
         shutil.rmtree(GlobalConfig().DEFAULT_LOG_PATH)
     except FileNotFoundError:
         pass
     os.makedirs(GlobalConfig().DEFAULT_LOG_PATH)
     self.assertFalse(ConsoleLogger().inited_flag)
     self.assertFalse(Logger().inited_flag)
Esempio n. 4
0
    def setUp(self):
        BaseTestCase.setUp(self)
        try:
            shutil.rmtree(GlobalConfig().DEFAULT_LOG_PATH)
        except FileNotFoundError:
            pass
        # os.makedirs(GlobalConfig().DEFAULT_LOG_PATH)
        # self.assertFalse(ConsoleLogger().inited_flag)
        # self.assertFalse(Logger().inited_flag)

        Logger().init(config_or_config_dict=GlobalConfig().DEFAULT_LOG_CONFIG_DICT,
                      log_path=GlobalConfig().DEFAULT_LOG_PATH,
                      log_level=GlobalConfig().DEFAULT_LOG_LEVEL)
        ConsoleLogger().init(logger_name='console_logger',
                             to_file_flag=True,
                             level=GlobalConfig().DEFAULT_LOG_LEVEL,
                             to_file_name=os.path.join(Logger().log_dir, 'console.log'))

        self.assertTrue(ConsoleLogger().inited_flag)
        self.assertTrue(Logger().inited_flag)
Esempio n. 5
0
def single_exp_runner(task_fn,
                      auto_choose_gpu_flag=False,
                      gpu_id: int = 0,
                      seed=None,
                      del_if_log_path_existed=False,
                      keep_session=False,
                      **task_fn_kwargs):
    """

    :param task_fn: task function defined bu users
    :type task_fn: method
    :param auto_choose_gpu_flag: auto choose gpu, default False
    :type auto_choose_gpu_flag: bool
    :param gpu_id: gpu id, default 0
    :type gpu_id: int
    :param seed: seed generated by system time
    :type seed: int
    :param del_if_log_path_existed:delete obsolete log file path if existed, by default False
    :type del_if_log_path_existed: bool
    :param task_fn_kwargs:
    :type task_fn_kwargs:
    :param keep_session: Whether to keep default session & graph
    :type keep_session:
    :return:
    :rtype:
    """
    os.environ['CUDA_DEVICE_ORDER'] = "PCI_BUS_ID"
    if auto_choose_gpu_flag is True:
        DEVICE_ID_LIST = Gpu.getFirstAvailable()
        DEVICE_ID = DEVICE_ID_LIST[0]
        os.environ["CUDA_VISIBLE_DEVICES"] = str(DEVICE_ID)
    else:
        os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
    if not seed:
        seed = int(round(time.time() * 1000)) % (2**32 - 1)
    _reset_global_seed(seed, keep_session)
    print("create log path at {}".format(GlobalConfig().DEFAULT_LOG_PATH),
          flush=True)

    file.create_path(path=GlobalConfig().DEFAULT_LOG_PATH,
                     del_if_existed=del_if_log_path_existed)
    Logger().init(config_or_config_dict=dict(),
                  log_path=GlobalConfig().DEFAULT_LOG_PATH,
                  log_level=GlobalConfig().DEFAULT_LOG_LEVEL)
    ConsoleLogger().init(
        to_file_flag=GlobalConfig().DEFAULT_WRITE_CONSOLE_LOG_TO_FILE_FLAG,
        to_file_name=os.path.join(
            GlobalConfig().DEFAULT_LOG_PATH,
            GlobalConfig().DEFAULT_CONSOLE_LOG_FILE_NAME),
        level=GlobalConfig().DEFAULT_LOG_LEVEL,
        logger_name=GlobalConfig().DEFAULT_CONSOLE_LOGGER_NAME)

    task_fn(**task_fn_kwargs)
Esempio n. 6
0
    def test_integration_with_dqn(self):
        env = make('Acrobot-v1')
        env_spec = EnvSpec(obs_space=env.observation_space,
                           action_space=env.action_space)

        mlp_q = MLPQValueFunction(env_spec=env_spec,
                                  name='mlp_q',
                                  name_scope='mlp_q',
                                  mlp_config=[{
                                      "ACT": "RELU",
                                      "B_INIT_VALUE": 0.0,
                                      "NAME": "1",
                                      "N_UNITS": 16,
                                      "TYPE": "DENSE",
                                      "W_NORMAL_STDDEV": 0.03
                                  }, {
                                      "ACT": "LINEAR",
                                      "B_INIT_VALUE": 0.0,
                                      "NAME": "OUPTUT",
                                      "N_UNITS": 1,
                                      "TYPE": "DENSE",
                                      "W_NORMAL_STDDEV": 0.03
                                  }])
        dqn = DQN(env_spec=env_spec,
                  name='dqn_test',
                  config_or_config_dict=dict(REPLAY_BUFFER_SIZE=1000,
                                             GAMMA=0.99,
                                             BATCH_SIZE=10,
                                             LEARNING_RATE=0.001,
                                             TRAIN_ITERATION=1,
                                             DECAY=0.5),
                  value_func=mlp_q)
        dqn.init()
        st = env.reset()
        from baconian.common.sampler.sample_data import TransitionData
        a = TransitionData(env_spec)
        res = []
        for i in range(100):
            ac = dqn.predict(obs=st, sess=self.sess, batch_flag=False)
            st_new, re, done, _ = env.step(action=ac)
            a.append(state=st,
                     new_state=st_new,
                     action=ac,
                     done=done,
                     reward=re)
            dqn.append_to_memory(a)
        res.append(
            dqn.train(batch_data=a,
                      train_iter=10,
                      sess=None,
                      update_target=True)['average_loss'])
        res.append(
            dqn.train(batch_data=None,
                      train_iter=10,
                      sess=None,
                      update_target=True)['average_loss'])
        self.assertTrue(dqn in dqn.recorder._obj_log)
        self.assertTrue('average_loss' in dqn.recorder._obj_log[dqn])
        self.assertTrue(len(dqn.recorder._obj_log[dqn]['average_loss']) == 2)
        self.assertTrue(
            np.equal(np.array(res), [
                x['log_val']
                for x in dqn.recorder._obj_log[dqn]['average_loss']
            ]).all())

        self.assertTrue(len(Logger()._registered_recorders) > 0)
        self.assertTrue(dqn.recorder in Logger()._registered_recorders)

        Logger().flush_recorder()
Esempio n. 7
0
 def tearDown(self):
     Logger().reset()
     ConsoleLogger().reset()
     BaseTestCase.tearDown(self)
     self.assertFalse(ConsoleLogger().inited_flag)
     self.assertFalse(Logger().inited_flag)
Esempio n. 8
0
    def test_integration_with_dqn(self):
        env = make('Acrobot-v1')
        env_spec = EnvSpec(obs_space=env.observation_space,
                           action_space=env.action_space)

        mlp_q = MLPQValueFunction(env_spec=env_spec,
                                  name='mlp_q',
                                  name_scope='mlp_q',
                                  mlp_config=[
                                      {
                                          "ACT": "RELU",
                                          "B_INIT_VALUE": 0.0,
                                          "NAME": "1",
                                          "N_UNITS": 16,
                                          "TYPE": "DENSE",
                                          "W_NORMAL_STDDEV": 0.03
                                      },
                                      {
                                          "ACT": "LINEAR",
                                          "B_INIT_VALUE": 0.0,
                                          "NAME": "OUPTUT",
                                          "N_UNITS": 1,
                                          "TYPE": "DENSE",
                                          "W_NORMAL_STDDEV": 0.03
                                      }
                                  ])
        dqn = DQN(env_spec=env_spec,
                  name='dqn_test',
                  config_or_config_dict=dict(REPLAY_BUFFER_SIZE=1000,
                                             GAMMA=0.99,
                                             BATCH_SIZE=10,
                                             LEARNING_RATE=0.001,
                                             TRAIN_ITERATION=1,
                                             DECAY=0.5),
                  value_func=mlp_q)
        agent = Agent(env=env, env_spec=env_spec,
                      algo=dqn,
                      name='agent')
        agent.init()
        # dqn.init()
        st = env.reset()
        from baconian.common.sampler.sample_data import TransitionData
        a = TransitionData(env_spec)
        res = []
        agent.sample(env=env,
                     sample_count=100,
                     in_which_status='TRAIN',
                     store_flag=True,
                     sample_type='transition')
        agent.sample(env=env,
                     sample_count=100,
                     in_which_status='TRAIN',
                     store_flag=True,
                     sample_type='transition')
        res.append(dqn.train(batch_data=a, train_iter=10, sess=None, update_target=True)['average_loss'])
        res.append(dqn.train(batch_data=None, train_iter=10, sess=None, update_target=True)['average_loss'])
        self.assertTrue(dqn in dqn.recorder._obj_log)
        self.assertTrue('average_loss' in dqn.recorder._obj_log[dqn])
        self.assertTrue(len(dqn.recorder._obj_log[dqn]['average_loss']) == 2)
        self.assertTrue(
            np.equal(np.array(res), [x['value'] for x in dqn.recorder._obj_log[dqn]['average_loss']]).all())

        self.assertTrue(len(Logger()._registered_recorders) > 0)
        self.assertTrue(dqn.recorder in Logger()._registered_recorders)
        res = dqn.recorder.get_log(attr_name='average_loss', filter_by_status=dict())
        self.assertEqual(len(res), 2)
        res = agent.recorder.get_log(attr_name='sum_reward', filter_by_status={'status': 'TRAIN'})
        self.assertEqual(len(res), 2)
        res = agent.recorder.get_log(attr_name='sum_reward', filter_by_status={'status': 'TEST'})
        self.assertEqual(len(res), 0)
        Logger().flush_recorder()