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 to save a trained model? #757

Closed
WuSiren opened this issue Jul 5, 2024 · 9 comments
Closed

How to save a trained model? #757

WuSiren opened this issue Jul 5, 2024 · 9 comments

Comments

@WuSiren
Copy link

WuSiren commented Jul 5, 2024

How to save a trained model? I've tried to save it with JLD2, but when I loaded the model, it told:

Error showing value of type JLD2.ReconstructedMutable{Symbol("PolicyGraph{Int64}"), (:objective_sense, :root_node, :root_children, :initial_root_state, :nodes, :belief_partition, :most_recent_training_results, :ext, :timer_output), Tuple{JLD2.ReconstructedPrimitive{:OptimizationSense, UInt32}, Int64, Vararg{Any, 7}}}:
ERROR: UndefRefError: access to undefined reference

So is there any proper method to saving a SDDP model?

@WuSiren
Copy link
Author

WuSiren commented Jul 9, 2024

I've tried to save a trained model as a StochOptFormat but I failed:

ERROR: StochOptFormat does not support writing after a call to `SDDP.train`.

But I need to save the trained model so that other people can reproduce the results I obtained easily. Could you please provide some advice? Thanks!

@Thuener
Copy link
Collaborator

Thuener commented Jul 12, 2024

You can try https://sddp.dev/stable/apireference/#SDDP.write_to_file, but it is still experimental.

What I have done was to use https://sddp.dev/stable/apireference/#SDDP.write_cuts_to_file and https://sddp.dev/stable/apireference/#SDDP.read_cuts_from_file.

I create a function to save all the parameters (save_parameters) that I use to create the subproblems into a .json file and then another load function to create the subproblems (load_parameters) using that .json file. Then, I create two functions: save_model, which calls save_parameters and write_cuts_to_file, and load_model, which calls load_parameters and read_cuts_from_file.

@odow
Copy link
Owner

odow commented Jul 13, 2024

You can not save a model to JLD2 because it will contain references to the underlying solver like Gurobi.

As @Thuener suggests, you should write cuts to a file.

Note that StochOptFormat is currently experimental.

My suggestion would be to share the script that rebuilds the model as Julia code, and also share the cuts .json file that you can read with read_cuts_from_file.

@WuSiren
Copy link
Author

WuSiren commented Jul 14, 2024

Thank you very much, @Thuener and @odow !

I think it may be necessary for me to get clear one thing that what essentially a trained decision rule is? Is it simply a parameterized function, or a new small optimization problem whose solution is the decision?

Please forgive me for not knowing it deeply enough! 🙏

@odow
Copy link
Owner

odow commented Jul 14, 2024

or a new small optimization problem whose solution is the decision

It's this

@WuSiren
Copy link
Author

WuSiren commented Jul 14, 2024

or a new small optimization problem whose solution is the decision

It's this

I see. Thanks. If I save the policy using SDDP.write_cuts_to_file(model, "myPolicy.json"), then how should someone else use it after he runs SDDP.read_cuts_from_file(model, "myPolicy.json"), for example, if he wants to perform in-sample or out-of-sample simulate, or just evaluate the decision rule for some given scenario? (I got it)

@WuSiren
Copy link
Author

WuSiren commented Jul 14, 2024

You can try https://sddp.dev/stable/apireference/#SDDP.write_to_file, but it is still experimental.

What I have done was to use https://sddp.dev/stable/apireference/#SDDP.write_cuts_to_file and https://sddp.dev/stable/apireference/#SDDP.read_cuts_from_file.

I create a function to save all the parameters (save_parameters) that I use to create the subproblems into a .json file and then another load function to create the subproblems (load_parameters) using that .json file. Then, I create two functions: save_model, which calls save_parameters and write_cuts_to_file, and load_model, which calls load_parameters and read_cuts_from_file.

Hi, @Thuener ! Is your method same with @odow 's suggestion?

My suggestion would be to share the script that rebuilds the model as Julia code, and also share the cuts .json file that you can read with read_cuts_from_file.

Could you please provide more details on how you save the parameters to a .json file? According to my understanding, your method can have reproducibility without providing the explicit model script to the user, is it?

@odow
Copy link
Owner

odow commented Jul 14, 2024

without providing the explicit model script to the user

No, you should still provide the script.

See, e.g., https://github.com/odow/SDDP.jl/blob/master/papers/policy_graph/paper.jl and https://github.com/odow/SDDP.jl/blob/master/papers/policy_graph/powder_data.json

@WuSiren
Copy link
Author

WuSiren commented Jul 15, 2024

Oh, I roughly understand how it works. Thank you very much Prof. @odow ! 🤝

@WuSiren WuSiren closed this as completed Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants