question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to save and load a trained model?

See original GitHub issue

Hi, after I learn a model I save it via

model.learn(total_timesteps=100000)
model.save("./data/PPO_MultiBinScaling.zip")

In another session I load it and apply the model on the same data it has been trained on (test=train) but receive completely different results.

model.load("./data/PPO_MultiBinScaling.zip")

Also I observed that the results differ between test (on train data) between trials.

Are there some strange seed somewhere, which make it inconvenient to reproduce the result after learning?

Thank you very much!

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
araffincommented, Jun 3, 2022

Yes, I check it.

However, I guess my problem was that the model.load does not change the object but rather returns the loaded model. I was assuming that the model object itself is changed. Using

model = PPO.load("./data/PPO_MultiBinScaling.zip", env=env)

worked.

yes, we have a warning about that in the “saving/loading” example in our doc: https://stable-baselines3.readthedocs.io/en/master/guide/examples.html#basic-usage-training-saving-loading

" load method re-creates the model from scratch and should be called on the Algorithm without instantiating it first, e.g. model = DQN.load(“dqn_lunar”, env=env) instead of model = DQN(env=env) followed by model.load(“dqn_lunar”). The latter will not work as load is not an in-place operation. If you want to load parameters without re-creating the model, e.g. to evaluate the same model with multiple different sets of parameters, consider using set_parameters instead. "

0reactions
ReHosscommented, Sep 7, 2022

I had exactly the same issue! Thanks for the answers!😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Save and load models | TensorFlow Core
Model progress can be saved during and after training. This means a model can resume where it left off and avoid long training...
Read more >
How to Save and Load Your Keras Deep Learning Model
You can save your model by calling the save() function on the model and specifying the filename. The example below demonstrates this by...
Read more >
Save and load models in Tensorflow - GeeksforGeeks
Save and load models in Tensorflow ; Code to create the model; The trained weights for the model ; Using the inbuilt function...
Read more >
Saving and Loading Models - PyTorch
When loading a model on a GPU that was trained and saved on CPU, set the map_location argument in the torch.load() function to...
Read more >
Save and Load a Model with TensorFlow's Keras API
Saving and loading the model in its entirety ... If we want to save a model at its current state after it was...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found