Skip to content

Rename create_ude_component to NeuralNetworkBlock #12

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

Merged
merged 2 commits into from
Apr 7, 2024
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
12 changes: 6 additions & 6 deletions src/UDEComponents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ using Lux: Lux
using Random: Xoshiro
using ComponentArrays: ComponentArray

export create_ude_component, multi_layer_feed_forward
export NeuralNetworkBlock, multi_layer_feed_forward

include("utils.jl")

"""
create_ude_component(n_input = 1, n_output = 1;
NeuralNetworkBlock(n_input = 1, n_output = 1;
chain = multi_layer_feed_forward(n_input, n_output),
rng = Xoshiro(0))

Create an `ODESystem` with a neural network inside.
"""
function create_ude_component(n_input = 1,
function NeuralNetworkBlock(n_input = 1,
n_output = 1;
chain = multi_layer_feed_forward(n_input, n_output),
rng = Xoshiro(0))
lux_p, st = Lux.setup(rng, chain)
ca = ComponentArray(lux_p)
rng = Xoshiro(0), eltype=Float64)
lux_p = Lux.initialparameters(rng, chain)
ca = ComponentArray{eltype}(lux_p)

@parameters p[1:length(ca)] = Vector(ca)
@parameters T::typeof(typeof(p))=typeof(p) [tunable = false]
Expand Down
8 changes: 4 additions & 4 deletions test/lotka_volterra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using StableRNGs

function lotka_ude()
@variables t x(t)=3.1 y(t)=1.5
@parameters α=1.3 β=0.9 γ=0.8 δ=1.8
@parameters α=1.3 [tunable=false] δ=1.8 [tunable=false]
Dt = ModelingToolkit.D_nounits
@named nn_in = RealInput(nin = 2)
@named nn_out = RealOutput(nout = 2)
Expand Down Expand Up @@ -44,7 +44,7 @@ end
model = lotka_ude()

chain = multi_layer_feed_forward(2, 2)
nn = create_ude_component(2, 2; chain, rng = StableRNG(42))
nn = NeuralNetworkBlock(2, 2; chain, rng = StableRNG(42))

eqs = [connect(model.nn_in, nn.output)
connect(model.nn_out, nn.input)]
Expand All @@ -67,7 +67,7 @@ get_refs = getu(model_true, [model_true.x, model_true.y])

function loss(x, (prob, sol_ref, get_vars, get_refs))
new_p = SciMLStructures.replace(Tunable(), prob.p, x)
new_prob = remake(prob, p = new_p)
new_prob = remake(prob, p = new_p, u0 = eltype(x).(prob.u0))
ts = sol_ref.t
new_sol = solve(new_prob, Rodas4(), saveat = ts)

Expand Down Expand Up @@ -115,7 +115,7 @@ res = solve(op, Adam(), maxiters = 5000)#, callback = plot_cb)

res_p = SciMLStructures.replace(Tunable(), prob.p, res)
res_prob = remake(prob, p = res_p)
res_sol = solve(res_prob, Rodas4())
res_sol = solve(res_prob, Rodas4(), saveat=sol_ref.t)

# using Plots
# plot(sol_ref, idxs = [model_true.x, model_true.y])
Expand Down