forked from SciML/NeuralPDE.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathode_solve.jl
476 lines (406 loc) · 16.3 KB
/
ode_solve.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
abstract type NeuralPDEAlgorithm <: DiffEqBase.AbstractODEAlgorithm end
"""
NNODE(chain, opt, init_params = nothing; autodiff = false, batch = 0, additional_loss = nothing, kwargs...)
Algorithm for solving ordinary differential equations using a neural network. This is a specialization
of the physics-informed neural network which is used as a solver for a standard `ODEProblem`.
!!! warn
Note that NNODE only supports ODEs which are written in the out-of-place form, i.e.
`du = f(u,p,t)`, and not `f(du,u,p,t)`. If not declared out-of-place, then the NNODE
will exit with an error.
## Positional Arguments
* `chain`: A neural network architecture, defined as a `Lux.AbstractExplicitLayer` or `Flux.Chain`.
`Flux.Chain` will be converted to `Lux` using `Lux.transform`.
* `opt`: The optimizer to train the neural network.
* `init_params`: The initial parameter of the neural network. By default, this is `nothing`
which thus uses the random initialization provided by the neural network library.
## Keyword Arguments
* `additional_loss`: A function additional_loss(phi, θ) where phi are the neural network trial solutions,
θ are the weights of the neural network(s).
* `autodiff`: The switch between automatic and numerical differentiation for
the PDE operators. The reverse mode of the loss function is always
automatic differentiation (via Zygote), this is only for the derivative
in the loss function (the derivative with respect to time).
* `batch`: The batch size to use for the internal quadrature. Defaults to `0`, which
means the application of the neural network is done at individual time points one
at a time. `batch>0` means the neural network is applied at a row vector of values
`t` simultaneously, i.e. it's the batch size for the neural network evaluations.
This requires a neural network compatible with batched data.
* `strategy`: The training strategy used to choose the points for the evaluations.
Default of `nothing` means that `QuadratureTraining` with QuadGK is used if no
`dt` is given, and `GridTraining` is used with `dt` if given.
* `kwargs`: Extra keyword arguments are splatted to the Optimization.jl `solve` call.
## Examples
```julia
u0 = [1.0, 1.0]
ts = [t for t in 1:100]
(u_, t_) = (analytical_func(ts), ts)
function additional_loss(phi, θ)
return sum(sum(abs2, [phi(t, θ) for t in t_] .- u_)) / length(u_)
end
alg = NNODE(chain, opt, additional_loss = additional_loss)
```
```julia
f(u,p,t) = cos(2pi*t)
tspan = (0.0, 1.0)
u0 = 0.0
prob = ODEProblem(linear, u0 ,tspan)
chain = Lux.Chain(Lux.Dense(1, 5, Lux.σ), Lux.Dense(5, 1))
opt = OptimizationOptimisers.Adam(0.1)
sol = solve(prob, NNODE(chain, opt), verbose = true, abstol = 1e-10, maxiters = 200)
```
## Solution Notes
Note that the solution is evaluated at fixed time points according to standard output handlers
such as `saveat` and `dt`. However, the neural network is a fully continuous solution so `sol(t)`
is an accurate interpolation (up to the neural network training result). In addition, the
`OptimizationSolution` is returned as `sol.k` for further analysis.
## References
Lagaris, Isaac E., Aristidis Likas, and Dimitrios I. Fotiadis. "Artificial neural networks for solving
ordinary and partial differential equations." IEEE Transactions on Neural Networks 9, no. 5 (1998): 987-1000.
"""
struct NNODE{C, O, P, B, K, AL <: Union{Nothing, Function},
S <: Union{Nothing, AbstractTrainingStrategy},
} <:
NeuralPDEAlgorithm
chain::C
opt::O
init_params::P
autodiff::Bool
batch::B
strategy::S
additional_loss::AL
kwargs::K
end
function NNODE(chain, opt, init_params = nothing;
strategy = nothing,
autodiff = false, batch = nothing, additional_loss = nothing, kwargs...)
!(chain isa Lux.AbstractExplicitLayer) && (chain = Lux.transform(chain))
NNODE(chain, opt, init_params, autodiff, batch, strategy, additional_loss, kwargs)
end
"""
ODEPhi(chain::Lux.AbstractExplicitLayer, t, u0, st)
Internal struct, used for representing the ODE solution as a neural network in a form that respects boundary conditions, i.e.
`phi(t) = u0 + t*NN(t)`.
"""
mutable struct ODEPhi{C, T, U, S}
chain::C
t0::T
u0::U
st::S
function ODEPhi(chain::Lux.AbstractExplicitLayer, t::Number, u0, st)
new{typeof(chain), typeof(t), typeof(u0), typeof(st)}(chain, t, u0, st)
end
end
function generate_phi_θ(chain::Lux.AbstractExplicitLayer, t, u0, init_params)
θ, st = Lux.setup(Random.default_rng(), chain)
if init_params === nothing
init_params = ComponentArrays.ComponentArray(θ)
else
init_params = ComponentArrays.ComponentArray(init_params)
end
ODEPhi(chain, t, u0, st), init_params
end
function (f::ODEPhi{C, T, U})(t::Number,
θ) where {C <: Lux.AbstractExplicitLayer, T, U <: Number}
y, st = f.chain(adapt(parameterless_type(ComponentArrays.getdata(θ)), [t]), θ, f.st)
ChainRulesCore.@ignore_derivatives f.st = st
f.u0 + (t - f.t0) * first(y)
end
function (f::ODEPhi{C, T, U})(t::AbstractVector,
θ) where {C <: Lux.AbstractExplicitLayer, T, U <: Number}
# Batch via data as row vectors
y, st = f.chain(adapt(parameterless_type(ComponentArrays.getdata(θ)), t'), θ, f.st)
ChainRulesCore.@ignore_derivatives f.st = st
f.u0 .+ (t' .- f.t0) .* y
end
function (f::ODEPhi{C, T, U})(t::Number, θ) where {C <: Lux.AbstractExplicitLayer, T, U}
y, st = f.chain(adapt(parameterless_type(ComponentArrays.getdata(θ)), [t]), θ, f.st)
ChainRulesCore.@ignore_derivatives f.st = st
f.u0 .+ (t .- f.t0) .* y
end
function (f::ODEPhi{C, T, U})(t::AbstractVector,
θ) where {C <: Lux.AbstractExplicitLayer, T, U}
# Batch via data as row vectors
y, st = f.chain(adapt(parameterless_type(ComponentArrays.getdata(θ)), t'), θ, f.st)
ChainRulesCore.@ignore_derivatives f.st = st
f.u0 .+ (t' .- f.t0) .* y
end
"""
ode_dfdx(phi, t, θ, autodiff)
Computes u' using either forward-mode automatic differentiation or numerical differentiation.
"""
function ode_dfdx end
function ode_dfdx(phi::ODEPhi{C, T, U}, t::Number, θ,
autodiff::Bool) where {C, T, U <: Number}
if autodiff
ForwardDiff.derivative(t -> phi(t, θ), t)
else
(phi(t + sqrt(eps(typeof(t))), θ) - phi(t, θ)) / sqrt(eps(typeof(t)))
end
end
function ode_dfdx(phi::ODEPhi{C, T, U}, t::Number, θ,
autodiff::Bool) where {C, T, U <: AbstractVector}
if autodiff
ForwardDiff.jacobian(t -> phi(t, θ), t)
else
(phi(t + sqrt(eps(typeof(t))), θ) - phi(t, θ)) / sqrt(eps(typeof(t)))
end
end
function ode_dfdx(phi::ODEPhi, t::AbstractVector, θ, autodiff::Bool)
if autodiff
ForwardDiff.jacobian(t -> phi(t, θ), t)
else
(phi(t .+ sqrt(eps(eltype(t))), θ) - phi(t, θ)) ./ sqrt(eps(eltype(t)))
end
end
"""
inner_loss(phi, f, autodiff, t, θ, p)
Simple L2 inner loss at a time `t` with parameters `θ` of the neural network.
"""
function inner_loss end
function inner_loss(phi::ODEPhi{C, T, U}, f, autodiff::Bool, t::Number, θ,
p) where {C, T, U <: Number}
sum(abs2, ode_dfdx(phi, t, θ, autodiff) - f(phi(t, θ), p, t))
end
function inner_loss(phi::ODEPhi{C, T, U}, f, autodiff::Bool, t::AbstractVector, θ,
p) where {C, T, U <: Number}
out = phi(t, θ)
fs = reduce(hcat, [f(out[i], p, t[i]) for i in axes(out, 2)])
dxdtguess = Array(ode_dfdx(phi, t, θ, autodiff))
sum(abs2, dxdtguess .- fs) / length(t)
end
function inner_loss(phi::ODEPhi{C, T, U}, f, autodiff::Bool, t::Number, θ,
p) where {C, T, U}
sum(abs2, ode_dfdx(phi, t, θ, autodiff) .- f(phi(t, θ), p, t))
end
function inner_loss(phi::ODEPhi{C, T, U}, f, autodiff::Bool, t::AbstractVector, θ,
p) where {C, T, U}
out = Array(phi(t, θ))
arrt = Array(t)
fs = reduce(hcat, [f(out[:, i], p, arrt[i]) for i in 1:size(out, 2)])
dxdtguess = Array(ode_dfdx(phi, t, θ, autodiff))
sum(abs2, dxdtguess .- fs) / length(t)
end
"""
generate_loss(strategy, phi, f, autodiff, tspan, p, batch)
Representation of the loss function, parametric on the training strategy `strategy`.
"""
function generate_loss(strategy::QuadratureTraining, phi, f, autodiff::Bool, tspan, p,
batch)
integrand(t::Number, θ) = abs2(inner_loss(phi, f, autodiff, t, θ, p))
integrand(ts, θ) = [abs2(inner_loss(phi, f, autodiff, t, θ, p)) for t in ts]
@assert batch == 0 # not implemented
function loss(θ, _)
intprob = IntegralProblem(integrand, tspan[1], tspan[2], θ)
sol = solve(intprob, QuadGKJL(); abstol = strategy.abstol, reltol = strategy.reltol)
sol.u
end
return loss
end
function generate_loss(strategy::GridTraining, phi, f, autodiff::Bool, tspan, p, batch)
ts = tspan[1]:(strategy.dx):tspan[2]
# sum(abs2,inner_loss(t,θ) for t in ts) but Zygote generators are broken
autodiff && throw(ArgumentError("autodiff not supported for GridTraining."))
function loss(θ, _)
if batch
sum(abs2, inner_loss(phi, f, autodiff, ts, θ, p))
else
sum(abs2, [inner_loss(phi, f, autodiff, t, θ, p) for t in ts])
end
end
return loss
end
function generate_loss(strategy::StochasticTraining, phi, f, autodiff::Bool, tspan, p,
batch)
# sum(abs2,inner_loss(t,θ) for t in ts) but Zygote generators are broken
autodiff && throw(ArgumentError("autodiff not supported for StochasticTraining."))
function loss(θ, _)
ts = adapt(parameterless_type(θ),
[(tspan[2] - tspan[1]) * rand() + tspan[1] for i in 1:(strategy.points)])
if batch
sum(abs2, inner_loss(phi, f, autodiff, ts, θ, p))
else
sum(abs2, [inner_loss(phi, f, autodiff, t, θ, p) for t in ts])
end
end
return loss
end
function generate_loss(strategy::WeightedIntervalTraining, phi, f, autodiff::Bool, tspan, p,
batch)
autodiff && throw(ArgumentError("autodiff not supported for WeightedIntervalTraining."))
minT = tspan[1]
maxT = tspan[2]
weights = strategy.weights ./ sum(strategy.weights)
N = length(weights)
points = strategy.points
difference = (maxT - minT) / N
data = Float64[]
for (index, item) in enumerate(weights)
temp_data = rand(1, trunc(Int, points * item)) .* difference .+ minT .+
((index - 1) * difference)
data = append!(data, temp_data)
end
ts = data
function loss(θ, _)
if batch
sum(abs2, inner_loss(phi, f, autodiff, ts, θ, p))
else
sum(abs2, [inner_loss(phi, f, autodiff, t, θ, p) for t in ts])
end
end
return loss
end
function evaluate_tstops_loss(phi, f, autodiff::Bool, tstops, p, batch)
# sum(abs2,inner_loss(t,θ) for t in ts) but Zygote generators are broken
function loss(θ, _)
if batch
sum(abs2, inner_loss(phi, f, autodiff, tstops, θ, p))
else
sum(abs2, [inner_loss(phi, f, autodiff, t, θ, p) for t in tstops])
end
end
return loss
end
function generate_loss(strategy::QuasiRandomTraining, phi, f, autodiff::Bool, tspan)
error("QuasiRandomTraining is not supported by NNODE since it's for high dimensional spaces only. Use StochasticTraining instead.")
end
struct NNODEInterpolation{T <: ODEPhi, T2}
phi::T
θ::T2
end
(f::NNODEInterpolation)(t, idxs::Nothing, ::Type{Val{0}}, p, continuity) = f.phi(t, f.θ)
(f::NNODEInterpolation)(t, idxs, ::Type{Val{0}}, p, continuity) = f.phi(t, f.θ)[idxs]
function (f::NNODEInterpolation)(t::Vector, idxs::Nothing, ::Type{Val{0}}, p, continuity)
out = f.phi(t, f.θ)
SciMLBase.RecursiveArrayTools.DiffEqArray([out[:, i] for i in axes(out, 2)], t)
end
function (f::NNODEInterpolation)(t::Vector, idxs, ::Type{Val{0}}, p, continuity)
out = f.phi(t, f.θ)
SciMLBase.RecursiveArrayTools.DiffEqArray([out[idxs, i] for i in axes(out, 2)], t)
end
SciMLBase.interp_summary(::NNODEInterpolation) = "Trained neural network interpolation"
function DiffEqBase.__solve(prob::DiffEqBase.AbstractODEProblem,
alg::NNODE,
args...;
dt = nothing,
timeseries_errors = true,
save_everystep = true,
adaptive = false,
abstol = 1.0f-6,
reltol = 1.0f-3,
verbose = false,
saveat = nothing,
maxiters = nothing,
tstops = nothing)
u0 = prob.u0
tspan = prob.tspan
f = prob.f
p = prob.p
t0 = tspan[1]
#hidden layer
chain = alg.chain
opt = alg.opt
autodiff = alg.autodiff
#train points generation
init_params = alg.init_params
!(chain isa Lux.AbstractExplicitLayer) && error("Only Lux.AbstractExplicitLayer neural networks are supported")
phi, init_params = generate_phi_θ(chain, t0, u0, init_params)
isinplace(prob) && throw(error("The NNODE solver only supports out-of-place ODE definitions, i.e. du=f(u,p,t)."))
try
phi(t0, init_params)
catch err
if isa(err, DimensionMismatch)
throw(DimensionMismatch("Dimensions of the initial u0 and chain should match"))
else
throw(err)
end
end
strategy = if alg.strategy === nothing
if dt !== nothing
GridTraining(dt)
else
QuadratureTraining(; quadrature_alg = QuadGKJL(),
reltol = convert(eltype(u0), reltol),
abstol = convert(eltype(u0), abstol), maxiters = maxiters,
batch = 0)
end
else
alg.strategy
end
batch = if alg.batch === nothing
if strategy isa QuadratureTraining
strategy.batch
else
true
end
else
alg.batch
end
inner_f = generate_loss(strategy, phi, f, autodiff, tspan, p, batch)
additional_loss = alg.additional_loss
# Creates OptimizationFunction Object from total_loss
function total_loss(θ, _)
L2_loss = inner_f(θ, phi)
if !(additional_loss isa Nothing)
L2_loss = L2_loss + additional_loss(phi, θ)
end
if !(tstops isa Nothing)
num_tstops_points = length(tstops)
tstops_loss_func = evaluate_tstops_loss(phi, f, autodiff, tstops, p, batch)
tstops_loss = tstops_loss_func(θ, phi)
if strategy isa GridTraining
num_original_points = length(tspan[1]:(strategy.dx):tspan[2])
elseif strategy isa Union{WeightedIntervalTraining, StochasticTraining}
num_original_points = strategy.points
else
return L2_loss + tstops_loss
end
total_original_loss = L2_loss * num_original_points
total_tstops_loss = tstops_loss * num_original_points
total_points = num_original_points + num_tstops_points
L2_loss = (total_original_loss + total_tstops_loss) / total_points
end
return L2_loss
end
# Choice of Optimization Algo for Training Strategies
opt_algo = if strategy isa QuadratureTraining
Optimization.AutoForwardDiff()
else
Optimization.AutoZygote()
end
# Creates OptimizationFunction Object from total_loss
optf = OptimizationFunction(total_loss, opt_algo)
iteration = 0
callback = function (p, l)
iteration += 1
verbose && println("Current loss is: $l, Iteration: $iteration")
l < abstol
end
optprob = OptimizationProblem(optf, init_params)
res = solve(optprob, opt; callback, maxiters, alg.kwargs...)
#solutions at timepoints
if saveat isa Number
ts = tspan[1]:saveat:tspan[2]
elseif saveat isa AbstractArray
ts = saveat
elseif dt !== nothing
ts = tspan[1]:dt:tspan[2]
elseif save_everystep
ts = range(tspan[1], tspan[2], length = 100)
else
ts = [tspan[1], tspan[2]]
end
if u0 isa Number
u = [first(phi(t, res.u)) for t in ts]
else
u = [phi(t, res.u) for t in ts]
end
sol = DiffEqBase.build_solution(prob, alg, ts, u;
k = res, dense = true,
interp = NNODEInterpolation(phi, res.u),
calculate_error = false,
retcode = ReturnCode.Success)
DiffEqBase.has_analytic(prob.f) &&
DiffEqBase.calculate_solution_errors!(sol; timeseries_errors = true,
dense_errors = false)
sol
end #solve