DLR-RM / stable-baselines3

PyTorch version of Stable Baselines, reliable implementations of reinforcement learning algorithms.
https://stable-baselines3.readthedocs.io
MIT License
9.24k stars 1.71k forks source link

[Bug]: DQN resets exploration rate from saved model #1629

Closed AMR-aa1405465 closed 1 year ago

AMR-aa1405465 commented 1 year ago

🐛 Bug

Hello everyone, I appreciate your work. I have a little bit embarrassing problem =|

I have recently encountered an issue while attempting to train, save, and reload my DQN model within a Gymnasium environment with only one environment. The problem lies in the knowledge transfer from the saved to the loaded model. During the initial training phase on (CartPole-v1), I successfully reached a reward of 200 after 100 K timesteps.

However, when I reload the model for further training, I expect to start with a reward of around 200 and retain the hyperparameters set during the initial training (e.g., exploration rate). Unfortunately, this is not happening as expected.

In my case, the learning seems to start from the beginning as I see small rewards only, in addition, the exploration rate gets back to 1, not 0.05 as before.

Unlike DQN, I have tried this feature before on PPO and was working fine.

To fix this issue, I tried the following without effect:

  1. Saving/reloading the replay buffer.
  2. Manually setting the exploration rate on the loaded model to 0.05.
  3. I set the env (i.e., model.set_env()) with dummyVecEnv and normal env.
  4. Used model.set_parameters() instead of model.load()
  5. I saw the different issues about knowledge transfer that people encountered and applied their fixes when applicable (such as https://github.com/DLR-RM/stable-baselines3/issues/29, https://github.com/hill-a/stable-baselines/issues/30, https://github.com/DLR-RM/stable-baselines3/issues/70)

To Reproduce

import gymnasium as gym

from stable_baselines3 import DQN
from stable_baselines3.common.monitor import Monitor
from stable_baselines3.common.vec_env import DummyVecEnv

env = gym.make("CartPole-v1")

model = DQN("MlpPolicy", env, verbose=1)
model.learn(total_timesteps=200000, log_interval=4)
model.save("dqn_cartpole")
model.save_replay_buffer("dqn_cartpole-buffer")

del model # remove to demonstrate saving and loading
# Here, I observe the last trend of rewards achieved by the agent

model = DQN.load("dqn_cartpole")
k = Monitor(gym.make("CartPole-v1"))
model.set_env(DummyVecEnv([lambda : k]))
model.load_replay_buffer("dqn_cartpole-buffer")
model.learn(total_timesteps=100000, log_interval=4)
# Here, please observe the rewards and the exploration rate of the agent, resets to 1 and rewards drops

Relevant log output / Error message

No response

System Info

Checklist

araffin commented 1 year ago

Hello, you are probably missing the reset_num_timesteps=False parameter (see doc).

Also related, why changing the exploration rate alone doesn't work (you need to change the schedule): https://github.com/DLR-RM/stable-baselines3/issues/735#issuecomment-1047638011

Also related: https://github.com/DLR-RM/stable-baselines3/issues/529

araffin commented 1 year ago

Looks like a duplicate of https://github.com/DLR-RM/stable-baselines3/issues/597#issuecomment-937207471 also related (for running multiple times learn()): https://github.com/DLR-RM/stable-baselines3/issues/957

AMR-aa1405465 commented 1 year ago

Yup, reset_num_timesteps=False did the trick =D Thanks for the help mate