Skip to content

Tensor product kernel #56

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

Closed
Closed
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
7 changes: 5 additions & 2 deletions src/KernelFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export RationalQuadraticKernel, GammaRationalQuadraticKernel
export MahalanobisKernel
export KernelSum, KernelProduct
export TransformedKernel, ScaledKernel
export TensorProductKernel

export Transform, SelectTransform, ChainTransform, ScaleTransform, LowRankTransform, IdentityTransform, FunctionTransform

Expand Down Expand Up @@ -46,14 +47,16 @@ include("distances/dotproduct.jl")
include("distances/delta.jl")
include("transform/transform.jl")

for k in ["exponential","matern","polynomial","constant","rationalquad","exponentiated","cosine","maha","fbm"]
include(joinpath("kernels",k*".jl"))
for f in readdir(joinpath(@__DIR__, "basekernels"))
endswith(f, ".jl") && include(joinpath("basekernels", f))
end

include("kernels/transformedkernel.jl")
include("kernels/scaledkernel.jl")
include("matrix/kernelmatrix.jl")
include("kernels/kernelsum.jl")
include("kernels/kernelproduct.jl")
include("kernels/tensorproduct.jl")
include("approximations/nystrom.jl")
include("generic.jl")

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Base.show(io::IO,κ::Kernel) = print(io,nameof(typeof(κ)))

### Syntactic sugar for creating matrices and using kernel functions
for k in subtypes(BaseKernel)
if k ∈ [FBMKernel] continue end #for kernels without `metric` or `kappa`
k === FBMKernel && continue #for kernels without `metric` or `kappa`
@eval begin
@inline (κ::$k)(d::Real) = kappa(κ,d) #TODO Add test
@inline (κ::$k)(x::AbstractVector{<:Real}, y::AbstractVector{<:Real}) = kappa(κ, x, y)
Expand Down
27 changes: 27 additions & 0 deletions src/kernels/tensorproduct.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
TensorProductKernel(kernel1, kernel2)

A tensor product kernel of the form
```math
k((x₁, x₂), (y₁, y₂)) = kernel1(x₁, y₁) * kernel2(x₂, y₂)
```
"""
struct TensorProductKernel{K1<:Kernel,K2<:Kernel} <: Kernel
kernel1::K1
kernel2::K2
end

kappa(kernel::TensorProductKernel, (x1, x2), (y1, y2)) =
kappa(kernel.kernel1, x1, y1) * kappa(kernel.kernel2, x2, y2)

(kernel::TensorProductKernel)(x, y) = kappa(kernel, x, y)

Base.show(io::IO, κ::TensorProductKernel) = printshifted(io, κ, 0)

function printshifted(io::IO, κ::TensorProductKernel, shift::Int)
print(io,"Tensor product kernel:")
print(io,"\n"*("\t"^(shift+1))*"- ")
printshifted(io, κ.kernel1, shift+2)
print(io,"\n"*("\t"^(shift+1))*"- ")
printshifted(io, κ.kernel2, shift+2)
end
10 changes: 10 additions & 0 deletions test/test_kernels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,14 @@ x = rand()*2; v1 = rand(3); v2 = rand(3); id = IdentityTransform()
@test kappa(k*k3,v1,v2) ≈ kappa(k3*k,v1,v2)
end
end
@testset "Tensor product" begin
k1 = SqExponentialKernel()
k2 = ExponentialKernel()
kernel = TensorProductKernel(k1, k2)

u1, u2 = rand(10), rand(10)

@test kernel((v1, u1), (v2, u2)) == kappa(kernel, (v1, u1), (v2, u2))
@test kernel((v1, u1), (v2, u2)) == kappa(kernel.kernel1, v1, v2) * kappa(kernel.kernel2, u1, u2)
end
end