Skip to content
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

Compatibility with @mtkmodel #34

Merged
merged 4 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ModelingToolkitNeuralNets"
uuid = "f162e290-f571-43a6-83d9-22ecc16da15f"
authors = ["Sebastian Micluța-Câmpeanu <[email protected]> and contributors"]
version = "1.1.0"
version = "1.2.0"

[deps]
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
Expand Down
11 changes: 8 additions & 3 deletions src/ModelingToolkitNeuralNets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export NeuralNetworkBlock, multi_layer_feed_forward
include("utils.jl")

"""
NeuralNetworkBlock(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),
init_params = Lux.initialparameters(rng, chain),
Expand All @@ -22,8 +22,7 @@ include("utils.jl")

Create an `ODESystem` with a neural network inside.
"""
function NeuralNetworkBlock(n_input = 1,
n_output = 1;
function NeuralNetworkBlock(; n_input = 1, n_output = 1,
chain = multi_layer_feed_forward(n_input, n_output),
rng = Xoshiro(0),
init_params = Lux.initialparameters(rng, chain),
Expand All @@ -46,6 +45,12 @@ function NeuralNetworkBlock(n_input = 1,
return ude_comp
end

# added to avoid a breaking change from moving n_input & n_output in kwargs
# https://github.com/SciML/ModelingToolkitNeuralNets.jl/issues/32
function NeuralNetworkBlock(n_input, n_output = 1; kwargs...)
NeuralNetworkBlock(; n_input, n_output, kwargs...)
end

function lazyconvert(T, x::Symbolics.Arr)
Symbolics.array_term(convert, T, x, size = size(x))
end
Expand Down
41 changes: 41 additions & 0 deletions test/macro.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using ModelingToolkit, Symbolics
using ModelingToolkit: t_nounits as t, D_nounits as D
using OrdinaryDiffEq
using ModelingToolkitNeuralNets
using ModelingToolkitStandardLibrary.Blocks
using Lux

@mtkmodel Friction_UDE begin
@variables begin
y(t) = 0.0
end
@parameters begin
Fu
end
@components begin
nn_in = RealInputArray(nin = 1)
nn_out = RealOutputArray(nout = 1)
end
@equations begin
D(y) ~ Fu - nn_in.u[1]
y ~ nn_out.u[1]
end
end

@mtkmodel TestFriction_UDE begin
@components begin
friction_ude = Friction_UDE(Fu = 120.0)
nn = NeuralNetworkBlock(n_input = 1, n_output = 1)
end
@equations begin
connect(friction_ude.nn_in, nn.output)
connect(friction_ude.nn_out, nn.input)
end
end

@mtkbuild sys = TestFriction_UDE()

prob = ODEProblem(sys, [], (0, 1.0), [])
sol = solve(prob, Rodas4())

@test SciMLBase.successful_retcode(sol)
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ using SafeTestsets
@testset verbose=true "ModelingToolkitNeuralNets.jl" begin
@safetestset "QA" include("qa.jl")
@safetestset "Basic" include("lotka_volterra.jl")
@safetestset "MTK model macro compatibility" include("macro.jl")
end
Loading