yawning_titan.yawning_titan_run.YawningTitanRun#
- class yawning_titan.yawning_titan_run.YawningTitanRun(network=None, game_mode=None, red_agent_class=<class 'yawning_titan.envs.generic.core.red_interface.RedInterface'>, blue_agent_class=<class 'yawning_titan.envs.generic.core.blue_interface.BlueInterface'>, print_metrics=False, show_metrics_every=1, collect_additional_per_ts_data=False, eval_freq=10000, total_timesteps=200000, training_runs=1, n_eval_episodes=1, deterministic=False, warn=True, render=False, verbose=1, logger=None, output_dir=None, auto=True, **kwargs)[source]#
Bases:
object
The
YawningTitanRun
class is the run class for training YT agents from a given set of parameters.The
YawningTitanRun
class can be used ‘straight out of the box’, as all params have default values.yt_run = YawningTitanRun()
The
YawningTitanRun
class can also be used manually by setting auto=False.yt_run = YawningTitanRun(auto=False) yt_run.setup() yt_run.train() yt_run.evaluate()
Trained agents can be saved by calling
.save()
. If no path is provided, a path is generated using the AGENTS_DIR, today’s date, and the uuid of the instance ofYawningTitanRun
.yt_run = YawningTitanRun() yt_run.save()
Todo
Build a reporting functionality that captures all logs and eval and generates a PDF report.
Add multiple training runs functionality for the same agent.
Add the ability to load a saved agent and continue training it.
The YawningTitanRun constructor.
# TODO: Add proper Sphinx mapping for classes/methods.
- Parameters:
network – An instance of
Network
.game_mode – An instance of
GameMode
.red_agent_class – The agent/action set class used for the red agent.
blue_agent_class – The agent/action set class used for the blue agent.
print_metrics – Print the metrics if True. Default value = True.
show_metrics_every – Prints the metrics every
show_metrics_every
time steps. Default value = 10.collect_additional_per_ts_data – Collects additional per-timestep data if True.Default value = False.
eval_freq – Evaluate the agent every
eval_freq
call of the callback. Default value = 10,000.total_timesteps – The number of samples (env steps) to train on. Default value = 200000.
training_runs – The number of times the agent is trained.
n_eval_episodes – The number of episodes to evaluate the agent. Default value = 1.
deterministic – Whether the evaluation should use stochastic or deterministic actions. Default value = False.
warn – Output additional warnings mainly related to the interaction with stable_baselines if True. Default value = True.
render – Renders the environment during evaluation if True. Default value = False.
verbose – Verbosity level: 0 for no output, 1 for info messages (such as device or wrappers used), 2 for debug messages. Default value = 1.
logger – An optional custom logger to override the use of the default module logger.
output_dir – An optional output path for eval output and saved agent zip file. If none is provided, a path is generated using the
yawning_titan.AGENTS_DIR
, today’s date, and the uuid of the instance ofYawningTitanRun
.auto – If True,
setup()
,train()
, andevaluate()
are called automatically.
Methods
Evaluates the trained agent.
Export the YawningTitanRun as a zip.
Import and return an exported YawningTitanRun.
Load and return a saved YawningTitanRun.
Saves the trained agent using the stable_baselines3 save as zip functionality.
Performs a setup of the
NetworkInterface
,GenericNetworkEnv
,PPO
algorithm.Trains the agent.
- setup(new=True, ppo_zip_path=None)[source]#
Performs a setup of the
NetworkInterface
,GenericNetworkEnv
,PPO
algorithm.The setup needs to be performed before training can occur.
- Parameters:
new – If True, a new instance of PPO is generated. If False, a ppo_zip_path must be passed tooo.
ppo_zip_path – Optional path to a saved ppo.zip file. Required if new = False.
- Raises:
AttributeError – When new=False and ppo_zip_path hasn’t been provided.
- evaluate()[source]#
Evaluates the trained agent.
- Returns:
Mean reward per episode, std of reward per episode.
- save()[source]#
Saves the trained agent using the stable_baselines3 save as zip functionality.
The instance of PPO is saved to ppo.zip.
The YawningTitanRun args are saved to args.json.
The YawningTitanRun.uuid is saved to UUID.
- Returns:
The path the agent has been saved to.
- export()[source]#
Export the YawningTitanRun as a zip.
The contents of output_dir is archived to the agents_dir exported dir.
Included is an INVENTORY file that contains all files and their sizes. This is used for file verification when an exported YawningTitanRun is imported.
- Returns:
The exported filepath as a str.
- classmethod load(path)[source]#
Load and return a saved YawningTitanRun.
YawningTitanRun’s that have auto=True will not be automatically ran on load.
- Parameters:
path – A saved YawningTitanRun path.
- Returns:
An instance of YawningTitanRun.
- classmethod import_from_export(exported_zip_file_path, overwrite_existing=False)[source]#
Import and return an exported YawningTitanRun.
YawningTitanRun’s that have auto=True will not be automatically ran on import.
- Parameters:
exported_zip_file_path – The path of an exported YawningTitanRun.
overwrite_existing – If True, if the uuid of the imported agent already exists in the trainer agents dir it is overwritten.
- Returns:
The imported instance of YawningTitanRun.
- Raises:
YawningTitanRunError – When the INVENTORY file fails its verification.