-
Notifications
You must be signed in to change notification settings - Fork 10
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
aeeceef
commit ce4b82d
Showing
7 changed files
with
119 additions
and
57 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
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 |
---|---|---|
@@ -1,41 +1,56 @@ | ||
using Lux, Zygote | ||
using Lux, Zygote, Enzyme, ComponentArrays | ||
|
||
const input = 2 | ||
const hidden = 16 | ||
|
||
model = Chain(Dense(input => hidden, Lux.relu), | ||
Dense(hidden => hidden, Lux.relu), | ||
Dense(hidden => 1), | ||
first) | ||
|
||
ps, st = Lux.setup(rng, model) | ||
|
||
trial(model, x) = x[1] * (1 - x[1]) * x[2] * (1 - x[2]) * model(x, ps, st)[1] | ||
function trial(model, x, ps, st) | ||
u, st = Lux.apply(model, x, ps, st) | ||
x[1] * (1 - x[1]) * x[2] * (1 - x[2]) * u | ||
end | ||
|
||
x = rand(Float32, input) | ||
trial(model, x) | ||
function loss_by_finitediff(model, x) | ||
ε = cbrt(eps(Float32)) | ||
ε₁ = [ε, 0] | ||
ε₂ = [0, ε] | ||
error = (trial(model, x + ε₁) + trial(model, x - ε₁) + trial(model, x + ε₂) + | ||
trial(model, x - ε₂) - 4 * trial(model, x)) / | ||
ε^2 + sin(π * x[1]) * sin(π * x[2]) | ||
function loss_by_finitediff(model, x, ps, st) | ||
T = eltype(x) | ||
ε = cbrt(eps(T)) | ||
ε₁ = [ε, zero(T)] | ||
ε₂ = [zero(T), ε] | ||
f(x) = trial(model, x, ps, st) | ||
error = (f(x + ε₁) + f(x - ε₁) + f(x + ε₂) + f(x - ε₂) - 4 * f(x)) / ε^2 + | ||
sin(π * x[1]) * sin(π * x[2]) | ||
abs2(error) | ||
end | ||
function loss_by_taylordiff(model, x) | ||
f(x) = trial(model, x) | ||
function loss_by_taylordiff(model, x, ps, st) | ||
f(x) = trial(model, x, ps, st) | ||
error = derivative(f, x, Float32[1, 0], Val(2)) + | ||
derivative(f, x, Float32[0, 1], Val(2)) + | ||
sin(π * x[1]) * sin(π * x[2]) | ||
abs2(error) | ||
end | ||
function loss_by_forwarddiff(model, x, ps, st) | ||
f(x) = trial(model, x, ps, st) | ||
error = derivative(f, x, Float32[1, 0], Val(2)) + | ||
derivative(f, x, Float32[0, 1], Val(2)) + | ||
sin(π * x[1]) * sin(π * x[2]) | ||
abs2(error) | ||
end | ||
|
||
const input = 2 | ||
const hidden = 16 | ||
model = Chain(Dense(input => hidden, exp), | ||
Dense(hidden => hidden, exp), | ||
Dense(hidden => 1), | ||
first) | ||
x = rand(Float32, input) | ||
dx = deepcopy(x) | ||
ps, st = Lux.setup(rng, model) | ||
ps = ps |> ComponentArray | ||
dps = deepcopy(ps) | ||
dx .= 0; | ||
dps .= 0; | ||
|
||
pinn_t = BenchmarkGroup("primal" => (@benchmarkable loss_by_taylordiff($model, $x)), | ||
pinn_t = BenchmarkGroup( | ||
"primal" => (@benchmarkable loss_by_taylordiff($model, $x, $ps, $st)), | ||
"gradient" => (@benchmarkable gradient(loss_by_taylordiff, $model, | ||
$x))) | ||
pinn_f = BenchmarkGroup("primal" => (@benchmarkable loss_by_finitediff($model, $x)), | ||
$x, $ps, $st))) | ||
pinn_f = BenchmarkGroup( | ||
"primal" => (@benchmarkable loss_by_finitediff($model, $x, $ps, $st)), | ||
"gradient" => (@benchmarkable gradient($loss_by_finitediff, $model, | ||
$x))) | ||
$x, $ps, $st))) | ||
pinn = BenchmarkGroup(["vector", "physical"], "taylordiff" => pinn_t, | ||
"finitediff" => pinn_f) |
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