This repository was archived by the owner on Sep 28, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 41
extend SpectralConv to n-dim #10
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
23cb507
extend SpectralConv to n-dim
foldfelis 7b24c31
refactor
foldfelis 3549344
skip NavierStokes
foldfelis c2a9d22
darcy flow data
foldfelis 4a341b2
deal with n-dim m
foldfelis 8e6e4a5
fix wrong dim
foldfelis dc018ec
fix wrong dim on permutation and shrink test
foldfelis 58cf994
revise api
foldfelis c419e72
Apply suggestions from code review
foldfelis 483dfcb
Apply suggestions from code review
foldfelis 7225fe5
fix dims for shaped array
foldfelis 99d775d
update deps for doc
foldfelis 3e06c3a
update compat
foldfelis 01167c5
diversion
foldfelis 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ module NeuralOperators | |
using DataDeps | ||
using Fetch | ||
using MAT | ||
using StatsBase | ||
|
||
using Flux | ||
using FFTW | ||
|
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 |
---|---|---|
@@ -1,53 +1,62 @@ | ||
export | ||
SpectralConv1d, | ||
SpectralConv, | ||
FourierOperator | ||
|
||
struct SpectralConv1d{T, S} | ||
struct SpectralConv{N, T, S} | ||
weight::T | ||
in_channel::S | ||
out_channel::S | ||
modes::S | ||
modes::NTuple{N, S} | ||
ndim::S | ||
σ | ||
end | ||
|
||
c_glorot_uniform(dims...) = Flux.glorot_uniform(dims...) + Flux.glorot_uniform(dims...)*im | ||
|
||
function SpectralConv1d( | ||
ch::Pair{<:Integer, <:Integer}, | ||
modes::Integer, | ||
function SpectralConv( | ||
ch::Pair{S, S}, | ||
modes::NTuple{N, S}, | ||
σ=identity; | ||
init=c_glorot_uniform, | ||
T::DataType=ComplexF32 | ||
) | ||
) where {S<:Integer, N} | ||
in_chs, out_chs = ch | ||
scale = one(T) / (in_chs * out_chs) | ||
weights = scale * init(out_chs, in_chs, modes) | ||
weights = scale * init(out_chs, in_chs, prod(modes)) | ||
|
||
return SpectralConv1d(weights, in_chs, out_chs, modes, σ) | ||
return SpectralConv(weights, in_chs, out_chs, modes, N, σ) | ||
end | ||
|
||
Flux.@functor SpectralConv1d | ||
Flux.@functor SpectralConv | ||
|
||
Base.ndims(::SpectralConv{N}) where {N} = N | ||
|
||
# [prod(m.modes), out_chs, batch] <- [prod(m.modes), in_chs, batch] * [out_chs, in_chs, prod(m.modes)] | ||
spectral_conv(𝐱₁, 𝐱₂) = @tullio 𝐲[m, o, b] := 𝐱₁[m, i, b] * 𝐱₂[o, i, m] | ||
|
||
function (m::SpectralConv1d)(𝐱::AbstractArray) | ||
𝐱ᵀ = permutedims(Zygote.hook(real, 𝐱), (2, 1, 3)) # [x, in_chs, batch] <- [in_chs, x, batch] | ||
𝐱_fft = fft(𝐱ᵀ, 1) # [x, in_chs, batch] | ||
function (m::SpectralConv)(𝐱::AbstractArray) | ||
n_dims = ndims(𝐱) | ||
|
||
𝐱ᵀ = permutedims(Zygote.hook(real, 𝐱), (ntuple(i->i+1, ndims(m))..., 1, ndims(m)+2)) # [x, in_chs, batch] <- [in_chs, x, batch] | ||
𝐱_fft = fft(𝐱ᵀ, 1:ndims(m)) # [x, in_chs, batch] | ||
|
||
𝐱_flattened = reshape(view(𝐱_fft, map(d->1:d, m.modes)..., :, :), :, size(𝐱_fft, n_dims-1), size(𝐱_fft, n_dims)) | ||
𝐱_weighted = spectral_conv(𝐱_flattened, m.weight) # [prod(m.modes), out_chs, batch], only 3-dims | ||
𝐱_shaped = reshape(𝐱_weighted, m.modes..., size(𝐱_weighted, 2), size(𝐱_weighted, 3)) | ||
|
||
# [modes, out_chs, batch] <- [modes, in_chs, batch] * [out_chs, in_chs, modes] | ||
𝐱_weighted = spectral_conv(view(𝐱_fft, 1:m.modes, :, :), m.weight) | ||
# [x, out_chs, batch] <- [modes, out_chs, batch] | ||
𝐱_padded = cat(𝐱_weighted, zeros(ComplexF32, size(𝐱_fft, 1)-m.modes, Base.tail(size(𝐱_weighted))...), dims=1) | ||
pad = zeros(ComplexF32, ntuple(i->size(𝐱_fft, i)-m.modes[i], ndims(m))..., size(𝐱_shaped, n_dims-1), size(𝐱_shaped, n_dims)) | ||
𝐱_padded = cat(𝐱_shaped, pad, dims=1:ndims(m)) | ||
|
||
𝐱_out = ifft(𝐱_padded, 1) # [x, out_chs, batch] | ||
𝐱_outᵀ = permutedims(real(𝐱_out), (2, 1, 3)) # [out_chs, x, batch] <- [x, out_chs, batch] | ||
𝐱_out = ifft(𝐱_padded, 1:ndims(m)) # [x, out_chs, batch] | ||
𝐱_outᵀ = permutedims(real(𝐱_out), (ndims(m)+1, 1:ndims(m)..., ndims(m)+2)) # [out_chs, x, batch] <- [x, out_chs, batch] | ||
|
||
return m.σ.(𝐱_outᵀ) | ||
end | ||
|
||
function FourierOperator(ch::Pair{<:Integer, <:Integer}, modes::Integer, σ=identity) | ||
function FourierOperator(ch::Pair{S, S}, modes::NTuple{N, S}, σ=identity) where {S<:Integer, N} | ||
return Chain( | ||
Parallel(+, Dense(ch.first, ch.second), SpectralConv1d(ch, modes)), | ||
Parallel(+, Dense(ch.first, ch.second), SpectralConv(ch, modes)), | ||
x -> σ.(x) | ||
) | ||
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
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 |
---|---|---|
@@ -1,34 +1,69 @@ | ||
@testset "SpectralConv1d" begin | ||
modes = 16 | ||
modes = (16, ) | ||
ch = 64 => 64 | ||
|
||
m = Chain( | ||
Dense(2, 64), | ||
SpectralConv1d(ch, modes) | ||
SpectralConv(ch, modes) | ||
) | ||
@test ndims(SpectralConv(ch, modes)) == 1 | ||
|
||
𝐱, _ = get_burgers_data(n=1000) | ||
@test size(m(𝐱)) == (64, 1024, 1000) | ||
𝐱, _ = get_burgers_data(n=5) | ||
@test size(m(𝐱)) == (64, 1024, 5) | ||
|
||
T = Float32 | ||
loss(x, y) = Flux.mse(m(x), y) | ||
data = [(T.(𝐱[:, :, 1:5]), rand(T, 64, 1024, 5))] | ||
data = [(𝐱, rand(Float32, 64, 1024, 5))] | ||
Flux.train!(loss, params(m), data, Flux.ADAM()) | ||
end | ||
|
||
@testset "FourierOperator" begin | ||
modes = 16 | ||
@testset "FourierOperator1d" begin | ||
modes = (16, ) | ||
ch = 64 => 64 | ||
|
||
m = Chain( | ||
Dense(2, 64), | ||
FourierOperator(ch, modes) | ||
) | ||
|
||
𝐱, _ = get_burgers_data(n=1000) | ||
@test size(m(𝐱)) == (64, 1024, 1000) | ||
𝐱, _ = get_burgers_data(n=5) | ||
@test size(m(𝐱)) == (64, 1024, 5) | ||
|
||
loss(x, y) = Flux.mse(m(x), y) | ||
data = [(Float32.(𝐱[:, :, 1:5]), rand(Float32, 64, 1024, 5))] | ||
data = [(𝐱, rand(Float32, 64, 1024, 5))] | ||
Flux.train!(loss, params(m), data, Flux.ADAM()) | ||
end | ||
|
||
@testset "SpectralConv2d" begin | ||
modes = (16, 16) | ||
ch = 64 => 64 | ||
|
||
m = Chain( | ||
Dense(1, 64), | ||
SpectralConv(ch, modes) | ||
) | ||
@test ndims(SpectralConv(ch, modes)) == 2 | ||
|
||
𝐱, _, _, _ = get_darcy_flow_data(n=5, Δsamples=20) | ||
@test size(m(𝐱)) == (64, 22, 22, 5) | ||
|
||
loss(x, y) = Flux.mse(m(x), y) | ||
data = [(𝐱, rand(Float32, 64, 22, 22, 5))] | ||
Flux.train!(loss, params(m), data, Flux.ADAM()) | ||
end | ||
|
||
@testset "FourierOperator2d" begin | ||
modes = (16, 16) | ||
ch = 64 => 64 | ||
|
||
m = Chain( | ||
Dense(1, 64), | ||
FourierOperator(ch, modes) | ||
) | ||
|
||
𝐱, _, _, _ = get_darcy_flow_data(n=5, Δsamples=20) | ||
@test size(m(𝐱)) == (64, 22, 22, 5) | ||
|
||
loss(x, y) = Flux.mse(m(x), y) | ||
data = [(𝐱, rand(Float32, 64, 22, 22, 5))] | ||
Flux.train!(loss, params(m), data, Flux.ADAM()) | ||
end |
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.