Try using eltype()
to debug. This often how I catch errors when I’m using ForwardDiff in my code. It’s likely that one of the many parameters might be having its type forced into something like an array of floats.
Same code using eltype()
below. Notice that special ForwardDiff.Dual type being created. This is why you want to avoid forcing the type of a parameter to be Float64 because Julia can’t convert ForwardDiff.Dual type to a Float64, hence the "“cannot convert ForwardDiff.Dual to Float64 error.”
If you force the type of your parameter to be an array of floats, your code will try to promote ForwardDiff.Dual type to a Float64 and will fail.
function f_new(meta::Array{T}, init::Array{F}) where {T<:Real, F<:Float64}
params = Array{T}(undef, size(init)[1], 1)
println(eltype(params))
for i=1:5
params[i] = init[i]
end
println(eltype(params))
for i=2:5
params[i] = meta[1] * params[i-1]
end
println(eltype(params))
return sum(params)
end
s = [1.,2.,3.,4.,5.]
ForwardDiff.gradient(x -> f_new(x, s), [2.0])
julia> ForwardDiff.gradient(x -> f_new(x, s), [2.0])
ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##73#74")),Float64},Float64,1}
ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##73#74")),Float64},Float64,1}
ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##73#74")),Float64},Float64,1}
1-element Array{Float64,1}:
49.0