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

Save/load generated surrogate model #389

Open
jacktang opened this issue Jul 29, 2022 · 5 comments
Open

Save/load generated surrogate model #389

jacktang opened this issue Jul 29, 2022 · 5 comments

Comments

@jacktang
Copy link

Hi,

I want to save/load the surrogate model using JLD2 and BSON, and find the packages can only serialize objects rather than function. So if I load the saved model, it throws error:

UndefVarError: XGBoost not defined

Stacktrace:
  [1] (::BSON.var"#31#32")(m::Module, f::String)
    @ BSON ~/.julia/packages/BSON/rOaki/src/extensions.jl:21
  [2] BottomRF
    @ ./reduce.jl:81 [inlined]
  [3] _foldl_impl(op::Base.BottomRF{BSON.var"#31#32"}, init::Module, itr::Vector{Any})
    @ Base ./reduce.jl:58
  [4] foldl_impl
    @ ./reduce.jl:48 [inlined]
  [5] mapfoldl_impl
    @ ./reduce.jl:44 [inlined]
  [6] _mapreduce_dim
    @ ./reducedim.jl:327 [inlined]
  [7] #mapreduce#725
    @ ./reducedim.jl:322 [inlined]
  [8] #reduce#727
    @ ./reducedim.jl:371 [inlined]
  [9] resolve(fs::Vector{Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/rOaki/src/extensions.jl:21
 [10] (::BSON.var"#35#36")(d::Dict{Symbol, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/rOaki/src/extensions.jl:64
 [11] _raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/rOaki/src/read.jl:80

BTW: the question on julialang discourse: https://discourse.julialang.org/t/save-and-load-the-model-generated-by-surrogates-jl/83302

@vikram-s-narayan
Copy link
Contributor

Can you please share your full minimum working example so that we can examine it more carefully? Also, can you try with some other surrogate like Radial Basis?

@jacktang
Copy link
Author

@vikram-s-narayan
For example the random forrest surrogate model: https://surrogates.sciml.ai/dev/randomforest/

using Surrogates
using SurrogatesRandomForest
using Plots
using JLD2
default()

f(x) = sin(x) + sin(10/3 * x)
n_samples = 80
lower_bound = 2.7
upper_bound = 7.5
x = sample(n_samples, lower_bound, upper_bound, SobolSample())
y = f.(x)
scatter(x, y, label="Sampled points", xlims=(lower_bound, upper_bound))
plot!(f, label="True function", xlims=(lower_bound, upper_bound), legend=:top)

num_round = 100
randomforest_surrogate = RandomForestSurrogate(x ,y ,lower_bound, upper_bound, num_round = num_round)
plot(x, y, seriestype=:scatter, label="Sampled points", xlims=(lower_bound, upper_bound), legend=:top)
plot!(f, label="True function",  xlims=(lower_bound, upper_bound), legend=:top)
plot!(randomforest_surrogate, label="Surrogate function",  xlims=(lower_bound, upper_bound), legend=:top)

save_object("/tmp/rf_model.jld", randomforest_surrogate)
sm = load_object("/tmp/rf_model.jld")
sm.(3.5)

PolynomialChaos model works fine, so I think Radial basis should be ok too.

@vikram-s-narayan
Copy link
Contributor

vikram-s-narayan commented Jul 29, 2022

Thanks. With the code you provided the error reads - "ERROR: Call to XGBoost C function XGBoosterPredict failed: ". This appears to be a known issue with XGBoost. One of the package dependencies in SurrogatesRandomForest is XGBoost.

@jacktang
Copy link
Author

@vikram-s-narayan If I understand it correctly, the solution is replacing XGBoost by EvoTrees.jl in Surrogates.jl?

@vikram-s-narayan
Copy link
Contributor

@jacktang - I'm not sure if there are any other dependencies involved and whether the function calls need to be changed.

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