Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I save a trained model to reuse it? #69

Open
shivambhandari99 opened this issue May 10, 2021 · 3 comments
Open

How do I save a trained model to reuse it? #69

shivambhandari99 opened this issue May 10, 2021 · 3 comments

Comments

@shivambhandari99
Copy link

I specifically want to save the DQN network i've been working on. I went through the documentation and can't find anything. I tried using pickle for my agent after training but that doesn't seem to work either. I'd appreciate any input.

@eleurent
Copy link
Owner

eleurent commented May 10, 2021

During training, the agent model is saved through the save_agent_model function:

def save_agent_model(self, identifier, do_save=True):

There are two cases where this function is used to automatically save the model in after_some_episodes:

def after_some_episodes(self, episode, rewards,

  1. When the "episode is selected", which is decided by openai gym.wrappers.Monitor.is_episode_selected method. By default, gym uses the capped_cubic_video_schedule, which is a cubic progression for early episodes (1,8,27,...) and then every 1000 episodes (1000, 2000, 3000...) (This can be changed by modifying the video_callable argument of the Monitor).

https://github.com/openai/gym/blob/a5a6ae6bc0a5cfc0ff1ce9be723d59593c165022/gym/wrappers/monitor.py#L254

  1. When a new best performance is reached (averaged over a window of 50 episodes).

The resulting models are saved as .tar files, by default both in the run directory and in an out/env/agent/saved_models directory.

Then, in order to load a save model, you just have to use the --recover flag to load the latest model from the out/env/agent/saved_models directory, or --recover-from=<path> to pick a specific model.

@shivambhandari99
Copy link
Author

shivambhandari99 commented May 10, 2021

Thank you for your reply. What should I do to recover the agent to be used outside of evaluation or experiments? For example, I want to run this snippet locally:

for time in range(100):
    if(time==0):
        action = 1
    else:
        action = agent.act(obs)
    #print(DiscreteMetaAction.ACTIONS_ALL[action])
    obs, reward, done, info = env.step(action)
    env.render()
    # plt.imshow(env.render(mode="rgb_array"))
    # plt.show()
    if done:
        print(time)
        break

@eleurent
Copy link
Owner

eleurent commented May 11, 2021

Oh, I see! Then you can just use
agent.save(model_path)
and
agent.load(model_path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants