浏览代码

Make --torch use torch even without config (#4400)

* Make --torch use torch even without config

* Update ml-agents/mlagents/trainers/trainer_util.py

Co-authored-by: andrewcoh <54679309+andrewcoh@users.noreply.github.com>

* Update ml-agents/mlagents/trainers/trainer_util.py

Co-authored-by: andrewcoh <54679309+andrewcoh@users.noreply.github.com>

* Update ml-agents/mlagents/trainers/trainer_util.py

Co-authored-by: andrewcoh <54679309+andrewcoh@users.noreply.github.com>

* renaming use_torch to force_torch

Co-authored-by: andrewcoh <54679309+andrewcoh@users.noreply.github.com>
/MLA-1734-demo-provider
GitHub 4 年前
当前提交
df685184
共有 3 个文件被更改,包括 41 次插入20 次删除
  1. 19
      ml-agents/mlagents/trainers/learn.py
  2. 4
      ml-agents/mlagents/trainers/settings.py
  3. 38
      ml-agents/mlagents/trainers/trainer_util.py

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


GaugeWriter,
ConsoleWriter,
)
from mlagents.trainers.cli_utils import parser
from mlagents.trainers.cli_utils import parser, DetectDefault
from mlagents_envs.environment import UnityEnvironment
from mlagents.trainers.settings import RunOptions

)
trainer_factory = TrainerFactory(
options.behaviors,
write_path,
not checkpoint_settings.inference,
checkpoint_settings.resume,
run_seed,
env_parameter_manager,
maybe_init_path,
False,
trainer_config=options.behaviors,
output_path=write_path,
train_model=not checkpoint_settings.inference,
load_model=checkpoint_settings.resume,
seed=run_seed,
param_manager=env_parameter_manager,
init_path=maybe_init_path,
multi_gpu=False,
force_torch="torch" in DetectDefault.non_default_args,
)
# Create controller and begin training.
tc = TrainerController(

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


else: # Base options
configured_dict[key] = val
# Apply --torch retroactively
if "torch" in DetectDefault.non_default_args:
for trainer_set in final_runoptions.behaviors.values():
trainer_set.framework = FrameworkType.PYTORCH
return final_runoptions
@staticmethod

38
ml-agents/mlagents/trainers/trainer_util.py


from mlagents.trainers.sac.trainer import SACTrainer
from mlagents.trainers.ghost.trainer import GhostTrainer
from mlagents.trainers.ghost.controller import GhostController
from mlagents.trainers.settings import TrainerSettings, TrainerType
from mlagents.trainers.settings import TrainerSettings, TrainerType, FrameworkType
logger = get_logger(__name__)

param_manager: EnvironmentParameterManager,
init_path: str = None,
multi_gpu: bool = False,
force_torch: bool = False,
"""
The TrainerFactory generates the Trainers based on the configuration passed as
input.
:param trainer_config: A dictionary from behavior name to TrainerSettings
:param output_path: The path to the directory where the artifacts generated by
the trainer will be saved.
:param train_model: If True, the Trainers will train the model and if False,
only perform inference.
:param load_model: If True, the Trainer will load neural networks weights from
the previous run.
:param seed: The seed of the Trainers. Dictates how the neural networks will be
initialized.
:param param_manager: The EnvironmentParameterManager that will dictate when/if
the EnvironmentParameters must change.
:param init_path: Path from which to load model.
:param multi_gpu: If True, multi-gpu will be used. (currently not available)
:param force_torch: If True, the Trainers will all use the PyTorch framework
instead of the TensorFlow framework.
"""
self.trainer_config = trainer_config
self.output_path = output_path
self.init_path = init_path

self.param_manager = param_manager
self.multi_gpu = multi_gpu
self.ghost_controller = GhostController()
self._force_torch = force_torch
def generate(self, brain_name: str) -> Trainer:
if brain_name not in self.trainer_config.keys():
def generate(self, behavior_name: str) -> Trainer:
if behavior_name not in self.trainer_config.keys():
f"Behavior name {brain_name} does not match any behaviors specified in the trainer configuration file:"
f"{sorted(self.trainer_config.keys())}"
f"Behavior name {behavior_name} does not match any behaviors specified"
f"in the trainer configuration file: {sorted(self.trainer_config.keys())}"
trainer_settings = self.trainer_config[behavior_name]
if self._force_torch:
trainer_settings.framework = FrameworkType.PYTORCH
self.trainer_config[brain_name],
brain_name,
trainer_settings,
behavior_name,
self.output_path,
self.train_model,
self.load_model,

正在加载...
取消
保存