-
Notifications
You must be signed in to change notification settings - Fork 3
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
Transformer integrator #131
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
783fe33
Made SympNet a NeuralNetwork integrator.
benedict-96 cd45020
Added transformer integrator.
benedict-96 6ab743e
Added the regular transformer integrator.
benedict-96 5038334
Added the ResNet as a NeuralNetworkIntegrator.
benedict-96 3fb2501
ResNet -> ResNetLayer.
benedict-96 948d1f3
ResNet -> ResNetLayer.
benedict-96 af6fbcf
Included the transformer integrator and made ResNet a specific (integ…
benedict-96 b8797a9
Updated the SymplecticPotential struct.
benedict-96 6a510f7
Added tests for all changes.
benedict-96 669728b
Adjusted to new interface.
benedict-96 9b622e9
Fixed typo.
benedict-96 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
@doc raw""" | ||
The regular transformer used as an integrator (multi-step method). | ||
|
||
The constructor is called with the following arguments: | ||
- `sys_dim::Int` | ||
- `transformer_dim::Int`: the default is `transformer_dim = sys_dim`. | ||
- `n_blocks::Int`: The default is `1`. | ||
- `n_heads::Int`: the number of heads in the multihead attentio layer (default is `n_heads = sys_dim`) | ||
- `L::Int` the number of transformer blocks (default is `L = 2`). | ||
- `upscaling_activation`: by default identity | ||
- `resnet_activation`: by default tanh | ||
- `add_connection:Bool=true` (keyword argument): if the input should be added to the output. | ||
""" | ||
struct RegularTransformerIntegrator{AT1, AT2} <: TransformerIntegrator | ||
sys_dim::Int | ||
transformer_dim::Int | ||
n_heads::Int | ||
n_blocks::Int | ||
L::Int | ||
upsacling_activation::AT1 | ||
resnet_activation::AT2 | ||
add_connection::Bool | ||
end | ||
|
||
# function RegularTransformerIntegrator(sys_dim::Int, transformer_dim::Int = sys_dim, n_heads::Int = sys_dim, n_blocks = 1, L::Int = 2, upscaling_activation = identity, resnet_activation = tanh; add_connection::Bool = true) | ||
# RegularTransformerIntegrator{typeof(upscaling_activation), typeof(resnet_activation)}(sys_dim, transformer_dim, n_heads, n_blocks, L, upscaling_activation, resnet_activation, add_connection) | ||
# end | ||
|
||
function RegularTransformerIntegrator(sys_dim::Int, transformer_dim::Int = sys_dim, n_heads::Int = sys_dim; n_blocks = 1, L::Int = 2, upscaling_activation = identity, resnet_activation = tanh, add_connection::Bool = true) | ||
RegularTransformerIntegrator(sys_dim, transformer_dim, n_heads, n_blocks, L, upscaling_activation, resnet_activation, add_connection) | ||
end | ||
|
||
function Chain(arch::RegularTransformerIntegrator) | ||
layers = arch.sys_dim == arch.transformer_dim ? () : (Dense(arch.sys_dim, arch.transformer_dim, arch.upsacling_activation), ) | ||
for _ in 1:arch.L | ||
layers = (layers..., MultiHeadAttention(arch.transformer_dim, arch.n_heads; add_connection = arch.add_connection)) | ||
layers = (layers..., Chain(ResNet(arch.transformer_dim, arch.n_blocks, arch.resnet_activation)).layers...) | ||
end | ||
layers = arch.sys_dim == arch.transformer_dim ? layers : (layers..., Dense(arch.transformer_dim, arch.sys_dim, identity)) | ||
|
||
Chain(layers...) | ||
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,18 @@ | ||
struct ResNet{AT} <: NeuralNetworkIntegrator | ||
sys_dim::Int | ||
n_blocks::Int | ||
activation::AT | ||
end | ||
|
||
function Chain(arch::ResNet{AT}) where AT | ||
layers = () | ||
for _ in 1:arch.n_blocks | ||
# nonlinear layers | ||
layers = (layers..., ResNetLayer(arch.sys_dim, arch.activation; use_bias=true)) | ||
end | ||
|
||
# linear layers for the output | ||
layers = (layers..., ResNetLayer(arch.sys_dim, identity; use_bias=true)) | ||
|
||
Chain(layers...) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
@doc raw""" | ||
Encompasses various transformer architectures, such as the structure-preserving transformer and the linear symplectic transformer. | ||
""" | ||
abstract type TransformerIntegrator <: Architecture end | ||
|
||
struct DummyTransformer <: TransformerIntegrator | ||
seq_length::Int | ||
end | ||
|
||
@doc raw""" | ||
This function computes a trajectory for a Transformer that has already been trained for valuation purposes. | ||
|
||
It takes as input: | ||
- `nn`: a `NeuralNetwork` (that has been trained). | ||
- `ics`: initial conditions (a matrix in ``\mathbb{R}^{2n\times\mathtt{seq\_length}}`` or `NamedTuple` of two matrices in ``\mathbb{R}^{n\times\mathtt{seq\_length}}``) | ||
- `n_points::Int=100` (keyword argument): The number of steps for which we run the prediction. | ||
- `prediction_window::Int=size(ics.q, 2)`: The prediction window (i.e. the number of steps we predict into the future) is equal to the sequence length (i.e. the number of input time steps) by default. | ||
""" | ||
function Base.iterate(nn::NeuralNetwork{<:TransformerIntegrator}, ics::NamedTuple{(:q, :p), Tuple{AT, AT}}; n_points::Int = 100, prediction_window::Union{Nothing, Int}=size(ics.q, 2)) where {T, AT<:AbstractMatrix{T}} | ||
|
||
seq_length = nn.architecture.seq_length | ||
|
||
n_dim = size(ics.q, 1) | ||
backend = KernelAbstractions.get_backend(ics.q) | ||
|
||
n_iterations = Int(ceil((n_points - seq_length) / prediction_window)) | ||
# Array to store the predictions | ||
q_valuation = KernelAbstractions.allocate(backend, T, n_dim, seq_length + n_iterations * prediction_window) | ||
p_valuation = KernelAbstractions.allocate(backend, T, n_dim, seq_length + n_iterations * prediction_window) | ||
|
||
# Initialisation | ||
q_valuation[:,1:seq_length] = ics.q | ||
p_valuation[:,1:seq_length] = ics.p | ||
|
||
# iteration in phase space | ||
@views for i in 1:n_iterations | ||
start_index = (i - 1) * prediction_window + 1 | ||
@views qp_temp = (q = q_valuation[:, start_index:(start_index + seq_length - 1)], p = p_valuation[:, start_index:(start_index + seq_length - 1)]) | ||
qp_prediction = nn(qp_temp) | ||
q_valuation[seq_length + (i - 1) * prediction_window, seq_length + i * prediction_window] = qp_prediction.q[:, (seq_length - prediction_window + 1):end] | ||
p_valuation[seq_length + (i - 1) * prediction_window, seq_length + i * prediction_window] = qp_prediction.p[:, (seq_length - prediction_window + 1):end] | ||
end | ||
|
||
(q=q_valuation[:, 1:n_points], p=p_valuation[:, 1:n_points]) | ||
end | ||
|
||
function Base.iterate(nn::NeuralNetwork{<:TransformerIntegrator}, ics::AT; n_points::Int = 100, prediction_window::Union{Nothing, Int} = size(ics, 2)) where {T, AT<:AbstractMatrix{T}} | ||
|
||
seq_length = typeof(nn.architecture) <: RegularTransformerIntegrator ? prediction_window : nn.architecture.seq_length | ||
|
||
n_dim = size(ics, 1) | ||
backend = KernelAbstractions.get_backend(ics) | ||
|
||
n_iterations = Int(ceil((n_points - seq_length) / prediction_window)) | ||
# Array to store the predictions | ||
valuation = KernelAbstractions.allocate(backend, T, n_dim, seq_length + n_iterations * prediction_window) | ||
|
||
# Initialisation | ||
valuation[:,1:seq_length] = ics | ||
|
||
# iteration in phase space | ||
@views for i in 1:n_iterations | ||
start_index = (i - 1) * prediction_window + 1 | ||
temp = valuation[:, start_index:(start_index + seq_length - 1)] | ||
prediction = nn(copy(temp)) | ||
valuation[:, (seq_length + (i - 1) * prediction_window + 1):(seq_length + i * prediction_window)] = prediction[:, (seq_length - prediction_window + 1):end] | ||
end | ||
|
||
valuation[:, 1:n_points] | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a typo: "the defualt is"