Skip to content

Stable version of KernelProduct and added test_type_stability #486

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 15 commits into from
Nov 1, 2022
Merged
5 changes: 4 additions & 1 deletion src/kernels/kernelproduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ Base.length(k::KernelProduct) = length(k.kernels)

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

_hadamard(f, ks::Tuple, x) = f(first(ks), x) .* _hadamard(f, Base.tail(ks), x)
_hadamard(f, ks::Tuple{Tx}, x) where {Tx} = f(only(ks), x)

function kernelmatrix(κ::KernelProduct, x::AbstractVector)
return reduce(hadamard, kernelmatrix(k, x) for k in κ.kernels)
return _hadamard(kernelmatrix, κ.kernels, x)
end

function kernelmatrix(κ::KernelProduct, x::AbstractVector, y::AbstractVector)
Expand Down
4 changes: 4 additions & 0 deletions test/kernels/kernelproduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@
KernelProduct(SqExponentialKernel(), LinearKernel(; c=c))
end
test_params(k1 * k2, (k1, k2))

nested_k = RBFKernel() * ((LinearKernel() + CosineKernel() * RBFKernel()) ∘ SelectTransform(1))
x = RowVecs(rand(10, 2))
@test (@inferred kernelmatrix(nested_k, x)) isa Matrix{Float64}
end