This repository has been 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
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 #16 from foldfelis/n-d_example
Fix functor bug and build project for Burgers' equation
- Loading branch information
Showing
13 changed files
with
144 additions
and
171 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name = "Burgers" | ||
uuid = "5b053d85-f964-4905-ae31-99551cd8d3ad" | ||
|
||
[deps] | ||
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" | ||
DataDeps = "124859b0-ceae-595e-8997-d05f6a7a8dfe" | ||
Fetch = "bb354801-46f6-40b6-9c3d-d42d7a74c775" | ||
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c" | ||
MAT = "23992714-dd62-5051-b70f-ba57cb901cac" | ||
NeuralOperators = "ea5c82af-86e5-48da-8ee1-382d6ad7af4b" | ||
|
||
[extras] | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["Test"] |
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,49 @@ | ||
module Burgers | ||
|
||
using NeuralOperators | ||
using Flux | ||
using CUDA | ||
|
||
include("data.jl") | ||
|
||
__init__() = register_burgers() | ||
|
||
function train() | ||
if has_cuda() | ||
@info "CUDA is on" | ||
device = gpu | ||
CUDA.allowscalar(false) | ||
else | ||
device = cpu | ||
end | ||
|
||
modes = (16, ) | ||
ch = 64 => 64 | ||
σ = gelu | ||
m = Chain( | ||
Dense(2, 64), | ||
FourierOperator(ch, modes, σ), | ||
FourierOperator(ch, modes, σ), | ||
FourierOperator(ch, modes, σ), | ||
FourierOperator(ch, modes), | ||
Dense(64, 128, σ), | ||
Dense(128, 1), | ||
flatten | ||
) |> device | ||
|
||
loss(𝐱, 𝐲) = sum(abs2, 𝐲 .- m(𝐱)) / size(𝐱)[end] | ||
|
||
loader_train, loader_test = get_dataloader() | ||
|
||
function validate() | ||
validation_losses = [loss(device(𝐱), device(𝐲)) for (𝐱, 𝐲) in loader_test] | ||
@info "loss: $(sum(validation_losses)/length(loader_test))" | ||
end | ||
|
||
data = [(𝐱, 𝐲) for (𝐱, 𝐲) in loader_train] |> device | ||
opt = Flux.Optimiser(WeightDecay(1f-4), Flux.ADAM(1f-3)) | ||
call_back = Flux.throttle(validate, 5, leading=false, trailing=true) | ||
Flux.@epochs 500 @time(Flux.train!(loss, params(m), data, opt, cb=call_back)) | ||
end | ||
|
||
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,44 @@ | ||
using DataDeps | ||
using Fetch | ||
using MAT | ||
|
||
export get_burgers_data | ||
|
||
function register_burgers() | ||
register(DataDep( | ||
"Burgers", | ||
""" | ||
Burgers' equation dataset from | ||
[fourier_neural_operator](https://github.com/zongyi-li/fourier_neural_operator) | ||
""", | ||
"https://drive.google.com/file/d/17MYsKzxUQVaLMWodzPbffR8hhDHoadPp/view?usp=sharing", | ||
"9cbbe5070556c777b1ba3bacd49da5c36ea8ed138ba51b6ee76a24b971066ecd", | ||
fetch_method=gdownload, | ||
post_fetch_method=unpack | ||
)) | ||
end | ||
|
||
function get_burgers_data(; n=2048, Δsamples=2^3, grid_size=div(2^13, Δsamples), T=Float32) | ||
file = matopen(joinpath(datadep"Burgers", "burgers_data_R10.mat")) | ||
x_data = T.(collect(read(file, "a")[1:n, 1:Δsamples:end]')) | ||
y_data = T.(collect(read(file, "u")[1:n, 1:Δsamples:end]')) | ||
close(file) | ||
|
||
x_loc_data = Array{T, 3}(undef, 2, grid_size, n) | ||
x_loc_data[1, :, :] .= reshape(repeat(LinRange(0, 1, grid_size), n), (grid_size, n)) | ||
x_loc_data[2, :, :] .= x_data | ||
|
||
return x_loc_data, y_data | ||
end | ||
|
||
function get_dataloader(; n_train=1800, n_test=200, batchsize=100) | ||
𝐱, 𝐲 = get_burgers_data(n=2048) | ||
|
||
𝐱_train, 𝐲_train = 𝐱[:, :, 1:n_train], 𝐲[:, 1:n_train] | ||
loader_train = Flux.DataLoader((𝐱_train, 𝐲_train), batchsize=batchsize, shuffle=true) | ||
|
||
𝐱_test, 𝐲_test = 𝐱[:, :, end-n_test+1:end], 𝐲[:, end-n_test+1:end] | ||
loader_test = Flux.DataLoader((𝐱_test, 𝐲_test), batchsize=batchsize, shuffle=false) | ||
|
||
return loader_train, loader_test | ||
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,6 @@ | ||
@testset "get burgers data" begin | ||
xs, ys = get_burgers_data(n=1000) | ||
|
||
@test size(xs) == (2, 1024, 1000) | ||
@test size(ys) == (1024, 1000) | ||
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,6 @@ | ||
using Burgers | ||
using Test | ||
|
||
@testset "Burgers" begin | ||
include("data.jl") | ||
end |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
cf8eddd
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.
@JuliaRegistrator register
cf8eddd
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.
Registration pull request updated: JuliaRegistries/General/43279
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: