Hi i am a beginner with autodiff packages. In a test function, one input variable is a Dict
with an x
and a y
component. x
type is changed by running the function through ForwardDiff or ReverseDiff.
:x => [6, 2, 3]
:x => ForwardDiff.Dual{ForwardDiff.Tag{var"#165#166", Int64}, Int64, 3}[Dual{ForwardDiff.Tag{var"#165#166", Int64}}(2,1,0,0), Dual{ForwardDiff.Tag{var"#165#166", Int64}}(3,0,1,0),
[not finished]is this expected? Is there documentation on this?
1) function / to be differentiated
function dfweight(dict,x_inp)
dict[:x] = x_inp
df1 =DataFrame(x = [1,2,3], y = 4:6, z = 9)
aa= sum(df1.x .* dict[:x])
return sum(aa)
end
2) REPL log.
julia> xloc = [2; 3; 7]
3-element Vector{Int64}:
2
3
7
julia> dic=Dict(:x =>[6;2;3], :y=>0.5)
Dict{Symbol, Any} with 2 entries:
:y => 0.5
:x => [6, 2, 3]
julia> dfweight(dic,xloc) # --> 29
29
julia> ForwardDiff.gradient(x -> dfweight(dic, x),xloc)
3-element Vector{Int64}:
1
2
3
julia> dic
Dict{Symbol, Any} with 2 entries:
:y => 0.5
:x => ForwardDiff.Dual{ForwardDiff.Tag{var"#165#166", Int64}, Int64, 3}[Dual{ForwardD…
Yes this is expected. Passing numbers of a weird type through your function is precisely how ForwardDiff works:
julia> id(x) = @show x;
julia> ForwardDiff.derivative(id, 5)
x = Dual{ForwardDiff.Tag{typeof(id), Int64}}(5,1)
1
julia> ForwardDiff.gradient(xs -> sum(abs2 ∘ id, xs), [7,11,13])
x = Dual{ForwardDiff.Tag{var"#9#10", Int64}}(7,1,0,0)
x = Dual{ForwardDiff.Tag{var"#9#10", Int64}}(11,0,1,0)
x = Dual{ForwardDiff.Tag{var"#9#10", Int64}}(13,0,0,1)
3-element Vector{Int64}:
14
22
26
There's a bit of description here: https://juliadiff.org/ForwardDiff.jl/latest/dev/how_it_works.html . There's a longer description here: https://github.com/MikeInnes/diff-zoo
see also this discussion on zulip: https://julialang.zulipchat.com/#narrow/stream/225542-helpdesk/topic/Comparing.20julia.20and.20numpy/near/209135246
Thank you Michael, j-fu!
Last updated: Nov 22 2024 at 04:41 UTC