You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sorry for bothering you again Chris. I really want to use ModelingToolKit in package I am developing. The idea of it seems to fit perfectly with my use case, but I am running into a couple bugs. I'll make two different Issues so they can be handled separately.
When I create a ODEProblem from an ODESystem, rather than from a function, initial condition, and time span, then try to use AutoDiff (through ForwardDiff.jl), I am getting all zeros for the gradient (and hessian), rather than seeing something true. I updated to the most recent of ModelingToolKit to v9.24.0 to make sure that was not a factor.
Here contained example that illustrates the issue:
@info"Loading external dependencies"using OrdinaryDiffEq, ModelingToolkit
using ModelingToolkit: D_nounits, t_nounits as t
using DiffEqParamEstim, Optimization, OptimizationOptimJL
using Statistics, Random
@info"Build test problem"## Call from ODEProblem Directlyfunctionf(du, u, w, t)
du[1] = w[1] * u[2] + w[2] * u[1]^3+ w[3] * u[1]^2+ w[4] * u[3]
du[2] = w[5] + w[6] * u[1]^2+ w[7] * u[2]
du[3] = w[8] * u[1] + w[9] + w[10] * u[3]
end
u0= [-1.31; -7.6; -0.2]
tspan = (0.0, 10.0)
wTrue = [10,-10,30,-10,10,-50,-10,0.04,0.0319,-0.01]
D =length(u0)
J =length(wTrue)
M =1024
σ =0.1# snr for noise to data
μ =0.1# snr for initCond
Random.seed!(1)
w0 = wTrue + μ .*abs.(wTrue) .*rand(J)
opt = OptimizationOptimJL.NewtonTrustRegion();
##
prob =ODEProblem(f, u0, tspan, wTrue)
sol =solve(prob, Rosenbrock23())
tt =collect(range(tspan[1], stop = tspan[end], length = M))
U_exact =reduce(hcat, sol(tt[i]) for i in1:M)
U = U_exact + σ*sqrt(mean(U_exact.^2))*rand(D,M);
##
obj =build_loss_objective(prob, Rosenbrock23(), L2Loss(tt, U), Optimization.AutoForwardDiff())
optprob = Optimization.OptimizationProblem(obj, w0);
##@info"Solving ODE param estimation problem with default ODEProb construction"
res =solve(optprob, opt, show_trace=true, show_every=100);
## try to use ModelingToolkit## See Wendy paper@mtkmodel HindmarshRoseModel begin@variablesbeginu1(t) =-1.31u2(t) =-7.6u3(t) =-0.2end@parametersbegin
w1 =10
w2 =-10
w3 =30
w4 =-10
w5 =10
w6 =-50
w7 =-10
w8 =0.04
w9 =0.0319
w10=-0.01end@equationsbeginD_nounits(u1) ~ w1 * u2 + w2 * u1^3+ w3 * u1^2+ w4 * u3
D_nounits(u2) ~ w5 + w6 * u1^2+ w7 * u2
D_nounits(u3) ~ w8 *u1 + w9 + w10 * u3
endend@mtkbuild HINDMARSH_ROSE_SYSTEM =HindmarshRoseModel()
mtk_prob =ODEProblem(
HINDMARSH_ROSE_SYSTEM,
ModelingToolkit.getdefault.(unknowns(HINDMARSH_ROSE_SYSTEM)),
tspan,
ModelingToolkit.getdefault.(parameters(HINDMARSH_ROSE_SYSTEM))
);
##
mtk_obj =build_loss_objective(mtk_prob, Rosenbrock23(), L2Loss(tt, U), Optimization.AutoForwardDiff())
mtk_optprob = Optimization.OptimizationProblem(mtk_obj, w0);
##@info"Solving ODE param estimation problem with default MTK construction"
res =solve(mtk_optprob, opt, show_trace=true, show_every=100);
[ Info: Loading external dependencies
[ Info: Build test problem
[ Info: Solving ODE param estimation problem with default ODEProb construction
Iter Function value Gradient norm
01.312220e+041.305949e+05* time:0.00011086463928222656*g(x): [-11835.392502819925, -9691.874869015533, 156.1049423257333, -663.0827228604223, 4414.979301648392, 3401.8983455768353, -16562.305669189358, 69040.26000225743, -130594.92982876718, 22732.5495097705]
* reached_subproblem_solution:true
* h(x): [-62845.94819434995 -97384.48440634544 -16156.682787035135 -3302.967251268705 33588.53008405125 22425.22567804772 -119028.39406901498 992829.1622789137 -2.061453836668006e6 324160.5471088927; -97384.48440634586 -120374.86852514069 -13103.651990135877 -5530.567493433912 45842.81050147195 31672.0700236485 -163122.6526739951 1.1653614367854204e6 -2.373263733398785e6 377015.87573420757; -16156.68278703505 -13103.651990136053 524.5852754025087 -1009.3018417674406 6380.571877247003 4705.357018856951 -22804.799125601785 113023.25181513408 -214290.95594199694 35299.40014421789; -3302.9672512683173 -5530.567493433798 -1009.3018417674479 -173.67182381047246 1896.2266218368597 1211.5760467176715 -6515.854304714365 51269.5393485764 -109817.9273281088 16916.77013686628; 33588.53008404843 45842.810501475775 6380.571877247027 1896.2266218369043 -17101.481071005666 -11314.851881762375 59577.716386724955 -460222.10215340357 954277.8226441568 -150437.95908093377; 22425.22567804585 31672.070023649158 4705.357018856973 1211.576046717452 -11314.851881762303 -7561.185197110738 40193.457567402365 -318409.4406011705 659266.1723043135 -103908.85994089546; -119028.39406900956 -163122.65267399285 -22804.799125602 -6515.85430471422 59577.71638672538 40193.45756739892 -211596.17710204114 1.6263770592364336e6 -3.3557797033448154e6 529773.325127917; 992829.1622788592 1.1653614367854064e6 113023.25181513846 51269.53934857463 -460222.10215342394 -318409.44060118176 1.626377059236409e6 -1.0725105955977047e7 2.179154503796496e7 -3.2896352567722457e6; -2.0614538366680928e6 -2.3732637333989474e6 -214290.95594200576 -109817.9273281094 954277.822644195 659266.1723043174 -3.3557797033447195e6 2.1791545037964992e7 -4.410993875327773e7 6.721912218517469e6; 324160.54710888927 377015.8757341983 35299.40014421667 16916.770136865394 -150437.95908093167 -103908.85994088984 529773.3251279388 -3.2896352567724423e6 6.721912218517537e6 -1.0059845601990005e6]
* x: [10.049171822148121, -9.880921183592493, 31.17981306967584, -9.975905689475471, 10.691857287534221, -46.162409729563045, -9.912746951087257, 0.043422870736438295, 0.034460168664655934, -0.009338574648315232]
* lambda:NaN* interior:true* hard case:false* delta:1.0
[ Info: Solving ODE param estimation problem with default MTK construction
┌ Warning: At t=0.01483221772898653, dt was forced below floating point epsilon 1.734723475976807e-18, and step error estimate = 1.8687259587471354. Aborting. There is either an error in your model specification or the true solution is unstable (or the true solution can not be represented in the precision of ForwardDiff.Dual{ForwardDiff.Tag{OptimizationForwardDiffExt.var"#37#55"{OptimizationFunction{true, AutoForwardDiff{nothing, Nothing}, DiffEqParamEstim.var"#29#30"{Nothing, typeof(DiffEqParamEstim.STANDARD_PROB_GENERATOR), Base.Pairs{Symbol, Union{}, Tuple{}, @NamedTuple{}}, ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, ModelingToolkit.MTKParameters{Tuple{Vector{Float64}}, Tuple{}, Tuple{}, Tuple{}, Tuple{}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x9c845606, 0xf2c37616, 0x9c88dfd9, 0xf11cefcd, 0x25a942b6), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1,), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe76332ee, 0x2e783b89, 0x43ba34c1, 0x38322bf4, 0x12861fd3), Nothing}}, ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#717"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe6a61356, 0x434e4e39, 0x499c058e, 0x64876f67, 0xadde982b), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xcc0ab0a6, 0x935170f1, 0x1d2f9bbf, 0x7646761d, 0xa0789215), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, @NamedTuple{}}, SciMLBase.StandardODEProblem}, Rosenbrock23{0, true, Nothing, typeof(OrdinaryDiffEq.DEFAULT_PRECS), Val{:forward}, true, nothing, typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!)}, L2Loss{Vector{Float64}, Matrix{Float64}, Nothing, Nothing, Nothing}, Nothing, Tuple{}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, OptimizationBase.ReInitCache{Vector{Float64}, SciMLBase.NullParameters}}, Float64}, Float64, 10}).
└ @ SciMLBase ~/.julia/packages/SciMLBase/rR75x/src/integrator_interface.jl:600
┌ Warning: At t=0.014818212783429985, dt was forced below floating point epsilon 1.734723475976807e-18, and step error estimate =53.2934187330348. Aborting. There is either an error in your model specification or the true solution is unstable (or the true solution can not be represented in the precision of Float64).
└ @ SciMLBase ~/.julia/packages/SciMLBase/rR75x/src/integrator_interface.jl:600
┌ Warning: At t=0.014850966833559073, dt was forced below floating point epsilon 1.734723475976807e-18, and step error estimate = 1.7472423420602798. Aborting. There is either an error in your model specification or the true solution is unstable (or the true solution can not be represented in the precision of ForwardDiff.Dual{ForwardDiff.Tag{OptimizationForwardDiffExt.var"#37#55"{OptimizationFunction{true, AutoForwardDiff{nothing, Nothing}, DiffEqParamEstim.var"#29#30"{Nothing, typeof(DiffEqParamEstim.STANDARD_PROB_GENERATOR), Base.Pairs{Symbol, Union{}, Tuple{}, @NamedTuple{}}, ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, ModelingToolkit.MTKParameters{Tuple{Vector{Float64}}, Tuple{}, Tuple{}, Tuple{}, Tuple{}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x9c845606, 0xf2c37616, 0x9c88dfd9, 0xf11cefcd, 0x25a942b6), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1,), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe76332ee, 0x2e783b89, 0x43ba34c1, 0x38322bf4, 0x12861fd3), Nothing}}, ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#717"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe6a61356, 0x434e4e39, 0x499c058e, 0x64876f67, 0xadde982b), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xcc0ab0a6, 0x935170f1, 0x1d2f9bbf, 0x7646761d, 0xa0789215), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, @NamedTuple{}}, SciMLBase.StandardODEProblem}, Rosenbrock23{0, true, Nothing, typeof(OrdinaryDiffEq.DEFAULT_PRECS), Val{:forward}, true, nothing, typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!)}, L2Loss{Vector{Float64}, Matrix{Float64}, Nothing, Nothing, Nothing}, Nothing, Tuple{}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, OptimizationBase.ReInitCache{Vector{Float64}, SciMLBase.NullParameters}}, Float64}, ForwardDiff.Dual{ForwardDiff.Tag{OptimizationForwardDiffExt.var"#37#55"{OptimizationFunction{true, AutoForwardDiff{nothing, Nothing}, DiffEqParamEstim.var"#29#30"{Nothing, typeof(DiffEqParamEstim.STANDARD_PROB_GENERATOR), Base.Pairs{Symbol, Union{}, Tuple{}, @NamedTuple{}}, ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, ModelingToolkit.MTKParameters{Tuple{Vector{Float64}}, Tuple{}, Tuple{}, Tuple{}, Tuple{}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x9c845606, 0xf2c37616, 0x9c88dfd9, 0xf11cefcd, 0x25a942b6), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1,), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe76332ee, 0x2e783b89, 0x43ba34c1, 0x38322bf4, 0x12861fd3), Nothing}}, ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#717"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe6a61356, 0x434e4e39, 0x499c058e, 0x64876f67, 0xadde982b), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xcc0ab0a6, 0x935170f1, 0x1d2f9bbf, 0x7646761d, 0xa0789215), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, @NamedTuple{}}, SciMLBase.StandardODEProblem}, Rosenbrock23{0, true, Nothing, typeof(OrdinaryDiffEq.DEFAULT_PRECS), Val{:forward}, true, nothing, typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!)}, L2Loss{Vector{Float64}, Matrix{Float64}, Nothing, Nothing, Nothing}, Nothing, Tuple{}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, OptimizationBase.ReInitCache{Vector{Float64}, SciMLBase.NullParameters}}, Float64}, Float64, 10}, 10}).
└ @ SciMLBase ~/.julia/packages/SciMLBase/rR75x/src/integrator_interface.jl:600
Iter Function value Gradient norm
0Inf0.000000e+00* time:5.698204040527344e-5*g(x): [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
* reached_subproblem_solution:true*h(x): [0.00.00.00.00.00.00.00.00.00.0; 0.00.00.00.00.00.00.00.00.00.0; 0.00.00.00.00.00.00.00.00.00.0; 0.00.00.00.00.00.00.00.00.00.0; 0.00.00.00.00.00.00.00.00.00.0; 0.00.00.00.00.00.00.00.00.00.0; 0.00.00.00.00.00.00.00.00.00.0; 0.00.00.00.00.00.00.00.00.00.0; 0.00.00.00.00.00.00.00.00.00.0; 0.00.00.00.00.00.00.00.00.00.0]
* x: [10.049171822148121, -9.880921183592493, 31.17981306967584, -9.975905689475471, 10.691857287534221, -46.162409729563045, -9.912746951087257, 0.043422870736438295, 0.034460168664655934, -0.009338574648315232]
* lambda:NaN* interior:true* hard case:false* delta:1.0
Environment (please complete the following information):
Sorry for bothering you again Chris. I really want to use ModelingToolKit in package I am developing. The idea of it seems to fit perfectly with my use case, but I am running into a couple bugs. I'll make two different Issues so they can be handled separately.
When I create a ODEProblem from an ODESystem, rather than from a function, initial condition, and time span, then try to use AutoDiff (through ForwardDiff.jl), I am getting all zeros for the gradient (and hessian), rather than seeing something true. I updated to the most recent of ModelingToolKit to
v9.24.0
to make sure that was not a factor.Here contained example that illustrates the issue:
Environment (please complete the following information):
using Pkg; Pkg.status()
using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
versioninfo()
The text was updated successfully, but these errors were encountered: