Ahmad Alismail
commited on
Commit
•
c3194f2
1
Parent(s):
65ff03f
Update README.md
Browse files
README.md
CHANGED
@@ -30,8 +30,37 @@ TODO: Add your code
|
|
30 |
|
31 |
|
32 |
```python
|
33 |
-
from
|
34 |
-
from
|
|
|
|
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
```
|
|
|
30 |
|
31 |
|
32 |
```python
|
33 |
+
from huggingface_hub import notebook_login
|
34 |
+
from stable_baselines3 import PPO
|
35 |
+
from stable_baselines3.common.evaluation import evaluate_policy
|
36 |
+
from stable_baselines3.common.env_util import make_vec_env
|
37 |
|
38 |
+
# Create the environment
|
39 |
+
env = make_vec_env('LunarLander-v2', n_envs=16)
|
40 |
+
|
41 |
+
model = PPO(
|
42 |
+
policy = 'MlpPolicy', # The policy to be optimized
|
43 |
+
env = env, # The environment in which the agent will act
|
44 |
+
n_steps = 2048, # The number of steps to run for each environment per update
|
45 |
+
batch_size = 64, # Minibatch size
|
46 |
+
n_epochs = 10, # Number of epoch when optimizing the surrogate loss
|
47 |
+
gamma = 0.999, # discount factor used to weigh future rewards in the total reward calculation
|
48 |
+
gae_lambda = 0.98, # parameter used in the Generalized Advantage Estimation (GAE) algorithm
|
49 |
+
ent_coef = 0.01, # Entropy coefficient for the loss calculation
|
50 |
+
verbose=0) # Verbosity level: 0 for no output, 1 for info messages (such as device or wrappers used), 2 for debug messages
|
51 |
+
|
52 |
+
# Train it for 1,500,000 timesteps
|
53 |
+
model.learn(total_timesteps=1500000, progress_bar=True)
|
54 |
+
# Specify file name for model and save the model to file
|
55 |
+
model_name = "ppo-LunarLander-v2"
|
56 |
+
model.save(model_name)
|
57 |
+
|
58 |
+
# Create a new environment for evaluation
|
59 |
+
eval_env = gym.make("LunarLander-v2")
|
60 |
+
|
61 |
+
# Evaluate the model with 10 evaluation episodes and deterministic=True
|
62 |
+
mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)
|
63 |
+
|
64 |
+
# Print the results
|
65 |
+
print(f"mean_reward={mean_reward:.2f} +/- {std_reward}")
|
66 |
```
|