-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add NormalizedKernel #274
Merged
devmotion
merged 11 commits into
JuliaGaussianProcesses:master
from
rossviljoen:normalization
Apr 15, 2021
Merged
Add NormalizedKernel #274
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cb67f54
Add NormalizedKernel and tests
rossviljoen c1fa785
Fixed formatting
rossviljoen 4804fbc
Removed extraneous normalization functions
rossviljoen 3752b1d
Optimised kernelmatrix_diag[!] for single input
rossviljoen e587997
Use fill instead of map for kernelmatrix_diag[!]
rossviljoen d86d81c
Use FillArrays instead of fill
rossviljoen 2d40b04
Use Ones instead of Fill
rossviljoen dfdfe9e
Bump version to 0.9.2
rossviljoen c125de7
Update Project.toml
devmotion 1c2f6bd
Merge branch 'master' into normalization
devmotion 61d41be
Update Project.toml
devmotion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,81 @@ | ||
""" | ||
NormalizedKernel(k::Kernel) | ||
|
||
A normalized kernel derived from `k`. | ||
|
||
# Definition | ||
|
||
For inputs ``x, x'``, the normalized kernel ``\\widetilde{k}`` derived from | ||
kernel ``k`` is defined as | ||
```math | ||
\\widetilde{k}(x, x'; k) = \\frac{k(x, x')}{\\sqrt{k(x, x) k(x', x')}}. | ||
``` | ||
""" | ||
struct NormalizedKernel{Tk<:Kernel} <: Kernel | ||
kernel::Tk | ||
end | ||
|
||
@functor NormalizedKernel | ||
|
||
(κ::NormalizedKernel)(x, y) = κ.kernel(x, y) / sqrt(κ.kernel(x, x) * κ.kernel(y, y)) | ||
|
||
function kernelmatrix(κ::NormalizedKernel, x::AbstractVector, y::AbstractVector) | ||
return kernelmatrix(κ.kernel, x, y) ./ | ||
sqrt.( | ||
kernelmatrix_diag(κ.kernel, x) .* permutedims(kernelmatrix_diag(κ.kernel, y)) | ||
) | ||
end | ||
|
||
function kernelmatrix(κ::NormalizedKernel, x::AbstractVector) | ||
x_diag = kernelmatrix_diag(κ.kernel, x) | ||
return kernelmatrix(κ.kernel, x) ./ sqrt.(x_diag .* permutedims(x_diag)) | ||
end | ||
|
||
function kernelmatrix_diag(κ::NormalizedKernel, x::AbstractVector) | ||
first_x = first(x) | ||
return Ones{typeof(κ(first_x, first_x))}(length(x)) | ||
end | ||
|
||
function kernelmatrix_diag(κ::NormalizedKernel, x::AbstractVector, y::AbstractVector) | ||
return kernelmatrix_diag(κ.kernel, x, y) ./ | ||
sqrt.(kernelmatrix_diag(κ.kernel, x) .* kernelmatrix_diag(κ.kernel, y)) | ||
end | ||
|
||
function kernelmatrix!( | ||
K::AbstractMatrix, κ::NormalizedKernel, x::AbstractVector, y::AbstractVector | ||
) | ||
kernelmatrix!(K, κ.kernel, x, y) | ||
K ./= | ||
sqrt.(kernelmatrix_diag(κ.kernel, x) .* permutedims(kernelmatrix_diag(κ.kernel, y))) | ||
return K | ||
end | ||
|
||
function kernelmatrix!(K::AbstractMatrix, κ::NormalizedKernel, x::AbstractVector) | ||
kernelmatrix!(K, κ.kernel, x) | ||
x_diag = kernelmatrix_diag(κ.kernel, x) | ||
K ./= sqrt.(x_diag .* permutedims(x_diag)) | ||
return K | ||
end | ||
|
||
function kernelmatrix_diag!( | ||
K::AbstractVector, κ::NormalizedKernel, x::AbstractVector, y::AbstractVector | ||
) | ||
kernelmatrix_diag!(K, κ.kernel, x, y) | ||
K ./= sqrt.(kernelmatrix_diag(κ.kernel, x) .* kernelmatrix_diag(κ.kernel, y)) | ||
return K | ||
end | ||
|
||
function kernelmatrix_diag!(K::AbstractVector, κ::NormalizedKernel, x::AbstractVector) | ||
first_x = first(x) | ||
return fill!(K, κ(first_x, first_x)) | ||
end | ||
|
||
Base.show(io::IO, κ::NormalizedKernel) = printshifted(io, κ, 0) | ||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function printshifted(io::IO, κ::NormalizedKernel, shift::Int) | ||
println(io, "Normalized Kernel:") | ||
for _ in 1:(shift + 1) | ||
print(io, "\t") | ||
end | ||
return printshifted(io, κ.kernel, shift + 1) | ||
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,16 @@ | ||
@testset "normalizedkernel" begin | ||
rng = MersenneTwister(123456) | ||
x = randn(rng) | ||
y = randn(rng) | ||
|
||
k = 4 * SqExponentialKernel() | ||
kn = NormalizedKernel(k) | ||
@test kn(x, y) == k(x, y) / sqrt(k(x, x) * k(y, y)) | ||
@test kn(x, x) ≈ one(x) atol = 1e-5 | ||
|
||
# Standardised tests. | ||
TestUtils.test_interface(kn, Float64) | ||
test_ADs(x -> NormalizedKernel(exp(x[1]) * SqExponentialKernel()), rand(1)) | ||
|
||
test_params(kn, k) | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Isn't it generally more efficient to ocmpute the
sqrt
first? This is probably a performance detail though (and I don't know about machine accuracy)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.
Doesn't this all get fused anyway though?