def test_vpg_cart_pole_ram(self):
     experiment_config['episode_runner_config']['number_of_episodes'] = 2
     experiment_config['data_saver_config']['store_on_ram_only'] = True
     self.experiment = Experiment(
         ExperimentConfig().create(config_dict=experiment_config))
     self.experiment.run()
     self.experiment.shutdown()
Ejemplo n.º 2
0
    def test_train_model_on_external_dataset_as_hdf5(self):
        network = eval(self.experiment_config['architecture_config']['architecture']).Net(
            config=ArchitectureConfig().create(config_dict=self.experiment_config['architecture_config'])
        )
        external_dataset = f'{os.environ["PWD"]}/test_dir/external_dataset'
        os.makedirs(external_dataset, exist_ok=True)
        info = generate_random_dataset_in_raw_data(output_dir=external_dataset,
                                                   num_runs=5,
                                                   input_size=network.input_size,
                                                   output_size=network.output_size,
                                                   continuous=not network.discrete,
                                                   store_hdf5=True)
        self.assertTrue(os.path.isfile(os.path.join(external_dataset, 'train.hdf5')))
        self.assertTrue(os.path.isfile(os.path.join(external_dataset, 'validation.hdf5')))

        self.experiment_config["trainer_config"]["data_loader_config"]["hdf5_files"] = [os.path.join(external_dataset,
                                                                                                     'train.hdf5')]
        self.experiment_config["evaluator_config"]["data_loader_config"]["hdf5_files"] = [os.path.join(external_dataset,
                                                                                                       'validation.hdf5')]
        experiment = Experiment(config=ExperimentConfig().create(config_dict=self.experiment_config))
        experiment.run()

        # check if 5 + 2 checkpoints were stored in torch_checkpoints
        self.assertTrue(len([f for f in os.listdir(os.path.join(self.output_dir, 'torch_checkpoints'))
                             if f.endswith('ckpt')]), 4)
        shutil.rmtree(external_dataset, ignore_errors=True)
 def test_ros_with_model_evaluation(self):
     experiment_config['output_path'] = self.output_dir
     self.experiment = Experiment(ExperimentConfig().create(config_dict=experiment_config))
     self.experiment.run()
     raw_data_dirs = [os.path.join(self.output_dir, 'raw_data', d)
                      for d in os.listdir(os.path.join(self.output_dir, 'raw_data'))]
     self.assertEqual(len(raw_data_dirs), 1)
     run_dir = raw_data_dirs[0]
     with open(os.path.join(run_dir, 'done.data'), 'r') as f:
         self.assertEqual(experiment_config["episode_runner_config"]["number_of_episodes"] *
                          experiment_config["environment_config"]["max_number_of_steps"],
                          len(f.readlines()))
     self.experiment.shutdown()
Ejemplo n.º 4
0
 def test_train_model_on_generated_dataset_with_tensorboard(self):
     network = eval(self.experiment_config['architecture_config']['architecture']).Net(
         config=ArchitectureConfig().create(config_dict=self.experiment_config['architecture_config'])
     )
     info = generate_random_dataset_in_raw_data(output_dir=self.output_dir,
                                                num_runs=5,
                                                input_size=network.input_size,
                                                output_size=network.output_size,
                                                continuous=not network.discrete,
                                                store_hdf5=True)
     self.experiment_config['tensorboard'] = True
     experiment = Experiment(config=ExperimentConfig().create(config_dict=self.experiment_config))
     experiment.run()
     self.assertGreater(len(glob(os.path.join(self.output_dir, 'events.*'))), 0)
class TestVPGGym(unittest.TestCase):
    def setUp(self) -> None:
        self.output_dir = f'{get_data_dir(os.environ["PWD"])}/test_dir/{get_filename_without_extension(__file__)}'
        os.makedirs(self.output_dir, exist_ok=True)
        experiment_config['output_path'] = self.output_dir

    def test_vpg_cart_pole_fs(self):
        experiment_config['episode_runner_config']['number_of_episodes'] = 2
        experiment_config['data_saver_config']['store_on_ram_only'] = False
        experiment_config['data_saver_config']['separate_raw_data_runs'] = True
        self.experiment = Experiment(
            ExperimentConfig().create(config_dict=experiment_config))
        self.experiment.run()
        self.experiment.shutdown()

    def test_vpg_cart_pole_ram(self):
        experiment_config['episode_runner_config']['number_of_episodes'] = 2
        experiment_config['data_saver_config']['store_on_ram_only'] = True
        self.experiment = Experiment(
            ExperimentConfig().create(config_dict=experiment_config))
        self.experiment.run()
        self.experiment.shutdown()

    def tearDown(self) -> None:
        self.experiment = None
        shutil.rmtree(self.output_dir, ignore_errors=True)
Ejemplo n.º 6
0
    def test_train_model_on_generated_dataset(self):
        network = eval(self.experiment_config['architecture_config']['architecture']).Net(
            config=ArchitectureConfig().create(config_dict=self.experiment_config['architecture_config'])
        )
        info = generate_random_dataset_in_raw_data(output_dir=self.output_dir,
                                                   num_runs=5,
                                                   input_size=network.input_size,
                                                   output_size=network.output_size,
                                                   continuous=not network.discrete,
                                                   store_hdf5=True)
        experiment = Experiment(config=ExperimentConfig().create(config_dict=self.experiment_config))
        experiment.run()

        print(os.listdir(os.path.join(self.output_dir, 'torch_checkpoints')))
        # check if checkpoints were stored in torch_checkpoints
        self.assertEqual(5, len([f for f in os.listdir(os.path.join(self.output_dir, 'torch_checkpoints'))
                                 if f.endswith('ckpt')]))
Ejemplo n.º 7
0
class TestGymModelEvaluation(unittest.TestCase):
    def setUp(self) -> None:
        self.output_dir = f'{os.environ["PWD"]}/test_dir/{get_filename_without_extension(__file__)}'
        shutil.rmtree(self.output_dir, ignore_errors=True)
        os.makedirs(self.output_dir, exist_ok=True)
        experiment_config['output_path'] = self.output_dir

    def test_discrete_stochastic_cart_pole(self):
        experiment_config["environment_config"]["gym_config"][
            "world_name"] = "CartPole-v0"
        experiment_config["architecture_config"][
            "architecture"] = "cart_pole_4_2d_stochastic"
        self.experiment = Experiment(
            ExperimentConfig().create(config_dict=experiment_config))
        self.experiment.run()
        raw_data_dirs = [
            os.path.join(self.output_dir, 'raw_data', d)
            for d in os.listdir(os.path.join(self.output_dir, 'raw_data'))
        ]
        self.assertEqual(len(raw_data_dirs), 1)
        run_dir = raw_data_dirs[0]
        with open(os.path.join(run_dir, 'done.data'), 'r') as f:
            self.assertEqual(
                experiment_config["episode_runner_config"]
                ["number_of_episodes"] *
                experiment_config["environment_config"]["max_number_of_steps"],
                len(f.readlines()))
        self.experiment.shutdown()

    def test_continuous_stochastic_pendulum(self):
        experiment_config["environment_config"]["gym_config"][
            "world_name"] = "Pendulum-v0"
        experiment_config["architecture_config"][
            "architecture"] = "pendulum_3_1c_stochastic"
        self.experiment = Experiment(
            ExperimentConfig().create(config_dict=experiment_config))
        self.experiment.run()
        raw_data_dirs = [
            os.path.join(self.output_dir, 'raw_data', d)
            for d in os.listdir(os.path.join(self.output_dir, 'raw_data'))
        ]
        self.assertEqual(len(raw_data_dirs), 1)
        run_dir = raw_data_dirs[0]
        with open(os.path.join(run_dir, 'done.data'), 'r') as f:
            self.assertEqual(
                len(f.readlines()), experiment_config["episode_runner_config"]
                ["number_of_episodes"] *
                experiment_config["environment_config"]["max_number_of_steps"])
        self.experiment.shutdown()

    def tearDown(self) -> None:
        shutil.rmtree(self.output_dir, ignore_errors=True)
Ejemplo n.º 8
0
 def test_continuous_stochastic_pendulum(self):
     experiment_config["environment_config"]["gym_config"][
         "world_name"] = "Pendulum-v0"
     experiment_config["architecture_config"][
         "architecture"] = "pendulum_3_1c_stochastic"
     self.experiment = Experiment(
         ExperimentConfig().create(config_dict=experiment_config))
     self.experiment.run()
     raw_data_dirs = [
         os.path.join(self.output_dir, 'raw_data', d)
         for d in os.listdir(os.path.join(self.output_dir, 'raw_data'))
     ]
     self.assertEqual(len(raw_data_dirs), 1)
     run_dir = raw_data_dirs[0]
     with open(os.path.join(run_dir, 'done.data'), 'r') as f:
         self.assertEqual(
             len(f.readlines()), experiment_config["episode_runner_config"]
             ["number_of_episodes"] *
             experiment_config["environment_config"]["max_number_of_steps"])
     self.experiment.shutdown()
class TestRosModelEvaluation(unittest.TestCase):

    def setUp(self) -> None:
        self.output_dir = f'{os.environ["PWD"]}/test_dir/{get_filename_without_extension(__file__)}'
        os.makedirs(self.output_dir, exist_ok=True)

    def test_ros_with_model_evaluation(self):
        experiment_config['output_path'] = self.output_dir
        self.experiment = Experiment(ExperimentConfig().create(config_dict=experiment_config))
        self.experiment.run()
        raw_data_dirs = [os.path.join(self.output_dir, 'raw_data', d)
                         for d in os.listdir(os.path.join(self.output_dir, 'raw_data'))]
        self.assertEqual(len(raw_data_dirs), 1)
        run_dir = raw_data_dirs[0]
        with open(os.path.join(run_dir, 'done.data'), 'r') as f:
            self.assertEqual(experiment_config["episode_runner_config"]["number_of_episodes"] *
                             experiment_config["environment_config"]["max_number_of_steps"],
                             len(f.readlines()))
        self.experiment.shutdown()

    def tearDown(self) -> None:
        shutil.rmtree(self.output_dir, ignore_errors=True)
class TestExperiment(unittest.TestCase):
    def setUp(self) -> None:
        self.output_dir = f'{os.environ["PWD"]}/test_dir/{get_filename_without_extension(__file__)}'
        os.makedirs(self.output_dir, exist_ok=True)

    def test_experiment_with_empty_config(self):
        self.config = ExperimentConfig().create(
            config_dict={'output_path': self.output_dir})
        self.experiment = Experiment(self.config)
        self.experiment.run()
        self.experiment.shutdown()

    def test_save_and_restore_architecture(self):
        config_dict = {
            'output_path': self.output_dir,
            'number_of_epochs': 10,
            'episode_runner_config': {
                'number_of_episodes': -1,
                'train_every_n_steps': 10
            },
            'load_checkpoint_dir': None,
            'load_checkpoint_found': True,
            'save_checkpoint_every_n': 3,
            'tensorboard': False,
            'environment_config': {
                "factory_key": "GYM",
                "max_number_of_steps": 50,
                'normalize_observations': True,
                'normalize_rewards': True,
                'observation_clipping': 10,
                'reward_clipping': 10,
                "gym_config": {
                    "random_seed": 123,
                    "world_name": 'CartPole-v0',
                    "render": False,
                },
            },
            'data_saver_config': {
                'clear_buffer_before_episode': True,
                'store_on_ram_only': True
            },
            'architecture_config': {
                'architecture': 'cart_pole_4_2d_stochastic',
                'log_std': 0.0,
                'device': 'cpu',
                'random_seed': 2048,
                'initialisation_type': 'orthogonal'
            },
            'trainer_config': {
                'criterion': 'MSELoss',
                'critic_learning_rate': 0.0001,
                'actor_learning_rate': 0.00015,
                'scheduler_config': {
                    'number_of_epochs': 488
                },
                'gradient_clip_norm': -1,
                'optimizer': 'Adam',
                'data_loader_config': {
                    'batch_size': 5,
                    'random_seed': 2048
                },
                'device': 'cpu',
                'discount': 0.99,
                'factory_key': 'PPO',
                'gae_lambda': 0.95,
                'phi_key': 'gae',
                'entropy_coefficient': 0.0,
                'max_actor_training_iterations': 10,
                'max_critic_training_iterations': 10,
                'epsilon': 0.2,
                'kl_target': 0.01
            },
            'evaluator_config': None,
        }
        self.config = ExperimentConfig().create(config_dict=config_dict)
        experiment = Experiment(self.config)
        experiment.run()
        self.assertEqual(
            len(glob(f'{self.output_dir}/torch_checkpoints/*.ckpt')), 6)
        # best checkpoint might not be latest, so remove this one.
        os.remove(f'{self.output_dir}/torch_checkpoints/checkpoint_best.ckpt')
        config_dict['architecture_config']['random_seed'] = 543
        new_experiment = Experiment(
            ExperimentConfig().create(config_dict=config_dict))

        # assert models are equal
        self.assertEqual(experiment._net.get_checksum(),
                         new_experiment._net.get_checksum())
        # assert learning rate is loaded correctly
        self.assertEqual(experiment._trainer._actor_scheduler.get_lr(),
                         new_experiment._trainer._actor_scheduler.get_lr())
        # assert filter values are loaded correctly
        self.assertEqual(
            experiment._environment._observation_filter._statistic.mean.sum(
            ).item(),
            new_experiment._environment._observation_filter._statistic.mean.
            sum().item())

    def tearDown(self) -> None:
        shutil.rmtree(self.output_dir, ignore_errors=True)
 def test_experiment_with_empty_config(self):
     self.config = ExperimentConfig().create(
         config_dict={'output_path': self.output_dir})
     self.experiment = Experiment(self.config)
     self.experiment.run()
     self.experiment.shutdown()