-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add L2 regularization * add unit test * format * add regularization to refs page in docs * fix typo * remove input arguments for regularize! template * clarify docstrings
- Loading branch information
1 parent
f713f46
commit 8c2b6b0
Showing
9 changed files
with
178 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using KernelInterpolation | ||
using Plots | ||
|
||
# interpolate Franke function | ||
function f(x) | ||
0.75 * exp(-0.25 * ((9 * x[1] - 2)^2 + (9 * x[2] - 2)^2)) + | ||
0.75 * exp(-(9 * x[1] + 1)^2 / 49 - (9 * x[2] + 1) / 10) + | ||
0.5 * exp(-0.25 * ((9 * x[1] - 7)^2 + (9 * x[2] - 3)^2)) - | ||
0.2 * exp(-(9 * x[1] - 4)^2 - (9 * x[2] - 7)^2) | ||
end | ||
|
||
n = 1089 | ||
nodeset = random_hypercube(n; dim = 2) | ||
values = f.(nodeset) .+ 0.03 * randn(n) | ||
|
||
kernel = ThinPlateSplineKernel{dim(nodeset)}() | ||
itp_reg = interpolate(nodeset, values, kernel, reg = L2Regularization(1e-2)) | ||
itp = interpolate(nodeset, values, kernel) | ||
|
||
N = 40 | ||
many_nodes = homogeneous_hypercube(N; dim = 2) | ||
|
||
p1 = plot(many_nodes, itp_reg, st = :surface, training_nodes = false) | ||
p2 = plot(many_nodes, itp, st = :surface, training_nodes = false) | ||
|
||
plot(p1, p2, layout = (2, 1)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
AbstractRegularization | ||
An abstract supertype of regularizations. A regularization implements a function | ||
[`regularize!`](@ref) that takes a matrix and returns a regularized version of it. | ||
""" | ||
abstract type AbstractRegularization end | ||
|
||
""" | ||
regularize!(A, reg::AbstractRegularization) | ||
Apply the regularization `reg` to the matrix `A` in place. | ||
""" | ||
function regularize! end | ||
|
||
""" | ||
NoRegularization() | ||
A regularization that does nothing. | ||
""" | ||
struct NoRegularization <: AbstractRegularization end | ||
|
||
function regularize!(A, ::NoRegularization) | ||
return nothing | ||
end | ||
|
||
""" | ||
L2Regularization(regularization_parameter::Real) | ||
A regularization that adds a multiple of the identity matrix to the input matrix. | ||
""" | ||
struct L2Regularization{RealT <: Real} <: AbstractRegularization | ||
regularization_parameter::RealT | ||
end | ||
|
||
function regularize!(A, reg::L2Regularization) | ||
A[diagind(A)] .+= reg.regularization_parameter | ||
return nothing | ||
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
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