|
| 1 | +struct NOMAD{T1, T2} |
| 2 | + approximator_net::T1 |
| 3 | + decoder_net::T2 |
| 4 | +end |
| 5 | + |
| 6 | +""" |
| 7 | +`NOMAD(architecture_approximator::Tuple, architecture_decoder::Tuple, |
| 8 | + act_approximator = identity, act_decoder=true; |
| 9 | + init_approximator = Flux.glorot_uniform, |
| 10 | + init_decoder = Flux.glorot_uniform, |
| 11 | + bias_approximator=true, bias_decoder=true)` |
| 12 | +`NOMAD(approximator_net::Flux.Chain, decoder_net::Flux.Chain)` |
| 13 | +
|
| 14 | +Create a Nonlinear Manifold Decoders for Operator Learning (NOMAD) as proposed by Lu et al. |
| 15 | +arXiv:2206.03551 |
| 16 | +
|
| 17 | +The decoder is defined as follows: |
| 18 | +
|
| 19 | +``\\tilde D (β, y) = f(β, y)`` |
| 20 | +
|
| 21 | +# Usage |
| 22 | +
|
| 23 | +```julia |
| 24 | +julia> model = NOMAD((16,32,16), (24,32)) |
| 25 | +NOMAD with |
| 26 | +Approximator net: (Chain(Dense(16 => 32), Dense(32 => 16))) |
| 27 | +Decoder net: (Chain(Dense(24 => 32, true))) |
| 28 | +
|
| 29 | +julia> model = NeuralOperators.NOMAD((32,64,32), (64,72), σ, tanh; init_approximator=Flux.glorot_normal, bias_decoder=false) |
| 30 | +NOMAD with |
| 31 | +Approximator net: (Chain(Dense(32 => 64, σ), Dense(64 => 32, σ))) |
| 32 | +Decoder net: (Chain(Dense(64 => 72, tanh; bias=false))) |
| 33 | +
|
| 34 | +julia> approximator = Chain(Dense(2,128),Dense(128,64)) |
| 35 | +Chain( |
| 36 | + Dense(2 => 128), # 384 parameters |
| 37 | + Dense(128 => 64), # 8_256 parameters |
| 38 | +) # Total: 4 arrays, 8_640 parameters, 34.000 KiB. |
| 39 | +
|
| 40 | +julia> decoder = Chain(Dense(72,24),Dense(24,12)) |
| 41 | +Chain( |
| 42 | + Dense(72 => 24), # 1_752 parameters |
| 43 | + Dense(24 => 12), # 300 parameters |
| 44 | +) # Total: 4 arrays, 2_052 parameters, 8.266 KiB. |
| 45 | +
|
| 46 | +julia> model = NOMAD(approximator, decoder) |
| 47 | +NOMAD with |
| 48 | +Approximator net: (Chain(Dense(2 => 128), Dense(128 => 64))) |
| 49 | +Decoder net: (Chain(Dense(72 => 24), Dense(24 => 12))) |
| 50 | +""" |
| 51 | +function NOMAD(architecture_approximator::Tuple, architecture_decoder::Tuple, |
| 52 | + act_approximator = identity, act_decoder=true; |
| 53 | + init_approximator = Flux.glorot_uniform, |
| 54 | + init_decoder = Flux.glorot_uniform, |
| 55 | + bias_approximator=true, bias_decoder=true) |
| 56 | + |
| 57 | + approximator_net = construct_subnet(architecture_approximator, act_approximator; |
| 58 | + init=init_approximator, bias=bias_approximator) |
| 59 | + |
| 60 | + decoder_net = construct_subnet(architecture_decoder, act_decoder; |
| 61 | + init=init_decoder, bias=bias_decoder) |
| 62 | + |
| 63 | + return NOMAD{typeof(approximator_net), typeof(decoder_net)}(approximator_net, decoder_net) |
| 64 | +end |
| 65 | + |
| 66 | +Flux.@functor NOMAD |
| 67 | + |
| 68 | +function (a::NOMAD)(x::AbstractArray, y::AbstractVecOrMat) |
| 69 | + # Assign the parameters |
| 70 | + approximator, decoder = a.approximator_net, a.decoder_net |
| 71 | + |
| 72 | + return decoder(cat(approximator(x), y', dims=1))' |
| 73 | +end |
| 74 | + |
| 75 | +# Print nicely |
| 76 | +function Base.show(io::IO, l::NOMAD) |
| 77 | + print(io, "NOMAD with\nApproximator net: (",l.approximator_net) |
| 78 | + print(io, ")\n") |
| 79 | + print(io, "Decoder net: (", l.decoder_net) |
| 80 | + print(io, ")\n") |
| 81 | +end |
0 commit comments