From d5a5a926f549f7f425119e6f6d679311d20b869c Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Mon, 30 Aug 2021 11:26:37 +0100 Subject: [PATCH 01/19] Restore additions --- src/matrix/kernelkroneckermat.jl | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/matrix/kernelkroneckermat.jl b/src/matrix/kernelkroneckermat.jl index 26f7d65a9..a47bc6e07 100644 --- a/src/matrix/kernelkroneckermat.jl +++ b/src/matrix/kernelkroneckermat.jl @@ -25,3 +25,37 @@ end k(x,x') = ∏ᵢᴰ k(xᵢ,x'ᵢ) """ @inline iskroncompatible(κ::Kernel) = false # Default return for kernels + +@inline ismatrixkroncompatible(κ::MOKernel) = false # Default return for kernels +@inline ismatrixkroncompatible(κ::IndependentMOKernel) = true +@inline ismatrixkroncompatible(κ::IntrinsicCoregionMOKernel) = true + +function _mo_kernelmatrix_kron(::MOInputIsotopicByFeatures, K, B) + return Kronecker.kronecker(K, B) +end + +function _mo_kernelmatrix_kron(::MOInputIsotopicByOutputs, K, B) + return Kronecker.kronecker(B, K) +end + +function kernelkronmat( + k::IndependentMOKernel, x::MOI, y::MOI +) where {MOI<:IsotopicMOInputsUnion} + @assert x.out_dim == y.out_dim + Ktmp = kernelmatrix(k.kernel, x.x, y.x) + mtype = eltype(Ktmp) + return _mo_kernelmatrix_kron(Ktmp, Eye{mtype}(x.out_dim), x) +end + +function kernelkronmat( + k::IntrinsicCoregionMOKernel, x::MOI, y::MOI +) where {MOI<:IsotopicMOInputsUnion} + @assert x.out_dim == y.out_dim + Ktmp = kernelmatrix(k.kernel, x.x, y.x) + return _mo_kernelmatrix_kron(Ktmp, k.B, x) +end + +function kernelkronmat(k::MOK, x::MOI) where {MOI<:IsotopicMOInputsUnion,MOK<:MOKernel} + @assert ismatrixkroncompatible(κ) "The chosen kernel is not compatible for Kronecker matrices" + return kernelkronmat(k, x, x) +end \ No newline at end of file From a7ad44eab590d67501a1a8e52f6874b38296ef02 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Mon, 30 Aug 2021 15:33:43 +0100 Subject: [PATCH 02/19] Improvements for lazy kron --- src/matrix/kernelkroneckermat.jl | 55 ++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/src/matrix/kernelkroneckermat.jl b/src/matrix/kernelkroneckermat.jl index a47bc6e07..49c558305 100644 --- a/src/matrix/kernelkroneckermat.jl +++ b/src/matrix/kernelkroneckermat.jl @@ -30,32 +30,47 @@ end @inline ismatrixkroncompatible(κ::IndependentMOKernel) = true @inline ismatrixkroncompatible(κ::IntrinsicCoregionMOKernel) = true -function _mo_kernelmatrix_kron(::MOInputIsotopicByFeatures, K, B) - return Kronecker.kronecker(K, B) +function _kernelmatrix_kroneckerjl_helper(::MOInputIsotopicByFeatures, Kfeatures, Koutputs) + return Kronecker.kronecker(Kfeatures, Koutputs) end -function _mo_kernelmatrix_kron(::MOInputIsotopicByOutputs, K, B) - return Kronecker.kronecker(B, K) +function _kernelmatrix_kroneckerjl_helper(::MOInputIsotopicByOutputs, Kfeatures, Koutputs) + return Kronecker.kronecker(Koutputs, Kfeatures) end -function kernelkronmat( - k::IndependentMOKernel, x::MOI, y::MOI -) where {MOI<:IsotopicMOInputsUnion} - @assert x.out_dim == y.out_dim - Ktmp = kernelmatrix(k.kernel, x.x, y.x) - mtype = eltype(Ktmp) - return _mo_kernelmatrix_kron(Ktmp, Eye{mtype}(x.out_dim), x) +_mo_output_covariance(k::IndependentMOKernel, out_dim) = Eye{Bool}(out_dim) +function _mo_output_covariance(k::IntrinsicCoregionMOKernel, out_dim) + @assert size(k.B) == (out_dim, out_dim) + return k.B end -function kernelkronmat( - k::IntrinsicCoregionMOKernel, x::MOI, y::MOI -) where {MOI<:IsotopicMOInputsUnion} +function kronecker_kernelmatrix( + k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, + x::IsotopicMOInputsUnion, + y::IsotopicMOInputsUnion, +) @assert x.out_dim == y.out_dim - Ktmp = kernelmatrix(k.kernel, x.x, y.x) - return _mo_kernelmatrix_kron(Ktmp, k.B, x) + Kfeatures = kernelmatrix(k.kernel, x.x, y.x) + Koutputs = _mo_output_covariance(k, x.out_dim) + return _kernelmatrix_kroneckerjl_helper(Kfeatures, Koutputs, x) end -function kernelkronmat(k::MOK, x::MOI) where {MOI<:IsotopicMOInputsUnion,MOK<:MOKernel} - @assert ismatrixkroncompatible(κ) "The chosen kernel is not compatible for Kronecker matrices" - return kernelkronmat(k, x, x) -end \ No newline at end of file +function kronecker_kernelmatrix( + k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, x::IsotopicMOInputsUnion +) + Kfeatures = kernelmatrix(k.kernel, x.x) + Koutputs = _mo_output_covariance(k, x.out_dim) + return _kernelmatrix_kroneckerjl_helper(Kfeatures, Koutputs, x) +end + +function kronecker_kernelmatrix( + k::MOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion +) + return throw( + ArgumentError("This kernel does not support a lazy kronecker kernelmatrix.") + ) +end + +function kronecker_kernelmatrix(k::MOKernel, x::IsotopicMOInputsUnion) + return kronecker_kernelmatrix(k, x, x) +end From dde0c08287c0230ad9a3d65eb58bc0a87ee1b01b Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Mon, 30 Aug 2021 15:45:27 +0100 Subject: [PATCH 03/19] Remove unneeded lines --- src/matrix/kernelkroneckermat.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/matrix/kernelkroneckermat.jl b/src/matrix/kernelkroneckermat.jl index 49c558305..b2034a0f0 100644 --- a/src/matrix/kernelkroneckermat.jl +++ b/src/matrix/kernelkroneckermat.jl @@ -26,10 +26,6 @@ end """ @inline iskroncompatible(κ::Kernel) = false # Default return for kernels -@inline ismatrixkroncompatible(κ::MOKernel) = false # Default return for kernels -@inline ismatrixkroncompatible(κ::IndependentMOKernel) = true -@inline ismatrixkroncompatible(κ::IntrinsicCoregionMOKernel) = true - function _kernelmatrix_kroneckerjl_helper(::MOInputIsotopicByFeatures, Kfeatures, Koutputs) return Kronecker.kronecker(Kfeatures, Koutputs) end From 88cb6066b301ad1bd39cb9b9dc6b9c1212c32760 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Sat, 4 Sep 2021 18:30:33 +0100 Subject: [PATCH 04/19] Small experiment with overwriting --- src/matrix/kernelkroneckermat.jl | 52 +++++++++++++++++++++--------- src/mokernels/independent.jl | 4 +-- src/mokernels/intrinsiccoregion.jl | 4 +-- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/matrix/kernelkroneckermat.jl b/src/matrix/kernelkroneckermat.jl index b2034a0f0..ed35dc57f 100644 --- a/src/matrix/kernelkroneckermat.jl +++ b/src/matrix/kernelkroneckermat.jl @@ -40,18 +40,40 @@ function _mo_output_covariance(k::IntrinsicCoregionMOKernel, out_dim) return k.B end -function kronecker_kernelmatrix( - k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, +function kernelmatrix( + k::IndependentMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion, ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, x.out_dim) - return _kernelmatrix_kroneckerjl_helper(Kfeatures, Koutputs, x) + Koutputs = Eye{Bool}(x.out_dim) + return _kernelmatrix_kroneckerjl_helper(x, Kfeatures, Koutputs) end -function kronecker_kernelmatrix( +function kernelmatrix( + k::IntrinsicCoregionMOKernel, + x::IsotopicMOInputsUnion, + y::IsotopicMOInputsUnion, +) + @assert x.out_dim == y.out_dim + Kfeatures = kernelmatrix(k.kernel, x.x, y.x) + Koutputs = k.B + return _kernelmatrix_kroneckerjl_helper(x, Kfeatures, Koutputs) +end + +# function kernelmatrix( +# k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, +# x::IsotopicMOInputsUnion, +# y::IsotopicMOInputsUnion, +# ) +# @assert x.out_dim == y.out_dim +# Kfeatures = kernelmatrix(k.kernel, x.x, y.x) +# Koutputs = _mo_output_covariance(k, x.out_dim) +# return _kernelmatrix_kroneckerjl_helper(Kfeatures, Koutputs, x) +# end + +function kernelmatrix( k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, x::IsotopicMOInputsUnion ) Kfeatures = kernelmatrix(k.kernel, x.x) @@ -59,14 +81,14 @@ function kronecker_kernelmatrix( return _kernelmatrix_kroneckerjl_helper(Kfeatures, Koutputs, x) end -function kronecker_kernelmatrix( - k::MOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion -) - return throw( - ArgumentError("This kernel does not support a lazy kronecker kernelmatrix.") - ) -end +# function kronecker_kernelmatrix( +# k::MOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion +# ) +# return throw( +# ArgumentError("This kernel does not support a lazy kronecker kernelmatrix.") +# ) +# end -function kronecker_kernelmatrix(k::MOKernel, x::IsotopicMOInputsUnion) - return kronecker_kernelmatrix(k, x, x) -end +# function kronecker_kernelmatrix(k::MOKernel, x::IsotopicMOInputsUnion) +# return kronecker_kernelmatrix(k, x, x) +# end diff --git a/src/mokernels/independent.jl b/src/mokernels/independent.jl index d7dde0cba..831b14346 100644 --- a/src/mokernels/independent.jl +++ b/src/mokernels/independent.jl @@ -36,8 +36,8 @@ function _kernelmatrix_kron_helper(::MOInputIsotopicByOutputs, Kfeatures, B) end function kernelmatrix( - k::IndependentMOKernel, x::MOI, y::MOI -) where {MOI<:IsotopicMOInputsUnion} + k::IndependentMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion +) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) mtype = eltype(Kfeatures) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index db1050fe4..6d589f8ef 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -43,8 +43,8 @@ function (k::IntrinsicCoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{ end function kernelmatrix( - k::IntrinsicCoregionMOKernel, x::MOI, y::MOI -) where {MOI<:IsotopicMOInputsUnion} + k::IntrinsicCoregionMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion +) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) return _kernelmatrix_kron_helper(x, Kfeatures, k.B) From af90bf12d372ad0d59c24a6d10d62e5d6fb4c060 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Sat, 4 Sep 2021 23:03:15 +0100 Subject: [PATCH 05/19] Reorder and overwrite --- src/KernelFunctions.jl | 2 +- src/matrix/kernelkroneckermat.jl | 63 +----------------------------- src/mokernels/independent.jl | 17 +------- src/mokernels/intrinsiccoregion.jl | 12 +++++- src/mokernels/mokernel.jl | 8 ++++ 5 files changed, 23 insertions(+), 79 deletions(-) diff --git a/src/KernelFunctions.jl b/src/KernelFunctions.jl index 4fded7dc5..8e0490176 100644 --- a/src/KernelFunctions.jl +++ b/src/KernelFunctions.jl @@ -105,8 +105,8 @@ include(joinpath("kernels", "neuralkernelnetwork.jl")) include(joinpath("approximations", "nystrom.jl")) include("generic.jl") -include(joinpath("mokernels", "mokernel.jl")) include(joinpath("mokernels", "moinput.jl")) +include(joinpath("mokernels", "mokernel.jl")) include(joinpath("mokernels", "independent.jl")) include(joinpath("mokernels", "slfm.jl")) include(joinpath("mokernels", "intrinsiccoregion.jl")) diff --git a/src/matrix/kernelkroneckermat.jl b/src/matrix/kernelkroneckermat.jl index ed35dc57f..70f518b68 100644 --- a/src/matrix/kernelkroneckermat.jl +++ b/src/matrix/kernelkroneckermat.jl @@ -26,69 +26,10 @@ end """ @inline iskroncompatible(κ::Kernel) = false # Default return for kernels -function _kernelmatrix_kroneckerjl_helper(::MOInputIsotopicByFeatures, Kfeatures, Koutputs) +function _kernelmatrix_kron_helper(::MOInputIsotopicByFeatures, Kfeatures, Koutputs) return Kronecker.kronecker(Kfeatures, Koutputs) end -function _kernelmatrix_kroneckerjl_helper(::MOInputIsotopicByOutputs, Kfeatures, Koutputs) +function _kernelmatrix_kron_helper(::MOInputIsotopicByOutputs, Kfeatures, Koutputs) return Kronecker.kronecker(Koutputs, Kfeatures) end - -_mo_output_covariance(k::IndependentMOKernel, out_dim) = Eye{Bool}(out_dim) -function _mo_output_covariance(k::IntrinsicCoregionMOKernel, out_dim) - @assert size(k.B) == (out_dim, out_dim) - return k.B -end - -function kernelmatrix( - k::IndependentMOKernel, - x::IsotopicMOInputsUnion, - y::IsotopicMOInputsUnion, -) - @assert x.out_dim == y.out_dim - Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = Eye{Bool}(x.out_dim) - return _kernelmatrix_kroneckerjl_helper(x, Kfeatures, Koutputs) -end - -function kernelmatrix( - k::IntrinsicCoregionMOKernel, - x::IsotopicMOInputsUnion, - y::IsotopicMOInputsUnion, -) - @assert x.out_dim == y.out_dim - Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = k.B - return _kernelmatrix_kroneckerjl_helper(x, Kfeatures, Koutputs) -end - -# function kernelmatrix( -# k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, -# x::IsotopicMOInputsUnion, -# y::IsotopicMOInputsUnion, -# ) -# @assert x.out_dim == y.out_dim -# Kfeatures = kernelmatrix(k.kernel, x.x, y.x) -# Koutputs = _mo_output_covariance(k, x.out_dim) -# return _kernelmatrix_kroneckerjl_helper(Kfeatures, Koutputs, x) -# end - -function kernelmatrix( - k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, x::IsotopicMOInputsUnion -) - Kfeatures = kernelmatrix(k.kernel, x.x) - Koutputs = _mo_output_covariance(k, x.out_dim) - return _kernelmatrix_kroneckerjl_helper(Kfeatures, Koutputs, x) -end - -# function kronecker_kernelmatrix( -# k::MOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion -# ) -# return throw( -# ArgumentError("This kernel does not support a lazy kronecker kernelmatrix.") -# ) -# end - -# function kronecker_kernelmatrix(k::MOKernel, x::IsotopicMOInputsUnion) -# return kronecker_kernelmatrix(k, x, x) -# end diff --git a/src/mokernels/independent.jl b/src/mokernels/independent.jl index 831b14346..18a0f521e 100644 --- a/src/mokernels/independent.jl +++ b/src/mokernels/independent.jl @@ -27,22 +27,9 @@ function (κ::IndependentMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,I return κ.kernel(x, y) * (px == py) end -function _kernelmatrix_kron_helper(::MOInputIsotopicByFeatures, Kfeatures, B) - return kron(Kfeatures, B) -end - -function _kernelmatrix_kron_helper(::MOInputIsotopicByOutputs, Kfeatures, B) - return kron(B, Kfeatures) -end +_mo_output_covariance(k::IndependentMOKernel, out_dim) = Eye{Bool}(out_dim) -function kernelmatrix( - k::IndependentMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion -) - @assert x.out_dim == y.out_dim - Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - mtype = eltype(Kfeatures) - return _kernelmatrix_kron_helper(x, Kfeatures, Eye{mtype}(x.out_dim)) -end +# for specialized kernelmatrix implementation see intrinsiccoregion.jl if VERSION >= v"1.6" function _kernelmatrix_kron_helper!(K, ::MOInputIsotopicByFeatures, Kfeatures, B) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index 6d589f8ef..e4ea2dc73 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -42,12 +42,20 @@ function (k::IntrinsicCoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{ return k.B[px, py] * k.kernel(x, y) end +function _mo_output_covariance(k::IntrinsicCoregionMOKernel, out_dim) + @assert size(k.B) == (out_dim, out_dim) + return k.B +end + function kernelmatrix( - k::IntrinsicCoregionMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion + k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, + x::IsotopicMOInputsUnion, + y::IsotopicMOInputsUnion, ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - return _kernelmatrix_kron_helper(x, Kfeatures, k.B) + Koutputs = _mo_output_covariance(k, x.out_dim) + return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs) end if VERSION >= v"1.6" diff --git a/src/mokernels/mokernel.jl b/src/mokernels/mokernel.jl index 2ebb07aad..da688cf8d 100644 --- a/src/mokernels/mokernel.jl +++ b/src/mokernels/mokernel.jl @@ -4,3 +4,11 @@ Abstract type for kernels with multiple outpus. """ abstract type MOKernel <: Kernel end + +function _kernelmatrix_kron_helper(::MOInputIsotopicByFeatures, Kfeatures, Koutputs) + return kron(Kfeatures, Koutputs) +end + +function _kernelmatrix_kron_helper(::MOInputIsotopicByOutputs, Kfeatures, Koutputs) + return kron(Koutputs, Kfeatures) +end \ No newline at end of file From c6e9f06527da7e8c8b4d64dd54995630075f64ad Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Sat, 4 Sep 2021 23:11:45 +0100 Subject: [PATCH 06/19] Format and kernelmatrix! --- src/mokernels/independent.jl | 21 --------------------- src/mokernels/intrinsiccoregion.jl | 10 +++++++--- src/mokernels/mokernel.jl | 12 +++++++++++- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/mokernels/independent.jl b/src/mokernels/independent.jl index 18a0f521e..b113f5fa8 100644 --- a/src/mokernels/independent.jl +++ b/src/mokernels/independent.jl @@ -31,27 +31,6 @@ _mo_output_covariance(k::IndependentMOKernel, out_dim) = Eye{Bool}(out_dim) # for specialized kernelmatrix implementation see intrinsiccoregion.jl -if VERSION >= v"1.6" - function _kernelmatrix_kron_helper!(K, ::MOInputIsotopicByFeatures, Kfeatures, B) - return kron!(K, Kfeatures, B) - end - - function _kernelmatrix_kron_helper!(K, ::MOInputIsotopicByOutputs, Kfeatures, B) - return kron!(K, B, Kfeatures) - end - - function kernelmatrix!( - K::AbstractMatrix, k::IndependentMOKernel, x::MOI, y::MOI - ) where {MOI<:IsotopicMOInputsUnion} - @assert x.out_dim == y.out_dim - Ktmp = kernelmatrix(k.kernel, x.x, y.x) - mtype = eltype(Ktmp) - return _kernelmatrix_kron_helper!( - K, x, Ktmp, Matrix{mtype}(I, x.out_dim, x.out_dim) - ) - end -end - function Base.show(io::IO, k::IndependentMOKernel) return print(io, string("Independent Multi-Output Kernel\n\t", string(k.kernel))) end diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index e4ea2dc73..cb3d91b9e 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -60,11 +60,15 @@ end if VERSION >= v"1.6" function kernelmatrix!( - K::AbstractMatrix, k::IntrinsicCoregionMOKernel, x::MOI, y::MOI - ) where {MOI<:IsotopicMOInputsUnion} + K::AbstractMatrix, + k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, + x::IsotopicMOInputsUnion, + y::IsotopicMOInputsUnion, + ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - return _kernelmatrix_kron_helper!(K, x, Kfeatures, k.B) + Koutputs = _mo_output_covariance(k, x.out_dim) + return _kernelmatrix_kron_helper!(K, x, Kfeatures, Koutputs) end end diff --git a/src/mokernels/mokernel.jl b/src/mokernels/mokernel.jl index da688cf8d..acf0b78d5 100644 --- a/src/mokernels/mokernel.jl +++ b/src/mokernels/mokernel.jl @@ -11,4 +11,14 @@ end function _kernelmatrix_kron_helper(::MOInputIsotopicByOutputs, Kfeatures, Koutputs) return kron(Koutputs, Kfeatures) -end \ No newline at end of file +end + +if VERSION >= v"1.6" + function _kernelmatrix_kron_helper!(K, ::MOInputIsotopicByFeatures, Kfeatures, Koutputs) + return kron!(K, Kfeatures, Koutputs) + end + + function _kernelmatrix_kron_helper!(K, ::MOInputIsotopicByOutputs, Kfeatures, Koutputs) + return kron!(K, Koutputs, Kfeatures) + end +end From 62e4b5e2894b9379d0af476d3bac429bd94fd576 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Wed, 15 Sep 2021 18:16:05 +0100 Subject: [PATCH 07/19] Reinstate separate method --- src/matrix/kernelkroneckermat.jl | 35 ++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/matrix/kernelkroneckermat.jl b/src/matrix/kernelkroneckermat.jl index 70f518b68..684391913 100644 --- a/src/matrix/kernelkroneckermat.jl +++ b/src/matrix/kernelkroneckermat.jl @@ -26,10 +26,41 @@ end """ @inline iskroncompatible(κ::Kernel) = false # Default return for kernels -function _kernelmatrix_kron_helper(::MOInputIsotopicByFeatures, Kfeatures, Koutputs) +function _kernelmatrix_kroneckerjl_helper(::MOInputIsotopicByFeatures, Kfeatures, Koutputs) return Kronecker.kronecker(Kfeatures, Koutputs) end -function _kernelmatrix_kron_helper(::MOInputIsotopicByOutputs, Kfeatures, Koutputs) +function _kernelmatrix_kroneckerjl_helper(::MOInputIsotopicByOutputs, Kfeatures, Koutputs) return Kronecker.kronecker(Koutputs, Kfeatures) end + +function kronecker_kernelmatrix( + k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, + x::IsotopicMOInputsUnion, + y::IsotopicMOInputsUnion, +) + @assert x.out_dim == y.out_dim + Kfeatures = kernelmatrix(k.kernel, x.x, y.x) + Koutputs = _mo_output_covariance(k, x.out_dim) + return _kernelmatrix_kroneckerjl_helper(Kfeatures, Koutputs, x) +end + +function kronecker_kernelmatrix( + k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, x::IsotopicMOInputsUnion +) + Kfeatures = kernelmatrix(k.kernel, x.x) + Koutputs = _mo_output_covariance(k, x.out_dim) + return _kernelmatrix_kroneckerjl_helper(Kfeatures, Koutputs, x) +end + +function kronecker_kernelmatrix( + k::MOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion +) + return throw( + ArgumentError("This kernel does not support a lazy kronecker kernelmatrix.") + ) +end + +function kronecker_kernelmatrix(k::MOKernel, x::IsotopicMOInputsUnion) + return kronecker_kernelmatrix(k, x, x) +end From d9287d7cdfeeb705f703ca901c7a1344c37dd422 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Wed, 15 Sep 2021 19:25:41 +0100 Subject: [PATCH 08/19] Adding tests --- src/matrix/kernelkroneckermat.jl | 5 ++-- test/matrix/kernelkroneckermat.jl | 46 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/matrix/kernelkroneckermat.jl b/src/matrix/kernelkroneckermat.jl index 684391913..1a25c7618 100644 --- a/src/matrix/kernelkroneckermat.jl +++ b/src/matrix/kernelkroneckermat.jl @@ -4,6 +4,7 @@ using .Kronecker: Kronecker export kernelkronmat +export kronecker_kernelmatrix function kernelkronmat(κ::Kernel, X::AbstractVector, dims::Int) @assert iskroncompatible(κ) "The chosen kernel is not compatible for kroenecker matrices (see [`iskroncompatible`](@ref))" @@ -42,7 +43,7 @@ function kronecker_kernelmatrix( @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) Koutputs = _mo_output_covariance(k, x.out_dim) - return _kernelmatrix_kroneckerjl_helper(Kfeatures, Koutputs, x) + return _kernelmatrix_kroneckerjl_helper(x, Kfeatures, Koutputs) end function kronecker_kernelmatrix( @@ -50,7 +51,7 @@ function kronecker_kernelmatrix( ) Kfeatures = kernelmatrix(k.kernel, x.x) Koutputs = _mo_output_covariance(k, x.out_dim) - return _kernelmatrix_kroneckerjl_helper(Kfeatures, Koutputs, x) + return _kernelmatrix_kroneckerjl_helper(x, Kfeatures, Koutputs) end function kronecker_kernelmatrix( diff --git a/test/matrix/kernelkroneckermat.jl b/test/matrix/kernelkroneckermat.jl index 8cb12ac80..9207e10cd 100644 --- a/test/matrix/kernelkroneckermat.jl +++ b/test/matrix/kernelkroneckermat.jl @@ -7,4 +7,50 @@ @test all(collect(kernelkronmat(k, collect(x), 2)) .≈ kernelmatrix(k, X; obsdim=1)) @test all(collect(kernelkronmat(k, [x, x])) .≈ kernelmatrix(k, X; obsdim=1)) @test_throws AssertionError kernelkronmat(LinearKernel(), collect(x), 2) + + @testset "lazy kernelmatrix" begin + rng = MersenneTwister(123) + + dims = (in=3, out=2, obs=3) + r = 1 + + A = randn(dims.out, r) + B = A * transpose(A) + Diagonal(rand(dims.out)) + + # XIF = [(rand(dims.in), rand(1:(dims.out))) for i in 1:(dims.obs)] + x = [rand(dims.in) for _ in 1:2] + XIF = KernelFunctions.MOInputIsotopicByFeatures(x, dims.out) + XIO = KernelFunctions.MOInputIsotopicByOutputs(x, dims.out) + y = [rand(dims.in) for _ in 1:2] + YIF = KernelFunctions.MOInputIsotopicByFeatures(y, dims.out) + YIO = KernelFunctions.MOInputIsotopicByOutputs(y, dims.out) + + skernel = GaussianKernel() + kIndMO = IndependentMOKernel(skernel) + + A = randn(dims.out, r) + B = A * transpose(A) + Diagonal(rand(dims.out)) + icoregionkernel = IntrinsicCoregionMOKernel(skernel, B) + + function test_kronecker_kernelmatrix(k, x) + res = kronecker_kernelmatrix(k, x) + @test typeof(res) <: Kronecker.KroneckerProduct + @test res == kernelmatrix(k, x) + end + function test_kronecker_kernelmatrix(k, x, y) + res = kronecker_kernelmatrix(k, x, y) + @test typeof(res) <: Kronecker.KroneckerProduct + @test res == kernelmatrix(k, x, y) + end + + for k in [kIndMO, icoregionkernel], x in [XIF, XIO] + test_kronecker_kernelmatrix(k, x) + end + for k in [kIndMO, icoregionkernel], (x, y) in ([XIF, YIF], [XIO, YIO]) + test_kronecker_kernelmatrix(k, x, y) + end + + struct TestMOKernel <: MOKernel end + @test_throws ArgumentError kronecker_kernelmatrix(TestMOKernel(), XIF) + end end From 7f239216581ba025a8a9a1dc3458792505795397 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Wed, 22 Sep 2021 13:15:31 +0100 Subject: [PATCH 09/19] Duplicate code for readability --- src/mokernels/independent.jl | 11 +++++++++++ src/mokernels/intrinsiccoregion.jl | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/mokernels/independent.jl b/src/mokernels/independent.jl index b113f5fa8..08f7b792f 100644 --- a/src/mokernels/independent.jl +++ b/src/mokernels/independent.jl @@ -29,6 +29,17 @@ end _mo_output_covariance(k::IndependentMOKernel, out_dim) = Eye{Bool}(out_dim) +function kernelmatrix( + k::IndependentMOKernel, + x::IsotopicMOInputsUnion, + y::IsotopicMOInputsUnion, +) + @assert x.out_dim == y.out_dim + Kfeatures = kernelmatrix(k.kernel, x.x, y.x) + Koutputs = _mo_output_covariance(k, x.out_dim) + return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs) +end + # for specialized kernelmatrix implementation see intrinsiccoregion.jl function Base.show(io::IO, k::IndependentMOKernel) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index cb3d91b9e..1bca191e8 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -48,7 +48,7 @@ function _mo_output_covariance(k::IntrinsicCoregionMOKernel, out_dim) end function kernelmatrix( - k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, + k::IntrinsicCoregionMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion, ) From ceab3cfe64c3cf7bc75b3ca62ef274921ebd73e8 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Wed, 22 Sep 2021 13:20:49 +0100 Subject: [PATCH 10/19] Format --- src/mokernels/independent.jl | 4 +--- src/mokernels/intrinsiccoregion.jl | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/mokernels/independent.jl b/src/mokernels/independent.jl index 08f7b792f..67390bcb9 100644 --- a/src/mokernels/independent.jl +++ b/src/mokernels/independent.jl @@ -30,9 +30,7 @@ end _mo_output_covariance(k::IndependentMOKernel, out_dim) = Eye{Bool}(out_dim) function kernelmatrix( - k::IndependentMOKernel, - x::IsotopicMOInputsUnion, - y::IsotopicMOInputsUnion, + k::IndependentMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index 1bca191e8..ccc3ed7ab 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -48,9 +48,7 @@ function _mo_output_covariance(k::IntrinsicCoregionMOKernel, out_dim) end function kernelmatrix( - k::IntrinsicCoregionMOKernel, - x::IsotopicMOInputsUnion, - y::IsotopicMOInputsUnion, + k::IntrinsicCoregionMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) From 8ed6be33db635eedb6b892ebd1e0b5d6c9c82a31 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Wed, 22 Sep 2021 13:23:38 +0100 Subject: [PATCH 11/19] Remove comment and patch bump --- Project.toml | 2 +- src/mokernels/independent.jl | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 3c0045cb4..5ded7e0da 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "KernelFunctions" uuid = "ec8451be-7e33-11e9-00cf-bbf324bd1392" -version = "0.10.17" +version = "0.10.18" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" diff --git a/src/mokernels/independent.jl b/src/mokernels/independent.jl index 67390bcb9..1b1cfbfc7 100644 --- a/src/mokernels/independent.jl +++ b/src/mokernels/independent.jl @@ -38,8 +38,6 @@ function kernelmatrix( return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs) end -# for specialized kernelmatrix implementation see intrinsiccoregion.jl - function Base.show(io::IO, k::IndependentMOKernel) return print(io, string("Independent Multi-Output Kernel\n\t", string(k.kernel))) end From f46bd61130165b0b0b47ca0fb8aea277ddc20d55 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Wed, 22 Sep 2021 13:36:46 +0100 Subject: [PATCH 12/19] Change kernelmatrix! --- src/mokernels/independent.jl | 14 ++++++++++++++ src/mokernels/intrinsiccoregion.jl | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/mokernels/independent.jl b/src/mokernels/independent.jl index 1b1cfbfc7..ec830d395 100644 --- a/src/mokernels/independent.jl +++ b/src/mokernels/independent.jl @@ -38,6 +38,20 @@ function kernelmatrix( return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs) end +if VERSION >= v"1.6" + function kernelmatrix!( + K::AbstractMatrix, + k::IndependentMOKernel, + x::IsotopicMOInputsUnion, + y::IsotopicMOInputsUnion, + ) + @assert x.out_dim == y.out_dim + Kfeatures = kernelmatrix(k.kernel, x.x, y.x) + Koutputs = _mo_output_covariance(k, x.out_dim) + return _kernelmatrix_kron_helper!(K, x, Kfeatures, Koutputs) + end +end + function Base.show(io::IO, k::IndependentMOKernel) return print(io, string("Independent Multi-Output Kernel\n\t", string(k.kernel))) end diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index ccc3ed7ab..7e5b884cd 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -59,7 +59,7 @@ end if VERSION >= v"1.6" function kernelmatrix!( K::AbstractMatrix, - k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, + k::IntrinsicCoregionMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion, ) From 9540fce217b956b42384a7a5e6ce48cd6bec9f48 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Wed, 22 Sep 2021 14:13:19 +0100 Subject: [PATCH 13/19] Change to output covariance type --- src/mokernels/independent.jl | 8 +++++--- src/mokernels/intrinsiccoregion.jl | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/mokernels/independent.jl b/src/mokernels/independent.jl index ec830d395..da2604dc0 100644 --- a/src/mokernels/independent.jl +++ b/src/mokernels/independent.jl @@ -27,14 +27,16 @@ function (κ::IndependentMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,I return κ.kernel(x, y) * (px == py) end -_mo_output_covariance(k::IndependentMOKernel, out_dim) = Eye{Bool}(out_dim) +function _mo_output_covariance(k::IndependentMOKernel, Kfeatures, out_dim) + return Eye{eltype(Kfeatures)}(out_dim) +end function kernelmatrix( k::IndependentMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, x.out_dim) + Koutputs = _mo_output_covariance(k, Kfeatures, x.out_dim) return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs) end @@ -47,7 +49,7 @@ if VERSION >= v"1.6" ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, x.out_dim) + Koutputs = _mo_output_covariance(k, Kfeatures, x.out_dim) return _kernelmatrix_kron_helper!(K, x, Kfeatures, Koutputs) end end diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index 7e5b884cd..99b17ddd7 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -42,7 +42,7 @@ function (k::IntrinsicCoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{ return k.B[px, py] * k.kernel(x, y) end -function _mo_output_covariance(k::IntrinsicCoregionMOKernel, out_dim) +function _mo_output_covariance(k::IntrinsicCoregionMOKernel, Kfeatures, out_dim) @assert size(k.B) == (out_dim, out_dim) return k.B end @@ -52,7 +52,7 @@ function kernelmatrix( ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, x.out_dim) + Koutputs = _mo_output_covariance(k, Kfeatures, x.out_dim) return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs) end @@ -65,7 +65,7 @@ if VERSION >= v"1.6" ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, x.out_dim) + Koutputs = _mo_output_covariance(k, Kfeatures, x.out_dim) return _kernelmatrix_kron_helper!(K, x, Kfeatures, Koutputs) end end From 09cd20e2d461d67d52766cb01ce59be248839660 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Wed, 22 Sep 2021 20:28:07 +0100 Subject: [PATCH 14/19] Change to output covariance type - revert --- src/mokernels/independent.jl | 8 +++++--- src/mokernels/intrinsiccoregion.jl | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/mokernels/independent.jl b/src/mokernels/independent.jl index ec830d395..da2604dc0 100644 --- a/src/mokernels/independent.jl +++ b/src/mokernels/independent.jl @@ -27,14 +27,16 @@ function (κ::IndependentMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,I return κ.kernel(x, y) * (px == py) end -_mo_output_covariance(k::IndependentMOKernel, out_dim) = Eye{Bool}(out_dim) +function _mo_output_covariance(k::IndependentMOKernel, Kfeatures, out_dim) + return Eye{eltype(Kfeatures)}(out_dim) +end function kernelmatrix( k::IndependentMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, x.out_dim) + Koutputs = _mo_output_covariance(k, Kfeatures, x.out_dim) return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs) end @@ -47,7 +49,7 @@ if VERSION >= v"1.6" ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, x.out_dim) + Koutputs = _mo_output_covariance(k, Kfeatures, x.out_dim) return _kernelmatrix_kron_helper!(K, x, Kfeatures, Koutputs) end end diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index 7e5b884cd..99b17ddd7 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -42,7 +42,7 @@ function (k::IntrinsicCoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{ return k.B[px, py] * k.kernel(x, y) end -function _mo_output_covariance(k::IntrinsicCoregionMOKernel, out_dim) +function _mo_output_covariance(k::IntrinsicCoregionMOKernel, Kfeatures, out_dim) @assert size(k.B) == (out_dim, out_dim) return k.B end @@ -52,7 +52,7 @@ function kernelmatrix( ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, x.out_dim) + Koutputs = _mo_output_covariance(k, Kfeatures, x.out_dim) return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs) end @@ -65,7 +65,7 @@ if VERSION >= v"1.6" ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, x.out_dim) + Koutputs = _mo_output_covariance(k, Kfeatures, x.out_dim) return _kernelmatrix_kron_helper!(K, x, Kfeatures, Koutputs) end end From 204d99c7fe165c440c51ff24611994a4fb900232 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Wed, 22 Sep 2021 22:28:33 +0100 Subject: [PATCH 15/19] Revert "Change to output covariance type - revert" This reverts commit 09cd20e2d461d67d52766cb01ce59be248839660. --- src/mokernels/independent.jl | 8 +++----- src/mokernels/intrinsiccoregion.jl | 6 +++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/mokernels/independent.jl b/src/mokernels/independent.jl index da2604dc0..ec830d395 100644 --- a/src/mokernels/independent.jl +++ b/src/mokernels/independent.jl @@ -27,16 +27,14 @@ function (κ::IndependentMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,I return κ.kernel(x, y) * (px == py) end -function _mo_output_covariance(k::IndependentMOKernel, Kfeatures, out_dim) - return Eye{eltype(Kfeatures)}(out_dim) -end +_mo_output_covariance(k::IndependentMOKernel, out_dim) = Eye{Bool}(out_dim) function kernelmatrix( k::IndependentMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, Kfeatures, x.out_dim) + Koutputs = _mo_output_covariance(k, x.out_dim) return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs) end @@ -49,7 +47,7 @@ if VERSION >= v"1.6" ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, Kfeatures, x.out_dim) + Koutputs = _mo_output_covariance(k, x.out_dim) return _kernelmatrix_kron_helper!(K, x, Kfeatures, Koutputs) end end diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index 99b17ddd7..7e5b884cd 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -42,7 +42,7 @@ function (k::IntrinsicCoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{ return k.B[px, py] * k.kernel(x, y) end -function _mo_output_covariance(k::IntrinsicCoregionMOKernel, Kfeatures, out_dim) +function _mo_output_covariance(k::IntrinsicCoregionMOKernel, out_dim) @assert size(k.B) == (out_dim, out_dim) return k.B end @@ -52,7 +52,7 @@ function kernelmatrix( ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, Kfeatures, x.out_dim) + Koutputs = _mo_output_covariance(k, x.out_dim) return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs) end @@ -65,7 +65,7 @@ if VERSION >= v"1.6" ) @assert x.out_dim == y.out_dim Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, Kfeatures, x.out_dim) + Koutputs = _mo_output_covariance(k, x.out_dim) return _kernelmatrix_kron_helper!(K, x, Kfeatures, Koutputs) end end From 8b5757a0938389ac82e20d0ac8f91c2648a313f7 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Wed, 22 Sep 2021 22:29:50 +0100 Subject: [PATCH 16/19] Revert "Change kernelmatrix!" This reverts commit f46bd61130165b0b0b47ca0fb8aea277ddc20d55. --- src/mokernels/independent.jl | 14 -------------- src/mokernels/intrinsiccoregion.jl | 2 +- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/src/mokernels/independent.jl b/src/mokernels/independent.jl index ec830d395..1b1cfbfc7 100644 --- a/src/mokernels/independent.jl +++ b/src/mokernels/independent.jl @@ -38,20 +38,6 @@ function kernelmatrix( return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs) end -if VERSION >= v"1.6" - function kernelmatrix!( - K::AbstractMatrix, - k::IndependentMOKernel, - x::IsotopicMOInputsUnion, - y::IsotopicMOInputsUnion, - ) - @assert x.out_dim == y.out_dim - Kfeatures = kernelmatrix(k.kernel, x.x, y.x) - Koutputs = _mo_output_covariance(k, x.out_dim) - return _kernelmatrix_kron_helper!(K, x, Kfeatures, Koutputs) - end -end - function Base.show(io::IO, k::IndependentMOKernel) return print(io, string("Independent Multi-Output Kernel\n\t", string(k.kernel))) end diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index 7e5b884cd..ccc3ed7ab 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -59,7 +59,7 @@ end if VERSION >= v"1.6" function kernelmatrix!( K::AbstractMatrix, - k::IntrinsicCoregionMOKernel, + k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion, ) From dfee74f7def7c694101292b64bb74a4ad0ab3eaf Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Wed, 22 Sep 2021 22:32:05 +0100 Subject: [PATCH 17/19] Add kernelmatrix! changes again --- src/mokernels/independent.jl | 14 ++++++++++++++ src/mokernels/intrinsiccoregion.jl | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/mokernels/independent.jl b/src/mokernels/independent.jl index 1b1cfbfc7..ec830d395 100644 --- a/src/mokernels/independent.jl +++ b/src/mokernels/independent.jl @@ -38,6 +38,20 @@ function kernelmatrix( return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs) end +if VERSION >= v"1.6" + function kernelmatrix!( + K::AbstractMatrix, + k::IndependentMOKernel, + x::IsotopicMOInputsUnion, + y::IsotopicMOInputsUnion, + ) + @assert x.out_dim == y.out_dim + Kfeatures = kernelmatrix(k.kernel, x.x, y.x) + Koutputs = _mo_output_covariance(k, x.out_dim) + return _kernelmatrix_kron_helper!(K, x, Kfeatures, Koutputs) + end +end + function Base.show(io::IO, k::IndependentMOKernel) return print(io, string("Independent Multi-Output Kernel\n\t", string(k.kernel))) end diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index ccc3ed7ab..7e5b884cd 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -59,7 +59,7 @@ end if VERSION >= v"1.6" function kernelmatrix!( K::AbstractMatrix, - k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, + k::IntrinsicCoregionMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion, ) From 24f6b4935bded3053da9b63f33638107a3b3dc16 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Wed, 22 Sep 2021 22:35:32 +0100 Subject: [PATCH 18/19] Change input types for pairwise pullback --- src/chainrules.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/chainrules.jl b/src/chainrules.jl index 55de7ddf9..3a0f100be 100644 --- a/src/chainrules.jl +++ b/src/chainrules.jl @@ -26,7 +26,7 @@ function ChainRulesCore.rrule( ::typeof(Distances.pairwise), d::Delta, X::AbstractMatrix, Y::AbstractMatrix; dims=2 ) P = Distances.pairwise(d, X, Y; dims=dims) - function pairwise_pullback(::AbstractMatrix) + function pairwise_pullback(::Any) return NoTangent(), NoTangent(), ZeroTangent(), ZeroTangent() end return P, pairwise_pullback @@ -36,7 +36,7 @@ function ChainRulesCore.rrule( ::typeof(Distances.pairwise), d::Delta, X::AbstractMatrix; dims=2 ) P = Distances.pairwise(d, X; dims=dims) - function pairwise_pullback(::AbstractMatrix) + function pairwise_pullback(::Any) return NoTangent(), NoTangent(), ZeroTangent() end return P, pairwise_pullback From a00ba6da9097a5167aac00467a48a312db6fa6c3 Mon Sep 17 00:00:00 2001 From: Steffen Ridderbusch Date: Thu, 23 Sep 2021 12:10:43 +0100 Subject: [PATCH 19/19] Missing changes to Any --- src/chainrules.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/chainrules.jl b/src/chainrules.jl index 3a0f100be..4b69a827f 100644 --- a/src/chainrules.jl +++ b/src/chainrules.jl @@ -46,7 +46,7 @@ function ChainRulesCore.rrule( ::typeof(Distances.colwise), d::Delta, X::AbstractMatrix, Y::AbstractMatrix ) C = Distances.colwise(d, X, Y) - function colwise_pullback(::AbstractVector) + function colwise_pullback(::Any) return NoTangent(), NoTangent(), ZeroTangent(), ZeroTangent() end return C, colwise_pullback @@ -70,7 +70,7 @@ function ChainRulesCore.rrule( dims=2, ) P = Distances.pairwise(d, X, Y; dims=dims) - function pairwise_pullback_cols(Δ::AbstractMatrix) + function pairwise_pullback_cols(Δ::Any) if dims == 1 return NoTangent(), NoTangent(), Δ * Y, Δ' * X else @@ -84,7 +84,7 @@ function ChainRulesCore.rrule( ::typeof(Distances.pairwise), d::DotProduct, X::AbstractMatrix; dims=2 ) P = Distances.pairwise(d, X; dims=dims) - function pairwise_pullback_cols(Δ::AbstractMatrix) + function pairwise_pullback_cols(Δ::Any) if dims == 1 return NoTangent(), NoTangent(), 2 * Δ * X else @@ -98,7 +98,7 @@ function ChainRulesCore.rrule( ::typeof(Distances.colwise), d::DotProduct, X::AbstractMatrix, Y::AbstractMatrix ) C = Distances.colwise(d, X, Y) - function colwise_pullback(Δ::AbstractVector) + function colwise_pullback(Δ::Any) return NoTangent(), NoTangent(), Δ' .* Y, Δ' .* X end return C, colwise_pullback