def test_run_experiment(num_sample, max_workers, chunksize): def run(config, seed, device, logdir): return config['ID'], seed, device, logdir config = Config( { 'network.lr': Grid([1e-3, 5e-3]), 'network.size': [32, 16], 'env.id': Grid(['CartPole-v1', 'Ant-v2']) }, num_sample=num_sample, keep_dict_order=True) seeds = [1, 2, 3] log_dir = './some_path' run_experiment(run, config, seeds, log_dir, max_workers, chunksize, use_gpu=False, gpu_ids=None) p = Path('./some_path') assert p.exists() assert (p / 'configs.pkl').exists() assert (p / 'source_files').exists() and (p / 'source_files').is_dir() # Check all configuration folders with their IDs and subfolders for all random seeds for i in range(4): config_p = p / str(i) assert config_p.exists() assert (config_p / 'config.yml').exists() for seed in seeds: assert (config_p / str(seed)).exists() # Clean the logging directory rmtree(p) # Test remove assert not p.exists()
from lagom.experiment import run_experiment from lagom.envs import make_vec_env from lagom.envs.wrappers import TimeLimit from lagom.envs.wrappers import ClipAction from lagom.envs.wrappers import VecMonitor from lagom.envs.wrappers import VecStandardizeObservation from baselines.openaies.openaies import OpenAIES from baselines.openaies.agent import Agent config = Config( {'log.freq': 10, 'checkpoint.num': 3, 'env.id': Grid(['HalfCheetah-v3', 'Hopper-v3', 'Walker2d-v3', 'Swimmer-v3']), 'env.standardize_obs': False, 'nn.sizes': [64, 64], # only for continuous control 'env.clip_action': True, # clip action within valid bound before step() 'agent.std0': 0.6, # initial std 'train.generations': int(1e3), # total number of ES generations 'train.popsize': 64, 'train.mu0': 0.0, 'train.std0': 1.0, 'train.lr': 1e-2, 'train.lr_decay': 1.0, 'train.min_lr': 1e-6,
config = Config({ 'cuda': True, ################## 'cuda_ids': [7], ################## 'log.dir': 'logs/default', 'log.freq': 1000, # every n timesteps 'checkpoint.num': 3, 'env.id': Grid(['HalfCheetah-v3' ]), ######, 'Hopper-v3', 'Walker2d-v3', 'Swimmer-v3']), 'agent.gamma': 0.99, 'agent.polyak': 0.995, # polyak averaging coefficient for targets update 'agent.actor.lr': 3e-4, 'agent.actor.use_lr_scheduler': False, 'agent.critic.lr': 3e-4, 'agent.critic.use_lr_scheduler': False, 'agent.policy_delay': 2, 'agent.initial_temperature':
from lagom.experiment import Config from lagom.experiment import Grid from lagom.experiment import run_experiment from lagom.envs import RecordEpisodeStatistics from lagom.envs import NormalizeObservation from lagom.envs import NormalizeReward from lagom.envs import TimeStepEnv from baselines.vpg.agent import Agent from baselines.vpg.agent_lstm import Agent as LSTMAgent from baselines.vpg.engine import Engine config = Config({ 'log.freq': 10, 'checkpoint.num': 3, 'env.id': Grid(['HalfCheetah-v3', 'Hopper-v3', 'Walker2d-v3']), 'env.normalize_obs': True, 'env.normalize_reward': True, 'use_lstm': Grid([True, False]), 'rnn.size': 128, 'nn.sizes': [64, 64], 'agent.lr': 1e-3, 'agent.use_lr_scheduler': False, 'agent.gamma': 0.99, 'agent.gae_lambda': 0.97, 'agent.standardize_adv': True, # standardize advantage estimates 'agent.max_grad_norm': 0.5, # grad clipping by norm 'agent.entropy_coef': 0.01, 'agent.value_coef': 0.5, # only for continuous control
from lagom.utils import pickle_dump from lagom.utils import set_global_seeds from lagom.experiment import Config from lagom.experiment import Grid from lagom.experiment import Sample from lagom.experiment import run_experiment from engine import Engine from model import VAE from model import ConvVAE config = Config( {'log.freq': 100, 'nn.type': Grid(['VAE', 'ConvVAE']), 'nn.z_dim': 8, 'lr': 1e-3, 'train.num_epoch': 100, 'train.batch_size': 128, 'eval.batch_size': 128 }) def make_dataset(config): train_dataset = datasets.MNIST('data/', train=True, download=True, transform=transforms.ToTensor())
from lagom.utils import tensorify from lagom.utils import set_global_seeds from lagom.experiment import Config from lagom.experiment import Grid from lagom.experiment import run_experiment from lagom.envs import TimeStepEnv from lagom import CEM from baselines.cem.agent import Agent config = Config( {'log.freq': 10, 'checkpoint.num': 3, 'env.id': Grid(['Acrobot-v1', 'BipedalWalker-v2', 'Pendulum-v0', 'LunarLanderContinuous-v2']), 'nn.sizes': [64, 64], # only for continuous control 'env.clip_action': True, # clip action within valid bound before step() 'agent.std0': 0.6, # initial std 'train.generations': 500, # total number of ES generations 'train.popsize': 32, 'train.worker_chunksize': 4, # must be divisible by popsize 'train.mu0': 0.0, 'train.std0': 1.0, 'train.elite_ratio': 0.2, 'train.noise_scheduler_args': [0.01, 0.001, 400, 0] # [initial, final, N, start] })
def test_config(num_sample, keep_dict_order): with pytest.raises(AssertionError): Config([1, 2, 3]) config = Config( { 'log.dir': 'some path', 'beta': Condition(lambda x: 'small' if x['alpha'] < 5 else 'large'), 'network.type': 'MLP', 'network.hidden_size': [64, 64], 'network.lr': Grid([1e-3, 1e-4]), 'env.id': 'HalfCheetah-v2', 'iter': Grid([10, 20, 30]), 'alpha': Sample(lambda: np.random.uniform(3, 10)) }, num_sample=num_sample, keep_dict_order=keep_dict_order) list_config = config.make_configs() assert len(list_config) == 2 * 3 * num_sample for ID in range(2 * 3 * num_sample): assert list_config[ID]['ID'] == ID for x in list_config: assert len( x.keys()) == len(config.items.keys()) + 1 # added one more 'ID' for key in config.items.keys(): assert key in x assert x['log.dir'] == 'some path' if x['alpha'] < 5: assert x['beta'] == 'small' else: assert x['beta'] == 'large' assert x['network.type'] == 'MLP' assert x['network.hidden_size'] == [64, 64] assert x['network.lr'] in [1e-3, 1e-4] assert x['env.id'] == 'HalfCheetah-v2' assert x['iter'] in [10, 20, 30] assert x['alpha'] >= 3 and x['alpha'] < 10 if keep_dict_order: assert list(x.keys()) == ['ID'] + list(config.items.keys()) else: assert list(x.keys()) != ['ID'] + list(config.items.keys()) assert list(x.keys()) == [ 'ID', 'log.dir', 'beta', 'network.type', 'network.hidden_size', 'env.id', 'network.lr', 'iter', 'alpha' ] # test for non-random sampling config = Config( { 'log.dir': 'some path', 'network.type': 'MLP', 'network.hidden_size': [64, 64], 'network.lr': Grid([1e-3, 1e-4]), 'env.id': 'HalfCheetah-v2', 'iter': Grid([10, 20, 30]), 'alpha': 0.1 }, num_sample=num_sample, keep_dict_order=keep_dict_order) list_config = config.make_configs() assert len( list_config ) == 2 * 3 * 1 # no matter how many num_sample, without repetition for ID in range(2 * 3 * 1): assert list_config[ID]['ID'] == ID # test for all fixed config = Config( { 'log.dir': 'some path', 'network.type': 'MLP', 'network.hidden_size': [64, 64], 'network.lr': 1e-3, 'env.id': 'HalfCheetah-v2', 'iter': 20, 'alpha': 0.1 }, num_sample=num_sample, keep_dict_order=keep_dict_order) list_config = config.make_configs() assert len( list_config) == 1 # no matter how many num_sample, without repetition for ID in range(1): assert list_config[ID]['ID'] == ID
def test_grid(values): grid = Grid(values) assert isinstance(grid, list) assert len(grid) == len(values) assert all([grid[i] == value for i, value in enumerate(values)])