-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the tests to use Lux and ForwardDiff
- Loading branch information
Showing
4 changed files
with
56 additions
and
48 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 |
---|---|---|
@@ -1,63 +1,66 @@ | ||
using DiffEqFlux, Zygote, OrdinaryDiffEq, ReverseDiff, Test | ||
using DiffEqFlux, Zygote, OrdinaryDiffEq, ForwardDiff, Test, Optimisers, Random, Lux, ComponentArrays | ||
|
||
# Checks for Shapes and Non-Zero Gradients | ||
u0 = rand(Float32, 6, 1) | ||
|
||
hnn = HamiltonianNN(Flux.Chain(Flux.Dense(6, 12, relu), Flux.Dense(12, 1))) | ||
p = hnn.p | ||
hnn = HamiltonianNN(Lux.Chain(Lux.Dense(6, 12, relu), Lux.Dense(12, 1))) | ||
ps, st = Lux.setup(Random.default_rng(), hnn) | ||
ps = ps |> ComponentArray | ||
|
||
@test size(hnn(u0)) == (6, 1) | ||
@test size(first(hnn(u0, ps, st))) == (6, 1) | ||
|
||
@test ! iszero(ReverseDiff.gradient(p -> sum(hnn(u0, p)), p)) | ||
@test !iszero(ForwardDiff.gradient(ps -> sum(first(hnn(u0, ps, st))), ps)) | ||
|
||
hnn = HamiltonianNN(Flux.Chain(Flux.Dense(6, 12, relu), Flux.Dense(12, 1))) | ||
p = hnn.p | ||
hnn = HamiltonianNN(Lux.Chain(Lux.Dense(6, 12, relu), Lux.Dense(12, 1))) | ||
ps, st = Lux.setup(Random.default_rng(), hnn) | ||
ps = ps |> ComponentArray | ||
|
||
@test size(hnn(u0)) == (6, 1) | ||
@test size(first(hnn(u0, ps, st))) == (6, 1) | ||
|
||
@test ! iszero(ReverseDiff.gradient(p -> sum(hnn(u0, p)), p)) | ||
@test !iszero(ForwardDiff.gradient(ps -> sum(first(hnn(u0, ps, st))), ps)) | ||
|
||
# Test Convergence on a toy problem | ||
t = range(0.0f0, 1.0f0, length = 64) | ||
t = range(0.0f0, 1.0f0, length=64) | ||
π_32 = Float32(π) | ||
q_t = reshape(sin.(2π_32 * t), 1, :) | ||
p_t = reshape(cos.(2π_32 * t), 1, :) | ||
dqdt = 2π_32 .* p_t | ||
dpdt = -2π_32 .* q_t | ||
|
||
data = cat(q_t, p_t, dims = 1) | ||
target = cat(dqdt, dpdt, dims = 1) | ||
data = vcat(q_t, p_t) | ||
target = vcat(dqdt, dpdt) | ||
|
||
hnn = HamiltonianNN(Flux.Chain(Flux.Dense(2, 16, relu), Flux.Dense(16, 1))) | ||
p = hnn.p | ||
hnn = HamiltonianNN(Lux.Chain(Lux.Dense(2, 16, relu), Lux.Dense(16, 1))) | ||
ps, st = Lux.setup(Random.default_rng(), hnn) | ||
ps = ps |> ComponentArray | ||
|
||
opt = ADAM(0.01) | ||
loss(x, y, p) = sum((hnn(x, p) .- y) .^ 2) | ||
st_opt = Optimisers.setup(opt, ps) | ||
loss(data, target, ps) = mean(abs2, first(hnn(data, ps, st)) .- target) | ||
|
||
initial_loss = loss(data, target, p) | ||
initial_loss = loss(data, target, ps) | ||
|
||
epochs = 100 | ||
for epoch in 1:epochs | ||
gs = ReverseDiff.gradient(p -> loss(data, target, p), p) | ||
Flux.Optimise.update!(opt, p, gs) | ||
for epoch in 1:100 | ||
# Forward Mode over Reverse Mode for Training | ||
gs = ForwardDiff.gradient(ps -> loss(data, target, ps), ps) | ||
st_opt, ps = Optimisers.update!(st_opt, ps, gs) | ||
end | ||
|
||
final_loss = loss(data, target, p) | ||
final_loss = loss(data, target, ps) | ||
|
||
@test initial_loss > final_loss | ||
@test initial_loss > 5 * final_loss | ||
|
||
# Test output and gradient of NeuralHamiltonianDE Layer | ||
tspan = (0.0f0, 1.0f0) | ||
|
||
model = NeuralHamiltonianDE( | ||
hnn, tspan, Tsit5(), | ||
save_everystep = false, save_start = true, | ||
saveat = range(tspan[1], tspan[2], length=10) | ||
save_everystep=false, save_start=true, | ||
saveat=range(tspan[1], tspan[2], length=10) | ||
) | ||
sol = Array(model(data[:, 1])) | ||
sol = Array(first(model(data[:, 1], ps, st))) | ||
@test size(sol) == (2, 10) | ||
|
||
ps = Flux.params(model) | ||
gs = Flux.gradient(() -> sum(Array(model(data[:, 1]))), ps) | ||
gs = only(Zygote.gradient(ps -> sum(Array(first(model(data[:, 1], ps, st)))), ps)) | ||
|
||
@test ! iszero(gs[model.p]) | ||
@test !iszero(gs) |