You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I naively tried ForwardDiff.gradient(det, A) and compared it to the output of (det(A) * inv(A))' * A)(Jacobi's Formula for a real square matrix A. They should be the same value, but they are radically different.
What am I getting wrong here? Shouldn't ForwardDiff support det out of the box since it's a core function of Julia?
ForwardDiff is giving the correct result, your implementation of Jacobi's Formula is incorrect:
julia>using ForwardDiff
julia> A =rand(3, 3);
julia>adj(A) =det(A) *inv(A)
adj (generic function with 1 method)
# what Jacobi's Formula actually says should hold true
julia>isapprox(adj(A)', ForwardDiff.gradient(det, A))
true
You can also verify by checking with finite differences and reverse-mode AD, which give similar results.
[UPDATE: please see comment below]
First of all, thank you for the prompt reply.
You are right, I could not find any reference as to why the Jacobi's Formula does not apply, with conditions on cofactors or otherwise (it is commonly stated as a general property, i.e. applying to all cases, which puzzles me very much).
But doing the derivative by hand does give (hopefully) the correct result and this coincides with the one from Forward diff.
Also, thank you for your time here and elsewhere, I am now trying to use Tensors.jl in research and it's great!
after looking more carefully at the problem, I still think there is an issue with gradient of the determinant function, either considering symbolic differentiation or a finite difference approximation, we arrive at the answer given by the Jacobi's formula.
Please take a look at position [2,3] of the gradient:
Symbolic
julia>using SymPy
julia> entries =@syms a b c d e f g h i real=true;
julia> A =reshape([entries...],(3,3))
3×3 Array{SymPy.Sym,2}
⎡a d g⎤
⎢ ⎥
⎢b e h⎥
⎢ ⎥
⎣c f i⎦
julia> detA =det(A) |> simplify
a⋅e⋅i - a⋅f⋅h - b⋅d⋅i + b⋅f⋅g + c⋅d⋅h - c⋅e⋅g
julia> dA =diff.(detA,A)
3×3 Array{SymPy.Sym,2}
⎡e⋅i - f⋅h -b⋅i + c⋅h b⋅f - c⋅e ⎤
⎢ ⎥
⎢-d⋅i + f⋅g a⋅i - c⋅g -a⋅f + c⋅d⎥
⎢ ⎥
⎣d⋅h - e⋅g -a⋅h + b⋅g a⋅e - b⋅d ⎦
julia> dA |>subs(b=>0,c=>0,d=>0,g=>0,h=>0)
3×3 Array{SymPy.Sym,2}
⎡e⋅i 00 ⎤
⎢ ⎥
⎢ 0 a⋅i -a⋅f⎥
⎢ ⎥
⎣ 00 a⋅e ⎦
It does seem like ForwardDiff is giving the wrong answer here, thanks for pursuing this further @schrauf. I checked against ReverseDiff, whose answer matches SymPy:
The problem is in Base definition of det. Using a hand written version:
<<det2(A) =return (
A[1,1]*(A[2,2]*A[3,3]-A[2,3]*A[3,2]) -
A[1,2]*(A[2,1]*A[3,3]-A[2,3]*A[3,1]) +
A[1,3]*(A[2,1]*A[3,2]-A[2,2]*A[3,1])
)
>> det2 (generic function with 1 method)
<< ForwardDiff.gradient(det2,X)
>>3×3 Array{Float64,2}:0.02815240.00.00.00.0479236-0.4955810.00.00.241191
When the matrix is triangular, base computes det from just the upper triangular part of the matrix and I think thereby the sensitivity is not properly propagated to all the partials.
Using the hand written det2 with the upper triangular part, yields:
Could it be because the Base det definition converts lower triangular inputs (our pathological input here) to UpperTriangular arrays:
functiondet{T}(A::AbstractMatrix{T})
ifistriu(A) ||istril(A)
S =typeof((one(T)*zero(T) +zero(T))/one(T))
returnconvert(S, det(UpperTriangular(A)))
endreturndet(lufact(A))
end
Not really. I'll do some digging though, this is pretty interesting. I wonder if there are similar cases in Base linear algebra code where the optimizations aren't dual-safe.
As far as naive solutions go, we can define something like det(x::UpperTriangular{D<:Dual}) = det(lufact(x.data)) (probably for LowerTriangular too).
So I'm at ISMP 2018 and just saw a talk where something clicked for me.
It turns out that these sorts of problematic functions can be modeled as piecewise differentiable from an AD perspective - meaning you can apply existing nonsmooth AD techniques to them (e.g. ANF) in order to recover information that is lost due to implementation structure. What that means is that If you have a caller that supports ANF, they can handle weird functions like these just fine. Of course, that requires that the caller (e.g. solver) understand and utilize ANF...
More of a theoretical tidbit, but thought I'd leave a note here for posterity.
I "solved" the issue with my own version of the determinant function. However, if this persists it will clearly waste someone else's time when they try to differentiate through some linear algebra with a determinant thrown in.
By the way, are there any other known functions in LinearAlgebra which don't differentiate through nicely?
Activity
[-]support for `det` function[/-][+]support for the determinant function[/+]jrevels commentedon Feb 21, 2017
ForwardDiff is giving the correct result, your implementation of Jacobi's Formula is incorrect:
You can also verify by checking with finite differences and reverse-mode AD, which give similar results.
schrauf commentedon Mar 9, 2017
I came too with tthe same issue.
While a generic matrix (generated randomly) seems to give the correct result, specific ones appear to be wrong:
I apologize in advance if this is a problem with my algebra, but I don't understand why I am getting it wrong.
Cheers, Matías
KristofferC commentedon Mar 9, 2017
Edit: This is wrong
The determinant of
is always
xyz. So the derivative should beSo Forward diff seems correct. Do all cofactors need to be nonzero for Jacobis formula to work?
schrauf commentedon Mar 9, 2017
[UPDATE: please see comment below]
First of all, thank you for the prompt reply.
You are right, I could not find any reference as to why the Jacobi's Formula does not apply, with conditions on cofactors or otherwise (it is commonly stated as a general property, i.e. applying to all cases, which puzzles me very much).
But doing the derivative by hand does give (hopefully) the correct result and this coincides with the one from Forward diff.
Also, thank you for your time here and elsewhere, I am now trying to use Tensors.jl in research and it's great!
schrauf commentedon Mar 13, 2017
Hello,
after looking more carefully at the problem, I still think there is an issue with gradient of the determinant function, either considering symbolic differentiation or a finite difference approximation, we arrive at the answer given by the Jacobi's formula.
Please take a look at position [2,3] of the gradient:
Symbolic
Finite differences
ForwardDiff
jrevels commentedon Mar 13, 2017
It does seem like ForwardDiff is giving the wrong answer here, thanks for pursuing this further @schrauf. I checked against ReverseDiff, whose answer matches SymPy:
KristofferC commentedon Mar 13, 2017
With Tensors.jl I get:
which is correct. Strange because we use the Dual numbers from here...
KristofferC commentedon Mar 13, 2017
The problem is in Base definition of
det. Using a hand written version:When the matrix is triangular, base computes
detfrom just the upper triangular part of the matrix and I think thereby the sensitivity is not properly propagated to all the partials.Using the hand written
det2with the upper triangular part, yields:which seems to confirm the analysis above.
jrevels commentedon Mar 13, 2017
Could it be because the Base
detdefinition converts lower triangular inputs (our pathological input here) toUpperTriangulararrays:EDIT: Yup, this is the reason. This ends up calling a special version of
detwhich only multiplies the diagonals:KristofferC commentedon Mar 13, 2017
Out of curiosity, do you know if there is a term for this type of "deficiency" in automatic differentiation?
jrevels commentedon Mar 13, 2017
Not really. I'll do some digging though, this is pretty interesting. I wonder if there are similar cases in Base linear algebra code where the optimizations aren't dual-safe.
As far as naive solutions go, we can define something like
det(x::UpperTriangular{D<:Dual}) = det(lufact(x.data))(probably forLowerTriangulartoo).jrevels commentedon Jul 4, 2018
So I'm at ISMP 2018 and just saw a talk where something clicked for me.
It turns out that these sorts of problematic functions can be modeled as piecewise differentiable from an AD perspective - meaning you can apply existing nonsmooth AD techniques to them (e.g. ANF) in order to recover information that is lost due to implementation structure. What that means is that If you have a caller that supports ANF, they can handle weird functions like these just fine. Of course, that requires that the caller (e.g. solver) understand and utilize ANF...
More of a theoretical tidbit, but thought I'd leave a note here for posterity.
dpsanders commentedon Sep 23, 2018
My Google-fu has deserted me and I am unable to find what ANF might be.
dpsanders commentedon Sep 23, 2018
Ah, could that be Abs Normal Form that you were talking about?
jrevels commentedon Sep 24, 2018
Yes.
PetrKryslUCSD commentedon Jul 28, 2019
Is there still no solution to this problem?
KristofferC commentedon Jul 28, 2019
You could use something like #165 and define the extend the derivative of
detforMatrix{<:Dual}based on the analytical expression.PetrKryslUCSD commentedon Jul 28, 2019
I "solved" the issue with my own version of the determinant function. However, if this persists it will clearly waste someone else's time when they try to differentiate through some linear algebra with a determinant thrown in.
By the way, are there any other known functions in LinearAlgebra which don't differentiate through nicely?
KristofferC commentedon Jul 29, 2019
Basically, anything that exploits the sparsity of an array to compute the result.