Skip to content
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 Periodic Kernel #50

Merged
merged 10 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444"

[compat]
Compat = "2.2, 3"
Distances = "0.8"
Distances = "0.8.2"
Requires = "1.0.1"
SpecialFunctions = "0.8, 0.9, 0.10"
StatsBase = "0.32, 0.33"
Expand Down
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ LinearKernel
PolynomialKernel
RationalQuadraticKernel
GammaRationalQuadraticKernel
PeriodicKernel
ZeroKernel
ConstantKernel
WhiteKernel
Expand Down
8 changes: 8 additions & 0 deletions docs/src/kernels.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ The [Polynomial Kernel](@ref KernelFunctions.PolynomialKernel) is defined as
k(x,x';c,d) = \left(\langle x,x'\rangle + c\right)^d
```

## Periodic Kernels

### PeriodicKernel

```math
k(x,x';r) = \exp\left(-0.5 \sum_i (sin (π(x_i - x'_i))/r_i)^2\right)
```

## Constant Kernels

### ConstantKernel
Expand Down
4 changes: 3 additions & 1 deletion src/KernelFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export MaternKernel, Matern32Kernel, Matern52Kernel
export LinearKernel, PolynomialKernel
export RationalQuadraticKernel, GammaRationalQuadraticKernel
export MahalanobisKernel, GaborKernel, PiecewisePolynomialKernel
export PeriodicKernel
export KernelSum, KernelProduct
export TransformedKernel, ScaledKernel

Expand Down Expand Up @@ -44,9 +45,10 @@ abstract type BaseKernel <: Kernel end
include("utils.jl")
include("distances/dotproduct.jl")
include("distances/delta.jl")
include("distances/sinus.jl")
include("transform/transform.jl")

for k in ["exponential","matern","polynomial","constant","rationalquad","exponentiated","cosine","maha","fbm","gabor","piecewisepolynomial"]
for k in ["exponential","matern","polynomial","constant","rationalquad","exponentiated","cosine","maha","fbm","gabor","periodic","piecewisepolynomial"]
include(joinpath("kernels",k*".jl"))
end
include("kernels/transformedkernel.jl")
Expand Down
6 changes: 3 additions & 3 deletions src/distances/delta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ end
@boundscheck if length(a) != length(b)
throw(DimensionMismatch("first array has length $(length(a)) which does not match the length of the second, $(length(b))."))
end
return a==b
return a == b
end

@inline (dist::Delta)(a::AbstractArray,b::AbstractArray) = Distances._evaluate(dist,a,b)
@inline (dist::Delta)(a::Number,b::Number) = a==b
@inline (dist::Delta)(a::AbstractArray, b::AbstractArray) = Distances._evaluate(dist, a, b)
@inline (dist::Delta)(a::Number,b::Number) = a == b
11 changes: 6 additions & 5 deletions src/distances/dotproduct.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
struct DotProduct <: Distances.PreMetric
end
struct DotProduct <: Distances.PreMetric end
# struct DotProduct <: Distances.UnionSemiMetric end

@inline function Distances._evaluate(::DotProduct,a::AbstractVector{T},b::AbstractVector{T}) where {T}
@inline function Distances._evaluate(::DotProduct, a::AbstractVector{T}, b::AbstractVector{T}) where {T}
@boundscheck if length(a) != length(b)
throw(DimensionMismatch("first array has length $(length(a)) which does not match the length of the second, $(length(b))."))
end
return dot(a,b)
end

@inline (dist::DotProduct)(a::AbstractArray,b::AbstractArray) = Distances._evaluate(dist,a,b)
@inline (dist::DotProduct)(a::Number,b::Number) = a*b
@inline Distances.eval_op(::DotProduct, a::Real, b::Real) = a * b
@inline (dist::DotProduct)(a::AbstractArray,b::AbstractArray) = Distances._evaluate(dist, a, b)
@inline (dist::DotProduct)(a::Number,b::Number) = a * b
16 changes: 16 additions & 0 deletions src/distances/sinus.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
struct Sinus{T} <: Distances.SemiMetric
# struct Sinus{T} <: Distances.UnionSemiMetric
r::Vector{T}
end

Distances.parameters(d::Sinus) = d.r
@inline Distances.eval_op(::Sinus, a::Real, b::Real, p::Real) = abs2(sinpi(a - b) / p)
@inline (dist::Sinus)(a::AbstractArray, b::AbstractArray) = Distances._evaluate(dist, a, b)
@inline (dist::Sinus)(a::Number, b::Number) = abs2(sinpi(a - b) / first(dist.r))

@inline function Distances._evaluate(d::Sinus, a::AbstractVector{T}, b::AbstractVector{T}) where {T}
@boundscheck if (length(a) != length(b)) || length(a) != length(d.r)
throw(DimensionMismatch("Dimensions of the inputs are not matching : a = $(length(a)), b = $(length(b)), r = $(length(d.r))"))
end
return sum(abs2, sinpi.(a - b) ./ d.r)
end
27 changes: 27 additions & 0 deletions src/kernels/periodic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
PeriodicKernel(r::AbstractVector)
PeriodicKernel(dims::Int)
PeriodicKernel(T::DataType, dims::Int)

Periodic Kernel as described in http://www.inference.org.uk/mackay/gpB.pdf eq. 47.
```
κ(x,y) = exp( - 0.5 sum_i(sin (π(x_i - y_i))/r_i))
```
"""
struct PeriodicKernel{T} <: BaseKernel
r::Vector{T}
function PeriodicKernel(; r::AbstractVector{T} = ones(Float64, 1)) where {T<:Real}
@assert all(r .> 0)
new{T}(r)
end
end

PeriodicKernel(dims::Int) = PeriodicKernel(Float64, dims)

PeriodicKernel(T::DataType, dims::Int = 1) = PeriodicKernel(r = ones(T, dims))

metric(κ::PeriodicKernel) = Sinus(κ.r)

kappa(κ::PeriodicKernel, d::Real) = exp(- 0.5d)

Base.show(io::IO, κ::PeriodicKernel) = print(io, "Periodic Kernel, length(r) = $(length(κ.r))")
2 changes: 2 additions & 0 deletions src/trainable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ trainable(k::MaternKernel) = (k.ν,)

trainable(k::LinearKernel) = (k.c,)

trainable(k::PeriodicKernel) = (k.r,)

trainable(k::PolynomialKernel) = (k.d, k.c)

trainable(k::RationalQuadraticKernel) = (k.α,)
Expand Down
12 changes: 10 additions & 2 deletions src/zygote_adjoints.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
@adjoint function evaluate(s::DotProduct, x::AbstractVector, y::AbstractVector)
dot(x,y), Δ -> begin
(nothing, Δ.*y, Δ.*x)
dot(x, y), Δ -> begin
(nothing, Δ .* y, Δ .* x)
end
end

# @adjoint function evaluate(s::Sinus, x::AbstractVector, y::AbstractVector)
# d = evaluate(s, x, y)
# s = sum(sin.(π*(x-y)))
# d, Δ -> begin
# (Sinus(Δ ./ s.r), 2Δ .* cos.(x - y) * d, -2Δ .* cos.(x - y) * d)
# end
# end
9 changes: 9 additions & 0 deletions test/distances/sinus.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@testset "sinus" begin
A = rand(10)
B = rand(10)
p = rand(10)
d = KernelFunctions.Sinus(p)
@test Distances.parameters(d) == p
@test evaluate(d, A, B) == sum(abs2.(sinpi.(A - B) ./ p))
@test d(3.0, 2.0) == abs2(sinpi(3.0 - 2.0) / first(p))
end
10 changes: 10 additions & 0 deletions test/kernels/periodic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@testset "Periodic Kernel" begin
x = rand()*2; v1 = rand(3); v2 = rand(3);
r = rand(3)
k = PeriodicKernel(r = r)
@test kappa(k, x) ≈ exp(-0.5x)
@test k(v1, v2) ≈ exp(-0.5 * sum(abs2, sinpi.(v1 - v2) ./ r))
@test k(v1, v2) == k(v2, v1)
@test PeriodicKernel(3)(v1, v2) == PeriodicKernel(r = ones(3))(v1, v2)
@test_nowarn println(k)
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ using KernelFunctions: metric
@testset "distances" begin
include(joinpath("distances", "dotproduct.jl"))
include(joinpath("distances", "delta.jl"))
include(joinpath("distances", "sinus.jl"))
end

@testset "transform" begin
Expand All @@ -71,6 +72,7 @@ using KernelFunctions: metric
include(joinpath("kernels", "kernelproduct.jl"))
include(joinpath("kernels", "kernelsum.jl"))
include(joinpath("kernels", "matern.jl"))
include(joinpath("kernels", "periodic.jl"))
include(joinpath("kernels", "polynomial.jl"))
include(joinpath("kernels", "piecewisepolynomial.jl"))
include(joinpath("kernels", "rationalquad.jl"))
Expand Down
5 changes: 4 additions & 1 deletion test/trainable.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@testset "trainable" begin
ν = 2.0; c = 3.0; d = 2.0; γ = 2.0; α = 2.5; h = 0.5
ν = 2.0; c = 3.0; d = 2.0; γ = 2.0; α = 2.5; h = 0.5; r = rand(3)

kc = ConstantKernel(c=c)
@test all(params(kc) .== params([c]))
Expand All @@ -22,6 +22,9 @@
kp = PolynomialKernel(c=c, d=d)
@test all(params(kp) .== params([d], [c]))

kpe = PeriodicKernel(r = r)
@test all(params(kpe) .== params(r))

kr = RationalQuadraticKernel(α=α)
@test all(params(kr) .== params([α]))

Expand Down