Skip to content

Added OTFlow Layers #963

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
3 changes: 2 additions & 1 deletion src/DiffEqFlux.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@

include("ffjord.jl")
include("neural_de.jl")

include("otflow.jl")
include("collocation.jl")
include("multiple_shooting.jl")

export NeuralODE, NeuralDSDE, NeuralSDE, NeuralCDDE, NeuralDAE, AugmentedNDELayer,
NeuralODEMM
export FFJORD, FFJORDDistribution
export OTFlow, OTFlowDistribution

Check warning on line 42 in src/DiffEqFlux.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"OT" should be "TO" or "OF" or "OR" or "NOT".

Check warning on line 42 in src/DiffEqFlux.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"OT" should be "TO" or "OF" or "OR" or "NOT".
export DimMover

export EpanechnikovKernel, UniformKernel, TriangularKernel, QuarticKernel, TriweightKernel,
Expand Down
98 changes: 98 additions & 0 deletions src/otflow.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
struct OTFlow <: AbstractLuxLayer

Check warning on line 1 in src/otflow.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"OT" should be "TO" or "OF" or "OR" or "NOT".
d::Int # Input dimension
m::Int # Hidden dimension
r::Int # Rank for low-rank approximation
end

# Constructor with default rank
OTFlow(d::Int, m::Int; r::Int=min(10,d)) = OTFlow(d, m, r)

Check warning on line 8 in src/otflow.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"OT" should be "TO" or "OF" or "OR" or "NOT".

Check warning on line 8 in src/otflow.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"OT" should be "TO" or "OF" or "OR" or "NOT".

# Initialize parameters and states
function Lux.initialparameters(rng::AbstractRNG, l::OTFlow)

Check warning on line 11 in src/otflow.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"OT" should be "TO" or "OF" or "OR" or "NOT".
w = randn(rng, Float32, l.m) .* 0.01
A = randn(rng, Float32, l.r, l.d + 1) .* 0.01
b = zeros(Float32, l.d + 1)
c = zero(Float32)
K0 = randn(rng, Float32, l.m, l.d + 1) .* 0.01
K1 = randn(rng, Float32, l.m, l.m) .* 0.01
b0 = zeros(Float32, l.m)
b1 = zeros(Float32, l.m)

ps = (w=w, A=A, b=b, c=c, K0=K0, K1=K1, b0=b0, b1=b1)
st = NamedTuple()
return ps, st
end

σ(x) = log(exp(x) + exp(-x))
σ′(x) = tanh(x)
σ′′(x) = 1 - tanh(x)^2

function resnet_forward(x::AbstractVector, t::Real, ps)
s = vcat(x, t)
u0 = σ.(ps.K0 * s .+ ps.b0)
u1 = u0 .+ σ.(ps.K1 * u0 .+ ps.b1) # h=1 as in paper
return u1
end

function potential(x::AbstractVector, t::Real, ps)
s = vcat(x, t)
N = resnet_forward(x, t, ps)
quadratic_term = 0.5 * s' * (ps.A' * ps.A) * s
linear_term = ps.b' * s
return ps.w' * N + quadratic_term + linear_term + ps.c
end

function gradient(x::AbstractVector, t::Real, ps, d::Int)
s = vcat(x, t)
u0 = σ.(ps.K0 * s .+ ps.b0)
z1 = ps.w .+ ps.K1' * (σ′.(ps.K1 * u0 .+ ps.b1) .* ps.w)
z0 = ps.K0' * (σ′.(ps.K0 * s .+ ps.b0) .* z1)

grad = z0 + (ps.A' * ps.A) * s + ps.b
return grad[1:d]
end

function trace(x::AbstractVector, t::Real, ps, d::Int)
s = vcat(x, t)
u0 = σ.(ps.K0 * s .+ ps.b0)
z1 = ps.w .+ ps.K1' * (σ′.(ps.K1 * u0 .+ ps.b1) .* ps.w)

K0_E = ps.K0[:, 1:d]
A_E = ps.A[:, 1:d]

t0 = sum(σ′′.(ps.K0 * s .+ ps.b0) .* z1 .* (K0_E .^ 2))
J = Diagonal(σ′.(ps.K0 * s .+ ps.b0)) * K0_E
t1 = sum(σ′′.(ps.K1 * u0 .+ ps.b1) .* ps.w .* (ps.K1 * J) .^ 2)
trace_A = tr(A_E' * A_E)

return t0 + t1 + trace_A
end

function (l::OTFlow)(xt::Tuple{AbstractVector, Real}, ps, st)

Check warning on line 71 in src/otflow.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"OT" should be "TO" or "OF" or "OR" or "NOT".
x, t = xt
v = -gradient(x, t, ps, l.d) # v = -∇Φ
tr = -trace(x, t, ps, l.d) # tr(∇v) = -tr(∇²Φ)
return (v, tr), st
end

function simple_loss(x::AbstractVector, t::Real, l::OTFlow, ps)

Check warning on line 78 in src/otflow.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"OT" should be "TO" or "OF" or "OR" or "NOT".
(v, tr), _ = l((x, t), ps, NamedTuple())
return sum(v.^2) / 2 - tr
end

function manual_gradient(x::AbstractVector, t::Real, l::OTFlow, ps)
s = vcat(x, t)
u0 = σ.(ps.K0 * s .+ ps.b0)
u1 = u0 .+ σ.(ps.K1 * u0 .+ ps.b1)

v = -gradient(x, t, ps, l.d)
tr = -trace(x, t, ps, l.d)

# Simplified gradients (not full implementation)
grad_w = u1
grad_A = (ps.A * s) * s'

return (w=grad_w, A=grad_A, b=similar(ps.b), c=0.0,
K0=zeros(l.m, l.d+1), K1=zeros(l.m, l.m),
b0=zeros(l.m), b1=zeros(l.m))
end
2 changes: 1 addition & 1 deletion test/cnf_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end
return -mean(logpx)
end

@testset "ADType: $(adtype)" for adtype in (Optimization.AutoForwardDiff(),
@testitem "ADType: $(adtype)" for adtype in (Optimization.AutoForwardDiff(),
Optimization.AutoReverseDiff(), Optimization.AutoTracker(),
Optimization.AutoZygote(), Optimization.AutoFiniteDiff())
@testset "regularize = $(regularize) & monte_carlo = $(monte_carlo)" for regularize in (
Expand Down
2 changes: 1 addition & 1 deletion test/collocation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
@test_throws ErrorException collocate_data(data, t, kernel, 0.001)
end
end
end
end
2 changes: 1 addition & 1 deletion test/neural_dae_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@
optprob = Optimization.OptimizationProblem(optfunc, p)
res = Optimization.solve(optprob, BFGS(; initial_stepnorm = 0.0001))
end
end
end
4 changes: 2 additions & 2 deletions test/neural_de_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ end
@test first(layer(r, ps, st))[:, :, :, 1] == r[:, :, 1, :]
end

@testitem "Neural DE CUDA" tags=[:cuda] skip=:(using LuxCUDA; !LuxCUDA.functional()) begin
@testitem "Neural DE CUDA" tags=[:cuda] begin
using LuxCUDA, Zygote, OrdinaryDiffEq, StochasticDiffEq, Test, Random, ComponentArrays
import Flux

Expand Down Expand Up @@ -323,4 +323,4 @@ end
end
end
end
end
end
65 changes: 65 additions & 0 deletions test/otflow_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
@testitem "Tests for OTFlow Layer Functionality" begin

Check warning on line 1 in test/otflow_tests.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"OT" should be "TO" or "OF" or "OR" or "NOT".
using Lux, LuxCore, Random, LinearAlgebra, Test, ComponentArrays, Flux, DiffEqFlux
rng = Xoshiro(0)
d = 2
m = 4
r = 2
otflow = OTFlow(d, m; r=r)

Check warning on line 7 in test/otflow_tests.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"OT" should be "TO" or "OF" or "OR" or "NOT".
ps, st = Lux.setup(rng, otflow)
ps = ComponentArray(ps)

x = Float32[1.0, 2.0]
t = 0.5f0

@testitem "Forward Pass" begin
(v, tr), st_new = otflow((x, t), ps, st)
@test length(v) == d
@test isa(tr, Float32)
@test st_new == st
end

@testitem "Potential Function" begin
phi = potential(x, t, ps)
@test isa(phi, Float32)
end

@testitem "Gradient Consistency" begin
grad = gradient(x, t, ps, d)
(v, _), _ = otflow((x, t), ps, st)
@test length(grad) == d
@test grad ≈ -v atol=1e-5 # v = -∇Φ
end

@testitem "Trace Consistency" begin
tr_manual = trace(x, t, ps, d)
(_, tr_forward), _ = otflow((x, t), ps, st)
@test tr_manual ≈ -tr_forward atol=1e-5
end

@testitem "ODE Integration" begin
x0 = Float32[1.0, 1.0]
tspan = (0.0f0, 1.0f0)
x_traj, t_vec = simple_ode_solve(otflow, x0, tspan, ps, st; dt=0.01f0)
@test size(x_traj) == (d, length(t_vec))
@test all(isfinite, x_traj)
@test x_traj[:, end] != x0
end

@testitem "Loss Function" begin
loss_val = simple_loss(x, t, otflow, ps)
@test isa(loss_val, Float32)
@test isfinite(loss_val)
end

@testitem "Manual Gradient" begin
grads = manual_gradient(x, t, otflow, ps)
@test haskey(grads, :w) && length(grads.w) == m
@test haskey(grads, :A) && size(grads.A) == (r, d+1)
@test haskey(grads, :b) && length(grads.b) == d+1
@test haskey(grads, :c) && isa(grads.c, Float32)
@test haskey(grads, :K0) && size(grads.K0) == (m, d+1)
@test haskey(grads, :K1) && size(grads.K1) == (m, m)
@test haskey(grads, :b0) && length(grads.b0) == m
@test haskey(grads, :b1) && length(grads.b1) == m
end
end
2 changes: 1 addition & 1 deletion test/stiff_nested_ad_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
end
end

@testset "Solver: $(nameof(typeof(solver)))" for solver in (
@testitem "Solver: $(nameof(typeof(solver)))" for solver in (
KenCarp4(), Rodas5(), RadauIIA5())
neuralde = NeuralODE(model, tspan, solver; saveat = t, reltol = 1e-7, abstol = 1e-9)
ps, st = Lux.setup(Xoshiro(0), neuralde)
Expand Down
Loading