Skip to content

Move to AbstractVector #99

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

Merged
merged 46 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
83d72ac
Removes _kernel
willtebbutt Apr 23, 2020
3d4f82d
Remove _scale
willtebbutt Apr 23, 2020
b84eb57
Removes binary kappa
willtebbutt Apr 23, 2020
8d2ff2d
Removes k(x, y) -> Matrix
willtebbutt Apr 23, 2020
4c8c5d8
Fixes tests and adds eval
willtebbutt Apr 23, 2020
a61ef4e
Removes metric from Transformed
willtebbutt Apr 23, 2020
e547ca4
Refactors eval
willtebbutt Apr 23, 2020
acfd72d
Remove eval from MyKernel
willtebbutt Apr 23, 2020
d68767f
Improves style src/basekernels/fbm.jl
willtebbutt Apr 24, 2020
4cb0b60
Reverts removal of _scale
willtebbutt Apr 24, 2020
16789dd
Merge branch 'wct/87' of https://github.com/JuliaGaussianProcesses/Ke…
willtebbutt Apr 24, 2020
e2f26a5
Reverts change to transforms.
willtebbutt Apr 24, 2020
f4356e8
Requires Julia 1.3
willtebbutt Apr 24, 2020
9080b83
Removes 1.0 CI
willtebbutt Apr 24, 2020
eb94e93
Reverts rationalquad.jl changes
willtebbutt Apr 24, 2020
f275dd7
Removes custom show
willtebbutt Apr 24, 2020
5b64e68
Adds reference to #96
willtebbutt Apr 24, 2020
5815d80
Moves abstract types around
willtebbutt Apr 24, 2020
ea7bdf1
Adds pairwise for RowVecs / ColVecs
willtebbutt Apr 24, 2020
3369cde
Adds pairwise for AV{<:Real}
willtebbutt Apr 24, 2020
422082b
Refactors transforms
willtebbutt Apr 24, 2020
73aaa7d
Refactors base kernels
willtebbutt Apr 24, 2020
2b26bd3
Refactors composite kernels
willtebbutt Apr 25, 2020
88bdfa7
Refactors kernelmatrix tests
willtebbutt Apr 25, 2020
92ce47e
Move tests around
willtebbutt Apr 25, 2020
2afef07
Resolves merge conflicts
willtebbutt Apr 25, 2020
a82c9aa
Moves KernelProduct tests around
willtebbutt Apr 25, 2020
1ef8a17
Final tweaks
willtebbutt Apr 25, 2020
dff6b5c
More tests
willtebbutt Apr 25, 2020
2254068
Style fix
willtebbutt Apr 25, 2020
54b1a34
Style fix
willtebbutt Apr 25, 2020
ec9a528
Style fix
willtebbutt Apr 25, 2020
3edba23
Style fix
willtebbutt Apr 25, 2020
6df3c28
Style fix
willtebbutt Apr 25, 2020
7dc179b
Fix Identity transform
willtebbutt Apr 25, 2020
20b9e63
Merge branch 'wct/43' of https://github.com/JuliaGaussianProcesses/Ke…
willtebbutt Apr 25, 2020
fe1906d
Fixes import bug
willtebbutt Apr 25, 2020
9f83bdc
eachslice -> eachrow / eachcol
willtebbutt Apr 26, 2020
45b8393
Tweaks slice for AbstractVector{<:Real}
willtebbutt Apr 26, 2020
de87f48
Style
willtebbutt Apr 26, 2020
05cb907
Style
willtebbutt Apr 26, 2020
6cf42d2
Style
willtebbutt Apr 26, 2020
cb4387d
Restore fallbacks
willtebbutt Apr 26, 2020
5781874
Merge branch 'wct/43' of https://github.com/JuliaGaussianProcesses/Ke…
willtebbutt Apr 26, 2020
ba37a15
Tweaks comment
willtebbutt Apr 26, 2020
88798bf
Extra tests
willtebbutt Apr 27, 2020
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 @@ -20,8 +20,8 @@ Requires = "1.0.1"
SpecialFunctions = "0.8, 0.9, 0.10"
StatsBase = "0.32, 0.33"
StatsFuns = "0.8, 0.9"
ZygoteRules = "0.2"
Zygote = "= 0.4.16"
ZygoteRules = "0.2"
julia = "1.3"

[extras]
Expand Down
2 changes: 0 additions & 2 deletions src/KernelFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ using StatsFuns: logtwo
using InteractiveUtils: subtypes
using StatsBase

const defaultobs = 2

"""
Abstract type defining a slice-wise transformation on an input matrix
"""
Expand Down
48 changes: 19 additions & 29 deletions src/basekernels/fbm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,35 @@ const sqroundoff = 1e-15

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

function kernelmatrix(κ::FBMKernel, X::AbstractMatrix; obsdim::Int = defaultobs)
@assert obsdim ∈ [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))"
modX = sum(abs2, X; dims = feature_dim(obsdim))
modXX = pairwise(SqEuclidean(sqroundoff), X, dims = obsdim)
return _fbm.(vec(modX), reshape(modX, 1, :), modXX, κ.h)
_mod(x::AbstractVector{<:Real}) = abs2.(x)
_mod(x::ColVecs) = vec(sum(abs2, x.X; dims=1))
_mod(x::RowVecs) = vec(sum(abs2, x.X; dims=2))

function kernelmatrix(κ::FBMKernel, x::AbstractVector)
modx = _mod(x)
modxx = pairwise(SqEuclidean(sqroundoff), x)
return _fbm.(modx, modx', modxx, κ.h)
end

function kernelmatrix!(K::AbstractMatrix, κ::FBMKernel, X::AbstractMatrix; obsdim::Int = defaultobs)
@assert obsdim ∈ [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))"
modX = sum(abs2, X; dims = feature_dim(obsdim))
modXX = pairwise(SqEuclidean(sqroundoff), X, dims = obsdim)
K .= _fbm.(vec(modX), reshape(modX, 1, :), modXX, κ.h)
function kernelmatrix!(K::AbstractMatrix, κ::FBMKernel, x::AbstractVector)
modx = _mod(x)
modxx = pairwise(SqEuclidean(sqroundoff), x)
K .= _fbm.(modx, modx', modxx, κ.h)
return K
end

function kernelmatrix(
κ::FBMKernel,
X::AbstractMatrix,
Y::AbstractMatrix;
obsdim::Int = defaultobs,
)
@assert obsdim ∈ [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))"
modX = sum(abs2, X, dims = feature_dim(obsdim))
modY = sum(abs2, Y, dims = feature_dim(obsdim))
modXY = pairwise(SqEuclidean(sqroundoff), X, Y,dims = obsdim)
return _fbm.(vec(modX), reshape(modY, 1, :), modXY, κ.h)
function kernelmatrix(κ::FBMKernel, x::AbstractVector, y::AbstractVector)
modxy = pairwise(SqEuclidean(sqroundoff), x, y)
return _fbm.(_mod(x), _mod(y)', modxy, κ.h)
end

function kernelmatrix!(
K::AbstractMatrix,
κ::FBMKernel,
X::AbstractMatrix,
Y::AbstractMatrix;
obsdim::Int = defaultobs,
X::AbstractVector,
Y::AbstractVector,
)
@assert obsdim ∈ [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))"
modX = sum(abs2, X, dims = feature_dim(obsdim))
modY = sum(abs2, Y, dims = feature_dim(obsdim))
modXY = pairwise(SqEuclidean(sqroundoff), X, Y,dims = obsdim)
K .= _fbm.(vec(modX), reshape(modY, 1, :), modXY, κ.h)
modxy = pairwise(SqEuclidean(sqroundoff), X, Y,dims = obsdim)
K .= _fbm.(_mod(x), _mod(y)', modxy, κ.h)
return K
end
13 changes: 13 additions & 0 deletions src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,16 @@ end

# Fallback implementation of evaluate for `SimpleKernel`s.
(k::SimpleKernel)(x, y) = kappa(k, evaluate(metric(k), x, y))

# This is type piracy. We should not doing this.
function Distances.pairwise(d::PreMetric, x::AbstractVector{<:Real})
return pairwise(d, reshape(x, :, 1); dims=1)
end

function Distances.pairwise(
d::PreMetric,
x::AbstractVector{<:Real},
y::AbstractVector{<:Real},
)
return pairwise(d, reshape(x, :, 1), reshape(y, :, 1); dims=1)
end
27 changes: 7 additions & 20 deletions src/kernels/kernelproduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,18 @@ Base.length(k::KernelProduct) = length(k.kernels)

(κ::KernelProduct)(x, y) = prod(k(x, y) for k in κ.kernels)

hadamard(x,y) = x.*y
hadamard(x, y) = x .* y

function kernelmatrix(
κ::KernelProduct,
X::AbstractMatrix;
obsdim::Int=defaultobs,
)
reduce(hadamard, kernelmatrix(κ.kernels[i], X, obsdim = obsdim) for i in 1:length(κ))
function kernelmatrix(κ::KernelProduct, x::AbstractVector)
return reduce(hadamard, kernelmatrix(κ.kernels[i], x) for i in 1:length(κ))
end

function kernelmatrix(
κ::KernelProduct,
X::AbstractMatrix,
Y::AbstractMatrix;
obsdim::Int=defaultobs,
)
reduce(hadamard, kernelmatrix(κ.kernels[i], X, Y, obsdim = obsdim) for i in 1:length(κ))
function kernelmatrix(κ::KernelProduct, x::AbstractVector, y::AbstractVector)
return reduce(hadamard, kernelmatrix(κ.kernels[i], x, y) for i in 1:length(κ))
end

function kerneldiagmatrix(
κ::KernelProduct,
X::AbstractMatrix;
obsdim::Int=defaultobs,
) #TODO Add test
reduce(hadamard, kerneldiagmatrix(κ.kernels[i], X, obsdim = obsdim) for i in 1:length(κ))
function kerneldiagmatrix(κ::KernelProduct, x::AbstractVector)
return reduce(hadamard, kerneldiagmatrix(κ.kernels[i], x) for i in 1:length(κ))
end

function Base.show(io::IO, κ::KernelProduct)
Expand Down
21 changes: 6 additions & 15 deletions src/kernels/kernelsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,16 @@ Base.length(k::KernelSum) = length(k.kernels)

(κ::KernelSum)(x, y) = sum(κ.weights[i] * κ.kernels[i](x, y) for i in 1:length(κ))

function kernelmatrix(κ::KernelSum, X::AbstractMatrix; obsdim::Int = defaultobs)
sum(κ.weights[i] * kernelmatrix(κ.kernels[i], X, obsdim = obsdim) for i in 1:length(κ))
function kernelmatrix(κ::KernelSum, x::AbstractVector)
return sum(κ.weights[i] * kernelmatrix(κ.kernels[i], x) for i in 1:length(κ))
end

function kernelmatrix(
κ::KernelSum,
X::AbstractMatrix,
Y::AbstractMatrix;
obsdim::Int = defaultobs,
)
sum(κ.weights[i] * kernelmatrix(κ.kernels[i], X, Y, obsdim = obsdim) for i in 1:length(κ))
function kernelmatrix(κ::KernelSum, x::AbstractVector, y::AbstractVector)
return sum(κ.weights[i] * kernelmatrix(κ.kernels[i], x, y) for i in 1:length(κ))
end

function kerneldiagmatrix(
κ::KernelSum,
X::AbstractMatrix;
obsdim::Int = defaultobs,
)
sum(κ.weights[i] * kerneldiagmatrix(κ.kernels[i], X, obsdim = obsdim) for i in 1:length(κ))
function kerneldiagmatrix(κ::KernelSum, x::AbstractVector)
return sum(κ.weights[i] * kerneldiagmatrix(κ.kernels[i], x) for i in 1:length(κ))
end

function Base.show(io::IO, κ::KernelSum)
Expand Down
37 changes: 34 additions & 3 deletions src/kernels/scaledkernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,42 @@ function ScaledKernel(kernel::Tk, σ²::Tσ²=1.0) where {Tk<:Kernel,Tσ²<:Real
return ScaledKernel{Tk, Tσ²}(kernel, [σ²])
end

kappa(k::ScaledKernel, x) = first(k.σ²) * kappa(k.kernel, x)

(k::ScaledKernel)(x, y) = first(k.σ²) * k.kernel(x, y)

metric(k::ScaledKernel) = metric(k.kernel)
function kernelmatrix(κ::ScaledKernel, x::AbstractVector, y::AbstractVector)
return κ.σ² .* kernelmatrix(κ.kernel, x, y)
end

function kernelmatrix(κ::ScaledKernel, x::AbstractVector)
return κ.σ² .* kernelmatrix(κ.kernel, x)
end

function kerneldiagmatrix(κ::ScaledKernel, x::AbstractVector)
return κ.σ² .* kerneldiagmatrix(κ.kernel, x)
end

function kernelmatrix!(
K::AbstractMatrix,
κ::ScaledKernel,
x::AbstractVector,
y::AbstractVector,
)
kernelmatrix!(K, κ, x, y)
K .*= κ.σ²
return K
end

function kernelmatrix!(K::AbstractMatrix, κ::ScaledKernel, x::AbstractVector)
kernelmatrix!(K, κ, x)
K .*= κ.σ²
return K
end

function kerneldiagmatrix!(K::AbstractVector, κ::ScaledKernel, x::AbstractVector)
kerneldiagmatrix!(K, κ, x)
K .*= κ.σ²
return K
end

Base.:*(w::Real, k::Kernel) = ScaledKernel(k, w)

Expand Down
Loading