Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions src/gradients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -372,37 +372,23 @@ function finite_difference_gradient!(
end
copyto!(c3, x)
if fdtype == Val(:forward)
fx0 = fx !== nothing ? fx : f(x)
for i in eachindex(x)
epsilon = compute_epsilon(fdtype, x[i], relstep, absstep, dir)
x_old = x[i]
if typeof(fx) != Nothing
c3[i] += epsilon
dfi = (f(c3) - fx) / epsilon
c3[i] = x_old
else
fx0 = f(x)
c3[i] += epsilon
dfi = (f(c3) - fx0) / epsilon
c3[i] = x_old
end
c3[i] += epsilon
dfi = (f(c3) - fx0) / epsilon
c3[i] = x_old

df[i] = real(dfi)
if eltype(df) <: Complex
if eltype(x) <: Complex
c3[i] += im * epsilon
if typeof(fx) != Nothing
dfi = (f(c3) - fx) / (im * epsilon)
else
dfi = (f(c3) - fx0) / (im * epsilon)
end
dfi = (f(c3) - fx0) / (im * epsilon)
c3[i] = x_old
else
c1[i] += im * epsilon
if typeof(fx) != Nothing
dfi = (f(c1) - fx) / (im * epsilon)
else
dfi = (f(c1) - fx0) / (im * epsilon)
end
dfi = (f(c1) - fx0) / (im * epsilon)
c1[i] = x_old
end
df[i] -= im * imag(dfi)
Expand Down
3 changes: 2 additions & 1 deletion test/downstream/ordinarydiffeq_tridiagonal_solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ function loss(p)
sol = solve(_prob, Rodas4P(autodiff=false), saveat=0.1)
sum((sol .- sol_true).^2)
end
@test ForwardDiff.gradient(loss, [1.0])[1] ≈ 0.6662949361011025
@test ForwardDiff.gradient(loss, [1.0])[1] ≈ 0.6645766813735486

1 change: 0 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ if GROUP == "All" || GROUP == "Downstream"
@time @safetestset "ODEs" begin
import OrdinaryDiffEq
@time @safetestset "OrdinaryDiffEq Tridiagonal" begin include("downstream/ordinarydiffeq_tridiagonal_solve.jl") end
include(joinpath(dirname(pathof(OrdinaryDiffEq)), "..", "test/interface/sparsediff_tests.jl"))
end
end

Expand Down
Loading