-
-
Notifications
You must be signed in to change notification settings - Fork 213
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
Fix ldiv / rdiv + implement pinv #223
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ca7f947
Implement pinv, fix ldiv
willtebbutt 21c90a9
Fix rdiv
willtebbutt e3c6d85
Uncomment tests
willtebbutt 7ec4f91
Remove old rdiv definition
willtebbutt f715adc
Make some structured matrices work
willtebbutt 94efc95
Reference pinv
willtebbutt 1141b6a
Resolve merge from cherry pick
under-Peter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,8 +171,18 @@ _backmean(xs, Δ, dims) = zero(xs) .+ Δ ./ mapreduce(i -> size(xs,i),*,dims) | |
end | ||
end | ||
|
||
@adjoint transpose(x) = transpose(x), Δ -> (transpose(Δ),) | ||
@adjoint Base.adjoint(x) = x', Δ -> (Δ',) | ||
@adjoint function transpose(x) | ||
back(Δ) = (transpose(Δ),) | ||
back(Δ::NamedTuple{(:parent,)}) = (Δ.parent,) | ||
return transpose(x), back | ||
end | ||
@adjoint function Base.adjoint(x) | ||
back(Δ) = (Δ',) | ||
back(Δ::NamedTuple{(:parent,)}) = (Δ.parent,) | ||
return x', back | ||
end | ||
|
||
|
||
@adjoint parent(x::LinearAlgebra.Adjoint) = parent(x), ȳ -> (LinearAlgebra.Adjoint(ȳ),) | ||
|
||
@adjoint dot(x::AbstractArray, y::AbstractArray) = dot(x, y), Δ->(Δ .* y, Δ .* x) | ||
|
@@ -204,18 +214,55 @@ end | |
@adjoint logabsdet(xs) = logabsdet(xs), Δ -> (Δ[1] * transpose(inv(xs)),) | ||
|
||
@adjoint function inv(A) | ||
return inv(A), function (Δ) | ||
Ainv = inv(A) | ||
∇A = - Ainv' * Δ * Ainv' | ||
return (∇A, ) | ||
end | ||
return inv(A), function (Δ) | ||
Ainv = inv(A) | ||
∇A = - Ainv' * Δ * Ainv' | ||
return (∇A, ) | ||
end | ||
end | ||
|
||
@adjoint function \(A::AbstractMatrix, B::AbstractVecOrMat) | ||
# Defaults for atol and rtol copied directly from LinearAlgebra. See the following for | ||
# derivation: | ||
# Golub, Gene H., and Victor Pereyra. "The differentiation of pseudo-inverses and nonlinear | ||
# least squares problems whose variables separate." SIAM Journal on numerical analysis 10.2 | ||
# (1973): 413-432. | ||
@adjoint function pinv( | ||
A::AbstractMatrix{T}; | ||
atol::Real = 0.0, | ||
rtol::Real = (eps(real(float(one(T))))*min(size(A)...))*iszero(atol), | ||
) where {T} | ||
Y = pinv(A) | ||
return Y, Δ->(-Y' * Δ * Y' + (I - A * Y) * Δ' * Y * Y' + Y' * Y * Δ' * (I - Y * A),) | ||
end | ||
|
||
@adjoint function \(A::Union{Diagonal, AbstractTriangular}, B::AbstractVecOrMat) | ||
Y = A \ B | ||
return Y, function(Ȳ) | ||
B̄ = A' \ Ȳ | ||
return (-B̄ * Y', B̄) | ||
B̄ = A' \ Ȳ | ||
return (-B̄ * Y', B̄) | ||
end | ||
end | ||
|
||
@adjoint function /(A::AbstractMatrix, B::Union{Diagonal, AbstractTriangular}) | ||
Y = A / B | ||
return Y, function(Ȳ) | ||
Ā = Ȳ / B' | ||
return (Ā, -Y' * Ā) | ||
end | ||
end | ||
|
||
@adjoint function \(A::AbstractMatrix, B::AbstractVecOrMat) | ||
Z = A \ B | ||
return Z, function(Z̄) | ||
B̄ = A' \ Z̄ | ||
if size(A, 1) == size(A, 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we also need that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just elected to follow the structure in base found here precisely. I think base assumes that the user will ensure that |
||
return (-B̄ * Z', B̄) | ||
else | ||
a = -B̄ * Z' | ||
b = (B - A * Z) * B̄' / A' | ||
c = A' \ Z * (Z̄' - B̄' * A) | ||
return (a + b + c, B̄) | ||
end | ||
end | ||
end | ||
|
||
|
@@ -227,6 +274,9 @@ end | |
# LinAlg Matrix Types | ||
# =================== | ||
|
||
@adjoint LinearAlgebra.LowerTriangular(A) = LowerTriangular(A), Δ->(LowerTriangular(Δ),) | ||
@adjoint LinearAlgebra.UpperTriangular(A) = UpperTriangular(A), Δ->(UpperTriangular(Δ),) | ||
|
||
# This is basically a hack while we don't have a working `ldiv!`. | ||
@adjoint function \(A::Cholesky, B::AbstractVecOrMat) | ||
Y, back = Zygote.forward((U, B)->U \ (U' \ B), A.U, B) | ||
|
@@ -236,14 +286,6 @@ end | |
end | ||
end | ||
|
||
@adjoint function /(A::AbstractMatrix, B::AbstractMatrix) | ||
Y = A / B | ||
return Y, function(Ȳ) | ||
Ā = Ȳ / B' | ||
return (Ā, -Y' * Ā) | ||
end | ||
end | ||
|
||
_symmetric_back(Δ) = UpperTriangular(Δ) + LowerTriangular(Δ)' - Diagonal(Δ) | ||
_symmetric_back(Δ::Union{Diagonal, UpperTriangular}) = Δ | ||
@adjoint function Symmetric(A::AbstractMatrix) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
/
for abstract matrices? Is that still covered and I'm missing it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup.
/
is implemented in terms of\
for abstract matrices :)