araffin commited on
Commit
08b00b7
1 Parent(s): fffa48e
Files changed (1) hide show
  1. README.md +96 -2
README.md CHANGED
@@ -24,6 +24,100 @@ model-index:
24
  This is a trained model of a **A2C** agent playing **Pendulum-v1**
25
  using the [stable-baselines3 library](https://github.com/DLR-RM/stable-baselines3).
26
 
27
- ## Usage (with Stable-baselines3)
28
- TODO: Add your code
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
 
24
  This is a trained model of a **A2C** agent playing **Pendulum-v1**
25
  using the [stable-baselines3 library](https://github.com/DLR-RM/stable-baselines3).
26
 
27
+ ## Usage (with Stable-baselines3)
28
+
29
+ ```python
30
+ from huggingface_sb3 import load_from_hub
31
+ from stable_baselines3 import A2C
32
+ from stable_baselines3.common.env_util import make_vec_env
33
+ from stable_baselines3.common.evaluation import evaluate_policy
34
+ from stable_baselines3.common.vec_env import VecNormalize
35
+
36
+ # Download checkpoint and stats
37
+ env_id = "Pendulum-v1"
38
+ checkpoint = load_from_hub(f"araffin/a2c-{env_id}", f"a2c-{env_id}.zip")
39
+ vec_normalize_stats = load_from_hub(f"araffin/a2c-{env_id}", f"vec_normalize.pkl")
40
+
41
+ # Load the model
42
+ model = A2C.load(checkpoint)
43
+
44
+ env = make_vec_env(env_id, n_envs=1)
45
+ env = VecNormalize.load(vec_normalize_stats, env)
46
+ # do not update them at test time
47
+ env.training = False
48
+ # reward normalization is not needed at test time
49
+ env.norm_reward = False
50
+
51
+ # Evaluate
52
+ print("Evaluating model")
53
+ mean_reward, std_reward = evaluate_policy(
54
+ model,
55
+ env,
56
+ n_eval_episodes=20,
57
+ deterministic=True,
58
+ )
59
+ print(f"Mean reward = {mean_reward:.2f} +/- {std_reward:.2f}")
60
+
61
+ # Start a new episode
62
+ obs = env.reset()
63
+
64
+ try:
65
+ while True:
66
+ action, _states = model.predict(obs, deterministic=True)
67
+ obs, rewards, dones, info = env.step(action)
68
+ env.render()
69
+ except KeyboardInterrupt:
70
+ pass
71
+ ```
72
+
73
+ ## Training Code
74
+
75
+ ```python
76
+ from huggingface_sb3 import package_to_hub
77
+ from stable_baselines3 import A2C
78
+ from stable_baselines3.common.env_util import make_vec_env
79
+ from stable_baselines3.common.vec_env import VecNormalize, sync_envs_normalization
80
+
81
+ # Create the environment
82
+ env_id = "Pendulum-v1"
83
+ env = make_vec_env(env_id, n_envs=8)
84
+ # Normalize
85
+ env = VecNormalize(env, gamma=0.9)
86
+
87
+ # Create the evaluation env (could be used in `EvalCallback`)
88
+ eval_env = make_vec_env(env_id, n_envs=1)
89
+ eval_env = VecNormalize(eval_env, gamma=0.9, training=False, norm_reward=False)
90
+
91
+ # Instantiate the agent
92
+ model = A2C(
93
+ "MlpPolicy",
94
+ env,
95
+ n_steps=8,
96
+ gamma=0.9,
97
+ gae_lambda=0.9,
98
+ use_sde=True,
99
+ policy_kwargs=dict(log_std_init=-2),
100
+ verbose=1,
101
+ )
102
+
103
+ # Train the agent
104
+ try:
105
+ model.learn(total_timesteps=int(1e6))
106
+ except KeyboardInterrupt:
107
+ pass
108
+
109
+
110
+ # Synchronize stats (done automatically in `EvalCallback`)
111
+ sync_envs_normalization(env, eval_env)
112
+
113
+ package_to_hub(
114
+ model=model,
115
+ model_name=f"a2c-{env_id}",
116
+ model_architecture="A2C",
117
+ env_id=env_id,
118
+ eval_env=eval_env,
119
+ repo_id=f"araffin/a2c-{env_id}",
120
+ commit_message="Initial commit",
121
+ )
122
+ ```
123