Skip to content

Commit

Permalink
vectorize backward noise for Diagonal SDEs
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek-1Bhatt committed Jan 1, 2023
1 parent 927442e commit 78b9b1d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/backsolve_adjoint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ end
size(sol.prob.noise_rate_prototype)[2]
noise_matrix = similar(z0, length(z0), m)
noise_matrix .= false
if _sol.W isa DiffEqNoiseProcess.NoiseProcess && _sol.W.dW isa AbstractMatrix
noise = DiffEqNoiseProcess.vec_NoiseProcess(_sol.W)
backwardnoise = reverse(noise)
end
end

return SDEProblem(sdefun, sense_diffusion, z0, tspan, p,
Expand Down
34 changes: 34 additions & 0 deletions test/sde_nondiag_stratonovich.jl
Original file line number Diff line number Diff line change
Expand Up @@ -750,3 +750,37 @@ end
gZy = Zygote.gradient(p -> loss(p, Z = Z, sensealg = ForwardDiffSensitivity()), p)[1]
@test gFinDgZy rtol=1e-4
end

@testset "Diagonal forward SDE and Non-Diagonal Reverse SDE with Non-Square input" begin
u = Float32.([0.0f0 1.0f0 2.0f0;
0.0f0 0.0f0 1.0f0])

p = Float32.([-1.5 0.05 0.2;
0.01 0.4 1.9])

tspan = (0.0f0, 1.0f0)

function f(u, p, t)
u .* p
end

function g(u, p, t)
u .* p + p
end

function dg!(out, u, p, t, i)
(out .= vec(u))
end

sdefun = SDEFunction(f, g)
prob2 = SDEProblem(sdefun, g, u, tspan, p)

sol = solve(prob2, EulerHeun(), dt = 0.01)

du, dp = adjoint_sensitivities(sol, EulerHeun(), t = tspan,
dgdu_discrete = dg!,
dt = 0.01, adaptive = false,
sensealg = BacksolveAdjoint())
@test !iszero(du)
@test !iszero(dp)
end

0 comments on commit 78b9b1d

Please sign in to comment.