Repository navigation
Add StandardAutoencoder, an autoencoder of Dense layers #336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
benedict-96
wants to merge
2
commits into
main
Choose a base branch
from
add/standard-autoencoder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,89 @@ | ||
| @doc raw""" | ||
| StandardAutoencoder(full_dim, reduced_dim) | ||
|
|
||
| Make an instance of `StandardAutoencoder` for dimensions `full_dim` and `reduced_dim`. | ||
|
|
||
| # The architecture | ||
|
|
||
| A standard autoencoder [goodfellow2016deep](@cite) has an encoder ``\Psi^\mathrm{enc}:\mathbb{R}^N\to\mathbb{R}^n`` and a decoder ``\Psi^\mathrm{dec}:\mathbb{R}^n\to\mathbb{R}^N`` that are both feedforward networks of `Dense` layers, with no structure imposed on either. The encoder is | ||
|
|
||
| ```math | ||
| \Psi^\mathrm{enc} = L^\mathrm{out}_{w\to{}n}\circ{}L_{w\to{}w}\circ\cdots\circ{}L_{w\to{}w}\circ{}L_{N\to{}w}, | ||
| ``` | ||
|
|
||
| with `n_encoder_layers` nonlinear layers ``L``, the first of which maps ``\mathbb{R}^N`` to the hidden width ``w`` and the others ``\mathbb{R}^w`` to itself, followed by an affine output layer ``L^\mathrm{out}`` without activation. The decoder is built the same way from ``\mathbb{R}^n`` to ``\mathbb{R}^N`` with `n_decoder_layers` nonlinear layers. | ||
|
|
||
| Unlike for a [`SymplecticAutoencoder`](@ref), ``\nabla\Psi^\mathrm{dec}`` is in general not symplectic, so the reduced system is not Hamiltonian, and neither ``N`` nor ``n`` has to be even. It is the unstructured counterpart of the [`SymplecticAutoencoder`](@ref), and serves to compare the two. | ||
|
|
||
| # Arguments | ||
|
|
||
| Besides the required arguments `full_dim` and `reduced_dim` you can provide the following keyword arguments: | ||
| - `width::Integer = 2full_dim`: the hidden width ``w``. | ||
| - `n_encoder_layers::Integer = 2`: the number of nonlinear layers in the encoder. | ||
| - `n_decoder_layers::Integer = 2`: the number of nonlinear layers in the decoder. | ||
| - `activation = tanh`: the activation of the nonlinear layers. | ||
|
|
||
| # Examples | ||
|
|
||
| The two parts compose to the whole network, and the number of parameters follows from the widths: | ||
|
|
||
| ```jldoctest | ||
| using GeometricMachineLearning | ||
| using GeometricMachineLearning: params | ||
|
|
||
| arch = StandardAutoencoder(4, 2; width = 10, n_encoder_layers = 2, n_decoder_layers = 3) | ||
| nn = NeuralNetwork(arch) | ||
| x = rand(4) | ||
|
|
||
| nn(x) ≈ decoder(nn)(encoder(nn)(x)), parameterlength(nn.model) | ||
|
|
||
| # output | ||
|
|
||
| (true, 476) | ||
| ``` | ||
|
|
||
| Here the encoder has ``(4 + 1)\cdot10 + (10 + 1)\cdot10 + (10 + 1)\cdot2 = 182`` parameters and the decoder ``(2 + 1)\cdot10 + 2\cdot(10 + 1)\cdot10 + (10 + 1)\cdot4 = 294``. | ||
| """ | ||
| struct StandardAutoencoder{AT} <: AutoEncoder | ||
| full_dim::Int | ||
| reduced_dim::Int | ||
| width::Int | ||
| n_encoder_layers::Int | ||
| n_decoder_layers::Int | ||
| n_encoder_blocks::Int | ||
| n_decoder_blocks::Int | ||
| activation::AT | ||
| end | ||
|
|
||
| function StandardAutoencoder(full_dim::Integer, reduced_dim::Integer; | ||
| width::Integer = 2full_dim, | ||
| n_encoder_layers::Integer = 2, | ||
| n_decoder_layers::Integer = 2, | ||
| activation = tanh) | ||
| @assert full_dim ≥ reduced_dim "The dimension of the full-order model has to be at least that of the reduced-order model!" | ||
| @assert n_encoder_layers ≥ 1 && n_decoder_layers ≥ 1 "Encoder and decoder need at least one nonlinear layer each!" | ||
| # The dimension changes once in each direction, from `full_dim` to `reduced_dim`; the hidden | ||
| # layers are not blocks in the sense of `compute_iterations`. | ||
| StandardAutoencoder{typeof(activation)}(full_dim, reduced_dim, width, n_encoder_layers, | ||
| n_decoder_layers, 2, 2, activation) | ||
| end | ||
|
|
||
| # The layers do not depend on intermediate dimensions, only on the two ends. `compute_iterations` | ||
| # would build a range of step zero for `full_dim == reduced_dim`, which the constructor allows. | ||
| compute_encoder_iterations(arch::StandardAutoencoder) = [arch.reduced_dim, arch.full_dim] | ||
| compute_decoder_iterations(arch::StandardAutoencoder) = [arch.reduced_dim, arch.full_dim] | ||
|
|
||
| function _standard_autoencoder_layers(arch::StandardAutoencoder, input_dim::Integer, | ||
| output_dim::Integer, n_layers::Integer) | ||
| (Dense(input_dim, arch.width, arch.activation), | ||
| Tuple(Dense(arch.width, arch.width, arch.activation) for _ in 2:n_layers)..., | ||
| Dense(arch.width, output_dim, identity)) | ||
| end | ||
|
|
||
| function encoder_layers_from_iteration(arch::StandardAutoencoder, ::AbstractVector{<:Integer}) | ||
| _standard_autoencoder_layers(arch, arch.full_dim, arch.reduced_dim, arch.n_encoder_layers) | ||
| end | ||
|
|
||
| function decoder_layers_from_iteration(arch::StandardAutoencoder, ::AbstractVector{<:Integer}) | ||
| _standard_autoencoder_layers(arch, arch.reduced_dim, arch.full_dim, arch.n_decoder_layers) | ||
| end | ||
This file contains hidden or 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 hidden or 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,60 @@ | ||
| using GeometricMachineLearning | ||
| using GeometricMachineLearning: params | ||
| using Test | ||
| using Zygote: jacobian | ||
| import Random | ||
|
|
||
| Random.seed!(123) | ||
|
|
||
| function test_encoder_and_decoder(N::Integer, n::Integer; kwargs...) | ||
| nn = NeuralNetwork(StandardAutoencoder(N, n; kwargs...)) | ||
| ae_encoder = encoder(nn) | ||
| ae_decoder = decoder(nn) | ||
|
|
||
| test_vec = rand(N) | ||
| test_mat = rand(N, N) | ||
|
|
||
| @test size(ae_encoder(test_vec)) == (n,) | ||
| @test size(ae_decoder(rand(n))) == (N,) | ||
| @test nn(test_vec) ≈ ae_decoder(ae_encoder(test_vec)) | ||
| @test nn(test_mat) ≈ ae_decoder(ae_encoder(test_mat)) | ||
| end | ||
|
|
||
| # Each nonlinear layer is one Dense layer with bias, and each part ends in an affine layer. | ||
| function test_parameter_count(N::Integer, n::Integer, w::Integer, n_enc::Integer, n_dec::Integer) | ||
| nn = NeuralNetwork(StandardAutoencoder(N, n; width = w, | ||
| n_encoder_layers = n_enc, n_decoder_layers = n_dec)) | ||
| n_encoder = (N + 1) * w + (n_enc - 1) * (w + 1) * w + (w + 1) * n | ||
| n_decoder = (n + 1) * w + (n_dec - 1) * (w + 1) * w + (w + 1) * N | ||
| @test parameterlength(nn.model) == n_encoder + n_decoder | ||
| @test parameterlength(encoder(nn).model) == n_encoder | ||
| @test parameterlength(decoder(nn).model) == n_decoder | ||
| @test length(encoder(nn).model.layers) == n_enc + 1 | ||
| @test length(decoder(nn).model.layers) == n_dec + 1 | ||
| end | ||
|
|
||
| function test_training_reduces_loss(N::Integer, n::Integer; n_epochs::Integer = 100) | ||
| dl = DataLoader(rand(N, 10 * N); autoencoder = true) | ||
| nn = NeuralNetwork(StandardAutoencoder(N, n)) | ||
| loss = AutoEncoderLoss() | ||
| loss_before = loss(nn, dl.input) | ||
| o = Optimizer(Adam(), nn) | ||
| o(nn, dl, Batch(10), n_epochs; show_progress = false) | ||
| @test loss(nn, dl.input) < loss_before | ||
| end | ||
|
|
||
| # Nothing makes the decoder symplectic: ∇Ψᵀ 𝕁 ∇Ψ differs from 𝕁 at a random point. | ||
| function test_not_symplectic(N::Integer, n::Integer) | ||
| ae_decoder = decoder(NeuralNetwork(StandardAutoencoder(N, n))) | ||
| J = jacobian(ae_decoder, rand(n))[1] | ||
| @test !(J' * PoissonTensor(N) * J ≈ PoissonTensor(n)) | ||
| end | ||
|
|
||
| for (N, n) in ((10, 4), (4, 2), (7, 3), (4, 4)) | ||
| test_encoder_and_decoder(N, n) | ||
| test_parameter_count(N, n, 2N, 2, 2) | ||
| end | ||
| test_encoder_and_decoder(4, 2; width = 16, n_encoder_layers = 1, n_decoder_layers = 4) | ||
| test_parameter_count(4, 2, 40, 3, 4) | ||
| test_training_reduces_loss(10, 4) | ||
| test_not_symplectic(10, 4) |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.