浏览代码

Renaming School to MetaCurriculum.

/develop-generalizationTraining-TrainerController
Deric Pang 7 年前
当前提交
06eb8037
共有 7 个文件被更改,包括 111 次插入111 次删除
  1. 2
      python/curricula/wall-jump/BigWallBrain.json
  2. 2
      python/unitytrainers/__init__.py
  3. 18
      python/unitytrainers/trainer_controller.py
  4. 14
      python/unitytrainers/meta_curriculum.py
  5. 93
      python/tests/test_meta_curriculum.py
  6. 93
      python/tests/test_school.py
  7. 0
      /python/unitytrainers/meta_curriculum.py

2
python/curricula/wall-jump/BigWallBrain.json


"parameters" :
{
"big_wall_min_height" : [0.0, 4.0, 6.0, 8.0],
"big_wall_max_height" : [4.0, 7.0, 8.0, 8.0],
"big_wall_max_height" : [4.0, 7.0, 8.0, 8.0]
}
}

2
python/unitytrainers/__init__.py


from .buffer import *
from .curriculum import *
from .school import *
from .meta_curriculum import *
from .models import *
from .trainer_controller import *
from .bc.models import *

18
python/unitytrainers/trainer_controller.py


from tensorflow.python.tools import freeze_graph
from unitytrainers.ppo.trainer import PPOTrainer
from unitytrainers.bc.trainer import BehavioralCloningTrainer
from unitytrainers import School
from unitytrainers import MetaCurriculum
from unityagents import UnityEnvironment, UnityEnvironmentException

self.env_name = 'editor_'+self.env.academy_name
else:
self.env_name = os.path.basename(os.path.normpath(env_path)) # Extract out name of environment
self.school = School(self.curriculum_folder, self.env._resetParameters)
self.meta_curriculum = MetaCurriculum(self.curriculum_folder, self.env._resetParameters)
for brain_name, curriculum in self.school.brains_to_curriculums.items():
for brain_name, curriculum in self.meta_curriculum.brains_to_curriculums.items():
if curriculum.measure == "progress":
progress = self.trainers[brain_name].get_step / self.trainers[brain_name].get_max_steps
brain_names_to_progresses[brain_name] = progress

def start_learning(self):
# TODO: Should be able to start learning at different lesson numbers for each curriculum.
self.school.set_all_curriculums_to_lesson_num(self.lesson)
self.meta_curriculum.set_all_curriculums_to_lesson_num(self.lesson)
trainer_config = self._load_config()
self._create_model_path(self.model_path)

else:
sess.run(init)
global_step = 0 # This is only for saving the model
self.school.increment_lessons(self._get_progresses())
curr_info = self.env.reset(config=self.school.get_config(), train_mode=self.fast_simulation)
self.meta_curriculum.increment_lessons(self._get_progresses())
curr_info = self.env.reset(config=self.meta_curriculum.get_config(), train_mode=self.fast_simulation)
if self.train_model:
for brain_name, trainer in self.trainers.items():
trainer.write_tensorboard_text('Hyperparameters', trainer.parameters)

self.school.increment_lessons(self._get_progresses())
curr_info = self.env.reset(config=self.school.get_config(), train_mode=self.fast_simulation)
self.meta_curriculum.increment_lessons(self._get_progresses())
curr_info = self.env.reset(config=self.meta_curriculum.get_config(), train_mode=self.fast_simulation)
for brain_name, trainer in self.trainers.items():
trainer.end_episode()
# Decide and take an action

# Perform gradient descent with experience buffer
trainer.update_model()
# Write training statistics to Tensorboard.
trainer.write_summary(self.school.lesson_nums)
trainer.write_summary(self.meta_curriculum.lesson_nums)
if self.train_model and trainer.get_step <= trainer.get_max_steps:
trainer.increment_step_and_update_last_reward()
if self.train_model:

14
python/unitytrainers/meta_curriculum.py


"""Contains the School class."""
"""Contains the MetaCurriculum class."""
class School(object):
"""A School holds curriculums. Each curriculum is associated to a particular
class MetaCurriculum(object):
"""A MetaCurriculum holds curriculums. Each curriculum is associated to a particular
"""Initializes a School object.
"""Initializes a MetaCurriculum object.
Args:
curriculum_folder (str): The relative or absolute path of the

self.brains_to_curriculums[brain_name].lesson_num = lesson
def increment_lessons(self, progresses):
"""Increments all the lessons of all the curriculums in this School.
"""Increments all the lessons of all the curriculums in this MetaCurriculum.
Args:
progresses (dict): A dict of brain name to progress.

def set_all_curriculums_to_lesson_num(self, lesson_num):
"""Sets all the curriculums in this school to a specified lesson number.
"""Sets all the curriculums in this meta curriculum to a specified lesson number.
Args:
lesson_num (int): The lesson number which all the curriculums will

def get_config(self):
"""Get the combined configuration of all curriculums in this School.
"""Get the combined configuration of all curriculums in this MetaCurriculum.
Returns:
A dict from parameter to value.

93
python/tests/test_meta_curriculum.py


import pytest
from unittest.mock import patch, call, Mock
from unitytrainers.meta_curriculum import MetaCurriculum
class MetaCurriculumTest(MetaCurriculum):
"""This class allows us to test MetaCurriculum objects without calling MetaCurriculum's
__init__ function.
"""
def __init__(self, brains_to_curriculums):
self._brains_to_curriculums = brains_to_curriculums
@pytest.fixture
def default_reset_parameters():
return {'param1' : 1, 'param2' : 2, 'param3' : 3}
@pytest.fixture
def more_reset_parameters():
return {'param4' : 4, 'param5' : 5, 'param6' : 6}
@pytest.fixture
def progresses():
return {'TestBrain1' : 0.2, 'TestBrain2' : 0.3}
@patch('unitytrainers.Curriculum.__init__', return_value=None)
@patch('os.listdir', return_value=['TestBrain1.json', 'TestBrain2.json'])
def test_init_meta_curriculum_happy_path(listdir, mock_curriculum_init, default_reset_parameters):
meta_curriculum = MetaCurriculum('test-meta-curriculum/', default_reset_parameters)
assert len(meta_curriculum.brains_to_curriculums) == 2
assert 'TestBrain1' in meta_curriculum.brains_to_curriculums
assert 'TestBrain2' in meta_curriculum.brains_to_curriculums
calls = [call('test-meta-curriculum/TestBrain1.json', default_reset_parameters), call('test-meta-curriculum/TestBrain2.json', default_reset_parameters)]
mock_curriculum_init.assert_has_calls(calls)
@patch('unitytrainers.Curriculum')
@patch('unitytrainers.Curriculum')
def test_set_lesson_nums(test_brain_1_curriculum, test_brain_2_curriculum):
meta_curriculum = MetaCurriculumTest({'TestBrain1' : test_brain_1_curriculum, 'TestBrain2' : test_brain_2_curriculum})
meta_curriculum.lesson_nums = {'TestBrain1' : 1, 'TestBrain2' : 3}
assert test_brain_1_curriculum.lesson_num == 1
assert test_brain_2_curriculum.lesson_num == 3
@patch('unitytrainers.Curriculum')
@patch('unitytrainers.Curriculum')
def test_increment_lessons(test_brain_1_curriculum, test_brain_2_curriculum, progresses):
meta_curriculum = MetaCurriculumTest({'TestBrain1' : test_brain_1_curriculum, 'TestBrain2' : test_brain_2_curriculum})
meta_curriculum.increment_lessons(progresses)
test_brain_1_curriculum.increment_lesson.assert_called_with(0.2)
test_brain_2_curriculum.increment_lesson.assert_called_with(0.3)
@patch('unitytrainers.Curriculum')
@patch('unitytrainers.Curriculum')
def test_set_all_curriculums_to_lesson_num(test_brain_1_curriculum, test_brain_2_curriculum):
meta_curriculum = MetaCurriculumTest({'TestBrain1' : test_brain_1_curriculum, 'TestBrain2' : test_brain_2_curriculum})
meta_curriculum.set_all_curriculums_to_lesson_num(2)
assert test_brain_1_curriculum.lesson_num == 2
assert test_brain_2_curriculum.lesson_num == 2
@patch('unitytrainers.Curriculum')
@patch('unitytrainers.Curriculum')
def test_get_config(test_brain_1_curriculum, test_brain_2_curriculum, default_reset_parameters, more_reset_parameters):
test_brain_1_curriculum.get_config.return_value = default_reset_parameters
test_brain_2_curriculum.get_config.return_value = default_reset_parameters
meta_curriculum = MetaCurriculumTest({'TestBrain1' : test_brain_1_curriculum, 'TestBrain2' : test_brain_2_curriculum})
assert meta_curriculum.get_config() == default_reset_parameters
test_brain_2_curriculum.get_config.return_value = more_reset_parameters
new_reset_parameters = dict(default_reset_parameters)
new_reset_parameters.update(more_reset_parameters)
assert meta_curriculum.get_config() == new_reset_parameters

93
python/tests/test_school.py


import pytest
from unittest.mock import patch, call, Mock
from unitytrainers.school import School
class SchoolTest(School):
"""This class allows us to test School objects without calling School's
__init__ function.
"""
def __init__(self, brains_to_curriculums):
self._brains_to_curriculums = brains_to_curriculums
@pytest.fixture
def default_reset_parameters():
return {'param1' : 1, 'param2' : 2, 'param3' : 3}
@pytest.fixture
def more_reset_parameters():
return {'param4' : 4, 'param5' : 5, 'param6' : 6}
@pytest.fixture
def progresses():
return {'TestBrain1' : 0.2, 'TestBrain2' : 0.3}
@patch('unitytrainers.Curriculum.__init__', return_value=None)
@patch('os.listdir', return_value=['TestBrain1.json', 'TestBrain2.json'])
def test_init_school_happy_path(listdir, mock_curriculum_init, default_reset_parameters):
school = School('test-school/', default_reset_parameters)
assert len(school.brains_to_curriculums) == 2
assert 'TestBrain1' in school.brains_to_curriculums
assert 'TestBrain2' in school.brains_to_curriculums
calls = [call('test-school/TestBrain1.json', default_reset_parameters), call('test-school/TestBrain2.json', default_reset_parameters)]
mock_curriculum_init.assert_has_calls(calls)
@patch('unitytrainers.Curriculum')
@patch('unitytrainers.Curriculum')
def test_set_lesson_nums(test_brain_1_curriculum, test_brain_2_curriculum):
school = SchoolTest({'TestBrain1' : test_brain_1_curriculum, 'TestBrain2' : test_brain_2_curriculum})
school.lesson_nums = {'TestBrain1' : 1, 'TestBrain2' : 3}
assert test_brain_1_curriculum.lesson_num == 1
assert test_brain_2_curriculum.lesson_num == 3
@patch('unitytrainers.Curriculum')
@patch('unitytrainers.Curriculum')
def test_increment_lessons(test_brain_1_curriculum, test_brain_2_curriculum, progresses):
school = SchoolTest({'TestBrain1' : test_brain_1_curriculum, 'TestBrain2' : test_brain_2_curriculum})
school.increment_lessons(progresses)
test_brain_1_curriculum.increment_lesson.assert_called_with(0.2)
test_brain_2_curriculum.increment_lesson.assert_called_with(0.3)
@patch('unitytrainers.Curriculum')
@patch('unitytrainers.Curriculum')
def test_set_all_curriculums_to_lesson_num(test_brain_1_curriculum, test_brain_2_curriculum):
school = SchoolTest({'TestBrain1' : test_brain_1_curriculum, 'TestBrain2' : test_brain_2_curriculum})
school.set_all_curriculums_to_lesson_num(2)
assert test_brain_1_curriculum.lesson_num == 2
assert test_brain_2_curriculum.lesson_num == 2
@patch('unitytrainers.Curriculum')
@patch('unitytrainers.Curriculum')
def test_get_config(test_brain_1_curriculum, test_brain_2_curriculum, default_reset_parameters, more_reset_parameters):
test_brain_1_curriculum.get_config.return_value = default_reset_parameters
test_brain_2_curriculum.get_config.return_value = default_reset_parameters
school = SchoolTest({'TestBrain1' : test_brain_1_curriculum, 'TestBrain2' : test_brain_2_curriculum})
assert school.get_config() == default_reset_parameters
test_brain_2_curriculum.get_config.return_value = more_reset_parameters
new_reset_parameters = dict(default_reset_parameters)
new_reset_parameters.update(more_reset_parameters)
assert school.get_config() == new_reset_parameters

/python/unitytrainers/school.py → /python/unitytrainers/meta_curriculum.py

正在加载...
取消
保存