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

Derivative of matrix inverse for diagonal matrix is not correct? #490

Closed
wwang2 opened this issue Dec 28, 2020 · 1 comment · Fixed by #481
Closed

Derivative of matrix inverse for diagonal matrix is not correct? #490

wwang2 opened this issue Dec 28, 2020 · 1 comment · Fixed by #481

Comments

@wwang2
Copy link

wwang2 commented Dec 28, 2020

I am here to reporting some dubious results from the derivative of a matrix inversion for the diagonal matrix.

Package version: [f6369f11] ForwardDiff v0.10.14

A = [0.5 0 0 ; 0 0.5 0; 0 0 0.5]

ForwardDiff.gradient(A -> sum(inv(A)), A)

result:

3×3 Array{Float64,2}:    
 -4.0  -4.0  -4.0
  0.0  -4.0  -4.0
  0.0   0.0  -4.0

However, the analytical result should be:

-inv(A) * ones(3,3) * inv(A)

result:

3×3 Array{Float64,2}:
 -4.0  -4.0  -4.0
 -4.0  -4.0  -4.0
 -4.0  -4.0  -4.0

The Zygote gradient function

Zygote.gradient(A -> sum(inv(A)), A)[1]

result:

3×3 Array{Float64,2}:
 -4.0  -4.0  -4.0
 -4.0  -4.0  -4.0
 -4.0  -4.0  -4.0
@andreasnoack
Copy link
Member

This is an unintended consequence of the polyalgorithm I wrote many years ago in Julia's LinearAlgebra module, https://github.com/JuliaLang/julia/blob/8e0183f2b66b5578d897a2c8318a63667a27fb8a/stdlib/LinearAlgebra/src/dense.jl#L803-L816. The matrix is detected to be triangular and then inverted as an UpperTriangular matrix which avoids the matrix factorization. I think #480 would fix this issue generally since istriu would then no longer be true. Short term, you can work around the issue by calling lu directly to bypass the polyalgorithm, i.e.

julia> ForwardDiff.gradient(t -> sum(inv(lu(t))), A)
3×3 Matrix{Float64}:
 -4.0  -4.0  -4.0
 -4.0  -4.0  -4.0
 -4.0  -4.0  -4.0

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

Successfully merging a pull request may close this issue.

2 participants