|
| 1 | +(examples)= |
| 2 | + |
| 3 | +# Examples |
| 4 | + |
| 5 | +## TQC |
| 6 | + |
| 7 | +Train a Truncated Quantile Critics (TQC) agent on the Pendulum environment. |
| 8 | + |
| 9 | +```python |
| 10 | +from sb3_contrib import TQC |
| 11 | + |
| 12 | +model = TQC("MlpPolicy", "Pendulum-v1", top_quantiles_to_drop_per_net=2, verbose=1) |
| 13 | +model.learn(total_timesteps=10_000, log_interval=4) |
| 14 | +model.save("tqc_pendulum") |
| 15 | +``` |
| 16 | + |
| 17 | +## QR-DQN |
| 18 | + |
| 19 | +Train a Quantile Regression DQN (QR-DQN) agent on the CartPole environment. |
| 20 | + |
| 21 | +```python |
| 22 | +from sb3_contrib import QRDQN |
| 23 | + |
| 24 | +policy_kwargs = dict(n_quantiles=50) |
| 25 | +model = QRDQN("MlpPolicy", "CartPole-v1", policy_kwargs=policy_kwargs, verbose=1) |
| 26 | +model.learn(total_timesteps=10_000, log_interval=4) |
| 27 | +model.save("qrdqn_cartpole") |
| 28 | +``` |
| 29 | + |
| 30 | +## MaskablePPO |
| 31 | + |
| 32 | +Train a PPO with invalid action masking agent on a toy environment. |
| 33 | + |
| 34 | +:::{warning} |
| 35 | +You must use `MaskableEvalCallback` from `sb3_contrib.common.maskable.callbacks` instead of the base `EvalCallback` to properly evaluate a model with action masks. |
| 36 | +Similarly, you must use `evaluate_policy` from `sb3_contrib.common.maskable.evaluation` instead of the SB3 one. |
| 37 | +::: |
| 38 | + |
| 39 | +```python |
| 40 | +from sb3_contrib import MaskablePPO |
| 41 | +from sb3_contrib.common.envs import InvalidActionEnvDiscrete |
| 42 | + |
| 43 | +env = InvalidActionEnvDiscrete(dim=80, n_invalid_actions=60) |
| 44 | +model = MaskablePPO("MlpPolicy", env, verbose=1) |
| 45 | +model.learn(5000) |
| 46 | +model.save("maskable_toy_env") |
| 47 | +``` |
| 48 | + |
| 49 | +## TRPO |
| 50 | + |
| 51 | +Train a Trust Region Policy Optimization (TRPO) agent on the Pendulum environment. |
| 52 | + |
| 53 | +```python |
| 54 | +from sb3_contrib import TRPO |
| 55 | + |
| 56 | +model = TRPO("MlpPolicy", "Pendulum-v1", gamma=0.9, verbose=1) |
| 57 | +model.learn(total_timesteps=100_000, log_interval=4) |
| 58 | +model.save("trpo_pendulum") |
| 59 | +``` |
| 60 | + |
| 61 | +## ARS |
| 62 | + |
| 63 | +Train an agent using Augmented Random Search (ARS) agent on the Pendulum environment |
| 64 | + |
| 65 | +```python |
| 66 | +from sb3_contrib import ARS |
| 67 | + |
| 68 | +model = ARS("LinearPolicy", "Pendulum-v1", verbose=1) |
| 69 | +model.learn(total_timesteps=10000, log_interval=4) |
| 70 | +model.save("ars_pendulum") |
| 71 | +``` |
| 72 | + |
| 73 | +## RecurrentPPO |
| 74 | + |
| 75 | +Train a PPO agent with a recurrent policy on the CartPole environment. |
| 76 | + |
| 77 | +:::{note} |
| 78 | +It is particularly important to pass the `lstm_states` |
| 79 | +and `episode_start` argument to the `predict()` method, |
| 80 | +so the cell and hidden states of the LSTM are correctly updated. |
| 81 | +::: |
| 82 | + |
| 83 | +```python |
| 84 | +import numpy as np |
| 85 | + |
| 86 | +from sb3_contrib import RecurrentPPO |
| 87 | + |
| 88 | +model = RecurrentPPO("MlpLstmPolicy", "CartPole-v1", verbose=1) |
| 89 | +model.learn(5000) |
| 90 | + |
| 91 | +vec_env = model.get_env() |
| 92 | +obs = vec_env.reset() |
| 93 | +# Cell and hidden state of the LSTM |
| 94 | +lstm_states = None |
| 95 | +num_envs = 1 |
| 96 | +# Episode start signals are used to reset the lstm states |
| 97 | +episode_starts = np.ones((num_envs,), dtype=bool) |
| 98 | +while True: |
| 99 | + action, lstm_states = model.predict(obs, state=lstm_states, episode_start=episode_starts, deterministic=True) |
| 100 | + # Note: vectorized environment resets automatically |
| 101 | + obs, rewards, dones, info = vec_env.step(action) |
| 102 | + episode_starts = dones |
| 103 | + vec_env.render("human") |
| 104 | +``` |
| 105 | + |
| 106 | +## CrossQ |
| 107 | + |
| 108 | +Train a CrossQ agent on the Pendulum environment. |
| 109 | + |
| 110 | +```python |
| 111 | +from sb3_contrib import CrossQ |
| 112 | + |
| 113 | +model = CrossQ( |
| 114 | + "MlpPolicy", |
| 115 | + "Pendulum-v1", |
| 116 | + verbose=1, |
| 117 | + policy_kwargs=dict( |
| 118 | + net_arch=dict( |
| 119 | + pi=[256, 256], |
| 120 | + qf=[1024, 1024], |
| 121 | + ) |
| 122 | + ), |
| 123 | +) |
| 124 | +model.learn(total_timesteps=5_000, log_interval=4) |
| 125 | +model.save("crossq_pendulum") |
| 126 | +``` |
0 commit comments