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

varmap_to_vars fails when used inside of loss function for optimization. #1692

Closed
mzhen77 opened this issue Jul 13, 2022 · 5 comments · Fixed by #1698
Closed

varmap_to_vars fails when used inside of loss function for optimization. #1692

mzhen77 opened this issue Jul 13, 2022 · 5 comments · Fixed by #1698

Comments

@mzhen77
Copy link

mzhen77 commented Jul 13, 2022

If I want to guarantee the order of parameters during the optimization I need to use varmap_to_vars, e.g:

loss function:

function loss(p::Vector{Float64})

    p1 = [param_vars[1] => p[1], param_vars[2] => p[2], param_vars[3] => p[3]]
    p2 = ModelingToolkit.varmap_to_vars(p1, param_vars)

    pred_sol = solve(prob, Tsit5(), p=p2, saveat=t_save)
    pred = Array(pred_sol')
    mse = mean((pred - obs_vals) .^ 2)
    return mse
end

where param_vars are the parameters of the MTK model.

The error I get:

ERROR: Compiling Tuple{Type{Dict}, Vector{Pair{Sym{Real, Base.ImmutableDict{DataType, Any}}, Float64}}}: try/catch is not supported. Refer to the Zygote documentation for fixes.

The full code description is here

@ChrisRackauckas
Copy link
Member

If you do idxs = ModelingToolkit.varmap_to_vars(1:3, param_vars), then idxs should be the index map for how to permute p. So then inside of your loss function, you can use `

function loss(p::Vector{Float64})
    pred_sol = solve(prob, Tsit5(), p=p[idxs], saveat=t_save)
    pred = Array(pred_sol')
    mse = mean((pred - obs_vals) .^ 2)
    return mse
end

with idxs being constant. Or just use indexof to do this.

If this is a good workaround, then we should document this in the FAQ.

@mzhen77
Copy link
Author

mzhen77 commented Jul 14, 2022

@ChrisRackauckas Thank you for your response.

But I can't understand it...

If I run the command as you adviced: idxs = ModelingToolkit.varmap_to_vars(1:3, param_vars) I get the error:
ERROR: MethodError: no method matching create_array(::Type{UnitRange{Int64}}, ::Type{Float64}, ::Val{1}, ::Val{3}, ::Float64, ::Float64, ::Float64)

If you mean something like that:

idxs = ModelingToolkit.varmap_to_vars([1,2,3], param_vars)
then the result will be [1,2,3] and doesn't depend on param_vars, so how can it help?

@ChrisRackauckas
Copy link
Member

I just meant generate an index map.

using ModelingToolkit
p = @parameters x y z
idxs = ModelingToolkit.varmap_to_vars([p[1] => 1, p[2] => 2, p[3] => 3], p)
p[Int.(idxs)]

@mzhen77
Copy link
Author

mzhen77 commented Jul 18, 2022

Thank you, @ChrisRackauckas.
It works. It is a good idea to document this in the FAQ.

@lamorton
Copy link
Contributor

lamorton commented Sep 12, 2022

Edit: I'm dumb, I was just feeding it the actual defaults instead of the indexes.

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

Successfully merging a pull request may close this issue.

3 participants