Julia 优化仅针对一个变量的值?

问题描述 投票:0回答:1

所以我正在使用 Julia 进行优化,到目前为止我可以使用以下代码来求解向量常微分方程系统。 (括号“ODE 模型”下的任何内容都不相关,因为它只是我的物理模型,相反,我重点关注括号“合成数据”和“成本函数+优化”)

使用微分方程、DiffEqParamEstim、RecursiveArrayTools 使用 Optim、绘图

###############################################
# ODE Model
###############################################
function f(du,u,p,t)
    du[1] = dc_n = p[1]*u[1] -u[1]*u[2]                     
    du[2] = dc_d = -p[2]*u[2]^2 + u[1]*u[2]
end

u0=[1.0,1.0]
tspan = (0.0,10.0)
p = [1.5,2.0,1.2]      

prob = ODEProblem(f,u0,tspan,p)
#Lösung ODE Problem
sol = solve(prob,Tsit5())
plot(sol)

###############################################
# Synthetic Data
###############################################
t = collect(range(0,stop=10,length=30))
streu = VectorOfArray([(sol(t[i]) + .01randn(2))
            for i in 1:length(t)]) 
data = convert(Array,streu) 

###############################################
# loss function and Optimization
###############################################

cost_function = build_loss_objective(prob, Tsit5(), L2Loss(t,data),maxiters=10000,verbose=false)

initialGuess = ones(3)
result = optimize(cost_function, initialGuess, BFGS()) 

newprob = remake(prob, p =result.minimizer)
print(result.minimizer)     
newsol = solve(newprob,Tsit5())    
plot!(newsol)

所以现在在“合成数据”下,而不是用“数据”(2x30Matrix)给出 u[1] 和 u\ 的成本函数数据,我只想给它 u[2] 的数据并让它运行优化就这样。我需要如何实现它才能成功估计参数?

提前致谢。

julia
1个回答
0
投票

设置

save_idxs
,使求解器仅输出第二个索引。即:

sol = solve(prob,Tsit5(), save_idxs = 2)

在数据生成器中,以及在参数估计损失中:

cost_function = build_loss_objective(prob, Tsit5(), L2Loss(t,data),maxiters=10000,verbose=false, save_idxs = 2)
© www.soinside.com 2019 - 2024. All rights reserved.