Why do I get a bivariate gradient function when I optimize the objective function?How can I solve it?
using Zygote
using Optim
using RecursiveArrayTools
#This is the target function that I’m optimizing
function get_f(A)
M=reshape(1:16,4,4)
U=exp(A)
T=UMU’
return tr(T)
end
function my_function()
g(A)=gradient(A->get_f(A), A)[1] # This is my gradient function
A0=zeros(4,4)
results=optimize(get_f,g,VectorOfArray([A0]), LBFGS())
end
The error message reads:
MethodError: no method matching (::var"#get_g#4")(::VectorOfArray{Float64,3,Array{Array{Float64,2},1}}, ::VectorOfArray{Float64,3,Array{Array{Float64,2},1}})
Closest candidates are:
get_g(::Any) at In[3]:2
…
Stacktrace:
[1] (::NLSolversBase.var"#fg!#8"{typeof(get_f),var"#get_g#4"})(::VectorOfArray{Float64,3,Array{Array{Float64,2},1}}, ::VectorOfArray{Float64,3,Array{Array{Float64,2},1}}) at C:\Users\38606.julia\packages\NLSolversBase\NsXIC\src\objective_types\abstract.jl:13
[2] value_gradient!!(::OnceDifferentiable{Float64,VectorOfArray{Float64,3,Array{Array{Float64,2},1}},VectorOfArray{Float64,3,Array{Array{Float64,2},1}}}, ::VectorOfArray{Float64,3,Array{Array{Float64,2},1}}) at C:\Users\38606.julia\packages\NLSolversBase\NsXIC\src\interface.jl:82
[3]initial_state(::LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},Optim.var"#15#17"}, ::Optim.Options{Float64,Nothing}, ::OnceDifferentiable{Float64,VectorOfArray{Float64,3,Array{Array{Float64,2},1}},VectorOfArray{Float64,3,Array{Array{Float64,2},1}}}, ::VectorOfArray{Float64,3,Array{Array{Float64,2},1}}) at C:\Users\38606.julia\packages\Optim\TNmSw\src\multivariate\solvers\first_order\l_bfgs.jl:158
[4]optimize(::OnceDifferentiable{Float64,VectorOfArray{Float64,3,Array{Array{Float64,2},1}},VectorOfArray{Float64,3,Array{Array{Float64,2},1}}}, ::VectorOfArray{Float64,3,Array{Array{Float64,2},1}}, ::LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},Optim.var"#15#17"}, ::Optim.Options{Float64,Nothing}) at C:\Users\38606.julia\packages\Optim\TNmSw\src\multivariate\optimize\optimize.jl:33
[5] #optimize#91 at C:\Users\38606.julia\packages\Optim\TNmSw\src\multivariate\optimize\interface.jl:129 [inlined]
[6] optimize(::Function, ::Function, ::VectorOfArray{Float64,3,Array{Array{Float64,2},1}}, ::LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},Optim.var"#15#17"}, ::Optim.Options{Float64,Nothing}) at C:\Users\38606.julia\packages\Optim\TNmSw\src\multivariate\optimize\interface.jl:127 (repeats 2 times)
[7] my_function() at .\In[3]:4
[8] top-level scope at In[4]:1
[9] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091