SIGN IN SIGN UP

The Unity Machine Learning Agents Toolkit (ML-Agents) is an open-source project that enables games and simulations to serve as environments for training intelligent agents using deep reinforcement learning and imitation learning.

Release 0.9.0 docs checklist and cleanup - v2 (#2372) * Included explicit version # for ZN * added explicit version for KR docs * minor fix in installation doc * Consistency with numbers for reset parameters * Removed extra verbiage. minor consistency * minor consistency * Cleaned up IL language * moved parameter sampling above in list * Cleaned up language in Env Parameter sampling * Cleaned up migrating content * updated consistency of Reset Parameter Sampling * Rename Training-Generalization-Learning.md to Training-Generalization-Reinforcement-Learning-Agents.md * Updated doc link for generalization * Rename Training-Generalization-Reinforcement-Learning-Agents.md to Training-Generalized-Reinforcement-Learning-Agents.md * Re-wrote the intro paragraph for generalization * add titles, cleaned up language for reset params * Update Training-Generalized-Reinforcement-Learning-Agents.md * cleanup of generalization doc * More cleanup in generalization * Fixed title * Clean up included sampler type section * cleaned up defining new sampler type in generalization * cleaned up training section of generalization * final cleanup for generalization * Clean up of Training w Imitation Learning doc * updated link for generalization, reordered * consistency fix * cleaned up training ml agents doc * Update and rename Profiling.md to Profiling-Python.md * Updated Python profiling link * minor clean up in profiling doc * Rename Training-BehavioralCloning.md to Training-Behavioral-Cloning.md * Updated link to BC * Rename Training-RewardSignals.md to Reward-Signals.md * fix reward links to new * cleaned up reward signal language * fixed broken links to reward signals * consistency fix * Updated readme with generalization * Added example for GAIL reward signal * minor fixes and consistency to Reward Signals * referencing GAIL in the recording demonstration * consistency * fixed desc of bc and gail * comment fix * comments fix * Fix broken links * Fix grammar in Overview for IL * Add optional params to reward signals comment to GAIL
2019-07-30 21:24:08 -07:00
# Profiling in Python
As part of the ML-Agents Toolkit, we provide a lightweight profiling system, in
order to identity hotspots in the training process and help spot regressions
from changes.
Timers are hierarchical, meaning that the time tracked in a block of code can be
further split into other blocks if desired. This also means that a function that
is called from multiple places in the code will appear in multiple places in the
timing output.
All timers operate using a "global" instance by default, but this can be
overridden if necessary (mainly for testing).
## Adding Profiling
There are two ways to indicate code should be included in profiling. The
simplest way is to add the `@timed` decorator to a function or method of
interested.
```python
class TrainerController:
# ....
@timed
def advance(self, env: EnvManager) -> int:
# do stuff
```
You can also used the `hierarchical_timer` context manager.
```python
with hierarchical_timer("communicator.exchange"):
outputs = self.communicator.exchange(step_input)
```
The context manager may be easier than the `@timed` decorator for profiling
different parts of a large function, or profiling calls to abstract methods that
might not use decorator.
## Output
By default, at the end of training, timers are collected and written in json
format to `{summaries_dir}/{run_id}_timers.json`. The output consists of node
objects with the following keys:
- total (float): The total time in seconds spent in the block, including child
calls.
- count (int): The number of times the block was called.
- self (float): The total time in seconds spent in the block, excluding child
calls.
- children (dictionary): A dictionary of child nodes, keyed by the node name.
- is_parallel (bool): Indicates that the block of code was executed in multiple
threads or processes (see below). This is optional and defaults to false.
### Parallel execution
#### Subprocesses
For code that executes in multiple processes (for example,
SubprocessEnvManager), we periodically send the timer information back to the
"main" process, aggregate the timers there, and flush them in the subprocess.
Note that (depending on the number of processes) this can result in timers where
the total time may exceed the parent's total time. This is analogous to the
difference between "real" and "user" values reported from the unix `time`
command. In the timer output, blocks that were run in parallel are indicated by
the `is_parallel` flag.
#### Threads
Timers currently use `time.perf_counter()` to track time spent, which may not
give accurate results for multiple threads. If this is problematic, set
`threaded: false` in your trainer configuration.