Skip to content

Commit 98815a4

Browse files
authored
Merge branch 'master' into compathelper/new_version/2021-10-26-00-42-21-941-03135690217
2 parents a6e2264 + 924925d commit 98815a4

31 files changed

+213
-261
lines changed

.github/workflows/benchmark.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Run benchmarks
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
Benchmark:
8+
runs-on: ubuntu-latest
9+
if: contains(github.event.pull_request.labels.*.name, 'performance critical')
10+
env:
11+
JULIA_DEBUG: BenchmarkCI
12+
steps:
13+
- uses: actions/checkout@v2
14+
- uses: julia-actions/setup-julia@latest
15+
with:
16+
version: 1.6
17+
- name: Install dependencies
18+
run: julia -e 'using Pkg; pkg"add PkgBenchmark BenchmarkCI"'
19+
- name: Run benchmarks
20+
run: julia -e "using BenchmarkCI; BenchmarkCI.judge()"
21+
- name: Post results
22+
run: julia -e "using BenchmarkCI; BenchmarkCI.postjudge()"
23+
env:
24+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/test/Manifest.toml
55
coverage/
66
.DS_store
7+
benchmark/Manifest.toml

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "KernelFunctions"
22
uuid = "ec8451be-7e33-11e9-00cf-bbf324bd1392"
3-
version = "0.10.26"
3+
version = "0.10.27"
44

55
[deps]
66
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
@@ -30,7 +30,7 @@ Functors = "0.1, 0.2"
3030
IrrationalConstants = "0.1"
3131
LogExpFunctions = "0.2.1, 0.3"
3232
Requires = "1.0.1"
33-
SpecialFunctions = "0.8, 0.9, 0.10, 1"
33+
SpecialFunctions = "0.8, 0.9, 0.10, 1, 2"
3434
StatsBase = "0.32, 0.33"
3535
TensorCore = "0.1"
3636
ZygoteRules = "0.2"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# KernelFunctions.jl
22

33
![CI](https://github.com/JuliaGaussianProcesses/KernelFunctions.jl/workflows/CI/badge.svg?branch=master)
4-
[![Coverage Status](https://coveralls.io/repos/github/JuliaGaussianProcesses/KernelFunctions.jl/badge.svg?branch=master)](https://coveralls.io/github/JuliaGaussianProcesses/KernelFunctions.jl?branch=master)
4+
[![codecov](https://codecov.io/gh/JuliaGaussianProcesses/KernelFunctions.jl/branch/master/graph/badge.svg?token=rmDh3gb7hN)](https://codecov.io/gh/JuliaGaussianProcesses/KernelFunctions.jl)
55
[![Documentation (stable)](https://img.shields.io/badge/docs-stable-blue.svg)](https://juliagaussianprocesses.github.io/KernelFunctions.jl/stable)
66
[![Documentation (latest)](https://img.shields.io/badge/docs-dev-blue.svg)](https://juliagaussianprocesses.github.io/KernelFunctions.jl/dev)
77
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://github.com/SciML/ColPrac)

benchmark/MLKernels.jl

Lines changed: 0 additions & 22 deletions
This file was deleted.

benchmark/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[deps]
2+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
3+
KernelFunctions = "ec8451be-7e33-11e9-00cf-bbf324bd1392"

benchmark/benchmarks.jl

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
11
using BenchmarkTools
2-
using Random
3-
using Distances, LinearAlgebra
2+
using KernelFunctions
43

5-
const SUITE = BenchmarkGroup()
4+
N1 = 10
5+
N2 = 20
66

7-
Random.seed!(1234)
7+
X = rand(N1, N2)
8+
Xc = ColVecs(X)
9+
Xr = RowVecs(X)
10+
Xv = collect.(eachcol(X))
11+
Y = rand(N1, N2)
12+
Yc = ColVecs(Y)
13+
Yr = RowVecs(Y)
14+
Yv = collect.(eachcol(Y))
815

9-
dim = 50
10-
N1 = 1000;
11-
N2 = 500;
12-
alpha = 2.0
16+
# Create the general suite of benchmarks
17+
SUITE = BenchmarkGroup()
1318

14-
X = rand(Float64, N1, dim)
15-
Y = rand(Float64, N2, dim)
19+
kernels = Dict(
20+
"SqExponential" => SqExponentialKernel(), "Exponential" => ExponentialKernel()
21+
)
1622

17-
KXY = rand(Float64, N1, N2)
18-
KX = rand(Float64, N1, N1)
19-
sKX = Symmetric(rand(Float64, N1, N1))
20-
kX = rand(Float64, N1)
23+
inputtypes = Dict("ColVecs" => (Xc, Yc), "RowVecs" => (Xr, Yr), "Vecs" => (Xv, Yv))
2124

22-
include("kernelmatrix.jl")
23-
include("MLKernels.jl")
25+
functions = Dict(
26+
"kernelmatrixX" => (kernel, X, Y) -> kernelmatrix(kernel, X),
27+
"kernelmatrixXY" => (kernel, X, Y) -> kernelmatrix(kernel, X, Y),
28+
"kernelmatrix_diagX" => (kernel, X, Y) -> kernelmatrix_diag(kernel, X),
29+
"kernelmatrix_diagXY" => (kernel, X, Y) -> kernelmatrix_diag(kernel, X, Y),
30+
)
31+
32+
for (kname, kernel) in kernels
33+
SUITE[kname] = sk = BenchmarkGroup()
34+
for (inputname, (X, Y)) in inputtypes
35+
sk[inputname] = si = BenchmarkGroup()
36+
for (fname, f) in functions
37+
si[fname] = @benchmarkable $f($kernel, $X, $Y)
38+
end
39+
end
40+
end
41+
42+
# Uncomment the following to run benchmark locally
43+
44+
# tune!(SUITE)
45+
46+
# results = run(SUITE, verbose=true)

benchmark/kernelmatrix.jl

Lines changed: 0 additions & 39 deletions
This file was deleted.

examples/gaussian-process-priors/script.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ kernels = [
120120
LinearKernel(),
121121
compose(PeriodicKernel(), ScaleTransform(0.2)),
122122
NeuralNetworkKernel(),
123+
GibbsKernel(; lengthscale=x -> sum(exp sin, x)),
123124
]
124125
plot(
125126
[visualize(k) for k in kernels]...;
Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# # Support Vector Machine
22
#
3+
# In this notebook we show how you can use KernelFunctions.jl to generate
4+
# kernel matrices for classification with a support vector machine, as
5+
# implemented by LIBSVM.
36

47
using Distributions
58
using KernelFunctions
@@ -8,39 +11,45 @@ using LinearAlgebra
811
using Plots
912
using Random
1013

11-
## Set plotting theme
12-
theme(:wong)
13-
1414
## Set seed
1515
Random.seed!(1234);
1616

17-
# Number of samples:
18-
N = 100;
17+
# ## Generate half-moon dataset
18+
19+
# Number of samples per class:
20+
nin = nout = 50;
1921

20-
# Select randomly between two classes:
21-
y_train = rand([-1, 1], N);
22+
# We generate data based on SciKit-Learn's sklearn.datasets.make_moons function:
2223

23-
# Random attributes for both classes:
24-
X = Matrix{Float64}(undef, 2, N)
25-
rand!(MvNormal(randn(2), I), view(X, :, y_train .== 1))
26-
rand!(MvNormal(randn(2), I), view(X, :, y_train .== -1));
27-
x_train = ColVecs(X);
24+
class1x = cos.(range(0, π; length=nout))
25+
class1y = sin.(range(0, π; length=nout))
26+
class2x = 1 .- cos.(range(0, π; length=nin))
27+
class2y = 1 .- sin.(range(0, π; length=nin)) .- 0.5
28+
X = hcat(vcat(class1x, class2x), vcat(class1y, class2y))
29+
X .+= 0.1randn(size(X))
30+
x_train = RowVecs(X)
31+
y_train = vcat(fill(-1, nout), fill(1, nin));
2832

29-
# Create a 2D grid:
33+
# Create a 100×100 2D grid for evaluation:
3034
test_range = range(floor(Int, minimum(X)), ceil(Int, maximum(X)); length=100)
3135
x_test = ColVecs(mapreduce(collect, hcat, Iterators.product(test_range, test_range)));
3236

37+
# ## SVM model
38+
#
3339
# Create kernel function:
34-
k = SqExponentialKernel() ScaleTransform(2.0)
40+
k = SqExponentialKernel() ScaleTransform(1.5)
3541

3642
# [LIBSVM](https://github.com/JuliaML/LIBSVM.jl) can make use of a pre-computed kernel matrix.
3743
# KernelFunctions.jl can be used to produce that.
38-
# Precomputed matrix for training (corresponds to linear kernel)
44+
#
45+
# Precomputed matrix for training
3946
model = svmtrain(kernelmatrix(k, x_train), y_train; kernel=LIBSVM.Kernel.Precomputed)
4047

4148
# Precomputed matrix for prediction
42-
y_pr, _ = svmpredict(model, kernelmatrix(k, x_train, x_test));
49+
y_pred, _ = svmpredict(model, kernelmatrix(k, x_train, x_test));
4350

44-
# Compute prediction on a grid:
45-
contourf(test_range, test_range, y_pr)
46-
scatter!(X[1, :], X[2, :]; color=y_train, lab="data", widen=false)
51+
# Visualize prediction on a grid:
52+
plot(; lim=extrema(test_range), aspect_ratio=1)
53+
contourf!(test_range, test_range, y_pred; levels=1, color=cgrad(:redsblues), alpha=0.7)
54+
scatter!(X[y_train .== -1, 1], X[y_train .== -1, 2]; color=:red, label="class 1")
55+
scatter!(X[y_train .== +1, 1], X[y_train .== +1, 2]; color=:blue, label="class 2")

src/basekernels/constant.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ end
7373

7474
@functor ConstantKernel
7575

76-
kappa::ConstantKernel, x::Real) = first.c) * one(x)
76+
kappa::ConstantKernel, x::Real) = only.c) * one(x)
7777

7878
metric(::ConstantKernel) = Delta()
7979

80-
Base.show(io::IO, κ::ConstantKernel) = print(io, "Constant Kernel (c = ", first.c), ")")
80+
Base.show(io::IO, κ::ConstantKernel) = print(io, "Constant Kernel (c = ", only.c), ")")

src/basekernels/exponential.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,14 @@ end
137137

138138
@functor GammaExponentialKernel
139139

140-
kappa::GammaExponentialKernel, d::Real) = exp(-d^first.γ))
140+
kappa::GammaExponentialKernel, d::Real) = exp(-d^only.γ))
141141

142142
metric(k::GammaExponentialKernel) = k.metric
143143

144144
iskroncompatible(::GammaExponentialKernel) = true
145145

146146
function Base.show(io::IO, κ::GammaExponentialKernel)
147147
return print(
148-
io, "Gamma Exponential Kernel (γ = ", first.γ), ", metric = ", κ.metric, ")"
148+
io, "Gamma Exponential Kernel (γ = ", only.γ), ", metric = ", κ.metric, ")"
149149
)
150150
end

src/basekernels/fbm.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ function (κ::FBMKernel)(x::AbstractVector{<:Real}, y::AbstractVector{<:Real})
2828
modX = sum(abs2, x)
2929
modY = sum(abs2, y)
3030
modXY = sqeuclidean(x, y)
31-
h = first.h)
31+
h = only.h)
3232
return (modX^h + modY^h - modXY^h) / 2
3333
end
3434

3535
function::FBMKernel)(x::Real, y::Real)
36-
return (abs2(x)^first.h) + abs2(y)^first.h) - abs2(x - y)^first.h)) / 2
36+
return (abs2(x)^only.h) + abs2(y)^only.h) - abs2(x - y)^only.h)) / 2
3737
end
3838

3939
function Base.show(io::IO, κ::FBMKernel)
40-
return print(io, "Fractional Brownian Motion Kernel (h = ", first.h), ")")
40+
return print(io, "Fractional Brownian Motion Kernel (h = ", only.h), ")")
4141
end
4242

4343
_fbm(modX, modY, modXY, h) = (modX^h + modY^h - modXY^h) / 2

src/basekernels/matern.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ MaternKernel(; nu::Real=1.5, ν::Real=nu, metric=Euclidean()) = MaternKernel(ν,
3434
@functor MaternKernel
3535

3636
@inline function kappa::MaternKernel, d::Real)
37-
result = _matern(first.ν), d)
37+
result = _matern(only.ν), d)
3838
return ifelse(iszero(d), one(result), result)
3939
end
4040

@@ -46,7 +46,7 @@ end
4646
metric(k::MaternKernel) = k.metric
4747

4848
function Base.show(io::IO, κ::MaternKernel)
49-
return print(io, "Matern Kernel (ν = ", first.ν), ", metric = ", κ.metric, ")")
49+
return print(io, "Matern Kernel (ν = ", only.ν), ", metric = ", κ.metric, ")")
5050
end
5151

5252
## Matern12Kernel = ExponentialKernel aliased in exponential.jl

src/basekernels/polynomial.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ LinearKernel(; c::Real=0.0) = LinearKernel(c)
2626

2727
@functor LinearKernel
2828

29-
kappa::LinearKernel, xᵀy::Real) = xᵀy + first.c)
29+
kappa::LinearKernel, xᵀy::Real) = xᵀy + only.c)
3030

3131
metric(::LinearKernel) = DotProduct()
3232

33-
Base.show(io::IO, κ::LinearKernel) = print(io, "Linear Kernel (c = ", first.c), ")")
33+
Base.show(io::IO, κ::LinearKernel) = print(io, "Linear Kernel (c = ", only.c), ")")
3434

3535
"""
3636
PolynomialKernel(; degree::Int=2, c::Real=0.0)
@@ -53,7 +53,7 @@ struct PolynomialKernel{Tc<:Real} <: SimpleKernel
5353

5454
function PolynomialKernel{Tc}(degree::Int, c::Vector{Tc}) where {Tc}
5555
@check_args(PolynomialKernel, degree, degree >= one(degree), "degree ≥ 1")
56-
@check_args(PolynomialKernel, c, first(c) >= zero(Tc), "c ≥ 0")
56+
@check_args(PolynomialKernel, c, only(c) >= zero(Tc), "c ≥ 0")
5757
return new{Tc}(degree, c)
5858
end
5959
end
@@ -68,10 +68,10 @@ function Functors.functor(::Type{<:PolynomialKernel}, x)
6868
return (c=x.c,), reconstruct_polynomialkernel
6969
end
7070

71-
kappa::PolynomialKernel, xᵀy::Real) = (xᵀy + first.c))^κ.degree
71+
kappa::PolynomialKernel, xᵀy::Real) = (xᵀy + only.c))^κ.degree
7272

7373
metric(::PolynomialKernel) = DotProduct()
7474

7575
function Base.show(io::IO, κ::PolynomialKernel)
76-
return print(io, "Polynomial Kernel (c = ", first.c), ", degree = ", κ.degree, ")")
76+
return print(io, "Polynomial Kernel (c = ", only.c), ", degree = ", κ.degree, ")")
7777
end

0 commit comments

Comments
 (0)