Skip to content

Commit

Permalink
Add docs for logging and plotting (#2147)
Browse files Browse the repository at this point in the history
* Add docs for logging and plotting

* Fix grammer error

* Fix isort
  • Loading branch information
yeukfu authored Oct 27, 2020
1 parent 792771d commit a63349a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ and how to implement new MDPs and new algorithms.
user/environment_libraries
user/concept_experiment
user/sampling
user/logging_plotting
.. toctree::
:maxdepth: 2
Expand Down
82 changes: 82 additions & 0 deletions docs/user/logging_plotting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Logging and plotting

## Logging

garage supports convenient and useful logging. garage uses [dowel](https://github.com/rlworkgroup/dowel)
for logging. The `logger` supports many outputs, including

- Std output
- Text output
- Csv output
- TensorBoard output

In garage's experiment, the `logger` will output to all of these.

Here is an example of logging in garage.

```py
from garage import wrap_experiment
from dowel import logger, tabular

@wrap_experiment
def log_experiment(ctxt=None):
for i in range(100):
# Log str directly
logger.log('Logging messages:')
# Log scalar values with the key 'AverageReturn'
tabular.record('AverageReturn', i)

# The Trainer will do these steps for you, if you log things in
# the algorithms.
logger.log(tabular)
logger.dump_all()

log_experiment()
```

Running the example will generate outputs like:

```sh
2020-10-21 14:06:04 | [log_experiment] Logging to [CUR_DIR]/data/local/experiment/log_experiment
2020-10-21 14:06:04 | [log_experiment] Logging messages:
------------- -
AverageReturn 0
------------- -
2020-10-21 14:06:04 | [log_experiment] Logging messages:
------------- -
AverageReturn 1
------------- -
2020-10-21 14:06:04 | [log_experiment] Logging messages:
------------- -
AverageReturn 2
------------- -
```

To look at outputs with TensorBoard, you can refer to this [page](monitor_experiments_with_tensorboard).

To set a customized log directory, just pass a `log_dir` argument to the
experiment.

```py
@wrap_experiment(log_dir='my_custom_log_fir')
```

## Plotting

In garage, as long as the environment implement the `visualize()` method, is
it easy to plot a policy running in the environment when training.

To visualize an experiment, just set the `plot` argument to `True` in the
[`train`](../_autoapi/garage/index.html#garage.Trainer.train) method of
`Trainer`. For example, in [example/tf/trpo_cartpole.py](https://github.com/rlworkgroup/garage/blob/master/examples/tf/trpo_cartpole.py),
change the train line into:

```py
trainer.train(n_epochs=100, batch_size=4000, plot=True)
```

If you want to pause in every epoch, just set `pause_for_plot` to `True`.

----

*This page was authored by Ruofu Wang ([@yeukfu](https://github.com/yeukfu)).*
17 changes: 7 additions & 10 deletions src/garage/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
"""Garage Base."""
# yapf: disable
from garage._dtypes import (EpisodeBatch,
InOutSpec,
StepType,
TimeStep,
from garage._dtypes import (EpisodeBatch, InOutSpec, StepType, TimeStep,
TimeStepBatch)
from garage._environment import Environment, EnvSpec, EnvStep, Wrapper
from garage._functions import (_Default,
log_multitask_performance,
log_performance,
make_optimizer,
obtain_evaluation_episodes,
rollout)
from garage._functions import (_Default, log_multitask_performance,
log_performance, make_optimizer,
obtain_evaluation_episodes, rollout)
from garage.experiment.experiment import wrap_experiment
from garage.trainer import TFTrainer, Trainer

# yapf: enable

Expand All @@ -33,4 +28,6 @@
'Wrapper',
'rollout',
'obtain_evaluation_episodes',
'Trainer',
'TFTrainer',
]

0 comments on commit a63349a

Please sign in to comment.