Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typeerror when computing the Hessian #148

Closed
YichengDWu opened this issue Jul 17, 2022 · 2 comments
Closed

Typeerror when computing the Hessian #148

YichengDWu opened this issue Jul 17, 2022 · 2 comments

Comments

@YichengDWu
Copy link
Contributor

YichengDWu commented Jul 17, 2022

julia> using ComponentArrays, Zygote

julia> ps = (;bias = rand(4))
(bias = [0.028680052413891932, 0.028488060933192783, 0.14102421410834098, 0.5148641146388963],)

julia> Zygote.gradient(x -> sum(x.^3 .+ ps.bias), Zygote.seed(rand(4),Val(12)))[1]
4-element Vector{ForwardDiff.Dual{Nothing, Float64, 12}}:
 Dual{Nothing}(1.9044946542432517,4.78057902883312,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
 Dual{Nothing}(0.9007160656424562,0.0,3.2876424361097234,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
 Dual{Nothing}(1.443553597479418,0.0,0.0,4.162047953802673,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
 Dual{Nothing}(0.854126057634067,0.0,0.0,0.0,3.201486013027201,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)

julia> ps = ComponentArray(;bias = rand(4))
ComponentVector{Float64}(bias = [0.8753842481152277, 0.816098534734367, 0.5547681434517868, 0.7963916275223])

julia> Zygote.gradient(x -> sum(x.^3 .+ ps), Zygote.seed(rand(4),Val(12)))[1]
4-element Vector{ForwardDiff.Dual{Nothing, Float64, 12}}:
 Dual{Nothing}(0.44589405810839866,2.313164217538561,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
 Dual{Nothing}(0.3204937948764665,0.0,1.9611031432634027,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
 Dual{Nothing}(1.2913481890316207,0.0,0.0,3.936518546683027,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
 Dual{Nothing}(0.43271702177241456,0.0,0.0,0.0,2.27872865020585,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)

julia> gradient(x -> sum(x.^3 .+ getdata(ps)), Zygote.seed(rand(4),Val(12)))[1]
4-element Vector{ForwardDiff.Dual{Nothing, Float64, 12}}:
 Dual{Nothing}(2.9040980928805133,5.903319160825219,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
 Dual{Nothing}(0.12702647408947199,0.0,1.234632612996135,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
 Dual{Nothing}(1.7155041799425579,0.0,0.0,4.537185268347623,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
 Dual{Nothing}(0.5602367500231364,0.0,0.0,0.0,2.592844191284474,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)

julia> Zygote.gradient(x -> sum(x.^3 .+ ps.bias), Zygote.seed(rand(4),Val(12)))[1]
ERROR: TypeError: in typeassert, expected Float64, got a value of type ForwardDiff.Dual{Nothing, Float64, 12}
Stacktrace:
  [1] setindex!(A::Vector{Float64}, x::ForwardDiff.Dual{Nothing, Float64, 12}, i1::Int64)
    @ Base .\array.jl:903
  [2] setindex!
    @ .\array.jl:917 [inlined]
  [3] macro expansion
    @ C:\Users\Luffy\.julia\packages\ComponentArrays\IQNQW\src\array_interface.jl:0 [inlined]
  [4] _setindex!(x::ComponentVector{Float64}, v::Vector{ForwardDiff.Dual{Nothing, Float64, 12}}, idx::Val{:bias})
    @ ComponentArrays C:\Users\Luffy\.julia\packages\ComponentArrays\IQNQW\src\array_interface.jl:129
  [5] setproperty!
    @ C:\Users\Luffy\.julia\packages\ComponentArrays\IQNQW\src\namedtuple_interface.jl:17 [inlined]
  [6] (::ComponentArrays.var"#getproperty_adjoint#88"{ComponentVector{Float64, Vector{Float64}, Tuple{Axis{(bias = 1:4,)}}}, Symbol})(Δ::Vector{ForwardDiff.Dual{Nothing, Float64, 12}})
    @ ComponentArrays C:\Users\Luffy\.julia\packages\ComponentArrays\IQNQW\src\compat\chainrulescore.jl:4
  [7] ZBack
    @ C:\Users\Luffy\.julia\packages\Zygote\IoW2g\src\compiler\chainrules.jl:205 [inlined]
  [8] Pullback
    @ .\REPL[271]:1 [inlined]
  [9] (::typeof((#509)))(Δ::ForwardDiff.Dual{Nothing, Float64, 12})
    @ Zygote C:\Users\Luffy\.julia\packages\Zygote\IoW2g\src\compiler\interface2.jl:0
 [10] (::Zygote.var"#60#61"{typeof((#509))})(Δ::ForwardDiff.Dual{Nothing, Float64, 12})
    @ Zygote C:\Users\Luffy\.julia\packages\Zygote\IoW2g\src\compiler\interface.jl:41
 [11] gradient(f::Function, args::Vector{ForwardDiff.Dual{Nothing, Float64, 12}})
    @ Zygote C:\Users\Luffy\.julia\packages\Zygote\IoW2g\src\compiler\interface.jl:76
 [12] top-level scope
    @ REPL[271]:1
@jonniedie
Copy link
Collaborator

Interesting, looks like we need to promote the eltype before the setproperty!

@YichengDWu
Copy link
Contributor Author

It's fixed! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants