浏览代码

Addressing Vince's offline comments.

- Warning logged if two curriculums attempt to reset the same parameter.
- Error is raised when a curriculum file is not named to match a brain.
/develop-generalizationTraining-TrainerController
Deric Pang 7 年前
当前提交
e678e691
共有 5 个文件被更改,包括 64 次插入24 次删除
  1. 2
      python/tests/test_meta_curriculum.py
  2. 22
      python/unitytrainers/curriculum.py
  3. 5
      python/unitytrainers/exception.py
  4. 19
      python/unitytrainers/meta_curriculum.py
  5. 40
      python/unitytrainers/trainer_controller.py

2
python/tests/test_meta_curriculum.py


return {'Brain1' : 0.2, 'Brain2' : 0.3}
@patch('unitytrainers.Curriculum.get_config', return_value={})
mock_curriculum_get_config,
default_reset_parameters):
meta_curriculum = MetaCurriculum('test/', default_reset_parameters)

22
python/unitytrainers/curriculum.py


import logging
logger = logging.getLogger("unityagents")
logger = logging.getLogger('unitytrainers')
class Curriculum(object):

self.data = json.load(data_file)
except IOError:
raise CurriculumError(
"The file {0} could not be found.".format(location))
'The file {0} could not be found.'.format(location))
raise CurriculumError("There was an error decoding {}".format(location))
raise CurriculumError('There was an error decoding {}'.format(location))
self.smoothing_value = 0
for key in ['parameters', 'measure', 'thresholds',
'min_lesson_length', 'signal_smoothing']:

for key in parameters:
if key not in default_reset_parameters:
raise CurriculumError(
"The parameter {0} in Curriculum {1} is not present in "
"the Environment".format(key, location))
'The parameter {0} in Curriculum {1} is not present in '
'the Environment'.format(key, location))
"The parameter {0} in Curriculum {1} must have {2} values "
"but {3} were found".format(key, location,
'The parameter {0} in Curriculum {1} must have {2} values '
'but {3} were found'.format(key, location,
self.max_lesson_num + 1, len(parameters[key])))
@property

"""
if self.data is None or progress is None:
return
if self.data["signal_smoothing"]:
if self.data['signal_smoothing']:
progress = self.smoothing_value * 0.25 + 0.75 * progress
self.smoothing_value = progress
self.lesson_length += 1

self.lesson_length = 0
self.lesson_num += 1
config = {}
parameters = self.data["parameters"]
parameters = self.data['parameters']
logger.info("\nLesson changed. Now in Lesson {0} : \t{1}"
logger.info('\nLesson changed. Now in Lesson {0} : \t{1}'
.format(self.lesson_num,
', '.join([str(x) + ' -> ' + str(config[x]) for x in config])))

lesson = self.lesson_num
lesson = max(0, min(lesson, self.max_lesson_num))
config = {}
parameters = self.data["parameters"]
parameters = self.data['parameters']
for key in parameters:
config[key] = parameters[key][lesson]
return config

5
python/unitytrainers/exception.py


Any error related to training with a curriculum.
"""
pass
class MetaCurriculumError(TrainerError):
"""
Any error related to the configuration of a metacurriculum.
"""

19
python/unitytrainers/meta_curriculum.py


"""Contains the MetaCurriculum class."""
import os
from unitytrainers import Curriculum
from unitytrainers.curriculum import Curriculum
from unitytrainers.exception import MetaCurriculumError
import logging
logger = logging.getLogger('unitytrainers')
class MetaCurriculum(object):
"""A MetaCurriculum holds curriculums. Each curriculum is associated to a particular

if curriculum_folder is None:
self._brains_to_curriculums = None
else:
used_reset_parameters = set()
self._brains_to_curriculums[brain_name] = \
Curriculum(curriculum_filepath, default_reset_parameters)
curriculum = Curriculum(curriculum_filepath, default_reset_parameters)
if any([(parameter in curriculum.get_config().keys()) for parameter in used_reset_parameters]):
logger.info('Two of more curriculums have attempted to change '
'the same reset parameter. The result will be '
'non-deterministic.')
used_reset_parameters.update(curriculum.get_config().keys())
self._brains_to_curriculums[brain_name] = curriculum
@property
def brains_to_curriculums(self):

40
python/unitytrainers/trainer_controller.py


# Launches unitytrainers for each External Brains in a Unity Environment
import logging
import numpy as np
import re
import tensorflow as tf
import numpy as np
import tensorflow as tf
from unityagents.environment import UnityEnvironment
from unityagents.exception import UnityEnvironmentException
from unitytrainers import MetaCurriculum
from unityagents import UnityEnvironment, UnityEnvironmentException
from unitytrainers.meta_curriculum import MetaCurriculum
from unitytrainers.exception import MetaCurriculumError
class TrainerController(object):

:param no_graphics: Whether to run the Unity simulator in no-graphics mode
"""
self.trainer_config_path = trainer_config_path
if env_path is not None:
env_path = (env_path.strip()
.replace('.app', '')

# Recognize and use docker volume if one is passed as an argument
if docker_target_name == '':
self.docker_training = False

if env_path is not None:
env_path = '/{docker_target_name}/{env_name}'.format(docker_target_name=docker_target_name,
env_name=env_path)
if curriculum_folder is None:
self.curriculum_folder = None
else:
if curriculum_folder is not None:
self.logger = logging.getLogger("unityagents")
self.run_id = run_id
self.save_freq = save_freq

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.meta_curriculum = MetaCurriculum(self.curriculum_folder, self.env._resetParameters)
if curriculum_folder is None:
self.meta_curriculum = None
else:
self.meta_curriculum = MetaCurriculum(self.curriculum_folder, self.env._resetParameters)
if self.meta_curriculum is not None and self.curriculum_folder is not None:
for brain_name in self.meta_curriculum.brains_to_curriculums.keys():
if brain_name not in self.env.external_brain_names:
raise MetaCurriculumError('One of the curriculums '
'defined in ' +
self.curriculum_folder +
'does not have a corresponding '
'Brain. Please check that the '
'curriculum file has the same '
'name as the Brain '
'whose curriculum it defines.')
if self.curriculum_folder is not None:
if self.meta_curriculum is not None:
brain_names_to_progresses = {}
for brain_name, curriculum in self.meta_curriculum.brains_to_curriculums.items():
if curriculum.measure == "progress":

正在加载...
取消
保存