Exemple #1
0
def main():
    '''Main script function.'''
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    tf.logging.set_verbosity(tf.logging.ERROR)
    parser = argparse.ArgumentParser()
    parser.add_argument("path", help="The path to save the results.")
    parser.add_argument("--alg", help="The algorithm to use.", default='PPO')
    parser.add_argument("--env_name", help="The gamma to use.",
                        default='MultiOptLRs-v0')
    parser.add_argument('--total_timesteps', default=int(1e5), type=int,
                        help="Number of timesteps per training session")
    parser.add_argument('--data_set', help="The data set to use.",
                        default='iris')
    parser.add_argument('--trials', help="The number of trials to run.",
                        default=10, type=int)
    args = parser.parse_args()
    parameters = vars(args).copy()
    del parameters['trials']
    path = Path(parameters['path'])
    if not path.exists():
        path.mkdir()
        parameters['kwargs'] = {'data_set': parameters['data_set']}
        utils_file.save_json(parameters, path / 'parameters.json')
    else:
        if (path / 'study.db').exists():
            print('Directory exists. Using existing study and parameters.')
            parameters = utils_file.load_json(path / 'parameters.json')
        else:
            raise FileExistsError(('Directory already exists and is not a '
                                   'study.'))
    storage = 'sqlite:///' + str(path / 'study.db')
    study = optuna.create_study(
        study_name=str(path.name),
        storage=storage, load_if_exists=True,
        pruner=optuna.pruners.MedianPruner()
    )
    print(study.best_value)
    print(study.best_trial.number)
    print(study.best_params)
    #params = study.best_params
    param_file = path / str(study.best_trial.number) / 'parameters.json'
    params = utils_file.load_json(param_file)
    kwargs = params['kwargs']
    parameters.update({
        'path': path / str(study.best_trial.number),
        'gamma': params['gamma'],
        'learning_rate': params['learning_rate'],
        'kwargs': {
            #'batch_size': int(params['batch_size']),
            'max_batches': int(kwargs['max_batches']),
            'max_history': int(kwargs['max_history']),
            'max_batches': 1e3
        }
    })
    live_plot(run_experiment(parameters), 'test')
def test_load_json(save_path):
    '''Test load_json function.'''
    path = create_test_file(save_path, name='test_load_json0')
    dictionary = utils_file.load_json(path)
    for i, (name, value) in enumerate(dictionary.items()):
        assert i == value
        assert name == chr(97 + i)
    path = create_test_file(save_path, name='test_load_json1', version=1)
    dictionary = utils_file.load_json(path, line_no=5)
    assert len(dictionary) == 26 - 5
    for i, (name, value) in enumerate(dictionary.items()):
        assert i == value
        assert name == chr(97 + i)
Exemple #3
0
def main():
    '''Main script function.'''
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    tf.logging.set_verbosity(tf.logging.ERROR)
    parser = argparse.ArgumentParser()
    parser.add_argument("path", help="The path to save the results.")
    parser.add_argument("--alg", help="The algorithm to use.",
                        default='MADDPG', type=str.upper)
    parser.add_argument("--env_name", help="The gamma to use.",
                        default='simple_spread')
    parser.add_argument('--total_timesteps', default=int(1e6), type=int,
                        help="Number of timesteps per training session")
    parser.add_argument('--trials', help="The number of trials to run.",
                        default=10, type=int)
    args = parser.parse_args()
    parameters = vars(args).copy()
    del parameters['trials']
    path = Path(parameters['path'])
    if not path.exists():
        path.mkdir()
        utils_file.save_json(parameters, path / 'parameters.json')
    else:
        if (path / 'study.db').exists():
            print('Directory exists. Using existing study and parameters.')
            parameters = utils_file.load_json(path / 'parameters.json')
        else:
            raise FileExistsError(('Directory already exists and is not a '
                                   'study.'))
    objective = partial(run_experiment, parameters)
    storage = 'sqlite:///' + str(path / 'study.db')
    study = optuna.create_study(study_name=str(path.name),
                                storage=storage, load_if_exists=True,
                                pruner=optuna.pruners.MedianPruner())
    study.optimize(objective, n_trials=args.trials)
def test_save_json(save_path):
    '''Test save_json function.'''
    data = {chr(97 + i): i for i in range(26)}
    path = save_path / 'test_save_json.json'
    utils_file.save_json(data, path)
    dictionary = utils_file.load_json(path)
    for i, (name, value) in enumerate(dictionary.items()):
        assert i == value
        assert name == chr(97 + i)
def main():
    '''Evaluate a trained model against logistic regression.'''
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    tf.logging.set_verbosity(tf.logging.ERROR)
    parser = argparse.ArgumentParser()
    parser.add_argument("path", help="The path to save the results.")
    parser.add_argument("--trials",
                        help="The number of trials.",
                        type=int,
                        default=10)
    parser.add_argument("--total_epochs",
                        help="The number of epochs.",
                        type=int,
                        default=40)
    parser.add_argument("--data_set",
                        help="The data set to trial against.",
                        type=str,
                        default='iris')
    args = parser.parse_args()
    parameters = vars(args).copy()
    del parameters['trials']
    path = Path(parameters['path'])
    if not path.exists():
        path.mkdir()
        utils_file.save_json(parameters, path / 'parameters.json')
    else:
        if (path / 'study.db').exists():
            print('Directory exists. Using existing study and parameters.')
            parameters = utils_file.load_json(path / 'parameters.json')
        else:
            raise FileExistsError(('Directory already exists and is not a '
                                   'study.'))
    objective = partial(train_model, parameters)
    storage = 'sqlite:///' + str(path / 'study.db')
    study = optuna.create_study(study_name=str(path.name),
                                storage=storage,
                                load_if_exists=True,
                                pruner=optuna.pruners.MedianPruner())
    study.optimize(objective, n_trials=args.trials)
def task(path, seed, batch_size=None, total_epochs=40, data_set='mnist'):
    '''
    Run the agent on a data set.
    '''
    parameters = utils_file.load_json(path / 'hyperparams.json')
    alg, *_ = path.name.split('-')
    save_path = path / 'model.pkl'
    sequence = load_data(data_set)
    num_of_samples = len(sequence.features)
    steps_per_epoch = ceil(num_of_samples / batch_size) if batch_size else 1
    kwargs = parameters.get('kwargs', {})
    kwargs['data_set'] = data_set
    kwargs['batch_size'] = batch_size
    kwargs['max_batches'] = steps_per_epoch * total_epochs
    env = partial(gym.make, parameters['env_name'], **kwargs)
    vec_env = OptVecEnv([env])
    if alg == 'PPO':
        with open(save_path, 'rb') as pkl:
            model = PPO2.load(pkl)  # , env=vec_env)
    elif alg == 'A2C':
        with open(save_path, 'rb') as pkl:
            model = A2C.load(pkl, env=vec_env)
    elif alg == 'DDPG':
        model = DDPG.load(save_path, env=vec_env)
    states = vec_env.reset()
    info_list = []
    cumulative_reward = 0
    for epoch_no in trange(total_epochs, leave=False):
        for step in trange(steps_per_epoch, leave=False):
            actions = model.predict(states, deterministic=False)[0]
            states, rewards, _, infos = vec_env.step(actions)
            cumulative_reward = cumulative_reward + rewards[0]
            info = infos[0]
            info['step'] = epoch_no * steps_per_epoch + step
            info['cumulative_reward'] = cumulative_reward
            info['seed'] = seed
            info['epoch'] = epoch_no
            info_list.append(info)
    return info_list