|
| 1 | +using Flux: length, reshape, train!, throttle, @epochs |
| 2 | +using OperatorLearning, Flux, MAT |
| 3 | + |
| 4 | +device = cpu; |
| 5 | + |
| 6 | +#= |
| 7 | +We would like to implement and train a DeepONet that infers the solution |
| 8 | +u(x) of the burgers equation on a grid of 1024 points at time one based |
| 9 | +on the initial condition a(x) = u(x,0) |
| 10 | +=# |
| 11 | + |
| 12 | +# Read the data from MAT file and store it in a dict |
| 13 | +# key "a" is the IC |
| 14 | +# key "u" is the desired solution at time 1 |
| 15 | +vars = matread("burgers_data_R10.mat") |> device |
| 16 | + |
| 17 | +# For trial purposes, we might want to train with different resolutions |
| 18 | +# So we sample only every n-th element |
| 19 | +subsample = 2^3; |
| 20 | + |
| 21 | +# create the x training array, according to our desired grid size |
| 22 | +xtrain = vars["a"][1:1000, 1:subsample:end]' |> device; |
| 23 | +# create the x test array |
| 24 | +xtest = vars["a"][end-99:end, 1:subsample:end]' |> device; |
| 25 | + |
| 26 | +# Create the y training array |
| 27 | +ytrain = vars["u"][1:1000, 1:subsample:end] |> device; |
| 28 | +# Create the y test array |
| 29 | +ytest = vars["u"][end-99:end, 1:subsample:end] |> device; |
| 30 | + |
| 31 | +# The data is missing grid data, so we create it |
| 32 | +# `collect` converts data type `range` into an array |
| 33 | +grid = collect(range(0, 1, length=1024))' |> device |
| 34 | + |
| 35 | +# Pass the data to the Flux DataLoader and give it a batch of 20 |
| 36 | +#train_loader = Flux.Data.DataLoader((xtrain, ytrain), batchsize=20, shuffle=true) |> device |
| 37 | +#test_loader = Flux.Data.DataLoader((xtest, ytest), batchsize=20, shuffle=false) |> device |
| 38 | + |
| 39 | +# Create the DeepONet: |
| 40 | +# IC is given on grid of 1024 points, and we solve for a fixed time t in one |
| 41 | +# spatial dimension x, making the branch input of size 1024 and trunk size 1 |
| 42 | +model = DeepONet((1024,1024,1024),(1,1024,1024)) |
| 43 | + |
| 44 | +# We use the ADAM optimizer for training |
| 45 | +learning_rate = 0.001 |
| 46 | +opt = ADAM(learning_rate) |
| 47 | + |
| 48 | +# Specify the model parameters |
| 49 | +parameters = params(model) |
| 50 | + |
| 51 | +# The loss function |
| 52 | +# We can't use the "vanilla" implementation of the mse here since we have |
| 53 | +# two distinct inputs to our DeepONet, so we wrap them into a tuple |
| 54 | +loss(xtrain,ytrain,sensor) = Flux.Losses.mse(model(xtrain,sensor),ytrain) |
| 55 | + |
| 56 | +# Define a callback function that gives some output during training |
| 57 | +evalcb() = @show(loss(xtest,ytest,grid)) |
| 58 | +# Print the callback only every 5 seconds |
| 59 | +throttled_cb = throttle(evalcb, 5) |
| 60 | + |
| 61 | +# Do the training loop |
| 62 | +Flux.@epochs 500 train!(loss, parameters, [(xtrain,ytrain,grid)], opt, cb = evalcb) |
| 63 | + |
| 64 | +# Accuracy metrics |
| 65 | +val_loader = Flux.Data.DataLoader((xtest, ytest), batchsize=1, shuffle=false) |> device |
| 66 | +loss = 0.0 |> device |
| 67 | + |
| 68 | +for (x,y) in val_loader |
| 69 | + ŷ = model(x) |
| 70 | + loss += Flux.Losses.mse(ŷ,y) |
| 71 | +end |
0 commit comments