Repository navigation
Closed
Description
I tried to compute the gradient of a function with quadratic form. However, it failed with ambiguity error as follows:
import ReverseDiff
const A = [1.0 2.0; 2.0 5.0]
quadratic(x) = x' * A * x
ReverseDiff.gradient(quadratic, ones(2))MethodError: *(::RowVector{ReverseDiff.TrackedReal{Float64,Float64,ReverseDiff.TrackedArray{Float64,Float64,1,Array{Float64,1},Array{Float64,1}}},ReverseDiff.TrackedArray{Float64,Float64,1,Array{Float64,1},Array{Float64,1}}}, ::ReverseDiff.TrackedArray{Float64,Float64,1,Array{Float64,1},Array{Float64,1}}) is ambiguous. Candidates:
*(x::AbstractArray{T,2} where T, y::ReverseDiff.TrackedArray{V,D,N,VA,DA} where DA where VA where N) where {V, D} in ReverseDiff at /Users/kenta/.julia/v0.6/ReverseDiff/src/derivatives/linalg/arithmetic.jl:193
*(x::AbstractArray, y::ReverseDiff.TrackedArray{V,D,N,VA,DA} where DA where VA where N) where {V, D} in ReverseDiff at /Users/kenta/.julia/v0.6/ReverseDiff/src/derivatives/linalg/arithmetic.jl:193
*(rowvec::RowVector{T,V} where V<:(AbstractArray{T,1} where T), vec::AbstractArray{T,1}) where T<:Real in Base.LinAlg at linalg/rowvector.jl:170
Possible fix, define
*(::RowVector{ReverseDiff.TrackedReal{V,D,ReverseDiff.TrackedArray{V,D,1,VA,DA}},V} where V<:(AbstractArray{T,1} where T), ::ReverseDiff.TrackedArray{V,D,1,VA,DA})
Stacktrace:
[1] * at ./operators.jl:424 [inlined]
[2] quadratic(::ReverseDiff.TrackedArray{Float64,Float64,1,Array{Float64,1},Array{Float64,1}}) at ./In[32]:2
[3] Type at /Users/kenta/.julia/v0.6/ReverseDiff/src/api/tape.jl:199 [inlined]
[4] gradient(::Function, ::Array{Float64,1}, ::ReverseDiff.GradientConfig{ReverseDiff.TrackedArray{Float64,Float64,1,Array{Float64,1},Array{Float64,1}}}) at /Users/kenta/.julia/v0.6/ReverseDiff/src/api/gradients.jl:22 (repeats 2 times)
I think matrix multiplication is already supported. I'm not sure whether it is an unsupported feature or a kind of bug, so let me file an issue here.
I'm using ReverseDiff.jl v0.1.4 on Julia 0.6.
Metadata
Metadata
Assignees
Labels
No labels
Activity
vmoens commentedon Jul 7, 2017
I don't know whether it tackles the problem as you'd have wished, but there are a few ad hoc solutions to that:
First, compute the vector * matrix product using the sum of the element-wise product
The other possibility could be to compute the products separately
Without the
Array()the product returns aRowVector{Float64,Array{Float64,1}}which makes ReverseDiff unhappy...jrevels commentedon Jul 7, 2017
This is the classic "new array types which define ambiguous method definitions" problem. It might be fixed just by adding
:RowVectorto the ambiguity list.bicycle1885 commentedon Jul 8, 2017
@ovimo Thanks. I'm avoiding the problem with similar techniques you suggested.
@jrevels I've tried your suggestion but it has still ambiguity problem.
mcabbott commentedon Jul 4, 2022
This no longer gives an error. But if
Ais not symmetric, it gives wrong results, as seen in this discourse thread:devmotion commentedon Sep 23, 2026
This no longer reproduces: on master, the example with the non-symmetric matrix gives the correct gradient.
🤖 Generated with Claude Code