forked from SciML/DiffEqFlux.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_de.jl
628 lines (541 loc) · 22.4 KB
/
neural_de.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
abstract type NeuralDELayer <: Lux.AbstractExplicitLayer end
basic_tgrad(u,p,t) = zero(u)
Flux.trainable(m::NeuralDELayer) = (m.p,)
"""
Constructs a continuous-time recurrant neural network, also known as a neural
ordinary differential equation (neural ODE), with a fast gradient calculation
via adjoints [1]. At a high level this corresponds to solving the forward
differential equation, using a second differential equation that propagates the
derivatives of the loss backwards in time.
```julia
NeuralODE(model,tspan,alg=nothing,args...;kwargs...)
NeuralODE(model::FastChain,tspan,alg=nothing,args...;
sensealg=InterpolatingAdjoint(autojacvec=DiffEqSensitivity.ReverseDiffVJP(true)),
kwargs...)
```
Arguments:
- `model`: A Chain or FastChain neural network that defines the ̇x.
- `tspan`: The timespan to be solved on.
- `alg`: The algorithm used to solve the ODE. Defaults to `nothing`, i.e. the
default algorithm from DifferentialEquations.jl.
- `sensealg`: The choice of differentiation algorthm used in the backpropogation.
Defaults to an adjoint method, and with `FastChain` it defaults to utilizing
a tape-compiled ReverseDiff vector-Jacobian product for extra efficiency. Seee
the [Local Sensitivity Analysis](https://diffeq.sciml.ai/dev/analysis/sensitivity/)
documentation for more details.
- `kwargs`: Additional arguments splatted to the ODE solver. See the
[Common Solver Arguments](https://diffeq.sciml.ai/dev/basics/common_solver_opts/)
documentation for more details.
References:
[1] Pontryagin, Lev Semenovich. Mathematical theory of optimal processes. CRC press, 1987.
"""
struct NeuralODE{M,P,RE,T,A,K} <: NeuralDELayer
model::M
p::P
re::RE
tspan::T
args::A
kwargs::K
function NeuralODE(model,tspan,args...;p = nothing,kwargs...)
_p,re = Flux.destructure(model)
if p === nothing
p = _p
end
new{typeof(model),typeof(p),typeof(re),
typeof(tspan),typeof(args),typeof(kwargs)}(
model,p,re,tspan,args,kwargs)
end
function NeuralODE(model::FastChain,tspan,args...;p=initial_params(model),kwargs...)
re = nothing
new{typeof(model),typeof(p),typeof(re),
typeof(tspan),typeof(args),typeof(kwargs)}(
model,p,re,tspan,args,kwargs)
end
function NeuralODE(model::Lux.AbstractExplicitLayer,tspan,args...;p=nothing,kwargs...)
re = nothing
new{typeof(model),typeof(p),typeof(re),
typeof(tspan),typeof(args),typeof(kwargs)}(
model,p,re,tspan,args,kwargs)
end
end
Lux.initialparameters(rng::AbstractRNG, n::NeuralODE) = Lux.initialparameters(rng, n.model)
Lux.initialstates(rng::AbstractRNG, n::NeuralODE) = Lux.initialstates(rng, n.model)
function (n::NeuralODE)(x,p=n.p)
dudt_(u,p,t) = n.re(p)(u)
ff = ODEFunction{false}(dudt_,tgrad=basic_tgrad)
prob = ODEProblem{false}(ff,x,getfield(n,:tspan),p)
sense = InterpolatingAdjoint(autojacvec=ZygoteVJP())
solve(prob,n.args...;sensealg=sense,n.kwargs...)
end
function (n::NeuralODE{M})(x,p=n.p) where {M<:FastChain}
dudt_(u,p,t) = n.model(u,p)
ff = ODEFunction{false}(dudt_,tgrad=basic_tgrad)
prob = ODEProblem{false}(ff,x,n.tspan,p)
sense = InterpolatingAdjoint(autojacvec=ZygoteVJP())
solve(prob,n.args...;sensealg=sense,n.kwargs...)
end
function (n::NeuralODE{M})(x,p,st) where {M<:Lux.AbstractExplicitLayer}
function dudt(u,p,t;st=st)
u_, st = n.model(u,p,st)
return u_
end
ff = ODEFunction{false}(dudt,tgrad=basic_tgrad)
prob = ODEProblem{false}(ff,x,n.tspan,p)
sense = InterpolatingAdjoint(autojacvec=ZygoteVJP())
return solve(prob,n.args...;sensealg=sense,n.kwargs...), st
end
"""
Constructs a neural stochastic differential equation (neural SDE) with diagonal noise.
```julia
NeuralDSDE(model1,model2,tspan,alg=nothing,args...;
sensealg=TrackerAdjoint(),kwargs...)
NeuralDSDE(model1::FastChain,model2::FastChain,tspan,alg=nothing,args...;
sensealg=TrackerAdjoint(),kwargs...)
```
Arguments:
- `model1`: A Chain or FastChain neural network that defines the drift function.
- `model2`: A Chain or FastChain neural network that defines the diffusion function.
Should output a vector of the same size as the input.
- `tspan`: The timespan to be solved on.
- `alg`: The algorithm used to solve the ODE. Defaults to `nothing`, i.e. the
default algorithm from DifferentialEquations.jl.
- `sensealg`: The choice of differentiation algorthm used in the backpropogation.
Defaults to using reverse-mode automatic differentiation via Tracker.jl
- `kwargs`: Additional arguments splatted to the ODE solver. See the
[Common Solver Arguments](https://diffeq.sciml.ai/dev/basics/common_solver_opts/)
documentation for more details.
"""
struct NeuralDSDE{M,P,RE,M2,RE2,T,A,K} <: NeuralDELayer
p::P
len::Int
model1::M
re1::RE
model2::M2
re2::RE2
tspan::T
args::A
kwargs::K
function NeuralDSDE(model1,model2,tspan,args...;p = nothing, kwargs...)
p1,re1 = Flux.destructure(model1)
p2,re2 = Flux.destructure(model2)
if p === nothing
p = [p1;p2]
end
new{typeof(model1),typeof(p),typeof(re1),typeof(model2),typeof(re2),
typeof(tspan),typeof(args),typeof(kwargs)}(p,
length(p1),model1,re1,model2,re2,tspan,args,kwargs)
end
function NeuralDSDE(model1::FastChain,model2::FastChain,tspan,args...;
p1 = initial_params(model1),
p = [p1;initial_params(model2)], kwargs...)
re1 = nothing
re2 = nothing
new{typeof(model1),typeof(p),typeof(re1),typeof(model2),typeof(re2),
typeof(tspan),typeof(args),typeof(kwargs)}(p,
length(p1),model1,re1,model2,re2,tspan,args,kwargs)
end
function NeuralDSDE(model1::Lux.Chain,model2::Lux.Chain,tspan,args...;
p1 =nothing,
p = nothing, kwargs...)
re1 = nothing
re2 = nothing
new{typeof(model1),typeof(p),typeof(re1),typeof(model2),typeof(re2),
typeof(tspan),typeof(args),typeof(kwargs)}(p,
Int(1),model1,re1,model2,re2,tspan,args,kwargs)
end
end
function (n::NeuralDSDE)(x,p=n.p)
dudt_(u,p,t) = n.re1(p[1:n.len])(u)
g(u,p,t) = n.re2(p[(n.len+1):end])(u)
ff = SDEFunction{false}(dudt_,g,tgrad=basic_tgrad)
prob = SDEProblem{false}(ff,g,x,n.tspan,p)
solve(prob,n.args...;sensealg=TrackerAdjoint(),n.kwargs...)
end
function (n::NeuralDSDE{M})(x,p=n.p) where {M<:FastChain}
dudt_(u,p,t) = n.model1(u,p[1:n.len])
g(u,p,t) = n.model2(u,p[(n.len+1):end])
ff = SDEFunction{false}(dudt_,g,tgrad=basic_tgrad)
prob = SDEProblem{false}(ff,g,x,n.tspan,p)
solve(prob,n.args...;sensealg=TrackerAdjoint(),n.kwargs...)
end
function Lux.initialparameters(rng::AbstractRNG, n::NeuralDSDE)
p1 = Lux.initialparameters(rng, n.model1)
p2 = Lux.initialparameters(rng, n.model2)
return Lux.ComponentArray((p1 = p1, p2 = p2))
end
function Lux.initialstates(rng::AbstractRNG, n::NeuralDSDE)
st1 = Lux.initialstates(rng, n.model1)
st2 = Lux.initialstates(rng, n.model2)
return (state1 = st1, state2 = st2)
end
function (n::NeuralDSDE{M})(x,p,st) where {M<:Lux.AbstractExplicitLayer}
st1 = st.state1
st2 = st.state2
function dudt_(u,p,t;st=st1)
u_, st = n.model1(u,p.p1,st)
return u_
end
function g(u,p,t;st=st2)
u_, st = n.model2(u,p.p2,st)
return u_
end
ff = SDEFunction{false}(dudt_,g,tgrad=basic_tgrad)
prob = SDEProblem{false}(ff,g,x,n.tspan,p)
return solve(prob,n.args...;sensealg=InterpolatingAdjoint(),n.kwargs...), (state1 = st1, state2 = st2)
end
"""
Constructs a neural stochastic differential equation (neural SDE).
```julia
NeuralSDE(model1,model2,tspan,nbrown,alg=nothing,args...;
sensealg=TrackerAdjoint(),kwargs...)
NeuralSDE(model1::FastChain,model2::FastChain,tspan,nbrown,alg=nothing,args...;
sensealg=TrackerAdjoint(),kwargs...)
```
Arguments:
- `model1`: A Chain or FastChain neural network that defines the drift function.
- `model2`: A Chain or FastChain neural network that defines the diffusion function.
Should output a matrix that is nbrown x size(x,1).
- `tspan`: The timespan to be solved on.
- `nbrown`: The number of Brownian processes
- `alg`: The algorithm used to solve the ODE. Defaults to `nothing`, i.e. the
default algorithm from DifferentialEquations.jl.
- `sensealg`: The choice of differentiation algorthm used in the backpropogation.
Defaults to using reverse-mode automatic differentiation via Tracker.jl
- `kwargs`: Additional arguments splatted to the ODE solver. See the
[Common Solver Arguments](https://diffeq.sciml.ai/dev/basics/common_solver_opts/)
documentation for more details.
"""
struct NeuralSDE{P,M,RE,M2,RE2,T,A,K} <: NeuralDELayer
p::P
len::Int
model1::M
re1::RE
model2::M2
re2::RE2
tspan::T
nbrown::Int
args::A
kwargs::K
function NeuralSDE(model1,model2,tspan,nbrown,args...;p=nothing,kwargs...)
p1,re1 = Flux.destructure(model1)
p2,re2 = Flux.destructure(model2)
if p === nothing
p = [p1;p2]
end
new{typeof(p),typeof(model1),typeof(re1),typeof(model2),typeof(re2),
typeof(tspan),typeof(args),typeof(kwargs)}(
p,length(p1),model1,re1,model2,re2,tspan,nbrown,args,kwargs)
end
function NeuralSDE(model1::FastChain,model2::FastChain,tspan,nbrown,args...;
p1 = initial_params(model1),
p = [p1;initial_params(model2)], kwargs...)
re1 = nothing
re2 = nothing
new{typeof(p),typeof(model1),typeof(re1),typeof(model2),typeof(re2),
typeof(tspan),typeof(args),typeof(kwargs)}(
p,length(p1),model1,re1,model2,re2,tspan,nbrown,args,kwargs)
end
function NeuralSDE(model1::Lux.AbstractExplicitLayer, model2::Lux.AbstractExplicitLayer,tspan,nbrown,args...;
p1 = nothing, p = nothing, kwargs...)
re1 = nothing
re2 = nothing
new{typeof(p),typeof(model1),typeof(re1),typeof(model2),typeof(re2),
typeof(tspan),typeof(args),typeof(kwargs)}(
p,Int(1),model1,re1,model2,re2,tspan,nbrown,args,kwargs)
end
end
function (n::NeuralSDE)(x,p=n.p)
dudt_(u,p,t) = n.re1(p[1:n.len])(u)
g(u,p,t) = n.re2(p[(n.len+1):end])(u)
ff = SDEFunction{false}(dudt_,g,tgrad=basic_tgrad)
prob = SDEProblem{false}(ff,g,x,n.tspan,p,noise_rate_prototype=zeros(Float32,length(x),n.nbrown))
solve(prob,n.args...;sensealg=TrackerAdjoint(),n.kwargs...)
end
function (n::NeuralSDE{P,M})(x,p=n.p) where {P,M<:FastChain}
dudt_(u,p,t) = n.model1(u,p[1:n.len])
g(u,p,t) = n.model2(u,p[(n.len+1):end])
ff = SDEFunction{false}(dudt_,g,tgrad=basic_tgrad)
prob = SDEProblem{false}(ff,g,x,n.tspan,p,noise_rate_prototype=zeros(Float32,length(x),n.nbrown))
solve(prob,n.args...;sensealg=TrackerAdjoint(),n.kwargs...)
end
function Lux.initialparameters(rng::AbstractRNG, n::NeuralSDE)
p1 = Lux.initialparameters(rng, n.model1)
p2 = Lux.initialparameters(rng, n.model2)
return Lux.ComponentArray((p1 = p1, p2 = p2))
end
function Lux.initialstates(rng::AbstractRNG, n::NeuralSDE)
st1 = Lux.initialstates(rng, n.model1)
st2 = Lux.initialstates(rng, n.model2)
return (state1 = st1, state2 = st2)
end
function (n::NeuralSDE{P,M})(x,p,st) where {P,M<:Lux.AbstractExplicitLayer}
st1 = st.state1
st2 = st.state2
function dudt_(u,p,t;st=st1)
u_, st = n.model1(u,p.p1,st)
return u_
end
function g(u,p,t;st=st2)
u_, st = n.model2(u,p.p2,st)
return u_
end
ff = SDEFunction{false}(dudt_,g,tgrad=basic_tgrad)
prob = SDEProblem{false}(ff,g,x,n.tspan,p,noise_rate_prototype=zeros(Float32,length(x),n.nbrown))
solve(prob,n.args...;sensealg=InterpolatingAdjoint(),n.kwargs...), (state1 = st1, state2 = st2)
end
"""
Constructs a neural delay differential equation (neural DDE) with constant
delays.
```julia
NeuralCDDE(model,tspan,hist,lags,alg=nothing,args...;
sensealg=TrackerAdjoint(),kwargs...)
NeuralCDDE(model::FastChain,tspan,hist,lags,alg=nothing,args...;
sensealg=TrackerAdjoint(),kwargs...)
```
Arguments:
- `model`: A Chain or FastChain neural network that defines the derivative function.
Should take an input of size `[x;x(t-lag_1);...;x(t-lag_n)]` and produce and
output shaped like `x`.
- `tspan`: The timespan to be solved on.
- `hist`: Defines the history function `h(t)` for values before the start of the
integration.
- `lags`: Defines the lagged values that should be utilized in the neural network.
- `alg`: The algorithm used to solve the ODE. Defaults to `nothing`, i.e. the
default algorithm from DifferentialEquations.jl.
- `sensealg`: The choice of differentiation algorthm used in the backpropogation.
Defaults to using reverse-mode automatic differentiation via Tracker.jl
- `kwargs`: Additional arguments splatted to the ODE solver. See the
[Common Solver Arguments](https://diffeq.sciml.ai/dev/basics/common_solver_opts/)
documentation for more details.
"""
struct NeuralCDDE{P,M,RE,H,L,T,A,K} <: NeuralDELayer
p::P
model::M
re::RE
hist::H
lags::L
tspan::T
args::A
kwargs::K
function NeuralCDDE(model,tspan,hist,lags,args...;p=nothing,kwargs...)
_p,re = Flux.destructure(model)
if p === nothing
p = _p
end
new{typeof(p),typeof(model),typeof(re),typeof(hist),typeof(lags),
typeof(tspan),typeof(args),typeof(kwargs)}(p,model,
re,hist,lags,tspan,args,kwargs)
end
function NeuralCDDE(model::FastChain,tspan,hist,lags,args...;p = initial_params(model),kwargs...)
re = nothing
new{typeof(p),typeof(model),typeof(re),typeof(hist),typeof(lags),
typeof(tspan),typeof(args),typeof(kwargs)}(p,model,
re,hist,lags,tspan,args,kwargs)
end
end
function (n::NeuralCDDE)(x,p=n.p)
function dudt_(u,h,p,t)
_u = vcat(u,(h(p,t-lag) for lag in n.lags)...)
n.re(p)(_u)
end
ff = DDEFunction{false}(dudt_,tgrad=basic_tgrad)
prob = DDEProblem{false}(ff,x,n.hist,n.tspan,p,constant_lags = n.lags)
solve(prob,n.args...;sensealg=TrackerAdjoint(),n.kwargs...)
end
function (n::NeuralCDDE{P,M})(x,p=n.p) where {P,M<:FastChain}
function dudt_(u,h,p,t)
_u = vcat(u,(h(p,t-lag) for lag in n.lags)...)
n.model(_u,p)
end
ff = DDEFunction{false}(dudt_,tgrad=basic_tgrad)
prob = DDEProblem{false}(ff,x,n.hist,n.tspan,p,constant_lags = n.lags)
solve(prob,n.args...;sensealg=TrackerAdjoint(),n.kwargs...)
end
"""
Constructs a neural differential-algebraic equation (neural DAE).
```julia
NeuralDAE(model,constraints_model,tspan,alg=nothing,args...;
sensealg=TrackerAdjoint(),kwargs...)
NeuralDAE(model::FastChain,constraints_model,tspan,alg=nothing,args...;
sensealg=TrackerAdjoint(),kwargs...)
```
Arguments:
- `model`: A Chain or FastChain neural network that defines the derivative function.
Should take an input of size `x` and produce the residual of `f(dx,x,t)`
for only the differential variables.
- `constraints_model`: A function `constraints_model(u,p,t)` for the fixed
constaints to impose on the algebraic equations.
- `tspan`: The timespan to be solved on.
- `alg`: The algorithm used to solve the ODE. Defaults to `nothing`, i.e. the
default algorithm from DifferentialEquations.jl.
- `sensealg`: The choice of differentiation algorthm used in the backpropogation.
Defaults to using reverse-mode automatic differentiation via Tracker.jl
- `kwargs`: Additional arguments splatted to the ODE solver. See the
[Common Solver Arguments](https://diffeq.sciml.ai/dev/basics/common_solver_opts/)
documentation for more details.
"""
struct NeuralDAE{P,M,M2,D,RE,T,DV,A,K} <: NeuralDELayer
model::M
constraints_model::M2
p::P
du0::D
re::RE
tspan::T
differential_vars::DV
args::A
kwargs::K
function NeuralDAE(model,constraints_model,tspan,du0=nothing,args...;p=nothing,differential_vars=nothing,kwargs...)
_p,re = Flux.destructure(model)
if p === nothing
p = _p
end
new{typeof(p),typeof(model),typeof(constraints_model),
typeof(du0),typeof(re),typeof(tspan),
typeof(differential_vars),typeof(args),typeof(kwargs)}(
model,constraints_model,p,du0,re,tspan,differential_vars,
args,kwargs)
end
end
function (n::NeuralDAE)(x,du0=n.du0,p=n.p)
function f(du,u,p,t)
nn_out = n.re(p)(vcat(u,du))
alg_out = n.constraints_model(u,p,t)
iter_nn = 0
iter_consts = 0
map(n.differential_vars) do isdiff
if isdiff
iter_nn += 1
nn_out[iter_nn]
else
iter_consts += 1
alg_out[iter_consts]
end
end
end
prob = DAEProblem{false}(f,du0,x,n.tspan,p,differential_vars=n.differential_vars)
solve(prob,n.args...;sensealg=TrackerAdjoint(),n.kwargs...)
end
"""
Constructs a physically-constrained continuous-time recurrant neural network,
also known as a neural differential-algebraic equation (neural DAE), with a
mass matrix and a fast gradient calculation via adjoints [1]. The mass matrix
formulation is:
```math
Mu' = f(u,p,t)
```
where `M` is semi-explicit, i.e. singular with zeros for rows corresponding to
the constraint equations.
```julia
NeuralODEMM(model,constraints_model,tspan,mass_matrix,alg=nothing,args...;kwargs...)
NeuralODEMM(model::FastChain,tspan,mass_matrix,alg=nothing,args...;
sensealg=InterpolatingAdjoint(autojacvec=DiffEqSensitivity.ReverseDiffVJP(true)),
kwargs...)
```
Arguments:
- `model`: A Chain or FastChain neural network that defines the ̇`f(u,p,t)`
- `constraints_model`: A function `constraints_model(u,p,t)` for the fixed
constaints to impose on the algebraic equations.
- `tspan`: The timespan to be solved on.
- `mass_matrix`: The mass matrix associated with the DAE
- `alg`: The algorithm used to solve the ODE. Defaults to `nothing`, i.e. the
default algorithm from DifferentialEquations.jl. This method requires an
implicit ODE solver compatible with singular mass matrices. Consult the
[DAE solvers](https://diffeq.sciml.ai/latest/solvers/dae_solve/) documentation for more details.
- `sensealg`: The choice of differentiation algorthm used in the backpropogation.
Defaults to an adjoint method, and with `FastChain` it defaults to utilizing
a tape-compiled ReverseDiff vector-Jacobian product for extra efficiency. Seee
the [Local Sensitivity Analysis](https://diffeq.sciml.ai/dev/analysis/sensitivity/)
documentation for more details.
- `kwargs`: Additional arguments splatted to the ODE solver. See the
[Common Solver Arguments](https://diffeq.sciml.ai/dev/basics/common_solver_opts/)
documentation for more details.
"""
struct NeuralODEMM{M,M2,P,RE,T,MM,A,K} <: NeuralDELayer
model::M
constraints_model::M2
p::P
re::RE
tspan::T
mass_matrix::MM
args::A
kwargs::K
function NeuralODEMM(model,constraints_model,tspan,mass_matrix,args...;
p = nothing, kwargs...)
_p,re = Flux.destructure(model)
if p === nothing
p = _p
end
new{typeof(model),typeof(constraints_model),typeof(p),typeof(re),
typeof(tspan),typeof(mass_matrix),typeof(args),typeof(kwargs)}(
model,constraints_model,p,re,tspan,mass_matrix,args,kwargs)
end
function NeuralODEMM(model::FastChain,constraints_model,tspan,mass_matrix,args...;
p = initial_params(model), kwargs...)
re = nothing
new{typeof(model),typeof(constraints_model),typeof(p),typeof(re),
typeof(tspan),typeof(mass_matrix),typeof(args),typeof(kwargs)}(
model,constraints_model,p,re,tspan,mass_matrix,args,kwargs)
end
function NeuralODEMM(model::Lux.Chain,constraints_model,tspan,mass_matrix,args...;
p=nothing,kwargs...)
re = nothing
new{typeof(model),typeof(constraints_model),typeof(p),typeof(re),
typeof(tspan),typeof(mass_matrix),typeof(args),typeof(kwargs)}(
model,constraints_model,p,re,tspan,mass_matrix,args,kwargs)
end
end
function (n::NeuralODEMM)(x,p=n.p)
function f(u,p,t)
nn_out = n.re(p)(u)
alg_out = n.constraints_model(u,p,t)
vcat(nn_out,alg_out)
end
dudt_= ODEFunction{false}(f,mass_matrix=n.mass_matrix,tgrad=basic_tgrad)
prob = ODEProblem{false}(dudt_,x,n.tspan,p)
sense = InterpolatingAdjoint(autojacvec=ZygoteVJP())
solve(prob,n.args...;sensealg=sense,n.kwargs...)
end
function (n::NeuralODEMM{M})(x,p=n.p) where {M<:FastChain}
function f(u,p,t)
nn_out = n.model(u,p)
alg_out = n.constraints_model(u,p,t)
vcat(nn_out,alg_out)
end
dudt_= ODEFunction{false}(f;mass_matrix=n.mass_matrix,tgrad=basic_tgrad)
prob = ODEProblem{false}(dudt_,x,n.tspan,p)
sense = InterpolatingAdjoint(autojacvec=ZygoteVJP())
solve(prob,n.args...;sensealg=sense,n.kwargs...)
end
function (n::NeuralODEMM{M})(x,p,st) where {M<:Lux.AbstractExplicitLayer}
function f(u,p,t)
nn_out,st = n.model(u,p,st)
alg_out = n.constraints_model(u,p,t)
return vcat(nn_out,alg_out)
end
dudt_= ODEFunction{false}(f;mass_matrix=n.mass_matrix,tgrad=basic_tgrad)
prob = ODEProblem{false}(dudt_,x,n.tspan,p)
sense = InterpolatingAdjoint(autojacvec=ZygoteVJP())
return solve(prob,n.args...;sensealg=sense,n.kwargs...), st
end
"""
Constructs an Augmented Neural Differential Equation Layer.
```julia
AugmentedNDELayer(nde, adim::Int)
```
Arguments:
- `nde`: Any Neural Differential Equation Layer
- `adim`: The number of dimensions the initial conditions should be lifted
References:
[1] Dupont, Emilien, Arnaud Doucet, and Yee Whye Teh. "Augmented neural ODEs." In Proceedings of the 33rd International Conference on Neural Information Processing Systems, pp. 3140-3150. 2019.
"""
struct AugmentedNDELayer{DE<:NeuralDELayer} <: NeuralDELayer
nde::DE
adim::Int
end
(ande::AugmentedNDELayer)(x, args...) = ande.nde(augment(x, ande.adim), args...)
augment(x::AbstractVector{S}, augment_dim::Int) where S =
cat(x, zeros(S, (augment_dim,)), dims = 1)
augment(x::AbstractArray{S, T}, augment_dim::Int) where {S, T} =
cat(x, zeros(S, (size(x)[1:(T - 2)]..., augment_dim, size(x, T))), dims = T - 1)
Base.getproperty(ande::AugmentedNDELayer, sym::Symbol) =
hasproperty(ande, sym) ? getfield(ande, sym) : getfield(ande.nde, sym)