@@ -15,13 +15,13 @@ using DifferentiationInterface
15
15
using SciMLSensitivity
16
16
using Zygote: Zygote
17
17
using Statistics
18
+ using Lux
18
19
19
- function lotka_ude ()
20
+ function lotka_ude (chain )
20
21
@variables t x (t)= 3.1 y (t)= 1.5
21
22
@parameters α= 1.3 [tunable= false ] δ= 1.8 [tunable= false ]
22
23
Dt = ModelingToolkit. D_nounits
23
24
24
- chain = multi_layer_feed_forward (2 , 2 )
25
25
@named nn = NeuralNetworkBlock (2 , 2 ; chain, rng = StableRNG (42 ))
26
26
27
27
eqs = [
36
36
37
37
function lotka_true ()
38
38
@variables t x (t)= 3.1 y (t)= 1.5
39
- @parameters α= 1.3 β= 0.9 γ= 0.8 δ= 1.8
39
+ @parameters α= 1.3 [tunable = false ] β= 0.9 γ= 0.8 δ= 1.8 [tunable = false ]
40
40
Dt = ModelingToolkit. D_nounits
41
41
42
42
eqs = [
43
43
Dt (x) ~ α * x - β * x * y,
44
- Dt (y) ~ - δ * y + δ * x * y
44
+ Dt (y) ~ - δ * y + γ * x * y
45
45
]
46
46
return System (eqs, ModelingToolkit. t_nounits, name = :lotka_true )
47
47
end
48
48
49
- ude_sys = lotka_ude ( )
49
+ rbf (x) = exp .( - (x .^ 2 ) )
50
50
51
- sys = mtkcompile (ude_sys, allow_symbolic = true )
51
+ chain = multi_layer_feed_forward (2 , 2 , width = 5 , initial_scaling_factor = 1 )
52
+ ude_sys = lotka_ude (chain)
52
53
53
- prob = ODEProblem {true, SciMLBase.FullSpecialize} (sys, [], (0 , 1.0 ))
54
+ sys = mtkcompile (ude_sys)
55
+
56
+ @test length (equations (sys)) == 2
57
+
58
+ prob = ODEProblem {true, SciMLBase.FullSpecialize} (sys, [], (0 , 5.0 ))
54
59
55
60
model_true = mtkcompile (lotka_true ())
56
- prob_true = ODEProblem {true, SciMLBase.FullSpecialize} (model_true, [], (0 , 1.0 ))
57
- sol_ref = solve (prob_true, Vern9 (), abstol = 1e-10 , reltol = 1e-8 )
61
+ prob_true = ODEProblem {true, SciMLBase.FullSpecialize} (model_true, [], (0 , 5.0 ))
62
+ sol_ref = solve (prob_true, Vern9 (), abstol = 1e-8 , reltol = 1e-8 )
63
+
64
+ ts = range (0 , 5.0 , length = 21 )
65
+ data = reduce (hcat, sol_ref (ts, idxs = [model_true. x, model_true. y]). u)
58
66
59
67
x0 = default_values (sys)[sys. nn. p]
60
68
61
69
get_vars = getu (sys, [sys. x, sys. y])
62
- get_refs = getu (model_true, [model_true. x, model_true. y])
63
- set_x = setp_oop (sys, sys. nn. p)
70
+ set_x = setsym_oop (sys, sys. nn. p)
64
71
65
- function loss (x, (prob, sol_ref, get_vars, get_refs, set_x))
66
- new_p = set_x (prob, x)
67
- new_prob = remake (prob, p = new_p, u0 = eltype (x).(prob. u0))
68
- ts = sol_ref. t
69
- new_sol = solve (new_prob, Vern9 (), abstol = 1e-10 , reltol = 1e-8 , saveat = ts)
72
+ function loss (x, (prob, get_vars, data, ts, set_x))
73
+ new_u0, new_p = set_x (prob, x)
74
+ new_prob = remake (prob, p = new_p, u0 = new_u0)
75
+ new_sol = solve (new_prob, Vern9 (), abstol = 1e-8 , reltol = 1e-8 , saveat = ts)
70
76
71
77
if SciMLBase. successful_retcode (new_sol)
72
- mean (abs2 .(reduce (hcat, get_vars (new_sol)) .- reduce (hcat, get_refs (sol_ref)) ))
78
+ mean (abs2 .(reduce (hcat, get_vars (new_sol)) .- data ))
73
79
else
74
80
Inf
75
81
end
76
82
end
77
83
78
84
of = OptimizationFunction {true} (loss, AutoZygote ())
79
85
80
- ps = (prob, sol_ref, get_vars, get_refs , set_x);
86
+ ps = (prob, get_vars, data, ts , set_x);
81
87
82
88
@test_call target_modules= (ModelingToolkitNeuralNets,) loss (x0, ps)
83
89
@test_opt target_modules= (ModelingToolkitNeuralNets,) loss (x0, ps)
@@ -89,7 +95,7 @@ ps = (prob, sol_ref, get_vars, get_refs, set_x);
89
95
@test all (.! isnan .(∇l1))
90
96
@test ! iszero (∇l1)
91
97
92
- @test ∇l1≈ ∇l2 rtol= 1e-5
98
+ @test ∇l1≈ ∇l2 rtol= 1e-4
93
99
@test ∇l1 ≈ ∇l3
94
100
95
101
op = OptimizationProblem (of, x0, ps)
@@ -99,34 +105,36 @@ op = OptimizationProblem(of, x0, ps)
99
105
# oh = []
100
106
101
107
# plot_cb = (opt_state, loss) -> begin
108
+ # opt_state.iter % 500 ≠ 0 && return false
102
109
# @info "step $(opt_state.iter), loss: $loss"
103
110
# push!(oh, opt_state)
104
111
# new_p = SciMLStructures.replace(Tunable(), prob.p, opt_state.u)
105
112
# new_prob = remake(prob, p = new_p)
106
- # sol = solve(new_prob, Rodas4() )
113
+ # sol = solve(new_prob, Vern9(), abstol = 1e-8, reltol = 1e-8 )
107
114
# display(plot(sol))
108
115
# false
109
116
# end
110
117
111
- res = solve (op, Adam (), maxiters = 10000 )# , callback = plot_cb)
118
+ res = solve (op, Adam (1e-3 ), maxiters = 25_000 )# , callback = plot_cb)
112
119
113
120
display (res. stats)
114
- @test res. objective < 1
121
+ @test res. objective < 1.5e-4
115
122
116
- res_p = set_x (prob, res. u)
117
- res_prob = remake (prob, p = res_p)
118
- res_sol = solve (res_prob, Vern9 ())
123
+ u0, p = set_x (prob, res. u)
124
+ res_prob = remake (prob; u0, p)
125
+ res_sol = solve (res_prob, Vern9 (), abstol = 1e-8 , reltol = 1e-8 , saveat = ts)
126
+
127
+ @test SciMLBase. successful_retcode (res_sol)
128
+ @test mean (abs2 .(reduce (hcat, get_vars (res_sol)) .- data)) ≈ res. objective
119
129
120
130
# using Plots
121
131
# plot(sol_ref, idxs = [model_true.x, model_true.y])
122
132
# plot!(res_sol, idxs = [sys.x, sys.y])
123
133
124
- @test SciMLBase. successful_retcode (res_sol)
125
-
126
134
function lotka_ude2 ()
127
135
@variables t x (t)= 3.1 y (t)= 1.5 pred (t)[1 : 2 ]
128
136
@parameters α= 1.3 [tunable= false ] δ= 1.8 [tunable= false ]
129
- chain = multi_layer_feed_forward (2 , 2 )
137
+ chain = multi_layer_feed_forward (2 , 2 ; width = 5 , initial_scaling_factor = 1 )
130
138
NN, p = SymbolicNeuralNetwork (; chain, n_input = 2 , n_output = 2 , rng = StableRNG (42 ))
131
139
Dt = ModelingToolkit. D_nounits
132
140
@@ -138,16 +146,16 @@ end
138
146
139
147
sys2 = mtkcompile (lotka_ude2 ())
140
148
141
- prob = ODEProblem {true, SciMLBase.FullSpecialize} (sys2, [], (0 , 1 .0 ))
149
+ prob = ODEProblem {true, SciMLBase.FullSpecialize} (sys2, [], (0 , 5 .0 ))
142
150
143
151
sol = solve (prob, Vern9 (), abstol = 1e-10 , reltol = 1e-8 )
144
152
145
153
@test SciMLBase. successful_retcode (sol)
146
154
147
- set_x2 = setp_oop (sys2, sys2. p)
148
- ps2 = (prob, sol_ref, get_vars, get_refs , set_x2);
155
+ set_x2 = setsym_oop (sys2, sys2. p)
156
+ ps2 = (prob, get_vars, data, ts , set_x2);
149
157
op2 = OptimizationProblem (of, x0, ps2)
150
158
151
- res2 = solve (op2, Adam (), maxiters = 10000 )
159
+ res2 = solve (op2, Adam (1e-3 ), maxiters = 25_000 )
152
160
153
161
@test res. u ≈ res2. u
0 commit comments