Views
No views yet
eval_callback to save the best model. I had to run model twice, because the first time it trapped into local
minimal, which was quite bad.1import gym
2
3from huggingface_sb3 import load_from_hub, package_to_hub, push_to_hub
4from huggingface_hub import notebook_login # To log to our Hugging Face account to be able to upload models to the Hub.
5
6from stable_baselines3 import PPO, DQN
7from stable_baselines3.common.evaluation import evaluate_policy
8from stable_baselines3.common.env_util import make_vec_env
9
10params = {
11 'learning_rate': 0.0001599504838637104, 'buffer_size': 683593, 'batch_size': 128, 'train_freq': 14,
12 'exploration_final_eps': 0.07019679001836276, 'target_update_interval': 183, 'max_grad_norm': 0.314826407057672,
13 'learning_starts': 0,
14 'gradient_steps': -1,
15 'exploration_fraction': 0.2,
16 'gamma': 0.99,
17 'policy_kwargs': {
18 'net_arch': [256] * 2,
19 'activation_fn': torch.nn.ReLU
20 }
21}
22env = make_vec_env('LunarLander-v2', n_envs=16)
23model = DQN('MlpPolicy', env, **params, verbose=1)
24
25eval_env = gym.make('LunarLander-v2')
26eval_callback = EvalCallback(eval_env, n_eval_episodes=10, deterministic=True, best_model_save_path="./logs/best_model", eval_freq=1250)
27model.learn(total_timesteps=300_000, callback=eval_callback, progress_bar=True)
28model_name = "dqn-LunarLander-v2"
29model.save(model_name)
30model = model.load('logs/best_model/best_model')
31...