浏览代码

[skip ci] adding initializer

/trainer-plugin
Anupam Bhatnagar 4 年前
当前提交
e9d3de8e
共有 4 个文件被更改,包括 68 次插入3 次删除
  1. 6
      ml-agents/mlagents/trainers/cli_utils.py
  2. 53
      ml-agents/mlagents/trainers/learn.py
  3. 2
      ml-agents/mlagents/trainers/saver/tf_saver.py
  4. 10
      ml-agents/mlagents/trainers/settings.py

6
ml-agents/mlagents/trainers/cli_utils.py


help="Whether to enable debug-level logging for some parts of the code",
)
argparser.add_argument(
"--plugins",
default="",
nargs="*",
help="Absolute paths of plugins to be added to the trainers.",
)
argparser.add_argument(
"--env-args",
default=None,
nargs=argparse.REMAINDER,

53
ml-agents/mlagents/trainers/learn.py


from mlagents.trainers.cli_utils import parser
from mlagents_envs.environment import UnityEnvironment
from mlagents.trainers.settings import RunOptions
from mlagents.trainers.trainer.rl_trainer import RLTrainer
from mlagents.trainers.training_status import GlobalTrainingStatus
from mlagents_envs.base_env import BaseEnv
from mlagents.trainers.subprocess_env_manager import SubprocessEnvManager

add_metadata as add_timer_metadata,
)
from mlagents_envs import logging_util
import sys
import importlib
import pkgutil
class Initializer:
def __init__(self):
pass
def load(self):
pass
def get_all_subclasses(cls):
all_subclasses = []
for subclass in cls.__subclasses__():
all_subclasses.append(subclass)
all_subclasses.extend(get_all_subclasses(subclass))
return all_subclasses
def find_initializer_trainer(paths):
original_initializers = set(Initializer.__subclasses__())
original_trainers = set(RLTrainer.__subclasses__())
print(original_initializers)
print(original_trainers)
for p in paths:
sys.path.append(p)
discovered_plugins = {
name: importlib.import_module(name)
for finder, name, ispkg in pkgutil.iter_modules(paths)
}
print(f"plugins available {discovered_plugins}")
all_initializers = set(get_all_subclasses(Initializer))
all_trainers = set(get_all_subclasses(RLTrainer))
new_initializers = all_initializers - original_initializers
new_trainers = all_trainers - original_trainers
print(f"Found %s new initializers" % len(new_initializers))
print(f"Found %s new trainers" % len(new_trainers))
print(all_initializers)
print(new_initializers)
print(all_trainers)
print(new_trainers)
def get_version_string() -> str:

options.environment_parameters, run_seed, restore=checkpoint_settings.resume
)
find_initializer_trainer(options.plugins)
trainer_factory = TrainerFactory(
options.behaviors,
write_path,

2
ml-agents/mlagents/trainers/saver/tf_saver.py


def export(self, output_filepath: str, brain_name: str) -> None:
# save model if there is only one worker or
# only on worker-0 if there are multiple workers
if self.policy and self.policy.rank is not None and self.policy.rank != 0:
if self.policy and self.rank is not None and self.rank != 0:
return
export_policy_model(
self.model_path, output_filepath, brain_name, self.graph, self.sess

10
ml-agents/mlagents/trainers/settings.py


class TrainerType(Enum):
PPO: str = "ppo"
SAC: str = "sac"
DistributedPPO: str = "distributed-ppo"
DistributedSAC: str = "distributed-sac"
_mapping = {TrainerType.PPO: PPOSettings, TrainerType.SAC: SACSettings}
_mapping = {
TrainerType.PPO: PPOSettings,
TrainerType.SAC: SACSettings,
TrainerType.DistributedPPO: PPOSettings,
TrainerType.DistributedSAC: SACSettings,
}
return _mapping[self]

# These are options that are relevant to the run itself, and not the engine or environment.
# They will be left here.
debug: bool = parser.get_default("debug")
plugins: List[str] = parser.get_default("plugins")
# Strict conversion
cattr.register_structure_hook(EnvironmentSettings, strict_to_cls)
cattr.register_structure_hook(EngineSettings, strict_to_cls)

正在加载...
取消
保存