Skip to content

Commit 9207a96

Browse files
willtebbutttheogf
andauthored
Remove _kernel, sort out kappa (#95)
* Removes _kernel * Remove _scale * Removes binary kappa * Removes k(x, y) -> Matrix * Fixes tests and adds eval * Removes metric from Transformed * Refactors eval * Remove eval from MyKernel * Improves style src/basekernels/fbm.jl Co-Authored-By: Théo Galy-Fajou <[email protected]> * Reverts removal of _scale * Reverts change to transforms. Co-Authored-By: Théo Galy-Fajou <[email protected]> * Requires Julia 1.3 * Removes 1.0 CI * Reverts rationalquad.jl changes * Removes custom show * Adds reference to #96 * Moves abstract types around * Pins Zygote to 0.4.16 * Adds extra tests Co-authored-by: Théo Galy-Fajou <[email protected]>
1 parent ff69083 commit 9207a96

31 files changed

+89
-93
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ os:
77
- linux
88
- osx
99
julia:
10-
- 1.0
1110
- 1.3
1211
- 1.4
1312
- nightly

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ SpecialFunctions = "0.8, 0.9, 0.10"
2121
StatsBase = "0.32, 0.33"
2222
StatsFuns = "0.8, 0.9"
2323
ZygoteRules = "0.2"
24-
julia = "1.0"
24+
Zygote = "= 0.4.16"
25+
julia = "1.3"
2526

2627
[extras]
2728
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"

docs/src/userguide.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ To premultiply the kernel by a variance, you can use `*` or create a `ScaledKern
2727
To compute the kernel function on two vectors you can call
2828
```julia
2929
k = SqExponentialKernel()
30-
x1 = rand(3); x2 = rand(3)
31-
kappa(k,x1,x2) == k(x1,x2) # Syntactic sugar
30+
x1 = rand(3)
31+
x2 = rand(3)
32+
k(x1,x2)
3233
```
3334

3435
## Creating a kernel matrix

src/KernelFunctions.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ KernelFunctions. [Github](https://github.com/JuliaGaussianProcesses/KernelFuncti
44
"""
55
module KernelFunctions
66

7-
export kernelmatrix, kernelmatrix!, kerneldiagmatrix, kerneldiagmatrix!, kappa
7+
export kernelmatrix, kernelmatrix!, kerneldiagmatrix, kerneldiagmatrix!
88
export transform
99
export duplicate, set! # Helpers
1010

@@ -42,6 +42,7 @@ const defaultobs = 2
4242
Abstract type defining a slice-wise transformation on an input matrix
4343
"""
4444
abstract type Transform end
45+
4546
abstract type Kernel end
4647
abstract type BaseKernel <: Kernel end
4748
abstract type SimpleKernel <: BaseKernel end

src/approximations/nystrom.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ end
1212
function nystrom_sample(k::Kernel, X::AbstractMatrix, S::Vector{<:Integer}; obsdim::Integer=defaultobs)
1313
obsdim [1, 2] || throw(ArgumentError("`obsdim` should be 1 or 2 (see docs of kernelmatrix))"))
1414
Xₘ = obsdim == 1 ? X[S, :] : X[:, S]
15-
C = k(Xₘ, X; obsdim=obsdim)
15+
C = kernelmatrix(k, Xₘ, X; obsdim=obsdim)
1616
Cs = C[:, S]
1717
return (C, Cs)
1818
end

src/basekernels/cosine.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The cosine kernel is a stationary kernel for a sinusoidal given by
99
struct CosineKernel <: SimpleKernel end
1010

1111
kappa::CosineKernel, d::Real) = cospi(d)
12+
1213
metric(::CosineKernel) = Euclidean()
1314

1415
Base.show(io::IO, ::CosineKernel) = print(io, "Cosine Kernel")

src/basekernels/exponential.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ related form of the kernel or [`GammaExponentialKernel`](@ref) for a generalizat
1212
struct SqExponentialKernel <: SimpleKernel end
1313

1414
kappa::SqExponentialKernel, d²::Real) = exp(-d²)
15-
iskroncompatible(::SqExponentialKernel) = true
15+
1616
metric(::SqExponentialKernel) = SqEuclidean()
1717

18+
iskroncompatible(::SqExponentialKernel) = true
19+
1820
Base.show(io::IO,::SqExponentialKernel) = print(io,"Squared Exponential Kernel")
1921

2022
## Aliases ##
@@ -33,9 +35,11 @@ The exponential kernel is a Mercer kernel given by the formula:
3335
struct ExponentialKernel <: SimpleKernel end
3436

3537
kappa::ExponentialKernel, d::Real) = exp(-d)
36-
iskroncompatible(::ExponentialKernel) = true
38+
3739
metric(::ExponentialKernel) = Euclidean()
3840

41+
iskroncompatible(::ExponentialKernel) = true
42+
3943
Base.show(io::IO, ::ExponentialKernel) = print(io, "Exponential Kernel")
4044

4145
## Alias ##
@@ -60,7 +64,9 @@ struct GammaExponentialKernel{Tγ<:Real} <: SimpleKernel
6064
end
6165

6266
kappa::GammaExponentialKernel, d²::Real) = exp(-^first.γ))
63-
iskroncompatible(::GammaExponentialKernel) = true
67+
6468
metric(::GammaExponentialKernel) = SqEuclidean()
6569

70+
iskroncompatible(::GammaExponentialKernel) = true
71+
6672
Base.show(io::IO, κ::GammaExponentialKernel) = print(io, "Gamma Exponential Kernel (γ = ", first.γ), ")")

src/basekernels/exponentiated.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ The exponentiated kernel is a Mercer kernel given by:
99
struct ExponentiatedKernel <: SimpleKernel end
1010

1111
kappa::ExponentiatedKernel, xᵀy::Real) = exp(xᵀy)
12+
1213
metric(::ExponentiatedKernel) = DotProduct()
14+
1315
iskroncompatible(::ExponentiatedKernel) = true
1416

1517
Base.show(io::IO, ::ExponentiatedKernel) = print(io, "Exponentiated Kernel")

src/basekernels/fbm.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ struct FBMKernel{T<:Real} <: BaseKernel
1717
end
1818
end
1919

20+
function::FBMKernel)(x::AbstractVector{<:Real}, y::AbstractVector{<:Real})
21+
modX = sum(abs2, x)
22+
modY = sum(abs2, y)
23+
modXY = evaluate(SqEuclidean(sqroundoff), x, y)
24+
h = first.h)
25+
return (modX^h + modY^h - modXY^h)/2
26+
end
27+
28+
::FBMKernel)(x::Real, y::Real) = (abs2(x)^first.h) + abs2(y)^first.h) - abs2(x - y)^first.h)) / 2
29+
2030
Base.show(io::IO, κ::FBMKernel) = print(io, "Fractional Brownian Motion Kernel (h = ", first.h), ")")
2131

2232
const sqroundoff = 1e-15
@@ -65,13 +75,3 @@ function kernelmatrix!(
6575
K .= _fbm.(vec(modX), reshape(modY, 1, :), modXY, κ.h)
6676
return K
6777
end
68-
69-
function kappa::FBMKernel, x::AbstractVector{<:Real}, y::AbstractVector{<:Real})
70-
modX = sum(abs2, x)
71-
modY = sum(abs2, y)
72-
modXY = evaluate(SqEuclidean(sqroundoff), x, y)
73-
h = first.h)
74-
return (modX^h + modY^h - modXY^h)/2
75-
end
76-
77-
::FBMKernel)(x::Real, y::Real) = (abs2(x)^first.h) + abs2(y)^first.h) - abs2(x-y)^first.h))/2

src/basekernels/gabor.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ struct GaborKernel{K<:Kernel} <: BaseKernel
1515
end
1616
end
1717

18+
::GaborKernel)(x, y) = κ.kernel(x ,y)
19+
1820
function _gabor(; ell = nothing, p = nothing)
1921
if ell === nothing
2022
if p === nothing
@@ -53,8 +55,6 @@ end
5355

5456
Base.show(io::IO, κ::GaborKernel) = print(io, "Gabor Kernel (ell = ", κ.ell, ", p = ", κ.p, ")")
5557

56-
kappa::GaborKernel, x, y) = kappa.kernel, x ,y)
57-
5858
function kernelmatrix(
5959
κ::GaborKernel,
6060
X::AbstractMatrix;

0 commit comments

Comments
 (0)