使用 Optim.jl 在 Julia 中进行优化 - 如何消除此错误?

Optimization in Julia with Optim.jl - How do I get rid of this error?

这里是新手

我正在尝试使用 optim.jl 最小化 Julia 中的函数。该函数有效,但是当我尝试对其进行优化时,它给出了以下错误消息:

MethodError: no method matching -(::Float64, ::Array{Float64,1})
For element-wise subtraction, use broadcasting with dot syntax: scalar .- array
Closest candidates are:
  -(::Float64, !Matched::Float64) at float.jl:403
  -(::Float64) at float.jl:393
  -(::Real, !Matched::Complex{Bool}) at complex.jl:302
  ...

Stacktrace:
 [1] _broadcast_getindex_evalf at ./broadcast.jl:648 [inlined]
 [2] _broadcast_getindex at ./broadcast.jl:621 [inlined]
 [3] getindex at ./broadcast.jl:575 [inlined]
 [4] copy at ./broadcast.jl:876 [inlined]
 [5] materialize at ./broadcast.jl:837 [inlined]
 [6] broadcast_preserving_zero_d at ./broadcast.jl:826 [inlined]
 [7] -(::Array{Float64,1}, ::Array{Array{Float64,1},1}) at ./arraymath.jl:39
 [8] objective(::Array{Float64,1}) at ./In[147]:4
 [9] value!!(::NonDifferentiable{Float64,Array{Float64,1}}, ::Array{Float64,1}) at /ext/julia/depot/packages/NLSolversBase/NsXIC/src/interface.jl:9
 [10] initial_state(::NelderMead{Optim.AffineSimplexer,Optim.AdaptiveParameters}, ::Optim.Options{Float64,Nothing}, ::NonDifferentiable{Float64,Array{Float64,1}}, ::Array{Float64,1}) at /ext/julia/depot/packages/Optim/TNmSw/src/multivariate/solvers/zeroth_order/nelder_mead.jl:158
 [11] optimize(::NonDifferentiable{Float64,Array{Float64,1}}, ::Array{Float64,1}, ::NelderMead{Optim.AffineSimplexer,Optim.AdaptiveParameters}, ::Optim.Options{Float64,Nothing}) at /ext/julia/depot/packages/Optim/TNmSw/src/multivariate/optimize/optimize.jl:33
 [12] optimize(::Function, ::Array{Float64,1}; inplace::Bool, autodiff::Symbol, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /ext/julia/depot/packages/Optim/TNmSw/src/multivariate/optimize/interface.jl:64
 [13] optimize(::Function, ::Array{Float64,1}) at /ext/julia/depot/packages/Optim/TNmSw/src/multivariate/optimize/interface.jl:58
 [14] top-level scope at In[148]:3
 [15] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

这是我的代码:

function objective(b)
    x = x1[:,2]
    b = fill(b,T)
    u = y - x.*b
    obj = sum(u.^2)
    return obj
end

using Optim

Sol = optimize(objective,[0.0;0.0])
b_optim = Optim.minimizer(Sol)

,其中 x 和 y 是 Array{Float64,1}

我不明白这条错误信息。如果我理解正确,它会告诉我我正在尝试从一个数字中减去一个数组,而我没有这样做?!广播也不行。

您可以通过以下方式重现您的错误:

julia> 4 - [2,5]
ERROR: MethodError: no method matching -(::Int64, ::Array{Int64,1})

这意味着您的 y 不是预期的矢量而是标量。

解决该问题的一种可能方法是广播 -:

julia> 4 .- [2,5]
2-element Array{Int64,1}:
  2
 -1

评论中已经提到的代码的另一个问题是全局变量。由于类型稳定性和性能,您永远不应该这样做。现在是时候阅读了:https://docs.julialang.org/en/v1/manual/performance-tips/

您应该向您的函数添加参数,以确保您正在传递正确的变量。那么你不应该使用 fill(b,T) 因为它无用地分配了一个新数组。此外,如果你有一个单变量问题,你应该只给求解器一个变量,并且如文档中所写(Optim.jl/stable/#user/minimization/)你应该使用另一个求解器,比如 LBFGS().

总结如下:

using Optim
function objective(b, x1, y)
    x = x1[:,2]
    u = y - x.*b
    obj = sum(u.^2)
    return obj
end

x1 = rand(10,10)
y = rand(10)
Sol = optimize(b -> objective(b,x1,y),[0.0], LBFGS())
b_optim = Optim.minimizer(Sol)