def test(args): """ Evaluate pre-trained DRL model (models.DDPG) using specified environment simulator (env.TrafficSimulator), by running a train_utils.Tester. Args: args: parsed commandline arguments """ # init experiment and log directories base_dir = args.base_dir dirs = train_utils.init_dir(base_dir, pathes=['eva_data', 'eva_log']) train_utils.init_log(dirs['eva_log']) # load config file and init simulator config = configparser.ConfigParser() config.read(args.config_dir) simulator = env.CACC(config['ENV_CONFIG']) # load pre-trained model if simulator.mode == 1: model_dir = base_dir + '/model' if not train_utils.check_dir(model_dir): train_utils.print_log('Evaluation: trained model does not exist!', level='error') return model = models.DDPG(8 * 3, 4, model_config=config['MODEL_CONFIG']) if not model.load(model_dir + '/'): return else: model = None # evaluate tester = train_utils.Tester(simulator, model, dirs['eva_data']) tester.run()
ddpg_opt['tau'] = 0.00001 ddpg_opt['alr'] = 0.00001 ddpg_opt['clr'] = 0.00001 ddpg_opt['model'] = opt.params n_states = opt.metric_num gamma = 0.9 memory_size = 100000 num_actions = opt.default_knobs + opt.other_knob ddpg_opt['gamma'] = gamma ddpg_opt['batch_size'] = opt.batch_size ddpg_opt['memory_size'] = memory_size model = models.DDPG( n_states=n_states, n_actions=num_actions, opt=ddpg_opt, ouprocess=True ) if not os.path.exists('log'): os.mkdir('log') if not os.path.exists('test_knob'): os.mkdir('test_knob') expr_name = 'eval_{}_{}'.format(opt.method, str(utils.get_timestamp())) logger = utils.Logger( name=opt.method, log_file='log/{}.log'.format(expr_name) )
def prepare(): if not os.path.exists(CONST.LOG_PATH): os.mkdir(CONST.LOG_PATH) if not os.path.exists(CONST.LOG_SYSBENCH_PATH): os.mkdir(CONST.LOG_SYSBENCH_PATH) global opt, task_detail, instance_detail, model_detail parser = argparse.ArgumentParser() parser.add_argument('--batch_size', type=int, default=2, help='Training Batch Size') parser.add_argument('--memory', type=str, default='', help='add replay memory') parser.add_argument('--task_id', type=int, required=True, help='get task info') parser.add_argument('--inst_id', type=int, required=True, help='get inst info') parser.add_argument('--model_id', type=int, required=True, help='get model info') parser.add_argument('--host', type=str, required=True, help='cluster host for set mysql param') opt = parser.parse_args() task_id = opt.task_id inst_id = opt.inst_id model_id = opt.model_id init_logger(task_id, False, True) CONST.TASK_ID = task_id logger.info("start cdbtune") logger.info(opt) # taskdb = database("127.0.0.1",3306,"root","123456","cdbtune") taskdb = database("10.249.50.200", 4839, "cdbtune", "123456", "cdbtune") rsp_task = taskdb.fetch_all("select * from tb_task where task_id = %d" % task_id) rsp_inst = taskdb.fetch_all( "select * from tb_mysql_inst where inst_id = %d" % inst_id) rsp_model = taskdb.fetch_all( "select * from tb_models where model_id = %d" % model_id) if len(rsp_task) == 0 or len(rsp_inst) == 0 or len(rsp_model) == 0: os_quit(Err.INPUT_ERROR, "task_id or inst_id or model_id doesn`t exit") task_detail = rsp_task[0] instance_detail = rsp_inst[0] model_detail = rsp_model[0] method = model_detail["method"] model_path = model_detail["position"] num_knobs = model_detail["knobs"] num_metrics = model_detail["dimension"] env = environment.TencentServer(instance=instance_detail, task_detail=task_detail, model_detail=model_detail, host=opt.host) # Build models if method == 'ddpg': ddpg_opt = dict() ddpg_opt['tau'] = 0.001 ddpg_opt['alr'] = 0.00001 ddpg_opt['clr'] = 0.00001 ddpg_opt['model'] = model_path gamma = 0.99 memory_size = 100000 ddpg_opt['gamma'] = gamma ddpg_opt['batch_size'] = opt.batch_size ddpg_opt['memory_size'] = memory_size model = models.DDPG(n_states=num_metrics, n_actions=num_knobs, opt=ddpg_opt, ouprocess=True) else: model = models.DQN() pass if len(opt.memory) > 0: model.replay_memory.load_memory(opt.memory) logger.info("Load Memory: {}".format(len(model.replay_memory))) # Load mean value and varianc current_knob = environment.get_init_knobs() return env, model
ddpg_opt = dict() ddpg_opt['tau'] = 0.00001 ddpg_opt['alr'] = 0.00001 ddpg_opt['clr'] = 0.00001 ddpg_opt['model'] = opt.params n_states = opt.metric_num gamma = 0.9 memory_size = 100000 num_actions = opt.default_knobs + opt.other_knob ddpg_opt['gamma'] = gamma ddpg_opt['batch_size'] = opt.batch_size ddpg_opt['memory_size'] = memory_size model = models.DDPG(n_states=n_states, n_actions=num_actions, opt=ddpg_opt, mean_var_path='mean_var.pkl', ouprocess=not opt.noisy) if not os.path.exists('log'): os.mkdir('log') if not os.path.exists('save_memory'): os.mkdir('save_memory') if not os.path.exists('save_knobs'): os.mkdir('save_knobs') if not os.path.exists('save_state_actions'): os.mkdir('save_state_actions')
ddpg_opt = dict() ddpg_opt['tau'] = 0.002 ddpg_opt['alr'] = 0.0005 ddpg_opt['clr'] = 0.0001 ddpg_opt['model'] = opt.params n_states = opt.metric_num gamma = 0.99 memory_size = 100000 num_actions = opt.default_knobs + opt.other_knob ddpg_opt['gamma'] = gamma ddpg_opt['batch_size'] = opt.batch_size ddpg_opt['memory_size'] = memory_size model = models.DDPG(n_states=n_states, n_actions=num_actions, opt=ddpg_opt, ouprocess=not opt.noisy) else: model = models.DQN() pass if not os.path.exists('log'): os.mkdir('log') if not os.path.exists('save_memory'): os.mkdir('save_memory') if not os.path.exists('save_knobs'): os.mkdir('save_knobs')
def train(args): """ Train DRL model using specified DRL algorithm (models.DDPG) and environment simulator (env.TrafficSimulator). It runs a train_utils.Trainer to train a DRL model, and run a train_utils.Tester to evaluate it afterwards. Args: args: parsed commandline arguments """ # init experiment and log directories base_dir = args.base_dir dirs = train_utils.init_dir(base_dir) train_utils.init_log(dirs['log']) # copy and load config file config_dir = args.config_dir train_utils.copy_file(config_dir, dirs['data']) config = configparser.ConfigParser() config.read(config_dir) # init env simulator simulator = env.CACC(config['ENV_CONFIG']) # init step counter total_step = int(config.getfloat('TRAIN_CONFIG', 'total_step')) test_step = int(config.getfloat('TRAIN_CONFIG', 'test_interval')) log_step = int(config.getfloat('TRAIN_CONFIG', 'log_interval')) global_counter = train_utils.Counter(total_step, test_step, log_step) # init DDPG agent if simulator.mode == 1: model = models.DDPG(8 * 3, 4, total_step=total_step, model_config=config['MODEL_CONFIG']) model.init_train() elif simulator.mode == 2: model = models.DDPG(8 * 3, 8, total_step=total_step, model_config=config['MODEL_CONFIG']) model.init_train() else: model = None # init tf summary writer and trainer summary_writer = tf.summary.FileWriter(dirs['log']) trainer = train_utils.Trainer(simulator, model, global_counter, summary_writer, output_path=dirs['data']) trainer.run() # save trained model final_step = global_counter.cur_step train_utils.print_log('Training: save final model at step %d ...' % final_step) model.save(dirs['model'], final_step) # evaluate trained model tester = train_utils.Tester(simulator, model, dirs['data']) tester.run()
opt = parser.parse_args() print(opt) tconfig = tuner_configs.config ddpg_opt = dict() ddpg_opt['tau'] = 0.01 ddpg_opt['alr'] = 0.0005 ddpg_opt['clr'] = 0.0001 ddpg_opt['model'] = opt.params ddpg_opt['gamma'] = "" ddpg_opt['batch_size'] = tconfig['batch_size'] ddpg_opt['memory_size'] = tconfig['memory_size'] batch_size = opt.batch_size model = models.DDPG(n_states=tconfig['num_states'], n_actions=tconfig['num_actions'], opt=ddpg_opt, supervised=True) if not os.path.exists('log'): os.mkdir('log') if opt.phase == 'train': if not os.path.exists('sl_model_params'): os.mkdir('sl_model_params') expr_name = 'sl_train_ddpg_{}'.format(str(utils.get_timestamp())) logger = utils.Logger( name='ddpg', log_file='log/{}.log'.format(expr_name) )
ddpg_opt['alr'] = 0.00001 ddpg_opt['clr'] = 0.00001 ddpg_opt['model'] = opt.params n_states = opt.metric_num gamma = 0.9 memory_size = 100000 num_actions = opt.default_knobs + opt.other_knob ddpg_opt['gamma'] = gamma ddpg_opt['batch_size'] = opt.batch_size ddpg_opt['memory_size'] = memory_size model = models.DDPG( n_states=n_states, n_actions=num_actions, opt=ddpg_opt, #mean_var_path='mean_var.pkl', mean_var_path=None, ouprocess=not opt.noisy #outprocess=True ) if not os.path.exists('log'): os.mkdir('log') if not os.path.exists('save_memory'): os.mkdir('save_memory') if not os.path.exists('save_knobs'): os.mkdir('save_knobs') if not os.path.exists('save_state_actions'):