JuliaDiff / ReverseDiff.jl

Reverse Mode Automatic Differentiation for Julia
Other
348 stars 57 forks source link

`MethodError: *(::Diagonal, ::ReverseDiff.TrackedArray)` is ambiguous. #223

Open tmigot opened 1 year ago

tmigot commented 1 year ago

I got the following error when using ReverseDiff

using LinearAlgebra, ReverseDiff
n = 30
D = Diagonal([0.1 + 0.9 * (i - 1) / (n - 1) for i = 1:n])
F = x -> sqrt.(D) * (x .- 1)
x = ones(n)
ReverseDiff.gradient(x -> dot(F(x), x), x)

and got the following error

ERROR: MethodError: *(::Diagonal{Float64, Vector{Float64}}, ::ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}) is ambiguous. Candidates:
  *(x::AbstractMatrix, y::ReverseDiff.TrackedArray{V, D, 1}) where {V, D} in ReverseDiff at .julia\packages\ReverseDiff\YkVxM\src\derivatives\linalg\arithmetic.jl:214
  *(x::AbstractArray, y::ReverseDiff.TrackedArray{V, D, 1}) where {V, D} in ReverseDiff at .julia\packages\ReverseDiff\YkVxM\src\derivatives\linalg\arithmetic.jl:214
  *(x::AbstractMatrix, y::ReverseDiff.TrackedArray{V, D}) where {V, D} in ReverseDiff at .julia\packages\ReverseDiff\YkVxM\src\derivatives\linalg\arithmetic.jl:214
  *(x::AbstractArray, y::ReverseDiff.TrackedArray{V, D}) where {V, D} in ReverseDiff at .julia\packages\ReverseDiff\YkVxM\src\derivatives\linalg\arithmetic.jl:214
  *(D::Diagonal, V::AbstractVector) in LinearAlgebra at julia-1.8.0\share\julia\stdlib\v1.8\LinearAlgebra\src\diagonal.jl:242
Possible fix, define
  *(::Diagonal, ::ReverseDiff.TrackedArray{V, D, 1}) where {V, D}
Stacktrace:
 [1] (::var"#3#4")(x::ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}})
   @ Main .\REPL[6]:1
 [2] (::var"#7#8")(x::ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}})
   @ Main .\REPL[9]:1
 [3] ReverseDiff.GradientTape(f::var"#7#8", input::Vector{Float64}, cfg::ReverseDiff.GradientConfig{ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}})
   @ ReverseDiff .julia\packages\ReverseDiff\YkVxM\src\api\tape.jl:199
 [4] gradient(f::Function, input::Vector{Float64}, cfg::ReverseDiff.GradientConfig{ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}}) (repeats 2 times)
   @ ReverseDiff .julia\packages\ReverseDiff\YkVxM\src\api\gradients.jl:22
 [5] top-level scope
   @ REPL[9]:1