-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
142 additions
and
39 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using Zygote, ForwardDiff | ||
using LinearSolve, LinearAlgebra, Test | ||
using FiniteDiff | ||
|
||
n = 4 | ||
A = rand(n, n); | ||
b1 = rand(n); | ||
|
||
function f(A, b1; alg = LUFactorization()) | ||
prob = LinearProblem(A, b1) | ||
|
||
sol1 = solve(prob, alg) | ||
|
||
s1 = sol1.u | ||
norm(s1) | ||
end | ||
|
||
f(A, b1) # Uses BLAS | ||
|
||
dA, db1 = Zygote.gradient(f, A, b1) | ||
|
||
dA2 = ForwardDiff.gradient(x -> f(x, eltype(x).(b1)), copy(A)) | ||
db12 = ForwardDiff.gradient(x -> f(eltype(x).(A), x), copy(b1)) | ||
|
||
@test dA ≈ dA2 | ||
@test db1 ≈ db12 | ||
|
||
A = rand(n, n); | ||
b1 = rand(n); | ||
|
||
_ff = (x, y) -> f(x, | ||
y; | ||
alg = LinearSolve.DefaultLinearSolver(LinearSolve.DefaultAlgorithmChoice.LUFactorization)) | ||
_ff(copy(A), copy(b1)) | ||
|
||
dA, db1 = Zygote.gradient(_ff, copy(A), copy(b1)) | ||
|
||
dA2 = ForwardDiff.gradient(x -> f(x, eltype(x).(b1)), copy(A)) | ||
db12 = ForwardDiff.gradient(x -> f(eltype(x).(A), x), copy(b1)) | ||
|
||
@test dA ≈ dA2 | ||
@test db1 ≈ db12 | ||
|
||
function f3(A, b1, b2; alg = KrylovJL_GMRES()) | ||
prob = LinearProblem(A, b1) | ||
sol1 = solve(prob, alg) | ||
prob = LinearProblem(A, b2) | ||
sol2 = solve(prob, alg) | ||
norm(sol1.u .+ sol2.u) | ||
end | ||
|
||
dA, db1, db2 = Zygote.gradient(f3, A, b1, b1) | ||
|
||
#= Needs ForwardDiff rules | ||
dA2 = ForwardDiff.gradient(x -> f3(x, eltype(x).(b1), eltype(x).(b1)), copy(A)) | ||
db12 = ForwardDiff.gradient(x -> f3(eltype(x).(A), x, eltype(x).(b1)), copy(b1)) | ||
db22 = ForwardDiff.gradient(x -> f3(eltype(x).(A), eltype(x).(b1), x), copy(b1)) | ||
@test dA ≈ dA2 atol=5e-5 | ||
@test db1 ≈ db12 | ||
@test db2 ≈ db22 | ||
=# | ||
|
||
A = rand(n, n); | ||
b1 = rand(n); | ||
for alg in ( | ||
LUFactorization(), | ||
RFLUFactorization(), | ||
KrylovJL_GMRES() | ||
) | ||
@show alg | ||
function fb(b) | ||
prob = LinearProblem(A, b) | ||
|
||
sol1 = solve(prob, alg) | ||
|
||
sum(sol1.u) | ||
end | ||
fb(b1) | ||
|
||
fd_jac = FiniteDiff.finite_difference_jacobian(fb, b1) |> vec | ||
@show fd_jac | ||
|
||
zyg_jac = Zygote.jacobian(fb, b1) |> first |> vec | ||
@show zyg_jac | ||
|
||
@test zyg_jac≈fd_jac rtol=1e-4 | ||
|
||
function fA(A) | ||
prob = LinearProblem(A, b1) | ||
|
||
sol1 = solve(prob, alg) | ||
|
||
sum(sol1.u) | ||
end | ||
fA(A) | ||
|
||
fd_jac = FiniteDiff.finite_difference_jacobian(fA, A) |> vec | ||
@show fd_jac | ||
|
||
zyg_jac = Zygote.jacobian(fA, A) |> first |> vec | ||
@show zyg_jac | ||
|
||
@test zyg_jac≈fd_jac rtol=1e-4 | ||
end |
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