forked from JuliaDiff/TaylorDiff.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08c8279
commit 9a1f965
Showing
4 changed files
with
52 additions
and
20 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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
[deps] | ||
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" | ||
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" | ||
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c" | ||
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" | ||
Lux = "b2108857-7c20-44ae-9111-449ecde12c47" | ||
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" | ||
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267" | ||
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" | ||
TaylorDiff = "b36ab563-344f-407b-a36a-4f200bebf99c" | ||
TaylorSeries = "6aa5eb33-94cf-58f4-a9d0-e4b2c4fc25ea" | ||
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" |
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 |
---|---|---|
@@ -1,26 +1,51 @@ | ||
using TaylorDiff, Zygote | ||
using Flux | ||
using ChainRulesCore: @opt_out | ||
using TaylorDiff | ||
using Zygote | ||
using Plots | ||
|
||
const input = 2 | ||
const hidden = 16 | ||
|
||
struct PINN | ||
W₁ | ||
b₁ | ||
W₂ | ||
b₂ | ||
model = Chain( | ||
Dense(input => hidden, sin), | ||
Dense(hidden => hidden, sin), | ||
Dense(hidden => 1), | ||
first | ||
) | ||
trial(model, x) = model(x) | ||
|
||
ε = cbrt(eps(Float32)) | ||
ε₁ = [ε, 0] | ||
ε₂ = [0, ε] | ||
|
||
M = 100 | ||
data = [rand(input) for _ in 1:M] | ||
function loss_by_finitediff(model, x) | ||
error = (trial(model, x + ε₁) + trial(model, x - ε₁) + trial(model, x + ε₂) + | ||
trial(model, x - ε₂) - 4 * trial(model, x)) / | ||
ε^2 + sin(π * x[1]) * sin(π * x[2]) | ||
abs2(error) | ||
end | ||
function loss_by_taylordiff(model, x) | ||
f(x) = trial(model, x) | ||
error = derivative(f, x, [1., 0.], 2) + derivative(f, x, [0., 1.], 2) + sin(π * x[1]) * sin(π * x[2]) | ||
abs2(error) | ||
end | ||
|
||
(pinn::PINN)(x) = x[1] * (1 - x[1]) * x[2] * (1 - x[2]) * first(pinn.W₂ * exp.(pinn.W₁ * x + pinn.b₁) + pinn.b₂) | ||
opt = Flux.setup(Adam(), model) | ||
|
||
dataset = [rand(input) for i in 1:10] | ||
function loss(pinn) | ||
out = 0.0 | ||
for x in dataset | ||
out += derivative(pinn, x, [1., 0.], Val(2)) | ||
end | ||
out | ||
allloss(model, loss) = sum([loss(model, x) for x in data]) | ||
for epoch in 1:1000 | ||
Flux.train!(loss_by_taylordiff, model, data, opt) | ||
end | ||
|
||
myPINN = PINN(rand(hidden, input), rand(hidden), rand(1, hidden), rand(1)) | ||
grid = 0:0.01:1 | ||
solution(x, y) = (sin(π * x) * sin(π * y)) / (2π^2) | ||
u = [trial(model, [x, y]) for x in grid, y in grid] | ||
utrue = [solution(x, y) for x in grid, y in grid] | ||
diff_u = abs.(u .- utrue) | ||
|
||
gradient(loss, myPINN) | ||
surface(u) | ||
surface(utrue) | ||
surface(diff_u) |
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