-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
ParamThakkar123
wants to merge
14
commits into
SciML:master
Choose a base branch
from
ParamThakkar123:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Added OTFlow Layers #963
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3b8de3c
Added OTFlow Layers
ParamThakkar123 0db3b5b
Formatted document
ParamThakkar123 472dcc2
Merge branch 'master' of https://github.com/SciML/DiffEqFlux.jl
ParamThakkar123 e496df6
Added OTFlow tests
ParamThakkar123 a8a20a0
Merge branch 'master' of https://github.com/SciML/DiffEqFlux.jl
ParamThakkar123 56ac16b
Added OTflow.jl with tests
ParamThakkar123 de3a09d
Added @testitem blocks
ParamThakkar123 3265ae9
Added testitem blocks
ParamThakkar123 0aa228e
Changes Made
ParamThakkar123 b31270c
Changes
ParamThakkar123 619a567
Made required changes
ParamThakkar123 9e97d17
Implemented Requested changes
ParamThakkar123 9a0c2ca
Made modifications in trainable parameters
ParamThakkar123 3073d3f
Made modifications in trainable parameters
ParamThakkar123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,98 @@ | ||
struct OTFlow <: AbstractLuxLayer | ||
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
|
||
|
||
# Initialize parameters and states | ||
function Lux.initialparameters(rng::AbstractRNG, l::OTFlow) | ||
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) | ||
ParamThakkar123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
ParamThakkar123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
σ(x) = log(exp(x) + exp(-x)) | ||
ParamThakkar123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
σ′(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) | ||
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) | ||
(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 |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,65 @@ | ||
@testitem "Tests for OTFlow Layer Functionality" begin | ||
using Lux, LuxCore, Random, LinearAlgebra, Test, ComponentArrays, Flux, DiffEqFlux | ||
rng = Xoshiro(0) | ||
d = 2 | ||
m = 4 | ||
r = 2 | ||
otflow = OTFlow(d, m; r=r) | ||
ps, st = Lux.setup(rng, otflow) | ||
ps = ComponentArray(ps) | ||
|
||
x = Float32[1.0, 2.0] | ||
t = 0.5f0 | ||
|
||
@testitem "Forward Pass" begin | ||
ParamThakkar123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(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 |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.