-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from JuliaGNI/clean_up_symplectic_autoencoder…
…_script Symplectic Autoencoder and PSD architecture
- Loading branch information
Showing
13 changed files
with
454 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,85 @@ | ||
""" | ||
Abstract `AutoEncoder` type. If a custom `<:AutoEncoder` architecture is implemented it should have the fields `full_dim`, `reduced_dim`, `n_encoder_blocks` and `n_decoder_blocks`. Further the routines `encoder`, `decoder`, `encoder_parameters` and `decoder_parameters` should be extended. | ||
""" | ||
abstract type AutoEncoder <: Architecture end | ||
|
||
struct AutoEncoder <: Architecture end | ||
""" | ||
Abstract `Encoder` type. If a custom `<:Encoder` architecture is implemented it should have the fields `full_dim`, `reduced_dim` and `n_encoder_blocks`. | ||
""" | ||
abstract type Encoder <: Architecture end | ||
|
||
""" | ||
Abstract `Decoder` type. If a custom `<:Decoder` architecture is implemented it should have the fields `full_dim`, `reduced_dim` and `n_decoder_blocks`. | ||
""" | ||
abstract type Decoder <: Architecture end | ||
|
||
struct UnknownEncoder <: Encoder | ||
full_dim::Int | ||
reduced_dim::Int | ||
n_encoder_blocks::Int | ||
end | ||
|
||
struct UnknownDecoder <: Decoder | ||
full_dim::Int | ||
reduced_dim::Int | ||
n_decoder_blocks::Int | ||
end | ||
|
||
""" | ||
This function gives iterations from the full dimension to the reduced dimension (i.e. the intermediate steps). The iterations are given in ascending order. | ||
""" | ||
function compute_iterations(full_dim::Integer, reduced_dim::Integer, n_blocks::Integer) | ||
iterations = Vector{Int}(reduced_dim : (full_dim - reduced_dim) ÷ (n_blocks - 1) : full_dim) | ||
iterations[end] = full_dim | ||
iterations | ||
end | ||
|
||
function compute_encoder_iterations(arch::AutoEncoder) | ||
compute_iterations(arch.full_dim, arch.reduced_dim, arch.n_encoder_blocks) | ||
end | ||
|
||
function compute_decoder_iterations(arch::AutoEncoder) | ||
compute_iterations(arch.full_dim, arch.reduced_dim, arch.n_decoder_blocks) | ||
end | ||
|
||
""" | ||
Takes as input the autoencoder architecture and a vector of integers specifying the layer dimensions in the encoder. Has to return a tuple of `AbstractExplicitLayer`s. | ||
""" | ||
encoder_layers_from_iteration(::AutoEncoder, ::AbstractVector{<:Integer}) = error("You have to implement `encoder_layers_from_iteration` for this autoencoder architecture!") | ||
|
||
""" | ||
Takes as input the autoencoder architecture and a vector of integers specifying the layer dimensions in the decoder. Has to return a tuple of `AbstractExplicitLayer`s. | ||
""" | ||
decoder_layers_from_iteration(::AutoEncoder, ::AbstractVector{<:Integer}) = error("You have to implement `decoder_layers_from_iteration` for this autoencoder architecture!") | ||
|
||
function encoder_model(arch::AutoEncoder) | ||
encoder_iterations = reverse(compute_encoder_iterations(arch)) | ||
Chain(encoder_layers_from_iteration(arch, encoder_iterations)...) | ||
end | ||
|
||
function decoder_model(arch::AutoEncoder) | ||
decoder_iterations = compute_decoder_iterations(arch) | ||
Chain(decoder_layers_from_iteration(arch, decoder_iterations)...) | ||
end | ||
|
||
function encoder_parameters(nn::NeuralNetwork{<:AutoEncoder}) | ||
n_encoder_layers = length(encoder_model(nn.architecture).layers) | ||
nn.params[1:n_encoder_layers] | ||
end | ||
|
||
function decoder_parameters(nn::NeuralNetwork{<:AutoEncoder}) | ||
n_decoder_layers = length(decoder_model(nn.architecture).layers) | ||
nn.params[(end - (n_decoder_layers - 1)):end] | ||
end | ||
|
||
function Chain(arch::AutoEncoder) | ||
Chain(encoder_model(arch).layers..., decoder_model(arch).layers...) | ||
end | ||
|
||
function encoder(nn::NeuralNetwork{<:AutoEncoder}) | ||
NeuralNetwork(UnknownEncoder(nn.architecture.full_dim, nn.architecture.reduced_dim, nn.architecture.n_encoder_blocks), encoder_model(nn.architecture), encoder_parameters(nn), get_backend(nn)) | ||
end | ||
|
||
function decoder(nn::NeuralNetwork{<:AutoEncoder}) | ||
NeuralNetwork(UnknownDecoder(nn.architecture.full_dim, nn.architecture.reduced_dim, nn.architecture.n_encoder_blocks), decoder_model(nn.architecture), decoder_parameters(nn), get_backend(nn)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
struct PSDArch <: AutoEncoder | ||
full_dim::Int | ||
reduced_dim::Int | ||
n_encoder_blocks::Int | ||
n_decoder_blocks::Int | ||
end | ||
|
||
function PSDArch(full_dim::Integer, reduced_dim::Integer) | ||
@assert iseven(full_dim) && iseven(reduced_dim) "Full order and reduced dimension have to be even!" | ||
PSDArch(full_dim, reduced_dim, 2, 2) | ||
end | ||
|
||
function encoder_layers_from_iteration(arch::PSDArch, encoder_iterations::AbstractVector{<:Integer}) | ||
@assert length(encoder_iterations) == 2 | ||
@assert arch.full_dim == encoder_iterations[1] | ||
@assert arch.reduced_dim == encoder_iterations[2] | ||
|
||
(PSDLayer(arch.full_dim, arch.reduced_dim), ) | ||
end | ||
|
||
function decoder_layers_from_iteration(arch::PSDArch, decoder_iterations::AbstractVector{<:Integer}) | ||
@assert length(decoder_iterations) == 2 | ||
@assert arch.full_dim == decoder_iterations[2] | ||
@assert arch.reduced_dim == decoder_iterations[1] | ||
|
||
(PSDLayer(arch.reduced_dim, arch.full_dim), ) | ||
end | ||
|
||
# this performs PSD | ||
function solve!(nn::NeuralNetwork{<:PSDArch}, input::AbstractMatrix) | ||
half_of_dimension_in_big_space = nn.architecture.full_dim ÷ 2 | ||
@views input_qp = hcat(input[1 : half_of_dimension_in_big_space, :], input[(half_of_dimension_in_big_space + 1) : end, :]) | ||
U_term = svd(input_qp).U | ||
@views nn.params[1].weight.A .= U_term[:, 1 : nn.architecture.reduced_dim ÷ 2] | ||
@views nn.params[2].weight.A .= U_term[:, 1 : nn.architecture.reduced_dim ÷ 2] | ||
|
||
AutoEncoderLoss()(nn, input) | ||
end | ||
|
||
function solve!(nn::NeuralNetwork{<:PSDArch}, input::AbstractArray{T, 3}) where T | ||
solve!(nn, reshape(input, size(input, 1), size(input, 2) * size(input, 3))) | ||
end | ||
|
||
function solve!(nn::NeuralNetwork{<:PSDArch}, dl::DataLoader{T, AT, <:Any, :RegularData}) where {T, AT <: AbstractArray{T}} | ||
solve!(nn, dl.input) | ||
end | ||
|
||
function solve!(nn::NeuralNetwork{<:PSDArch}, dl::DataLoader{T, NT, <:Any, :RegularData}) where {T, AT <: AbstractArray{T}, NT <: NamedTuple{(:q, :p), Tuple{AT, AT}}} | ||
solve!(nn, vcat(dl.input.q, dl.input.p)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
struct SymplecticAutoencoder{EncoderInit, DecoderInit, AT} <: AutoEncoder | ||
full_dim::Int | ||
reduced_dim::Int | ||
n_encoder_layers::Int | ||
n_encoder_blocks::Int | ||
n_decoder_layers::Int | ||
n_decoder_blocks::Int | ||
sympnet_upscale::Int | ||
activation::AT | ||
end | ||
|
||
struct SymplecticEncoder{AT} <: Encoder | ||
full_dim::Int | ||
reduced_dim::Int | ||
n_encoder_layers::Int | ||
n_encoder_blocks::Int | ||
sympnet_upscale::Int | ||
activation::AT | ||
end | ||
|
||
struct SymplecticDecoder{AT} <: Encoder | ||
full_dim::Int | ||
reduced_dim::Int | ||
n_decoder_layers::Int | ||
n_decoder_blocks::Int | ||
sympnet_upscale::Int | ||
activation::AT | ||
end | ||
|
||
function SymplecticAutoencoder(full_dim::Integer, reduced_dim::Integer; n_encoder_layers::Integer = 4, n_encoder_blocks::Integer = 2, n_decoder_layers::Integer = 1, n_decoder_blocks::Integer = 3, sympnet_upscale::Integer = 5, activation = tanh, encoder_init_q::Bool = true, decoder_init_q::Bool = true) | ||
@assert full_dim ≥ reduced_dim "The dimension of the full-order model hast to be larger than the dimension of the reduced order model!" | ||
@assert iseven(full_dim) && iseven(reduced_dim) "The full-order model and the reduced-order model need to be even dimensional!" | ||
|
||
if encoder_init_q && decoder_init_q | ||
SymplecticAutoencoder{:EncoderInitQ, :DecoderInitQ, typeof(activation)}(full_dim, reduced_dim, n_encoder_layers, n_encoder_blocks, n_decoder_layers, n_decoder_blocks, sympnet_upscale, activation) | ||
elseif encoder_init_q && !decoder_init_q | ||
SymplecticAutoencoder{:EncoderInitQ, :DecoderInitP, typeof(activation)}(full_dim, reduced_dim, n_encoder_layers, n_encoder_blocks, n_decoder_layers, n_decoder_blocks, sympnet_upscale, activation) | ||
elseif !encoder_init_q && decoder_init_q | ||
SymplecticAutoencoder{:EncoderInitP, :DecoderInitQ, typeof(activation)}(full_dim, reduced_dim, n_encoder_layers, n_encoder_blocks, n_decoder_layers, n_decoder_blocks, sympnet_upscale, activation) | ||
elseif !encoder_init_q && !decoder_init_q | ||
SymplecticAutoencoder{:EncoderInitP, :DecoderInitP, typeof(activation)}(full_dim, reduced_dim, n_encoder_layers, n_encoder_blocks, n_decoder_layers, n_decoder_blocks, sympnet_upscale, activation) | ||
end | ||
end | ||
|
||
""" | ||
This function gives iterations from the full dimension to the reduced dimension (i.e. the intermediate steps). The iterations are given in ascending order. Only even steps are allowed here. | ||
""" | ||
function compute_iterations_for_symplectic_system(full_dim::Integer, reduced_dim::Integer, n_blocks::Integer) | ||
full_dim2 = full_dim ÷ 2 | ||
reduced_dim2 = reduced_dim ÷ 2 | ||
iterations = Vector{Int}(reduced_dim2 : (full_dim2 - reduced_dim2) ÷ (n_blocks - 1) : full_dim2) | ||
iterations[end] = full_dim2 | ||
iterations * 2 | ||
end | ||
|
||
function compute_encoder_iterations(arch::SymplecticAutoencoder) | ||
compute_iterations_for_symplectic_system(arch.full_dim, arch.reduced_dim, arch.n_encoder_blocks) | ||
end | ||
|
||
function compute_decoder_iterations(arch::SymplecticAutoencoder) | ||
compute_iterations_for_symplectic_system(arch.full_dim, arch.reduced_dim, arch.n_decoder_blocks) | ||
end | ||
|
||
function encoder_or_decoder_layers_from_iteration(arch::SymplecticAutoencoder, encoder_iterations::AbstractVector{<:Integer}, n_encoder_layers::Integer, _determine_layer_type) | ||
encoder_layers = () | ||
encoder_iterations_reduced = encoder_iterations[1:(end - 1)] | ||
for (i, it) in zip(axes(encoder_iterations_reduced, 1), encoder_iterations_reduced) | ||
for layer_index in 1:n_encoder_layers | ||
encoder_layers = _determine_layer_type(layer_index) ? (encoder_layers..., GradientLayerQ(it, arch.sympnet_upscale * it, arch.activation)) : (encoder_layers..., GradientLayerP(it, arch.sympnet_upscale * it, arch.activation)) | ||
end | ||
encoder_layers = (encoder_layers..., PSDLayer(it, encoder_iterations[i + 1])) | ||
end | ||
|
||
encoder_layers | ||
end | ||
|
||
function encoder_layers_from_iteration(arch::SymplecticAutoencoder{:EncoderInitQ}, encoder_iterations::AbstractVector{<:Integer}) | ||
encoder_or_decoder_layers_from_iteration(arch, encoder_iterations, arch.n_encoder_layers, isodd) | ||
end | ||
|
||
function encoder_layers_from_iteration(arch::SymplecticAutoencoder{:EncoderInitP}, encoder_iterations::AbstractVector{<:Integer}) | ||
encoder_or_decoder_layers_from_iteration(arch, encoder_iterations, arch.n_encoder_layers, iseven) | ||
end | ||
|
||
function decoder_layers_from_iteration(arch::SymplecticAutoencoder{<:Any, :DecoderInitQ}, decoder_iterations::AbstractVector{<:Integer}) | ||
encoder_or_decoder_layers_from_iteration(arch, decoder_iterations, arch.n_decoder_layers, isodd) | ||
end | ||
|
||
function decoder_layers_from_iteration(arch::SymplecticAutoencoder{<:Any, :DecoderInitP}, decoder_iterations::AbstractVector{<:Integer}) | ||
encoder_or_decoder_layers_from_iteration(arch, decoder_iterations, arch.n_decoder_layers, iseven) | ||
end | ||
|
||
function encoder(nn::NeuralNetwork{<:SymplecticAutoencoder}) | ||
arch = SymplecticEncoder(nn.architecture.full_dim, nn.architecture.reduced_dim, nn.architecture.n_encoder_layers, nn.architecture.n_encoder_blocks, nn.architecture.sympnet_upscale, nn.architecture.activation) | ||
NeuralNetwork(arch, encoder_model(nn.architecture), encoder_parameters(nn), get_backend(nn)) | ||
end | ||
|
||
function decoder(nn::NeuralNetwork{<:SymplecticAutoencoder}) | ||
arch = SymplecticDecoder(nn.architecture.full_dim, nn.architecture.reduced_dim, nn.architecture.n_decoder_layers, nn.architecture.n_decoder_blocks, nn.architecture.sympnet_upscale, nn.architecture.activation) | ||
NeuralNetwork(arch, decoder_model(nn.architecture), decoder_parameters(nn), get_backend(nn)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.