forked from SciML/NeuralPDE.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinn_types.jl
554 lines (510 loc) · 18.3 KB
/
pinn_types.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
"""
???
"""
struct LogOptions
log_frequency::Int64
# TODO: add in an option for saving plots in the log. this is currently not done because the type of plot is dependent on the PDESystem
# possible solution: pass in a plot function?
# this is somewhat important because we want to support plotting adaptive weights that depend on pde independent variables
# and not just one weight for each loss function, i.e. pde_loss_weights(i, t, x) and since this would be function-internal,
# we'd want the plot & log to happen internally as well
# plots of the learned function can happen in the outer callback, but we might want to offer that here too
SciMLBase.@add_kwonly function LogOptions(; log_frequency = 50)
new(convert(Int64, log_frequency))
end
end
"""This function is defined here as stubs to be overriden by the subpackage NeuralPDELogging if imported"""
function logvector(logger, v::AbstractVector{R}, name::AbstractString,
step::Integer) where {R <: Real}
nothing
end
"""This function is defined here as stubs to be overriden by the subpackage NeuralPDELogging if imported"""
function logscalar(logger, s::R, name::AbstractString, step::Integer) where {R <: Real}
nothing
end
"""
PhysicsInformedNN(chain,
strategy;
init_params = nothing,
phi = nothing,
param_estim = false,
additional_loss = nothing,
adaptive_loss = nothing,
logger = nothing,
log_options = LogOptions(),
iteration = nothing,
kwargs...)
A `discretize` algorithm for the ModelingToolkit PDESystem interface, which transforms a
`PDESystem` into an `OptimizationProblem` using the Physics-Informed Neural Networks (PINN)
methodology.
## Positional Arguments
* `chain`: a vector of Lux/Flux chains with a d-dimensional input and a
1-dimensional output corresponding to each of the dependent variables. Note that this
specification respects the order of the dependent variables as specified in the PDESystem.
Flux chains will be converted to Lux internally using `Lux.transform`.
* `strategy`: determines which training strategy will be used. See the Training Strategy
documentation for more details.
## Keyword Arguments
* `init_params`: the initial parameters of the neural networks. If `init_params` is not
given, then the neural network default parameters are used. Note that for Lux, the default
will convert to Float64.
* `phi`: a trial solution, specified as `phi(x,p)` where `x` is the coordinates vector for
the dependent variable and `p` are the weights of the phi function (generally the weights
of the neural network defining `phi`). By default, this is generated from the `chain`. This
should only be used to more directly impose functional information in the training problem,
for example imposing the boundary condition by the test function formulation.
* `adaptive_loss`: the choice for the adaptive loss function. See the
[adaptive loss page](@ref adaptive_loss) for more details. Defaults to no adaptivity.
* `additional_loss`: a function `additional_loss(phi, θ, p_)` where `phi` are the neural
network trial solutions, `θ` are the weights of the neural network(s), and `p_` are the
hyperparameters of the `OptimizationProblem`. If `param_estim = true`, then `θ` additionally
contains the parameters of the differential equation appended to the end of the vector.
* `param_estim`: whether the parameters of the differential equation should be included in
the values sent to the `additional_loss` function. Defaults to `false`.
* `logger`: ?? needs docs
* `log_options`: ?? why is this separate from the logger?
* `iteration`: used to control the iteration counter???
* `kwargs`: Extra keyword arguments which are splatted to the `OptimizationProblem` on `solve`.
"""
struct PhysicsInformedNN{T, P, PH, DER, PE, AL, ADA, LOG, K} <: AbstractPINN
chain::Any
strategy::T
init_params::P
phi::PH
derivative::DER
param_estim::PE
additional_loss::AL
adaptive_loss::ADA
logger::LOG
log_options::LogOptions
iteration::Vector{Int64}
self_increment::Bool
multioutput::Bool
kwargs::K
@add_kwonly function PhysicsInformedNN(chain,
strategy;
init_params = nothing,
phi = nothing,
derivative = nothing,
param_estim = false,
additional_loss = nothing,
adaptive_loss = nothing,
logger = nothing,
log_options = LogOptions(),
iteration = nothing,
kwargs...)
multioutput = chain isa AbstractArray
if multioutput
!all(i -> i isa Lux.AbstractExplicitLayer, chain) && (chain = Lux.transform.(chain))
else
!(chain isa Lux.AbstractExplicitLayer) && (chain = Lux.transform(chain))
end
if phi === nothing
if multioutput
_phi = Phi.(chain)
else
_phi = Phi(chain)
end
else
if multioutput
all([phi.f[i] isa Lux.AbstractExplicitLayer for i in eachindex(phi.f)]) || throw(ArgumentError("Only Lux Chains are supported"))
else
(phi.f isa Lux.AbstractExplicitLayer) || throw(ArgumentError("Only Lux Chains are supported"))
end
_phi = phi
end
if derivative === nothing
_derivative = numeric_derivative
else
_derivative = derivative
end
if iteration isa Vector{Int64}
self_increment = false
else
iteration = [1]
self_increment = true
end
new{typeof(strategy), typeof(init_params), typeof(_phi), typeof(_derivative),
typeof(param_estim),
typeof(additional_loss), typeof(adaptive_loss), typeof(logger), typeof(kwargs)}(chain,
strategy,
init_params,
_phi,
_derivative,
param_estim,
additional_loss,
adaptive_loss,
logger,
log_options,
iteration,
self_increment,
multioutput,
kwargs)
end
end
"""
BayesianPINN(chain,
strategy;
init_params = nothing,
phi = nothing,
param_estim = false,
additional_loss = nothing,
adaptive_loss = nothing,
logger = nothing,
log_options = LogOptions(),
iteration = nothing,
dataset = nothing,
kwargs...)
A `discretize` algorithm for the ModelingToolkit PDESystem interface, which transforms a
`PDESystem` into a likelihood function used for HMC based Posterior Sampling Algorithms [AdvancedHMC.jl](https://turinglang.org/AdvancedHMC.jl/stable/)
which is later optimized upon to give the Solution Distribution of the PDE, using the Physics-Informed Neural Networks (PINN)
methodology.
## Positional Arguments
* `chain`: a vector of Lux.jl chains with a d-dimensional input and a
1-dimensional output corresponding to each of the dependent variables. Note that this
specification respects the order of the dependent variables as specified in the PDESystem.
* `strategy`: determines which training strategy will be used. See the Training Strategy
documentation for more details.
## Keyword Arguments
* `Dataset`: A vector of matrix, each matrix for ith dependant
variable and first col in matrix is for dependant variables,
remaining coloumns for independant variables. Needed for inverse problem solving.
* `init_params`: the initial parameters of the neural networks. If `init_params` is not
given, then the neural network default parameters are used. Note that for Lux, the default
will convert to Float64.
* `phi`: a trial solution, specified as `phi(x,p)` where `x` is the coordinates vector for
the dependent variable and `p` are the weights of the phi function (generally the weights
of the neural network defining `phi`). By default, this is generated from the `chain`. This
should only be used to more directly impose functional information in the training problem,
for example imposing the boundary condition by the test function formulation.
* `adaptive_loss`: (STILL WIP), the choice for the adaptive loss function. See the
[adaptive loss page](@ref adaptive_loss) for more details. Defaults to no adaptivity.
* `additional_loss`: a function `additional_loss(phi, θ, p_)` where `phi` are the neural
network trial solutions, `θ` are the weights of the neural network(s), and `p_` are the
hyperparameters . If `param_estim = true`, then `θ` additionally
contains the parameters of the differential equation appended to the end of the vector.
* `param_estim`: whether the parameters of the differential equation should be included in
the values sent to the `additional_loss` function. Defaults to `false`.
* `logger`: ?? needs docs
* `log_options`: ?? why is this separate from the logger?
* `iteration`: used to control the iteration counter???
* `kwargs`: Extra keyword arguments.
"""
struct BayesianPINN{T, P, PH, DER, PE, AL, ADA, LOG, D, K} <: AbstractPINN
chain::Any
strategy::T
init_params::P
phi::PH
derivative::DER
param_estim::PE
additional_loss::AL
adaptive_loss::ADA
logger::LOG
log_options::LogOptions
iteration::Vector{Int64}
self_increment::Bool
multioutput::Bool
dataset::D
kwargs::K
@add_kwonly function BayesianPINN(chain,
strategy;
init_params = nothing,
phi = nothing,
derivative = nothing,
param_estim = false,
additional_loss = nothing,
adaptive_loss = nothing,
logger = nothing,
log_options = LogOptions(),
iteration = nothing,
dataset = nothing,
kwargs...)
multioutput = chain isa AbstractArray
if multioutput
!all(i -> i isa Lux.AbstractExplicitLayer, chain) && (chain = Lux.transform.(chain))
else
!(chain isa Lux.AbstractExplicitLayer) && (chain = Lux.transform(chain))
end
if phi === nothing
if multioutput
_phi = Phi.(chain)
else
_phi = Phi(chain)
end
else
if multioutput
all([phi.f[i] isa Lux.AbstractExplicitLayer for i in eachindex(phi.f)]) || throw(ArgumentError("Only Lux Chains are supported"))
else
(phi.f isa Lux.AbstractExplicitLayer) || throw(ArgumentError("Only Lux Chains are supported"))
end
_phi = phi
end
if derivative === nothing
_derivative = numeric_derivative
else
_derivative = derivative
end
if iteration isa Vector{Int64}
self_increment = false
else
iteration = [1]
self_increment = true
end
if dataset isa Nothing
dataset = (nothing, nothing)
end
new{typeof(strategy), typeof(init_params), typeof(_phi), typeof(_derivative),
typeof(param_estim),
typeof(additional_loss), typeof(adaptive_loss), typeof(logger), typeof(dataset),
typeof(kwargs)}(chain,
strategy,
init_params,
_phi,
_derivative,
param_estim,
additional_loss,
adaptive_loss,
logger,
log_options,
iteration,
self_increment,
multioutput,
dataset,
kwargs)
end
end
"""
`PINNRepresentation``
An internal representation of a physics-informed neural network (PINN). This is the struct
used internally and returned for introspection by `symbolic_discretize`.
## Fields
$(FIELDS)
"""
mutable struct PINNRepresentation
"""
The equations of the PDE
"""
eqs::Any
"""
The boundary condition equations
"""
bcs::Any
"""
The domains for each of the independent variables
"""
domains::Any
"""
???
"""
eq_params::Any
"""
???
"""
defaults::Any
"""
???
"""
default_p::Any
"""
Whether parameters are to be appended to the `additional_loss`
"""
param_estim::Any
"""
The `additional_loss` function as provided by the user
"""
additional_loss::Any
"""
The adaptive loss function
"""
adaloss::Any
"""
The dependent variables of the system
"""
depvars::Any
"""
The independent variables of the system
"""
indvars::Any
"""
A dictionary form of the independent variables. Define the structure ???
"""
dict_indvars::Any
"""
A dictionary form of the dependent variables. Define the structure ???
"""
dict_depvars::Any
"""
???
"""
dict_depvar_input::Any
"""
The logger as provided by the user
"""
logger::Any
"""
Whether there are multiple outputs, i.e. a system of PDEs
"""
multioutput::Bool
"""
The iteration counter used inside the cost function
"""
iteration::Vector{Int}
"""
The initial parameters as provided by the user. If the PDE is a system of PDEs, this
will be an array of arrays. If Lux.jl is used, then this is an array of ComponentArrays.
"""
init_params::Any
"""
The initial parameters as a flattened array. This is the array that is used in the
construction of the OptimizationProblem. If a Lux.jl neural network is used, then this
flattened form is a `ComponentArray`. If the equation is a system of equations, then
`flat_init_params.depvar.x` are the parameters for the neural network corresponding
to the dependent variable `x`, and i.e. if `depvar[i] == :x` then for `phi[i]`.
If `param_estim = true`, then `flat_init_params.p` are the parameters and
`flat_init_params.depvar.x` are the neural network parameters, so
`flat_init_params.depvar.x` would be the parameters of the neural network for the
dependent variable `x` if it's a system.
"""
flat_init_params::Any
"""
The representation of the test function of the PDE solution
"""
phi::Any
"""
The function used for computing the derivative
"""
derivative::Any
"""
The training strategy as provided by the user
"""
strategy::AbstractTrainingStrategy
"""
???
"""
pde_indvars::Any
"""
???
"""
bc_indvars::Any
"""
???
"""
pde_integration_vars::Any
"""
???
"""
bc_integration_vars::Any
"""
???
"""
integral::Any
"""
The PDE loss functions as represented in Julia AST
"""
symbolic_pde_loss_functions::Any
"""
The boundary condition loss functions as represented in Julia AST
"""
symbolic_bc_loss_functions::Any
"""
The PINNLossFunctions, i.e. the generated loss functions
"""
loss_functions::Any
end
"""
`PINNLossFunctions``
The generated functions from the PINNRepresentation
## Fields
$(FIELDS)
"""
struct PINNLossFunctions
"""
The boundary condition loss functions
"""
bc_loss_functions::Any
"""
The PDE loss functions
"""
pde_loss_functions::Any
"""
The full loss function, combining the PDE and boundary condition loss functions.
This is the loss function that is used by the optimizer.
"""
full_loss_function::Any
"""
The wrapped `additional_loss`, as pieced together for the optimizer.
"""
additional_loss_function::Any
"""
The pre-data version of the PDE loss function
"""
datafree_pde_loss_functions::Any
"""
The pre-data version of the BC loss function
"""
datafree_bc_loss_functions::Any
end
"""
An encoding of the test function phi that is used for calculating the PDE
value at domain points x
Fields:
- `f`: A representation of the chain function.
- `st`: The state of the Lux.AbstractExplicitLayer. It should be updated on each call.
"""
mutable struct Phi{C, S}
f::C
st::S
function Phi(chain::Lux.AbstractExplicitLayer)
st = Lux.initialstates(Random.default_rng(), chain)
new{typeof(chain), typeof(st)}(chain, st)
end
end
function (f::Phi{<:Lux.AbstractExplicitLayer})(x::Number, θ)
y, st = f.f(adapt(parameterless_type(ComponentArrays.getdata(θ)), [x]), θ, f.st)
ChainRulesCore.@ignore_derivatives f.st = st
y
end
function (f::Phi{<:Lux.AbstractExplicitLayer})(x::AbstractArray, θ)
y, st = f.f(adapt(parameterless_type(ComponentArrays.getdata(θ)), x), θ, f.st)
ChainRulesCore.@ignore_derivatives f.st = st
y
end
function (f::Phi{<:Optimisers.Restructure})(x, θ)
f.f(θ)(adapt(parameterless_type(θ), x))
end
function get_u()
u = (cord, θ, phi) -> phi(cord, θ)
end
# the method to calculate the derivative
function numeric_derivative(phi, u, x, εs, order, θ)
_type = parameterless_type(ComponentArrays.getdata(θ))
ε = εs[order]
_epsilon = inv(first(ε[ε .!= zero(ε)]))
ε = adapt(_type, ε)
x = adapt(_type, x)
# any(x->x!=εs[1],εs)
# εs is the epsilon for each order, if they are all the same then we use a fancy formula
# if order 1, this is trivially true
if order > 4 || any(x -> x != εs[1], εs)
return (numeric_derivative(phi, u, x .+ ε, @view(εs[1:(end - 1)]), order - 1, θ)
.-
numeric_derivative(phi, u, x .- ε, @view(εs[1:(end - 1)]), order - 1, θ)) .*
_epsilon ./ 2
elseif order == 4
return (u(x .+ 2 .* ε, θ, phi) .- 4 .* u(x .+ ε, θ, phi)
.+
6 .* u(x, θ, phi)
.-
4 .* u(x .- ε, θ, phi) .+ u(x .- 2 .* ε, θ, phi)) .* _epsilon^4
elseif order == 3
return (u(x .+ 2 .* ε, θ, phi) .- 2 .* u(x .+ ε, θ, phi) .+ 2 .* u(x .- ε, θ, phi)
-
u(x .- 2 .* ε, θ, phi)) .* _epsilon^3 ./ 2
elseif order == 2
return (u(x .+ ε, θ, phi) .+ u(x .- ε, θ, phi) .- 2 .* u(x, θ, phi)) .* _epsilon^2
elseif order == 1
return (u(x .+ ε, θ, phi) .- u(x .- ε, θ, phi)) .* _epsilon ./ 2
else
error("This shouldn't happen!")
end
end