Skip to content

Commit 25efb5d

Browse files
committed
add doc content
1 parent 6de923f commit 25efb5d

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed

docs/basic_usage.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
There are a few different ways you can use Mighty:
2+
3+
### Running Meta-Methods
4+
This is the easiest part. We have several algorithms and meta-methods implemented in Mighty and you should be able to run them directly on any environment of your choosing. The most difficult part will likely be the configuration of each method since they might require specific keywords or are only compatible with a given base algorithm. So you will likely want to read up on whatever method you choose. Then you also need to know if your method is of the runner or meta module type. Each have their own configuration keyword. An example for using a specific runner is:
5+
6+
```bash
7+
python mighty/run_mighty runner=es popsize=5 iterations=100 es=evosax.CMA_ES search_targets=["learning_rate", "_batch_size"] rl_train_agent=true
8+
```
9+
This will use the evosax CMA-ES implementation with population size 5 to optimize the learning rate and batch size in 100 iterations. Meta modules, on the other hand, use a different keyword:
10+
```bash
11+
python mighty/run_mighty.py +algorithm_kwargs.meta_methods=[mighty.mighty_meta.PrioritizedLevelReplay]
12+
```
13+
This meta methods list collects all meta modules in the order they should be used. So while you can't use multiple runners, you can use layers of meta modules.
14+
15+
### Implementing New Components
16+
Of course Mighty currently only supports a limited amount of methods. This is where you come in! It should be fairly easy for you to add your own. We recommend following these steps:
17+
1. What are you adding? A runner, meta module, exploration policy, buffer, update variation or model? Make sure you choose the best level to implement your idea in.
18+
2. Implement your method using the abstract class and existing methods as templates.
19+
3. Plug your class into your Mighty config file. This works by replacing the default value with the import path of your custom class.
20+
4. Run the algorithm.
21+
22+
Since you are passing the place from which to import your new class, you do not need to work within the Mighty codebase directly, but keep your changes separate. This way you can add several new methods to Mighty without copying the code.
23+
24+
### Combining Different Ideas
25+
You can combine different approaches with Mighty by varying the runner, exploration, buffer, update class and network architecture and combining them with an arbitrary number of meta modules.
26+
At this point, configuration might become very difficult. We recommend that you take a close look at how to use different hydra configuration files to separately configure each of your methods so that you can keep track of everything.
27+
Depending on what exactly you want to do, it can make sense to keep separate configuration files for each variation you make. This can be confusing, especially if you haven't worked with hydra before, so we recommed you take the time to focus on configurations when attempting combinations of several methods.

docs/methods/algorithms.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- Algorithms generally
2+
- exploration policies
3+
- buffers
4+
- update classes

docs/methods/architectures.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- how models are implemented (feature extractor -> rest)
2+
- existing models
3+
- changing structure
4+
- when to add new models

docs/methods/inner_loops.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- general meta module idea
2+
- what info they get
3+
- when they can act
4+
- combining modules

docs/methods/outer_loops.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- when to use runners
2+
- what they have access to
3+
- examples?

docs/package_structure.md

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
Mighty is desined to be highly modular, enabling access to the RL loop on different levels. This means it's not designed to be the absolute fastest way to run RL, but the most convenient one to apply different sorts of RL, MetaRL and AutoRL methods. As such, there are a few things you should know about the structure of Mighty.
2+
3+
### For Multiple Inner Runs: Mighty Runners
4+
Mighty uses runner classes to control the outer training loop. In the simplest case, a runner will just directly call the agent's train and evaluation functions without any changes:
5+
6+
```python
7+
def run(self) -> Tuple[Dict, Dict]:
8+
train_results = self.train(self.num_steps)
9+
eval_results = self.evaluate()
10+
return train_results, eval_results
11+
```
12+
This will result in a standard RL agent training run. Of course, we can at this point also run agents multiple times, make changes to their setup (hyperparameters, weights, environments) and integrate learning on this meta-level.
13+
A still fairly simple example is our ESRunner for outer loops with Evolutionary Strategies:
14+
15+
```python
16+
def run(self) -> Tuple[Dict, Dict]:
17+
es_state = self.es.initialize(self.rng)
18+
for _ in range(self.iterations):
19+
rng_ask, _ = jax.random.split(self.rng, 2)
20+
x, es_state = self.es.ask(rng_ask, es_state)
21+
eval_rewards = []
22+
23+
for individual in x:
24+
if self.search_params:
25+
self.apply_parameters(individual[: self.total_n_params])
26+
individual = individual[self.total_n_params :]
27+
28+
for i, target in enumerate(self.search_targets):
29+
if target == "parameters":
30+
continue
31+
new_value = np.asarray(individual[i]).item()
32+
if target in ["_batch_size", "n_units"]:
33+
new_value = max(0, int(new_value))
34+
setattr(self.agent, target, new_value)
35+
36+
if self.train_agent:
37+
self.train(self.num_steps_per_iteration)
38+
39+
eval_results = self.evaluate()
40+
eval_rewards.append(eval_results["mean_eval_reward"])
41+
42+
fitness = self.fit_shaper.apply(x, jnp.array(eval_rewards))
43+
es_state = self.es.tell(x, fitness, es_state)
44+
45+
eval_results = self.evaluate()
46+
return {"step": self.iterations}, eval_results
47+
```
48+
Here we can change all sorts of things about the agent, train in between or only evaluate and use the ES to get fresh inputs. Runner classes are defined with these multiple evaluations of RL tasks in mind, i.e. these classes will usually train multiple agents, reset their policies completely or otherwise start over at some point.
49+
50+
### For In-The-Loop Methods: Mighty Meta Modules
51+
52+
Not all Meta- or AutoRL methods operate in an outer loop, however. For the ones that configure training while it is still ongoing, we use the Mighty Meta Modules.
53+
These are classes that maintain lists of function calls to make at different points in training:
54+
55+
```python
56+
def __init__(self) -> None:
57+
"""Meta module init.
58+
59+
:return:
60+
"""
61+
self.pre_step_methods = []
62+
self.post_step_methods = []
63+
self.pre_update_methods = []
64+
self.post_update_methods = []
65+
self.pre_episode_methods = []
66+
self.post_episode_methods = []
67+
```
68+
This gives meta modules a lot of flexibility of when to act upon training. Additionally, each of these function calls is given a "metrics" dictionary. This dictionary contains most, if not all, relevant information about training progress, e.g.:
69+
- the last transitions
70+
- the last losses, errors and predictions
71+
- policy, Q- and value-networks
72+
- hyperparameters
73+
74+
This means meta modules can use everything from the current timestep to agent predictions.
75+
76+
77+
### Algorithm Components: Mighty Exploration, Buffers and Updates
78+
79+
The Mighty algorithms themselves also have modules which can be easily switched. These are exploration policies, buffers and update classes.
80+
Exploration policies and buffers furthermore have access to the same metrics dictionary as meta modules, meaning you can get creative as to what they do with this information.
81+
The way they are used in the RL loop is fixed, however, such that these are a bit more streamlined than the completely free meta-modules.
82+
83+
84+
### Inside the Agent: Mighty Models
85+
86+
Agent loops outside of exploration, buffers and updates are harder to alter in Mighty, since Mighty is primarily focused on meta-methods.
87+
You can control the network architecture of your agent fairly easily, however.
88+
There are two principal avenues for this:
89+
1. You can use one of the pre-defined Mighty Models and configure it to use a different network architecture in the config. We use torch internally, that means you can allocate torch.nn layers and activations in different parts of these networks to form a custom architecture.
90+
2. If you also want to customize what exactly the network predicts or add things like frozen weights, you probably want to implement your own Mighty Model. These always contain a 'feature_extractor' as a base and can vary beyond that.

0 commit comments

Comments
 (0)