12

I'm using Ray & RLlib to train RL agents on an Ubuntu system. Tensorboard is used to monitor the training progress by pointing it to ~/ray_results where all the log files for all runs are stored. Ray Tune is not being used.

For example, on starting a new Ray/RLlib training run, a new directory will be created at

~/ray_results/DQN_ray_custom_env_2020-06-07_05-26-32djwxfdu1

To visualize the training progress, we need to start Tensorboard using

tensorboard --logdir=~/ray_results

Question: Is it possible to configure Ray/RLlib to change the output directory of the log files from ~/ray_results to another location?

Additionally, instead of logging to a directory named something like DQN_ray_custom_env_2020-06-07_05-26-32djwxfdu1, can this directory name by set by ourselves?


Failed Attempt: Tried setting

os.environ['TUNE_RESULT_DIR'] = '~/another_dir`

before running ray.init(), but the result log files were still being written to ~/ray_results.

Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • Should work if you set `upload_dir` for tune: https://docs.ray.io/en/master/tune/tutorials/tune-usage.html?highlight=upload_dir#uploading-results – stefanbschneider Jun 19 '20 at 09:38
  • @CGFoX I am not using Tune, just Ray and RLlib... Will your suggestion still apply? – Nyxynyx Jun 19 '20 at 16:06
  • I haven't found a way to configure the directory without `tune` yet, but I'll update you once I do. Btw, with `tune` you'll have to set `local_dir` not `upload_dir`. – stefanbschneider Jun 20 '20 at 20:40

3 Answers3

6

Without using Tune, you can change the logdir using rllib's "Trainer". The "Trainer" class takes in an optional "logger_creator" if you want to specify where to save the log (see here).

A concrete example:

  1. Define your customized logger creator (you can simply modify from the default one):
def custom_log_creator(custom_path, custom_str):

    timestr = datetime.today().strftime("%Y-%m-%d_%H-%M-%S")
    logdir_prefix = "{}_{}".format(custom_str, timestr)

    def logger_creator(config):

        if not os.path.exists(custom_path):
            os.makedirs(custom_path)
        logdir = tempfile.mkdtemp(prefix=logdir_prefix, dir=custom_path)
        return UnifiedLogger(config, logdir, loggers=None)

    return logger_creator
  1. Pass this logger_creator to the trainer, and start training:
trainer = PPOTrainer(config=config, env='CartPole-v0',
                     logger_creator=custom_log_creator(os.path.expanduser("~/another_ray_results/subdir"), 'custom_dir'))

for i in range(ITER_NUM):
    result = trainer.train()

You will find the training results (i.e., TensorBoard events file, params, model, ...) saved under "~/another_ray_results/subdir" with your specified naming convention.

X. Zhang
  • 358
  • 3
  • 12
  • Is there a reason to use `tempfile.mkdtemp` instead of `os.makedirs`? To me it doesn't seem required if we specify the log directory manually like this. – André Feb 18 '21 at 15:57
5

Is it possible to configure Ray/RLlib to change the output directory of the log files from ~/ray_results to another location?

There is currently no way to configure this using RLib CLI tool (rllib).

If you're okay with Python API, then, as described in documentation, local_dir parameter of tune.run is responsible for specifying output directory, default is ~/ray_results.

Additionally, instead of logging to a directory named something like DQN_ray_custom_env_2020-06-07_05-26-32djwxfdu1, can this directory name by set by ourselves?

This is governed by trial_name_creator parameter of tune.run. It must be a function that accepts trial object and formats it into a string like so:

def trial_name_id(trial):
    return f"{trial.trainable_name}_{trial.trial_id}"

tune.run(...trial_name_creator=trial_name_id)
ptyshevs
  • 1,602
  • 11
  • 26
0

Just for anyone who bumps into this problem with Ray Tune.

You can specify local_dir for run_config within tune.Tuner:

# This logs to 2 different trial folders:
# ./results/test_experiment/trial_name_1 and ./results/test_experiment/trial_name_2
# Only trial_name is autogenerated.
tuner = tune.Tuner(trainable,
    tune_config=tune.TuneConfig(num_samples=2),
    run_config=air.RunConfig(local_dir="./results", name="test_experiment"))
results = tuner.fit()

Please see this link for more info.