|
| 1 | +# Logging and plotting |
| 2 | + |
| 3 | +## Logging |
| 4 | + |
| 5 | +garage supports convenient and useful logging. garage uses [dowel](https://github.com/rlworkgroup/dowel) |
| 6 | +for logging. The `logger` supports many outputs, including |
| 7 | + |
| 8 | +- Std output |
| 9 | +- Text output |
| 10 | +- Csv output |
| 11 | +- TensorBoard output |
| 12 | + |
| 13 | +In garage's experiment, the `logger` will output to all of these. |
| 14 | + |
| 15 | +Here is an example of logging in garage. |
| 16 | + |
| 17 | +```py |
| 18 | +from garage import wrap_experiment |
| 19 | +from dowel import logger, tabular |
| 20 | + |
| 21 | +@wrap_experiment |
| 22 | +def log_experiment(ctxt=None): |
| 23 | + for i in range(100): |
| 24 | + # Log str directly |
| 25 | + logger.log('Logging messages:') |
| 26 | + # Log scalar values with the key 'AverageReturn' |
| 27 | + tabular.record('AverageReturn', i) |
| 28 | + |
| 29 | + # The Trainer will do these steps for you, if you log things in |
| 30 | + # the algorithms. |
| 31 | + logger.log(tabular) |
| 32 | + logger.dump_all() |
| 33 | + |
| 34 | +log_experiment() |
| 35 | +``` |
| 36 | + |
| 37 | +Running the example will generate outputs like: |
| 38 | + |
| 39 | +```sh |
| 40 | +2020-10-21 14:06:04 | [log_experiment] Logging to [CUR_DIR]/data/local/experiment/log_experiment |
| 41 | +2020-10-21 14:06:04 | [log_experiment] Logging messages: |
| 42 | +------------- - |
| 43 | +AverageReturn 0 |
| 44 | +------------- - |
| 45 | +2020-10-21 14:06:04 | [log_experiment] Logging messages: |
| 46 | +------------- - |
| 47 | +AverageReturn 1 |
| 48 | +------------- - |
| 49 | +2020-10-21 14:06:04 | [log_experiment] Logging messages: |
| 50 | +------------- - |
| 51 | +AverageReturn 2 |
| 52 | +------------- - |
| 53 | +``` |
| 54 | + |
| 55 | +To look at outputs with TensorBoard, you can refer to this [page](monitor_experiments_with_tensorboard). |
| 56 | + |
| 57 | +To set a customized log directory, just pass a `log_dir` argument to the |
| 58 | +experiment. |
| 59 | + |
| 60 | +```py |
| 61 | +@wrap_experiment(log_dir='my_custom_log_fir') |
| 62 | +``` |
| 63 | + |
| 64 | +## Plotting |
| 65 | + |
| 66 | +In garage, as long as the environment implement the `visualize()` method, is |
| 67 | +it easy to plot a policy running in the environment when training. |
| 68 | + |
| 69 | +To visualize an experiment, just set the `plot` argument to `True` in the |
| 70 | +[`train`](../_autoapi/garage/index.html#garage.Trainer.train) method of |
| 71 | +`Trainer`. For example, in [example/tf/trpo_cartpole.py](https://github.com/rlworkgroup/garage/blob/master/examples/tf/trpo_cartpole.py), |
| 72 | +change the train line into: |
| 73 | + |
| 74 | +```py |
| 75 | +trainer.train(n_epochs=100, batch_size=4000, plot=True) |
| 76 | +``` |
| 77 | + |
| 78 | +If you want to pause in every epoch, just set `pause_for_plot` to `True`. |
| 79 | + |
| 80 | +---- |
| 81 | + |
| 82 | +*This page was authored by Ruofu Wang ([@yeukfu](https://github.com/yeukfu)).* |
0 commit comments