-
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
Add NormalizedKernel #274
Changes from 6 commits
cb67f54
c1fa785
4804fbc
3752b1d
e587997
d86d81c
2d40b04
dfdfe9e
c125de7
1c2f6bd
61d41be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 Fill(κ(first_x, first_x), length(x)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we replace
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or can some kernel return a negative value... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look like this fixes the AD at least, cheers :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose k(x, x) could be zero as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But then we're in trouble cause we would be dividing by 0 and you would probably have bigger problems generally. |
||||||
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 |
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 |
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?