Skip to content

Commit c7f0d0c

Browse files
committed
Update to MTK@v10 and drop the dependency on MTKStdLib
We can simplify the NeuralNetworkBlock a bit by using normal vector variables instead of RealInput/OutputArrays.
1 parent a14c987 commit c7f0d0c

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

Project.toml

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
name = "ModelingToolkitNeuralNets"
22
uuid = "f162e290-f571-43a6-83d9-22ecc16da15f"
33
authors = ["Sebastian Micluța-Câmpeanu <[email protected]> and contributors"]
4-
version = "1.7.0"
4+
version = "2.0.0-DEV"
55

66
[deps]
77
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
88
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
99
Lux = "b2108857-7c20-44ae-9111-449ecde12c47"
1010
LuxCore = "bb33d45b-7691-41d6-9220-0943567d0623"
1111
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
12-
ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739"
1312
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1413
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
1514

1615
[compat]
1716
Aqua = "0.8"
1817
ComponentArrays = "0.15.11"
19-
DifferentiationInterface = "0.6"
20-
ForwardDiff = "0.10.36"
18+
DifferentiationInterface = "0.6, 0.7"
19+
ForwardDiff = "0.10.36, 1"
2120
IntervalSets = "0.7.10"
22-
JET = "0.8, 0.9"
23-
Lux = "1"
24-
LuxCore = "1"
25-
ModelingToolkit = "9.64"
26-
ModelingToolkitStandardLibrary = "2.7"
27-
Optimization = "3.24, 4"
21+
JET = "0.8, 0.9, 0.10"
22+
Lux = "1.14"
23+
LuxCore = "1.2"
24+
ModelingToolkit = "10"
25+
ModelingToolkitStandardLibrary = "2.24"
26+
Optimization = "4"
2827
OptimizationOptimisers = "0.2.1, 0.3"
29-
OrdinaryDiffEq = "6.74"
28+
OrdinaryDiffEqVerner = "1.2"
3029
Random = "1.10"
3130
SafeTestsets = "0.1"
3231
SciMLSensitivity = "7.72"
3332
SciMLStructures = "1.1.0"
3433
StableRNGs = "1"
35-
SymbolicIndexingInterface = "0.3.15"
36-
Symbolics = "6.36"
34+
Statistics = "1.10"
35+
SymbolicIndexingInterface = "0.3.41"
36+
Symbolics = "6.43"
3737
Test = "1.10"
38-
Zygote = "0.6.73"
38+
Zygote = "0.6.73, 0.7"
3939
julia = "1.10"
4040

4141
[extras]
@@ -45,14 +45,19 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
4545
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
4646
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
4747
OptimizationOptimisers = "42dfb2eb-d2b4-4451-abcd-913932933ac1"
48-
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
48+
OrdinaryDiffEqVerner = "79d7bb75-1356-48c1-b8c0-6832512096c2"
49+
ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739"
4950
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
5051
SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1"
5152
SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226"
5253
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
54+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
5355
SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
5456
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
5557
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
5658

5759
[targets]
58-
test = ["Aqua", "JET", "Test", "OrdinaryDiffEq", "DifferentiationInterface", "SciMLSensitivity", "Zygote", "ForwardDiff", "Optimization", "OptimizationOptimisers", "SafeTestsets", "SciMLStructures", "StableRNGs", "SymbolicIndexingInterface"]
60+
test = ["Aqua", "JET", "Test", "OrdinaryDiffEqVerner", "DifferentiationInterface",
61+
"SciMLSensitivity", "Zygote", "ForwardDiff", "ModelingToolkitStandardLibrary",
62+
"Optimization", "OptimizationOptimisers", "SafeTestsets", "SciMLStructures",
63+
"StableRNGs", "Statistics", "SymbolicIndexingInterface"]

src/ModelingToolkitNeuralNets.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
module ModelingToolkitNeuralNets
22

3-
using ModelingToolkit: @parameters, @named, ODESystem, t_nounits
3+
using ModelingToolkit: @parameters, @named, @variables, System, t_nounits
44
using IntervalSets: var".."
5-
using ModelingToolkitStandardLibrary.Blocks: RealInputArray, RealOutputArray
65
using Symbolics: Symbolics, @register_array_symbolic, @wrapped
7-
using LuxCore: stateless_apply
6+
using LuxCore: stateless_apply, outputsize
87
using Lux: Lux
98
using Random: Xoshiro
109
using ComponentArrays: ComponentArray
@@ -21,7 +20,7 @@ include("utils.jl")
2120
eltype = Float64,
2221
name)
2322
24-
Create an `ODESystem` with a neural network inside.
23+
Create a component neural network as a `System`.
2524
"""
2625
function NeuralNetworkBlock(; n_input = 1, n_output = 1,
2726
chain = multi_layer_feed_forward(n_input, n_output),
@@ -31,19 +30,21 @@ function NeuralNetworkBlock(; n_input = 1, n_output = 1,
3130
name)
3231
ca = ComponentArray{eltype}(init_params)
3332

34-
@parameters p[1:length(ca)] = Vector(ca)
33+
@parameters p[1:length(ca)]=Vector(ca) [tunable = true]
3534
@parameters T::typeof(typeof(ca))=typeof(ca) [tunable = false]
36-
@parameters lux_model::typeof(chain) = chain
35+
@parameters lux_model::typeof(chain)=chain [tunable = false]
3736

38-
@named input = RealInputArray(nin = n_input)
39-
@named output = RealOutputArray(nout = n_output)
37+
@variables inputs(t_nounits)[1:n_input] [input = true]
38+
@variables outputs(t_nounits)[1:n_output] [output = true]
4039

41-
out = stateless_apply(lux_model, input.u, lazyconvert(T, p))
40+
expected_outsz = only(outputsize(chain, inputs, rng))
41+
msg = "The outputsize of the given Lux network ($expected_outsz) does not match `n_output = $n_output`"
42+
@assert n_output==expected_outsz msg
4243

43-
eqs = [output.u ~ out]
44+
eqs = [outputs ~ stateless_apply(lux_model, inputs, lazyconvert(T, p))]
4445

45-
ude_comp = ODESystem(
46-
eqs, t_nounits, [], [lux_model, p, T]; systems = [input, output], name)
46+
ude_comp = System(
47+
eqs, t_nounits, [inputs, outputs], [lux_model, p, T]; name)
4748
return ude_comp
4849
end
4950

0 commit comments

Comments
 (0)