From cdfae04ed7f7e4fcaa9c9f3eea4973d0724e0da9 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 20 Mar 2021 15:20:29 +0000 Subject: [PATCH 01/62] Adds first version of Intrinsic Coregionalization Multi-Output Kernel. --- src/KernelFunctions.jl | 3 ++- src/mokernels/icm.jl | 45 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/mokernels/icm.jl diff --git a/src/KernelFunctions.jl b/src/KernelFunctions.jl index 7a6ec8a6c..6229523de 100644 --- a/src/KernelFunctions.jl +++ b/src/KernelFunctions.jl @@ -49,7 +49,7 @@ export spectral_mixture_kernel, spectral_mixture_product_kernel export ColVecs, RowVecs export MOInput -export IndependentMOKernel, LatentFactorMOKernel +export IndependentMOKernel, LatentFactorMOKernel, IntrinsicCoregionalizationMOKernel # Reexports export tensor, ⊗ @@ -111,6 +111,7 @@ include(joinpath("mokernels", "mokernel.jl")) include(joinpath("mokernels", "moinput.jl")) include(joinpath("mokernels", "independent.jl")) include(joinpath("mokernels", "slfm.jl")) +include(joinpath("mokernels", "icm.jl")) include("zygote_adjoints.jl") diff --git a/src/mokernels/icm.jl b/src/mokernels/icm.jl new file mode 100644 index 000000000..c7087c3eb --- /dev/null +++ b/src/mokernels/icm.jl @@ -0,0 +1,45 @@ +struct IntrinsicCoregionalizationMOKernel{Tu<:Kernel, TA<:AbstractMatrix, Tv<:LinearAlgebra.Diagonal, TB<:AbstractMatrix, Trank<:Integer, Tnoutputs<:Integer} <: MOKernel + u::Tu + A::TA + v::Tv + B::TB + rank::Trank + noutputs::Tnoutputs + + function IntrinsicCoregionalizationMOKernel(u, noutputs, rank) + noutputs >= rank || error("`noutputs` should be greater or equal than `rank`") + + A = rand(noutputs, rank) + v = Diagonal(rand(noutputs)) + B = A * transpose(A) + v + + return new{typeof(u), typeof(A), typeof(v), typeof(B), typeof(rank), typeof(noutputs)}(u,A,v,B,rank,noutputs) + end + + function IntrinsicCoregionalizationMOKernel(u, A, v::AbstractVector) + rank = size(A,2) + noutputs = size(A,1) + v = Diagonal(v) + B = A * transpose(A) + v + return new{typeof(u), typeof(A), typeof(v), typeof(B), typeof(rank), typeof(noutputs)}(u,A,v,B,rank,noutputs) + end +end + + + +function (κ::IntrinsicCoregionalizationMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,Int}) + return κ.B[px,py] * κ.u(x,y) +end + + +# function kernelmatrix(κ::IntrinsicCoregionalizationMOKernel, +# x::AbstractVector{Tuple{Any,Int}}, +# y::AbstractVector{Tuple{Any,Int}}) + +# return κ.(x, permutedims(y)) +# end + + +function Base.show(io::IO, κ::IntrinsicCoregionalizationMOKernel) + return print(io, "Intrinsic Coregionalization Multi-Output Kernel") +end From 5e4efc383d63cd34103783dbbdf21c3349ef9ff8 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 21 Mar 2021 16:57:10 +0000 Subject: [PATCH 02/62] Changes the name of the kernel. Removes "implementation code" for the parameterization of the coregionalization matrix. Removes white space. Renames file. Updates names in module file. Corrects check_args. Corrects using incorrect field name in coregion covariance function. Cleans white space. --- src/KernelFunctions.jl | 4 ++-- src/mokernels/coregion.jl | 21 ++++++++++++++++++ src/mokernels/icm.jl | 45 --------------------------------------- 3 files changed, 23 insertions(+), 47 deletions(-) create mode 100644 src/mokernels/coregion.jl delete mode 100644 src/mokernels/icm.jl diff --git a/src/KernelFunctions.jl b/src/KernelFunctions.jl index 6229523de..a53bf6acf 100644 --- a/src/KernelFunctions.jl +++ b/src/KernelFunctions.jl @@ -49,7 +49,7 @@ export spectral_mixture_kernel, spectral_mixture_product_kernel export ColVecs, RowVecs export MOInput -export IndependentMOKernel, LatentFactorMOKernel, IntrinsicCoregionalizationMOKernel +export IndependentMOKernel, LatentFactorMOKernel, CoregionMOKernel # Reexports export tensor, ⊗ @@ -111,7 +111,7 @@ include(joinpath("mokernels", "mokernel.jl")) include(joinpath("mokernels", "moinput.jl")) include(joinpath("mokernels", "independent.jl")) include(joinpath("mokernels", "slfm.jl")) -include(joinpath("mokernels", "icm.jl")) +include(joinpath("mokernels", "coregion.jl")) include("zygote_adjoints.jl") diff --git a/src/mokernels/coregion.jl b/src/mokernels/coregion.jl new file mode 100644 index 000000000..cdec4c001 --- /dev/null +++ b/src/mokernels/coregion.jl @@ -0,0 +1,21 @@ +struct CoregionMOKernel{K<:Kernel,T<:AbstractMatrix} <: MOKernel + kernel::K + B::T + + function CoregionMOKernel{K,T}(kernel::K, B::T) where {K,T} + @check_args(CoregionMOKernel, B, (eigmin(B) >= 0), "B is Positive semi-definite") + return new{K,T}(kernel, B) + end +end + +function CoregionMOKernel(kernel::Kernel, B::AbstractMatrix) + return CoregionMOKernel{typeof(kernel),typeof(B)}(kernel, B) +end + +function (k::CoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,Int}) + return k.B[px,py] * k.kernel(x,y) +end + +function Base.show(io::IO, k::CoregionMOKernel) + return print(io, "Coregion Multi-Output Kernel") +end diff --git a/src/mokernels/icm.jl b/src/mokernels/icm.jl deleted file mode 100644 index c7087c3eb..000000000 --- a/src/mokernels/icm.jl +++ /dev/null @@ -1,45 +0,0 @@ -struct IntrinsicCoregionalizationMOKernel{Tu<:Kernel, TA<:AbstractMatrix, Tv<:LinearAlgebra.Diagonal, TB<:AbstractMatrix, Trank<:Integer, Tnoutputs<:Integer} <: MOKernel - u::Tu - A::TA - v::Tv - B::TB - rank::Trank - noutputs::Tnoutputs - - function IntrinsicCoregionalizationMOKernel(u, noutputs, rank) - noutputs >= rank || error("`noutputs` should be greater or equal than `rank`") - - A = rand(noutputs, rank) - v = Diagonal(rand(noutputs)) - B = A * transpose(A) + v - - return new{typeof(u), typeof(A), typeof(v), typeof(B), typeof(rank), typeof(noutputs)}(u,A,v,B,rank,noutputs) - end - - function IntrinsicCoregionalizationMOKernel(u, A, v::AbstractVector) - rank = size(A,2) - noutputs = size(A,1) - v = Diagonal(v) - B = A * transpose(A) + v - return new{typeof(u), typeof(A), typeof(v), typeof(B), typeof(rank), typeof(noutputs)}(u,A,v,B,rank,noutputs) - end -end - - - -function (κ::IntrinsicCoregionalizationMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,Int}) - return κ.B[px,py] * κ.u(x,y) -end - - -# function kernelmatrix(κ::IntrinsicCoregionalizationMOKernel, -# x::AbstractVector{Tuple{Any,Int}}, -# y::AbstractVector{Tuple{Any,Int}}) - -# return κ.(x, permutedims(y)) -# end - - -function Base.show(io::IO, κ::IntrinsicCoregionalizationMOKernel) - return print(io, "Intrinsic Coregionalization Multi-Output Kernel") -end From fa3c1243e3b20b16792d7fe60c8ba9dae70ca5db Mon Sep 17 00:00:00 2001 From: David Date: Mon, 22 Mar 2021 01:14:37 +0000 Subject: [PATCH 03/62] Adds tests for CoregionMOKernel. --- test/mokernels/coregion.jl | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 test/mokernels/coregion.jl diff --git a/test/mokernels/coregion.jl b/test/mokernels/coregion.jl new file mode 100644 index 000000000..40787aed4 --- /dev/null +++ b/test/mokernels/coregion.jl @@ -0,0 +1,26 @@ +@testset "coregion" begin + rng = MersenneTwister(123) + + n_obs = 3 + in_dim = 2 + out_dim = 2 + rank = 1 + + A = randn(out_dim, rank) + B = A * transpose(A) + + X = [(rand(in_dim), rand(1:out_dim)) for i in 1:n_obs] + + kernel = ExponentialKernel() + coregionkernel = CoregionMOKernel(kernel, B) + + @test coregionkernel isa CoregionMOKernel + @test coregionkernel isa MOKernel + @test coregionkernel isa Kernel + @test coregionkernel(X[1], X[1]) isa Real + @test coregionkernel(X[1], X[1]) ≈ B[X[1][2], X[1][2]] * kernel(X[1][1], X[1][1]) + + @test kernelmatrix(coregionkernel, X, X) ≈ kernelmatrix(coregionkernel, X) + + @test string(coregionkernel) == "Coregion Multi-Output Kernel" +end From 1f89b004b35874fdf326c24f9f7e29ad80658297 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 22 Mar 2021 01:17:37 +0000 Subject: [PATCH 04/62] Adds coregion tests to runtests file. --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index 7ad679905..5086cd22d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -138,6 +138,7 @@ include("test_utils.jl") include(joinpath("mokernels", "moinput.jl")) include(joinpath("mokernels", "independent.jl")) include(joinpath("mokernels", "slfm.jl")) + include(joinpath("mokernels", "coregion.jl")) end @info "Ran tests on Multi-Output Kernels" From 0704989817335a9944b5f4075b73accd0441ac24 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 22 Mar 2021 02:07:24 +0000 Subject: [PATCH 05/62] Adds docstring to coregion kernel. --- src/mokernels/coregion.jl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/mokernels/coregion.jl b/src/mokernels/coregion.jl index cdec4c001..3a6828b08 100644 --- a/src/mokernels/coregion.jl +++ b/src/mokernels/coregion.jl @@ -1,3 +1,18 @@ +@doc raw""" + CoregionMOKernel(kernel::Kernel, B::AbstractMatrix) + +Kernel associated with the intrinsic coregionalization model. + +# Definition + +For inputs ``x, x'`` and output dimensions ``p_x, p_{x'}'``, the kernel is defined as[^ARL] +```math +k\big((x, p_x), (x, p_{x'})\big) = [B]_{p_x, p_{x'}} k^{x}\big(x, x'\big), +``` +where ``B`` is a positive semidefinite matrix of size ``m \times m``, with ``m`` being the number of outputs, and k^{x} is a scalar-valued kernel shared by the latent processes. + +[^ARL]: M. Álvarez, L. Rosasco, & N. Lawrence (2012). [Kernels for Vector-Valued Functions: a Review](https://arxiv.org/pdf/1106.6251.pdf). +""" struct CoregionMOKernel{K<:Kernel,T<:AbstractMatrix} <: MOKernel kernel::K B::T From 5574ec912665483146b8323e5939856b07016b59 Mon Sep 17 00:00:00 2001 From: 4aHxKzD <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 22 Mar 2021 23:44:38 +0000 Subject: [PATCH 06/62] Update src/mokernels/coregion.jl Makes parameter explicit in function definition. Co-authored-by: David Widmann --- src/mokernels/coregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mokernels/coregion.jl b/src/mokernels/coregion.jl index 3a6828b08..d7f34f178 100644 --- a/src/mokernels/coregion.jl +++ b/src/mokernels/coregion.jl @@ -7,7 +7,7 @@ Kernel associated with the intrinsic coregionalization model. For inputs ``x, x'`` and output dimensions ``p_x, p_{x'}'``, the kernel is defined as[^ARL] ```math -k\big((x, p_x), (x, p_{x'})\big) = [B]_{p_x, p_{x'}} k^{x}\big(x, x'\big), +k\big((x, p_x), (x', p_{x'}); B, \tilde{k}\big) = [B]_{p_x, p_{x'}} \tilde{k}\big(x, x'\big), ``` where ``B`` is a positive semidefinite matrix of size ``m \times m``, with ``m`` being the number of outputs, and k^{x} is a scalar-valued kernel shared by the latent processes. From ac411ace383a02093aac511579bd0ecbb90e6fc2 Mon Sep 17 00:00:00 2001 From: 4aHxKzD <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 22 Mar 2021 23:48:02 +0000 Subject: [PATCH 07/62] Makes notation in docstring conformant with the rest of the package. Co-authored-by: David Widmann --- src/mokernels/coregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mokernels/coregion.jl b/src/mokernels/coregion.jl index d7f34f178..6d51b5474 100644 --- a/src/mokernels/coregion.jl +++ b/src/mokernels/coregion.jl @@ -9,7 +9,7 @@ For inputs ``x, x'`` and output dimensions ``p_x, p_{x'}'``, the kernel is defin ```math k\big((x, p_x), (x', p_{x'}); B, \tilde{k}\big) = [B]_{p_x, p_{x'}} \tilde{k}\big(x, x'\big), ``` -where ``B`` is a positive semidefinite matrix of size ``m \times m``, with ``m`` being the number of outputs, and k^{x} is a scalar-valued kernel shared by the latent processes. +where ``B`` is a positive semidefinite matrix of size ``m \times m``, with ``m`` being the number of outputs, and ``\tilde{k}`` is a scalar-valued kernel shared by the latent processes. [^ARL]: M. Álvarez, L. Rosasco, & N. Lawrence (2012). [Kernels for Vector-Valued Functions: a Review](https://arxiv.org/pdf/1106.6251.pdf). """ From 4500e21ef9e6c829bd595dc179b824eeed1fd57c Mon Sep 17 00:00:00 2001 From: David Date: Mon, 22 Mar 2021 23:55:34 +0000 Subject: [PATCH 08/62] Makes code respect formating style guidelines. --- src/mokernels/coregion.jl | 2 +- test/mokernels/coregion.jl | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mokernels/coregion.jl b/src/mokernels/coregion.jl index 3a6828b08..b2d1241f6 100644 --- a/src/mokernels/coregion.jl +++ b/src/mokernels/coregion.jl @@ -28,7 +28,7 @@ function CoregionMOKernel(kernel::Kernel, B::AbstractMatrix) end function (k::CoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,Int}) - return k.B[px,py] * k.kernel(x,y) + return k.B[px, py] * k.kernel(x, y) end function Base.show(io::IO, k::CoregionMOKernel) diff --git a/test/mokernels/coregion.jl b/test/mokernels/coregion.jl index 40787aed4..3e33dab43 100644 --- a/test/mokernels/coregion.jl +++ b/test/mokernels/coregion.jl @@ -5,15 +5,15 @@ in_dim = 2 out_dim = 2 rank = 1 - + A = randn(out_dim, rank) B = A * transpose(A) - X = [(rand(in_dim), rand(1:out_dim)) for i in 1:n_obs] + X = [(rand(in_dim), rand(1:out_dim)) for i in 1:n_obs] kernel = ExponentialKernel() coregionkernel = CoregionMOKernel(kernel, B) - + @test coregionkernel isa CoregionMOKernel @test coregionkernel isa MOKernel @test coregionkernel isa Kernel From eaeffb414d2b8beaa9858848c4d338e438b8082c Mon Sep 17 00:00:00 2001 From: David Date: Tue, 23 Mar 2021 00:46:09 +0000 Subject: [PATCH 09/62] Adds Coregion kernel to docs file. --- docs/src/kernels.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/kernels.md b/docs/src/kernels.md index ee769eb3c..83d54d962 100644 --- a/docs/src/kernels.md +++ b/docs/src/kernels.md @@ -133,4 +133,5 @@ KernelTensorProduct MOKernel IndependentMOKernel LatentFactorMOKernel +CoregionMOKernel ``` From 13d2584fea240554b97bc0f08f67371fc02715f5 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Thu, 27 May 2021 13:00:34 +0100 Subject: [PATCH 10/62] Removes unused argument. --- src/mokernels/coregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mokernels/coregion.jl b/src/mokernels/coregion.jl index 5cae38db2..5438974c4 100644 --- a/src/mokernels/coregion.jl +++ b/src/mokernels/coregion.jl @@ -31,6 +31,6 @@ function (k::CoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,Int}) return k.B[px, py] * k.kernel(x, y) end -function Base.show(io::IO, k::CoregionMOKernel) +function Base.show(io::IO, ::CoregionMOKernel) return print(io, "Coregion Multi-Output Kernel") end From e6311d6da5da715fb50b75f66b9e4431c28af1f3 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Thu, 27 May 2021 13:17:55 +0100 Subject: [PATCH 11/62] Creates new methods for Finitedifferences.to_vec to deal with the inputs of multi-output kernels. --- test/finitedifferences.jl | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 test/finitedifferences.jl diff --git a/test/finitedifferences.jl b/test/finitedifferences.jl new file mode 100644 index 000000000..105e796d7 --- /dev/null +++ b/test/finitedifferences.jl @@ -0,0 +1,8 @@ +function FiniteDifferences.to_vec(x::Tuple{T, Int}) where {T} + function MOinput_from_vec(x_vec) + return first(x_vec) + end + return [x], MOinput_from_vec +end + +FiniteDifferences.to_vec(x::Vector{Tuple{T, Int}}) where {T} = (x, identity) From a5fc6b20871460b5c1408c779b1275c599ba13ff Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Thu, 27 May 2021 13:20:15 +0100 Subject: [PATCH 12/62] Creates new methods to compute the jacobian of multioutput kernels. --- test/finitedifferences.jl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/finitedifferences.jl b/test/finitedifferences.jl index 105e796d7..99feeede9 100644 --- a/test/finitedifferences.jl +++ b/test/finitedifferences.jl @@ -6,3 +6,26 @@ function FiniteDifferences.to_vec(x::Tuple{T, Int}) where {T} end FiniteDifferences.to_vec(x::Vector{Tuple{T, Int}}) where {T} = (x, identity) + +function FiniteDifferences._j′vp(fdm, f, ȳ::Vector{<:Real}, x::Vector{Tuple{T, Int}}) where {T} + isempty(x) && return eltype(ȳ)[] # if x is empty, then so is the jacobian and x̄ + return transpose(first(jacobian(fdm, f, x))) * ȳ +end + +function FiniteDifferences.jacobian(fdm, f, x::Vector{Tuple{T, Int}}; len=nothing) where {T} + len !== nothing && Base.depwarn( + "`len` keyword argument to `jacobian` is no longer required " * + "and will not be permitted in the future.", + :jacobian + ) + ẏs = map(eachindex(x)) do n + return fdm(zero(eltype(x).types[1])) do ε + xn = x[n] + x[n] = (xn[1] + ε, xn[2]) + ret = copy(first(to_vec(f(x)))) # copy required incase `f(x)` returns something that aliases `x` + x[n] = xn # Can't do `x[n] -= ϵ` as floating-point math is not associative + return ret + end + end + return (hcat(ẏs...), ) +end From 13cbe37304df3ba5c3ea0c31a24eeb762caae1fd Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Thu, 27 May 2021 13:56:40 +0100 Subject: [PATCH 13/62] Formatting. --- test/finitedifferences.jl | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test/finitedifferences.jl b/test/finitedifferences.jl index 99feeede9..53c24147c 100644 --- a/test/finitedifferences.jl +++ b/test/finitedifferences.jl @@ -1,22 +1,24 @@ -function FiniteDifferences.to_vec(x::Tuple{T, Int}) where {T} +function FiniteDifferences.to_vec(x::Tuple{T,Int}) where {T} function MOinput_from_vec(x_vec) return first(x_vec) end return [x], MOinput_from_vec end -FiniteDifferences.to_vec(x::Vector{Tuple{T, Int}}) where {T} = (x, identity) +FiniteDifferences.to_vec(x::Vector{Tuple{T,Int}}) where {T} = (x, identity) -function FiniteDifferences._j′vp(fdm, f, ȳ::Vector{<:Real}, x::Vector{Tuple{T, Int}}) where {T} +function FiniteDifferences._j′vp( + fdm, f, ȳ::Vector{<:Real}, x::Vector{Tuple{T,Int}} + ) where {T} isempty(x) && return eltype(ȳ)[] # if x is empty, then so is the jacobian and x̄ return transpose(first(jacobian(fdm, f, x))) * ȳ end -function FiniteDifferences.jacobian(fdm, f, x::Vector{Tuple{T, Int}}; len=nothing) where {T} +function FiniteDifferences.jacobian(fdm, f, x::Vector{Tuple{T,Int}}; len=nothing) where {T} len !== nothing && Base.depwarn( "`len` keyword argument to `jacobian` is no longer required " * "and will not be permitted in the future.", - :jacobian + :jacobian ) ẏs = map(eachindex(x)) do n return fdm(zero(eltype(x).types[1])) do ε @@ -27,5 +29,5 @@ function FiniteDifferences.jacobian(fdm, f, x::Vector{Tuple{T, Int}}; len=nothin return ret end end - return (hcat(ẏs...), ) + return (hcat(ẏs...),) end From 81c24768138aac0ac4031488af75bef56b25030d Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Fri, 28 May 2021 11:46:46 +0100 Subject: [PATCH 14/62] Reworkes to_vec so that the reimplementation of _jvp and jacobian is not needed. --- test/finitedifferences.jl | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/test/finitedifferences.jl b/test/finitedifferences.jl index 53c24147c..4602c75b7 100644 --- a/test/finitedifferences.jl +++ b/test/finitedifferences.jl @@ -1,33 +1,7 @@ function FiniteDifferences.to_vec(x::Tuple{T,Int}) where {T} - function MOinput_from_vec(x_vec) - return first(x_vec) + x_vec, first_x_from_vec = to_vec(first(x)) + function Tuple_from_vec(x_vec::AbstractVector{<:Real}) + return (first_x_from_vec(x_vec), last(x)) end - return [x], MOinput_from_vec -end - -FiniteDifferences.to_vec(x::Vector{Tuple{T,Int}}) where {T} = (x, identity) - -function FiniteDifferences._j′vp( - fdm, f, ȳ::Vector{<:Real}, x::Vector{Tuple{T,Int}} - ) where {T} - isempty(x) && return eltype(ȳ)[] # if x is empty, then so is the jacobian and x̄ - return transpose(first(jacobian(fdm, f, x))) * ȳ -end - -function FiniteDifferences.jacobian(fdm, f, x::Vector{Tuple{T,Int}}; len=nothing) where {T} - len !== nothing && Base.depwarn( - "`len` keyword argument to `jacobian` is no longer required " * - "and will not be permitted in the future.", - :jacobian - ) - ẏs = map(eachindex(x)) do n - return fdm(zero(eltype(x).types[1])) do ε - xn = x[n] - x[n] = (xn[1] + ε, xn[2]) - ret = copy(first(to_vec(f(x)))) # copy required incase `f(x)` returns something that aliases `x` - x[n] = xn # Can't do `x[n] -= ϵ` as floating-point math is not associative - return ret - end - end - return (hcat(ẏs...),) + return x_vec, Tuple_from_vec end From 72f124eca0256d2782f9462741711f4e3c31bd99 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Fri, 28 May 2021 11:48:32 +0100 Subject: [PATCH 15/62] Renames kernel. --- src/mokernels/coregion.jl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/mokernels/coregion.jl b/src/mokernels/coregion.jl index 5438974c4..7ea50e6b5 100644 --- a/src/mokernels/coregion.jl +++ b/src/mokernels/coregion.jl @@ -1,5 +1,5 @@ @doc raw""" - CoregionMOKernel(kernel::Kernel, B::AbstractMatrix) + IntrinsicIntrinsicCoregionMOKernel(kernel::Kernel, B::AbstractMatrix) Kernel associated with the intrinsic coregionalization model. @@ -13,24 +13,24 @@ where ``B`` is a positive semidefinite matrix of size ``m \times m``, with ``m`` [^ARL]: M. Álvarez, L. Rosasco, & N. Lawrence (2012). [Kernels for Vector-Valued Functions: a Review](https://arxiv.org/pdf/1106.6251.pdf). """ -struct CoregionMOKernel{K<:Kernel,T<:AbstractMatrix} <: MOKernel +struct IntrinsicCoregionMOKernel{K<:Kernel,T<:AbstractMatrix} <: MOKernel kernel::K B::T - function CoregionMOKernel{K,T}(kernel::K, B::T) where {K,T} - @check_args(CoregionMOKernel, B, (eigmin(B) >= 0), "B is Positive semi-definite") + function IntrinsicCoregionMOKernel{K,T}(kernel::K, B::T) where {K,T} + @check_args(IntrinsicCoregionMOKernel, B, (eigmin(B) >= 0), "B is Positive semi-definite") return new{K,T}(kernel, B) end end -function CoregionMOKernel(kernel::Kernel, B::AbstractMatrix) - return CoregionMOKernel{typeof(kernel),typeof(B)}(kernel, B) +function IntrinsicCoregionMOKernel(kernel::Kernel, B::AbstractMatrix) + return IntrinsicCoregionMOKernel{typeof(kernel),typeof(B)}(kernel, B) end -function (k::CoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,Int}) +function (k::IntrinsicCoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,Int}) return k.B[px, py] * k.kernel(x, y) end -function Base.show(io::IO, ::CoregionMOKernel) - return print(io, "Coregion Multi-Output Kernel") +function Base.show(io::IO, ::IntrinsicCoregionMOKernel) + return print(io, "Intrinsic Coregion Multi-Output Kernel") end From cbf4cbac46d1bfd15f258ac164611be16c844c16 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Fri, 28 May 2021 11:55:00 +0100 Subject: [PATCH 16/62] Changes all affected files by the kernel renaming. --- docs/src/kernels.md | 2 +- src/KernelFunctions.jl | 4 ++-- src/mokernels/{coregion.jl => intrinsiccoregion.jl} | 0 test/mokernels/{coregion.jl => intrinsiccoregion.jl} | 0 test/runtests.jl | 2 +- 5 files changed, 4 insertions(+), 4 deletions(-) rename src/mokernels/{coregion.jl => intrinsiccoregion.jl} (100%) rename test/mokernels/{coregion.jl => intrinsiccoregion.jl} (100%) diff --git a/docs/src/kernels.md b/docs/src/kernels.md index 77d6cf623..ffc92f501 100644 --- a/docs/src/kernels.md +++ b/docs/src/kernels.md @@ -133,5 +133,5 @@ NormalizedKernel MOKernel IndependentMOKernel LatentFactorMOKernel -CoregionMOKernel +IntrinsicCoregionMOKernel ``` diff --git a/src/KernelFunctions.jl b/src/KernelFunctions.jl index 68f29d516..862d8e4dd 100644 --- a/src/KernelFunctions.jl +++ b/src/KernelFunctions.jl @@ -36,7 +36,7 @@ export spectral_mixture_kernel, spectral_mixture_product_kernel export ColVecs, RowVecs export MOInput -export IndependentMOKernel, LatentFactorMOKernel, CoregionMOKernel +export IndependentMOKernel, LatentFactorMOKernel, IntrinsicCoregionMOKernel # Reexports export tensor, ⊗, compose @@ -105,7 +105,7 @@ include(joinpath("mokernels", "mokernel.jl")) include(joinpath("mokernels", "moinput.jl")) include(joinpath("mokernels", "independent.jl")) include(joinpath("mokernels", "slfm.jl")) -include(joinpath("mokernels", "coregion.jl")) +include(joinpath("mokernels", "intrinsiccoregion.jl")) include("chainrules.jl") include("zygoterules.jl") diff --git a/src/mokernels/coregion.jl b/src/mokernels/intrinsiccoregion.jl similarity index 100% rename from src/mokernels/coregion.jl rename to src/mokernels/intrinsiccoregion.jl diff --git a/test/mokernels/coregion.jl b/test/mokernels/intrinsiccoregion.jl similarity index 100% rename from test/mokernels/coregion.jl rename to test/mokernels/intrinsiccoregion.jl diff --git a/test/runtests.jl b/test/runtests.jl index 61b78b206..e878d8158 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -139,7 +139,7 @@ include("test_utils.jl") include(joinpath("mokernels", "moinput.jl")) include(joinpath("mokernels", "independent.jl")) include(joinpath("mokernels", "slfm.jl")) - include(joinpath("mokernels", "coregion.jl")) + include(joinpath("mokernels", "intrinsiccoregion.jl")) end @info "Ran tests on Multi-Output Kernels" From 30c2a5c83abb5a6f29ced82cb7433564206626df Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Fri, 28 May 2021 17:18:38 +0100 Subject: [PATCH 17/62] Adds test_interface method to generate inputs for MOkernels. --- src/test_utils.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test_utils.jl b/src/test_utils.jl index 8b1cbd68c..e1b3e08e4 100644 --- a/src/test_utils.jl +++ b/src/test_utils.jl @@ -97,6 +97,18 @@ function test_interface( ) end +function test_interface( + rng::AbstractRNG, k::MOKernel, ::Type{Vector{Tuple{T,Int}}}; kwargs... +) where {T<:Real} + return test_interface( + k, + [(randn(rng, T), 1) for i in 1:1001], + [(randn(rng, T), 1) for i in 1:1001], + [(randn(rng, T), 1) for i in 1:1000]; + kwargs... + ) +end + function test_interface( rng::AbstractRNG, k::Kernel, ::Type{<:ColVecs{T}}; dim_in=2, kwargs... ) where {T<:Real} From 87650771d1e4b0af0bd76680a8678a55d1f88f68 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Fri, 28 May 2021 17:19:50 +0100 Subject: [PATCH 18/62] Makes IntrinsicCoregionMOKernel use the standard suit of tests. --- test/mokernels/intrinsiccoregion.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 3e33dab43..2be90abcb 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -1,4 +1,4 @@ -@testset "coregion" begin +@testset "intrinsiccoregion" begin rng = MersenneTwister(123) n_obs = 3 @@ -7,20 +7,20 @@ rank = 1 A = randn(out_dim, rank) - B = A * transpose(A) + B = A * transpose(A) + Diagonal(randn(out_dim)) X = [(rand(in_dim), rand(1:out_dim)) for i in 1:n_obs] kernel = ExponentialKernel() - coregionkernel = CoregionMOKernel(kernel, B) + icoregionkernel = IntrinsicCoregionMOKernel(kernel, B) - @test coregionkernel isa CoregionMOKernel - @test coregionkernel isa MOKernel - @test coregionkernel isa Kernel - @test coregionkernel(X[1], X[1]) isa Real - @test coregionkernel(X[1], X[1]) ≈ B[X[1][2], X[1][2]] * kernel(X[1][1], X[1][1]) + @test icoregionkernel isa IntrinsicCoregionMOKernel + @test icoregionkernel isa MOKernel + @test icoregionkernel isa Kernel + @test icoregionkernel(X[1], X[1]) isa Real + @test icoregionkernel(X[1], X[1]) ≈ B[X[1][2], X[1][2]] * kernel(X[1][1], X[1][1]) - @test kernelmatrix(coregionkernel, X, X) ≈ kernelmatrix(coregionkernel, X) + KernelFunctions.TestUtils.test_interface(icoregionkernel, Vector{Tuple{Float64,Int}}) - @test string(coregionkernel) == "Coregion Multi-Output Kernel" + @test string(coregionkernel) == "Intrinsic Coregion Multi-Output Kernel" end From 51a0a7e300a57fc39e3860ac3d344412b79819df Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Fri, 28 May 2021 17:56:50 +0100 Subject: [PATCH 19/62] Improves MOkernels tests to test different outputs dimensions. --- src/test_utils.jl | 8 ++++---- test/mokernels/intrinsiccoregion.jl | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test_utils.jl b/src/test_utils.jl index e1b3e08e4..ac6587ca0 100644 --- a/src/test_utils.jl +++ b/src/test_utils.jl @@ -98,13 +98,13 @@ function test_interface( end function test_interface( - rng::AbstractRNG, k::MOKernel, ::Type{Vector{Tuple{T,Int}}}; kwargs... + rng::AbstractRNG, k::MOKernel, ::Type{Vector{Tuple{T,Int}}}; dim_out=1, kwargs... ) where {T<:Real} return test_interface( k, - [(randn(rng, T), 1) for i in 1:1001], - [(randn(rng, T), 1) for i in 1:1001], - [(randn(rng, T), 1) for i in 1:1000]; + [(randn(rng, T), rand(1:dim_out)) for i in 1:51], + [(randn(rng, T), rand(1:dim_out)) for i in 1:51], + [(randn(rng, T), rand(1:dim_out)) for i in 1:50]; kwargs... ) end diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 2be90abcb..b1d757586 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -20,7 +20,7 @@ @test icoregionkernel(X[1], X[1]) isa Real @test icoregionkernel(X[1], X[1]) ≈ B[X[1][2], X[1][2]] * kernel(X[1][1], X[1][1]) - KernelFunctions.TestUtils.test_interface(icoregionkernel, Vector{Tuple{Float64,Int}}) + KernelFunctions.TestUtils.test_interface(icoregionkernel, Vector{Tuple{Float64,Int}}, dim_out=out_dim) @test string(coregionkernel) == "Intrinsic Coregion Multi-Output Kernel" end From df66dc0b2b0bf311065e0a7cea8092661c825e5d Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Fri, 28 May 2021 18:08:43 +0100 Subject: [PATCH 20/62] Passes RNG object to rand method. --- src/test_utils.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test_utils.jl b/src/test_utils.jl index ac6587ca0..e8bf7b072 100644 --- a/src/test_utils.jl +++ b/src/test_utils.jl @@ -102,9 +102,9 @@ function test_interface( ) where {T<:Real} return test_interface( k, - [(randn(rng, T), rand(1:dim_out)) for i in 1:51], - [(randn(rng, T), rand(1:dim_out)) for i in 1:51], - [(randn(rng, T), rand(1:dim_out)) for i in 1:50]; + [(randn(rng, T), rand(rng, 1:dim_out)) for i in 1:51], + [(randn(rng, T), rand(rng, 1:dim_out)) for i in 1:51], + [(randn(rng, T), rand(rng, 1:dim_out)) for i in 1:50]; kwargs... ) end From 936bd94beb5b11326bbf340795dbf8912dc67783 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Sun, 30 May 2021 11:40:15 +0100 Subject: [PATCH 21/62] Adds MOKernel methods for testfunction and testdiagfunction. --- test/test_utils.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test_utils.jl b/test/test_utils.jl index caa04b8d3..cb325e10b 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -53,6 +53,11 @@ testfunction(k, A, dim) = sum(kernelmatrix(k, A; obsdim=dim)) testdiagfunction(k, A, dim) = sum(kernelmatrix_diag(k, A; obsdim=dim)) testdiagfunction(k, A, B, dim) = sum(kernelmatrix_diag(k, A, B; obsdim=dim)) +testfunction(k::MOKernel, A, B) = sum(kernelmatrix(k, A, B)) +testfunction(k::MOKernel, A) = sum(kernelmatrix(k, A)) +testdiagfunction(k::MOKernel, A) = sum(kernelmatrix_diag(k, A)) +testdiagfunction(k::MOKernel, A, B) = sum(kernelmatrix_diag(k, A, B)) + function test_ADs( kernelfunction, args=nothing; ADs=[:Zygote, :ForwardDiff, :ReverseDiff], dims=[3, 3] ) From e10059ab7a8571a44d87d043888668d974ae72de Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Sun, 30 May 2021 11:42:45 +0100 Subject: [PATCH 22/62] Adds test_FiniteDiff method for MOKernels. --- test/test_utils.jl | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/test_utils.jl b/test/test_utils.jl index cb325e10b..aedcd2995 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -133,6 +133,47 @@ function test_FiniteDiff(kernelfunction, args=nothing, dims=[3, 3]) end end +function test_FiniteDiff(kernelfunction<:MOKernel, args, n_obs=3, dim_in=2, dim_out=2) + # Init arguments : + k = kernelfunction(args) + + rng = MersenneTwister(42) + @testset "FiniteDifferences" begin + ## Testing Kernel Functions + x = (rand(rng, n_obs), rand(rng, 1:dim_out)) + y = (rand(rng, n_obs), rand(rng, 1:dim_out)) + + @test_nowarn gradient(:FiniteDiff, x) do x + k(x, y) + end + + ## Testing Kernel Matrices + + A = [(randn(rng, dim_in), rand(rng, 1:dim_out)) for i in 1:n_obs] + B = [(randn(rng, dim_in), rand(rng, 1:dim_out)) for i in 1:n_obs] + + @test_nowarn gradient(:FiniteDiff, A) do a + testfunction(k, a) + end + @test_nowarn gradient(:FiniteDiff, A) do a + testfunction(k, a, B) + end + @test_nowarn gradient(:FiniteDiff, B) do b + testfunction(k, A, b) + end + + @test_nowarn gradient(:FiniteDiff, A) do a + testdiagfunction(k, a) + end + @test_nowarn gradient(:FiniteDiff, A) do a + testdiagfunction(k, a, B) + end + @test_nowarn gradient(:FiniteDiff, B) do b + testdiagfunction(k, A, b) + end + end +end + function test_AD(AD::Symbol, kernelfunction, args=nothing, dims=[3, 3]) @testset "$(AD)" begin # Test kappa function From 01490b70effc5048769c2c86ecee82d5b1d0c59e Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 10:46:08 +0100 Subject: [PATCH 23/62] Changes test_FiniteDiff for MOKernel signature to have the same number of arguments as the methods for other kernels. --- test/test_utils.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test_utils.jl b/test/test_utils.jl index aedcd2995..9045f4cf7 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -133,15 +133,15 @@ function test_FiniteDiff(kernelfunction, args=nothing, dims=[3, 3]) end end -function test_FiniteDiff(kernelfunction<:MOKernel, args, n_obs=3, dim_in=2, dim_out=2) +function test_FiniteDiff(kernelfunction<:MOKernel, args, dims=(in=3, out=3, obs=3)) # Init arguments : k = kernelfunction(args) rng = MersenneTwister(42) @testset "FiniteDifferences" begin ## Testing Kernel Functions - x = (rand(rng, n_obs), rand(rng, 1:dim_out)) - y = (rand(rng, n_obs), rand(rng, 1:dim_out)) + x = (rand(rng, dims.obs), rand(rng, 1:dims.out)) + y = (rand(rng, dims.obs), rand(rng, 1:dims.out)) @test_nowarn gradient(:FiniteDiff, x) do x k(x, y) @@ -149,8 +149,8 @@ function test_FiniteDiff(kernelfunction<:MOKernel, args, n_obs=3, dim_in=2, dim_ ## Testing Kernel Matrices - A = [(randn(rng, dim_in), rand(rng, 1:dim_out)) for i in 1:n_obs] - B = [(randn(rng, dim_in), rand(rng, 1:dim_out)) for i in 1:n_obs] + A = [(randn(rng, dims.in), rand(rng, 1:dims.out)) for i in 1:dims.obs] + B = [(randn(rng, dims.in), rand(rng, 1:dims.out)) for i in 1:dims.obs] @test_nowarn gradient(:FiniteDiff, A) do a testfunction(k, a) From 2e04b2927a2025a3ed8318f8eb06a61b70d69ba2 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 12:05:30 +0100 Subject: [PATCH 24/62] Corrects bug in tests where the created covariance matrix between outputs could be non PSD. --- test/mokernels/intrinsiccoregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index b1d757586..938259f29 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -7,7 +7,7 @@ rank = 1 A = randn(out_dim, rank) - B = A * transpose(A) + Diagonal(randn(out_dim)) + B = A * transpose(A) + Diagonal(rand(out_dim)) X = [(rand(in_dim), rand(1:out_dim)) for i in 1:n_obs] From 28d912df7dfaad5307ad360d5589d6c30a669bfc Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 12:09:58 +0100 Subject: [PATCH 25/62] Adapts compare_gradient for MOKernels. --- test/test_utils.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/test_utils.jl b/test/test_utils.jl index 9045f4cf7..de9ba8866 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -48,6 +48,18 @@ function compare_gradient(f, AD::Symbol, args) @test grad_AD ≈ grad_FD atol = 1e-8 rtol = 1e-5 end +function compare_gradient(f, AD::Symbol, args::Vector{Tuple{T,Int}}) where {T} + grad_AD = first.(gradient(f, AD, args)) + grad_FD = first.(gradient(f, :FiniteDiff, args)) + @test grad_AD ≈ grad_FD atol = 1e-8 rtol = 1e-5 +end + +function compare_gradient(f, AD::Symbol, args::Tuple{T,Int}) where {T} + grad_AD = first(gradient(f, AD, args)) + grad_FD = first(gradient(f, :FiniteDiff, args)) + @test grad_AD ≈ grad_FD atol = 1e-8 rtol = 1e-5 +end + testfunction(k, A, B, dim) = sum(kernelmatrix(k, A, B; obsdim=dim)) testfunction(k, A, dim) = sum(kernelmatrix(k, A; obsdim=dim)) testdiagfunction(k, A, dim) = sum(kernelmatrix_diag(k, A; obsdim=dim)) From 6bef1b75189867c78febdbe40e4e75a9c6d8030c Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 12:10:33 +0100 Subject: [PATCH 26/62] Adapts test_AD for MOKernels. --- test/test_utils.jl | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/test_utils.jl b/test/test_utils.jl index de9ba8866..472486638 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -252,3 +252,48 @@ function test_AD(AD::Symbol, kernelfunction, args=nothing, dims=[3, 3]) end end end + +function test_AD(AD::Symbol, kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out=2, obs=3)) + @testset "$(AD)" begin + # Test kappa function + k = kernelfunction(args...) + + rng = MersenneTwister(42) + + # Testing kernel evaluations + x = (rand(rng, dims.obs), rand(rng, 1:dims.out)) + y = (rand(rng, dims.obs), rand(rng, 1:dims.out)) + + compare_gradient(AD, x) do x + k(x, y) + end + compare_gradient(AD, y) do y + k(x, y) + end + + # Testing kernel matrices + A = [(randn(rng, dims.in), rand(rng, 1:dims.out)) for i in 1:dims.obs] + B = [(randn(rng, dims.in), rand(rng, 1:dims.out)) for i in 1:dims.obs] + + compare_gradient(AD, A) do a + testfunction(k, a) + end + compare_gradient(AD, A) do a + testfunction(k, a, B) + end + compare_gradient(AD, B) do b + testfunction(k, A, b) + end + + compare_gradient(AD, A) do a + testdiagfunction(k, a) + end + compare_gradient(AD, A) do a + testdiagfunction(k, a, B) + end + compare_gradient(AD, B) do b + testdiagfunction(k, A, b) + end + end +end + From a6689ad3dffa984464fdec6964c8133982c829e9 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 12:22:35 +0100 Subject: [PATCH 27/62] Adapts test_ADs for MOKernels. --- test/test_utils.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/test_utils.jl b/test/test_utils.jl index 472486638..05674d74d 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -81,6 +81,17 @@ function test_ADs( end end +function test_ADs( + kernelfunction::Type{<:MOKernel}, args; ADs=[:Zygote], dims=(in=3, out=2, obs=3) +) + test_fd = test_FiniteDiff(kernelfunction, args, dims) + if !test_fd.anynonpass + for AD in ADs + test_AD(AD, kernelfunction, args, dims) + end + end +end + function test_FiniteDiff(kernelfunction, args=nothing, dims=[3, 3]) # Init arguments : k = if args === nothing From bc58b167b8fbc2f83a2f9e562c73d38b936328f9 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 12:24:16 +0100 Subject: [PATCH 28/62] Corrects type annotation. --- test/test_utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_utils.jl b/test/test_utils.jl index 05674d74d..61a12236b 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -156,7 +156,7 @@ function test_FiniteDiff(kernelfunction, args=nothing, dims=[3, 3]) end end -function test_FiniteDiff(kernelfunction<:MOKernel, args, dims=(in=3, out=3, obs=3)) +function test_FiniteDiff(kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out=2, obs=3)) # Init arguments : k = kernelfunction(args) From 53f4d65cf6b28b4bf0024f45bc607b5aeab4e99f Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 12:25:00 +0100 Subject: [PATCH 29/62] Corrects argument splatting. --- test/test_utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_utils.jl b/test/test_utils.jl index 61a12236b..a2a4e9f6f 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -158,7 +158,7 @@ end function test_FiniteDiff(kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out=2, obs=3)) # Init arguments : - k = kernelfunction(args) + k = kernelfunction(args...) rng = MersenneTwister(42) @testset "FiniteDifferences" begin From dcabde98515f219ea17057a59a8e815a374dd0d0 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 12:26:41 +0100 Subject: [PATCH 30/62] Rewrites variables as named tuple. --- test/mokernels/intrinsiccoregion.jl | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 938259f29..caf3e3f8f 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -1,15 +1,13 @@ @testset "intrinsiccoregion" begin rng = MersenneTwister(123) - n_obs = 3 - in_dim = 2 - out_dim = 2 + dims = (in=3, out=2, obs=3) rank = 1 + + A = randn(dims.out, rank) + B = A * transpose(A) + Diagonal(rand(dims.out)) - A = randn(out_dim, rank) - B = A * transpose(A) + Diagonal(rand(out_dim)) - - X = [(rand(in_dim), rand(1:out_dim)) for i in 1:n_obs] + X = [(rand(dims.in), rand(1:dims.out)) for i in 1:dims.obs] kernel = ExponentialKernel() icoregionkernel = IntrinsicCoregionMOKernel(kernel, B) From b02976379051861e4db8a1b7e6420730d80f4c63 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 12:27:57 +0100 Subject: [PATCH 31/62] Updates arguments. --- test/mokernels/intrinsiccoregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index caf3e3f8f..5147e4a90 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -18,7 +18,7 @@ @test icoregionkernel(X[1], X[1]) isa Real @test icoregionkernel(X[1], X[1]) ≈ B[X[1][2], X[1][2]] * kernel(X[1][1], X[1][1]) - KernelFunctions.TestUtils.test_interface(icoregionkernel, Vector{Tuple{Float64,Int}}, dim_out=out_dim) + KernelFunctions.TestUtils.test_interface(icoregionkernel, Vector{Tuple{Float64,Int}}, dim_out=dims.out) @test string(coregionkernel) == "Intrinsic Coregion Multi-Output Kernel" end From b31625fdbee08ab89e759002520da49bee4eb5b4 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 12:28:18 +0100 Subject: [PATCH 32/62] Adds standardised AD tests for IntrinsicCoregionMOKernel. --- test/mokernels/intrinsiccoregion.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 5147e4a90..ae64e4c74 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -19,6 +19,7 @@ @test icoregionkernel(X[1], X[1]) ≈ B[X[1][2], X[1][2]] * kernel(X[1][1], X[1][1]) KernelFunctions.TestUtils.test_interface(icoregionkernel, Vector{Tuple{Float64,Int}}, dim_out=dims.out) + test_ADs(IntrinsicCoregionMOKernel, (kernel, B)) @test string(coregionkernel) == "Intrinsic Coregion Multi-Output Kernel" end From 4a603e2297660b50805f0938d794a5f6d31a4efb Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 12:35:01 +0100 Subject: [PATCH 33/62] Adds include of finitedifferences file to runtests. --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index e878d8158..89a4f7787 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -52,6 +52,7 @@ using KernelFunctions.TestUtils: test_interface @info "Packages Loaded" include("test_utils.jl") +include("finitedifferences.jl") @testset "KernelFunctions" begin include("utils.jl") From 01e31c1b110e5a38dd4bfac3039a18bd39c15514 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 14:53:17 +0100 Subject: [PATCH 34/62] Passes dims argument. --- test/mokernels/intrinsiccoregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index ae64e4c74..64a79211f 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -19,7 +19,7 @@ @test icoregionkernel(X[1], X[1]) ≈ B[X[1][2], X[1][2]] * kernel(X[1][1], X[1][1]) KernelFunctions.TestUtils.test_interface(icoregionkernel, Vector{Tuple{Float64,Int}}, dim_out=dims.out) - test_ADs(IntrinsicCoregionMOKernel, (kernel, B)) + test_ADs(IntrinsicCoregionMOKernel, (kernel, B), dims=dims) @test string(coregionkernel) == "Intrinsic Coregion Multi-Output Kernel" end From d823057deaf2ce4785236867d93dc1d2242da65d Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 14:57:21 +0100 Subject: [PATCH 35/62] Formatting. --- test/mokernels/intrinsiccoregion.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 64a79211f..c88fb25e0 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -3,11 +3,11 @@ dims = (in=3, out=2, obs=3) rank = 1 - + A = randn(dims.out, rank) B = A * transpose(A) + Diagonal(rand(dims.out)) - X = [(rand(dims.in), rand(1:dims.out)) for i in 1:dims.obs] + X = [(rand(dims.in), rand(1:dims.out)) for i in 1:(dims.obs)] kernel = ExponentialKernel() icoregionkernel = IntrinsicCoregionMOKernel(kernel, B) @@ -18,7 +18,10 @@ @test icoregionkernel(X[1], X[1]) isa Real @test icoregionkernel(X[1], X[1]) ≈ B[X[1][2], X[1][2]] * kernel(X[1][1], X[1][1]) - KernelFunctions.TestUtils.test_interface(icoregionkernel, Vector{Tuple{Float64,Int}}, dim_out=dims.out) + KernelFunctions.TestUtils.test_interface( + icoregionkernel, Vector{Tuple{Float64,Int}}, dim_out=dims.out + ) + test_ADs(IntrinsicCoregionMOKernel, (kernel, B), dims=dims) @test string(coregionkernel) == "Intrinsic Coregion Multi-Output Kernel" From 49886673df09dc77d7fde76dec70ce17fa2f530b Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 15:01:26 +0100 Subject: [PATCH 36/62] Formatting. --- test/test_utils.jl | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/test/test_utils.jl b/test/test_utils.jl index a2a4e9f6f..6165c7453 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -163,8 +163,8 @@ function test_FiniteDiff(kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out rng = MersenneTwister(42) @testset "FiniteDifferences" begin ## Testing Kernel Functions - x = (rand(rng, dims.obs), rand(rng, 1:dims.out)) - y = (rand(rng, dims.obs), rand(rng, 1:dims.out)) + x = (rand(rng, dims.obs), rand(rng, 1:(dims.out))) + y = (rand(rng, dims.obs), rand(rng, 1:(dims.out))) @test_nowarn gradient(:FiniteDiff, x) do x k(x, y) @@ -172,8 +172,8 @@ function test_FiniteDiff(kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out ## Testing Kernel Matrices - A = [(randn(rng, dims.in), rand(rng, 1:dims.out)) for i in 1:dims.obs] - B = [(randn(rng, dims.in), rand(rng, 1:dims.out)) for i in 1:dims.obs] + A = [(randn(rng, dims.in), rand(rng, 1:(dims.out))) for i in 1:(dims.obs)] + B = [(randn(rng, dims.in), rand(rng, 1:(dims.out))) for i in 1:(dims.obs)] @test_nowarn gradient(:FiniteDiff, A) do a testfunction(k, a) @@ -264,7 +264,9 @@ function test_AD(AD::Symbol, kernelfunction, args=nothing, dims=[3, 3]) end end -function test_AD(AD::Symbol, kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out=2, obs=3)) +function test_AD( + AD::Symbol, kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out=2, obs=3) + ) @testset "$(AD)" begin # Test kappa function k = kernelfunction(args...) @@ -272,8 +274,8 @@ function test_AD(AD::Symbol, kernelfunction::Type{<:MOKernel}, args, dims=(in=3, rng = MersenneTwister(42) # Testing kernel evaluations - x = (rand(rng, dims.obs), rand(rng, 1:dims.out)) - y = (rand(rng, dims.obs), rand(rng, 1:dims.out)) + x = (rand(rng, dims.obs), rand(rng, 1:(dims.out))) + y = (rand(rng, dims.obs), rand(rng, 1:(dims.out))) compare_gradient(AD, x) do x k(x, y) @@ -283,8 +285,8 @@ function test_AD(AD::Symbol, kernelfunction::Type{<:MOKernel}, args, dims=(in=3, end # Testing kernel matrices - A = [(randn(rng, dims.in), rand(rng, 1:dims.out)) for i in 1:dims.obs] - B = [(randn(rng, dims.in), rand(rng, 1:dims.out)) for i in 1:dims.obs] + A = [(randn(rng, dims.in), rand(rng, 1:(dims.out))) for i in 1:(dims.obs)] + B = [(randn(rng, dims.in), rand(rng, 1:(dims.out))) for i in 1:(dims.obs)] compare_gradient(AD, A) do a testfunction(k, a) From f30273e5f59d02d18fcf717c463109bb63734e02 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Mon, 31 May 2021 15:29:42 +0100 Subject: [PATCH 37/62] Corrects a bug where the wrong tuple fields were used. --- test/test_utils.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test_utils.jl b/test/test_utils.jl index 6165c7453..ef4facce8 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -163,8 +163,8 @@ function test_FiniteDiff(kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out rng = MersenneTwister(42) @testset "FiniteDifferences" begin ## Testing Kernel Functions - x = (rand(rng, dims.obs), rand(rng, 1:(dims.out))) - y = (rand(rng, dims.obs), rand(rng, 1:(dims.out))) + x = (rand(rng, dims.in), rand(rng, 1:(dims.out))) + y = (rand(rng, dims.in), rand(rng, 1:(dims.out))) @test_nowarn gradient(:FiniteDiff, x) do x k(x, y) @@ -274,8 +274,8 @@ function test_AD( rng = MersenneTwister(42) # Testing kernel evaluations - x = (rand(rng, dims.obs), rand(rng, 1:(dims.out))) - y = (rand(rng, dims.obs), rand(rng, 1:(dims.out))) + x = (rand(rng, dims.in), rand(rng, 1:(dims.out))) + y = (rand(rng, dims.in), rand(rng, 1:(dims.out))) compare_gradient(AD, x) do x k(x, y) From bf9d217e28a65f71e5b92692351fa381fad6281c Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 13:09:32 +0100 Subject: [PATCH 38/62] Corrects test. --- test/mokernels/intrinsiccoregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index c88fb25e0..9f348dab7 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -24,5 +24,5 @@ test_ADs(IntrinsicCoregionMOKernel, (kernel, B), dims=dims) - @test string(coregionkernel) == "Intrinsic Coregion Multi-Output Kernel" + @test string(icoregionkernel) == "Intrinsic Coregion Multi-Output Kernel" end From 1ac68f6e144a7c252e45dc2fdb560e9947ee2c3f Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 13:10:00 +0100 Subject: [PATCH 39/62] Implements @theogf approach to testing gradients of MOkernels. --- test/test_utils.jl | 116 ++++++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 54 deletions(-) diff --git a/test/test_utils.jl b/test/test_utils.jl index ef4facce8..2e9ab8fba 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -48,18 +48,6 @@ function compare_gradient(f, AD::Symbol, args) @test grad_AD ≈ grad_FD atol = 1e-8 rtol = 1e-5 end -function compare_gradient(f, AD::Symbol, args::Vector{Tuple{T,Int}}) where {T} - grad_AD = first.(gradient(f, AD, args)) - grad_FD = first.(gradient(f, :FiniteDiff, args)) - @test grad_AD ≈ grad_FD atol = 1e-8 rtol = 1e-5 -end - -function compare_gradient(f, AD::Symbol, args::Tuple{T,Int}) where {T} - grad_AD = first(gradient(f, AD, args)) - grad_FD = first(gradient(f, :FiniteDiff, args)) - @test grad_AD ≈ grad_FD atol = 1e-8 rtol = 1e-5 -end - testfunction(k, A, B, dim) = sum(kernelmatrix(k, A, B; obsdim=dim)) testfunction(k, A, dim) = sum(kernelmatrix(k, A; obsdim=dim)) testdiagfunction(k, A, dim) = sum(kernelmatrix_diag(k, A; obsdim=dim)) @@ -81,16 +69,9 @@ function test_ADs( end end -function test_ADs( - kernelfunction::Type{<:MOKernel}, args; ADs=[:Zygote], dims=(in=3, out=2, obs=3) -) - test_fd = test_FiniteDiff(kernelfunction, args, dims) - if !test_fd.anynonpass - for AD in ADs - test_AD(AD, kernelfunction, args, dims) - end - end -end +test_ADs(kernelfunction::Type{<:MOKernel}, args, ADs, dims) = test_ADs( + kernelfunction, args=nothing; + ADs=[:Zygote, :ForwardDiff, :ReverseDiff], dims=(in=3, out=2, obs=3)) function test_FiniteDiff(kernelfunction, args=nothing, dims=[3, 3]) # Init arguments : @@ -166,8 +147,10 @@ function test_FiniteDiff(kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out x = (rand(rng, dims.in), rand(rng, 1:(dims.out))) y = (rand(rng, dims.in), rand(rng, 1:(dims.out))) - @test_nowarn gradient(:FiniteDiff, x) do x - k(x, y) + x_out_idx = x[2] + + @test_nowarn gradient(:FiniteDiff, x[1]) do x + k((x, x_out_idx), y) end ## Testing Kernel Matrices @@ -175,24 +158,27 @@ function test_FiniteDiff(kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out A = [(randn(rng, dims.in), rand(rng, 1:(dims.out))) for i in 1:(dims.obs)] B = [(randn(rng, dims.in), rand(rng, 1:(dims.out))) for i in 1:(dims.obs)] - @test_nowarn gradient(:FiniteDiff, A) do a - testfunction(k, a) + A_out_idxs = last.(A) + B_out_idxs = last.(B) + + @test_nowarn gradient(:FiniteDiff, first.(A)) do a + testfunction(k, tuple.(a, A_out_idxs)) end - @test_nowarn gradient(:FiniteDiff, A) do a - testfunction(k, a, B) + @test_nowarn gradient(:FiniteDiff, first.(A)) do a + testfunction(k, tuple.(a, A_out_idxs), B) end - @test_nowarn gradient(:FiniteDiff, B) do b - testfunction(k, A, b) + @test_nowarn gradient(:FiniteDiff, first.(B)) do b + testfunction(k, A, tuple.(b, B_out_idxs)) end - @test_nowarn gradient(:FiniteDiff, A) do a - testdiagfunction(k, a) + @test_nowarn gradient(:FiniteDiff, first.(A)) do a + testdiagfunction(k, tuple.(a, A_out_idxs)) end - @test_nowarn gradient(:FiniteDiff, A) do a - testdiagfunction(k, a, B) + @test_nowarn gradient(:FiniteDiff, first.(A)) do a + testdiagfunction(k, tuple.(a, A_out_idxs), B) end - @test_nowarn gradient(:FiniteDiff, B) do b - testdiagfunction(k, A, b) + @test_nowarn gradient(:FiniteDiff, first.(B)) do b + testdiagfunction(k, A, tuple.(b, B_out_idxs)) end end end @@ -277,36 +263,58 @@ function test_AD( x = (rand(rng, dims.in), rand(rng, 1:(dims.out))) y = (rand(rng, dims.in), rand(rng, 1:(dims.out))) - compare_gradient(AD, x) do x - k(x, y) + x_out_idx = x[2] + y_out_idx = y[2] + + compare_gradient(AD, x[1]) do x + k((x, x_out_idx), y) end - compare_gradient(AD, y) do y - k(x, y) + compare_gradient(AD, y[1]) do y + k(x, (y, y_out_idx)) end # Testing kernel matrices A = [(randn(rng, dims.in), rand(rng, 1:(dims.out))) for i in 1:(dims.obs)] B = [(randn(rng, dims.in), rand(rng, 1:(dims.out))) for i in 1:(dims.obs)] - compare_gradient(AD, A) do a - testfunction(k, a) + A_out_idxs = last.(A) + B_out_idxs = last.(B) + + compare_gradient(AD, hcat(first.(A)...)) do a + matrix = hcat(first.(A)...) + vector_of_vectors = [matrix[:,i] for i in 1:size(matrix,2)] + input = tuple.(vector_of_vectors, A_out_idxs) + testfunction(k, input) end - compare_gradient(AD, A) do a - testfunction(k, a, B) + compare_gradient(AD, hcat(first.(A)...)) do a + matrix = hcat(first.(A)...) + vector_of_vectors = [matrix[:,i] for i in 1:size(matrix,2)] + input = tuple.(vector_of_vectors, A_out_idxs) + testfunction(k, input, B) end - compare_gradient(AD, B) do b - testfunction(k, A, b) + compare_gradient(AD, hcat(first.(B)...)) do b + matrix = hcat(first.(B)...) + vector_of_vectors = [matrix[:,i] for i in 1:size(matrix,2)] + input = tuple.(vector_of_vectors, B_out_idxs) + testfunction(k, A, input) end - - compare_gradient(AD, A) do a - testdiagfunction(k, a) + compare_gradient(AD, hcat(first.(A)...)) do a + matrix = hcat(first.(A)...) + vector_of_vectors = [matrix[:,i] for i in 1:size(matrix,2)] + input = tuple.(vector_of_vectors, A_out_idxs) + testdiagfunction(k, input) end - compare_gradient(AD, A) do a - testdiagfunction(k, a, B) + compare_gradient(AD, hcat(first.(A)...)) do a + matrix = hcat(first.(A)...) + vector_of_vectors = [matrix[:,i] for i in 1:size(matrix,2)] + input = tuple.(vector_of_vectors, A_out_idxs) + testdiagfunction(k, input, B) end - compare_gradient(AD, B) do b - testdiagfunction(k, A, b) + compare_gradient(AD, hcat(first.(B)...)) do b + matrix = hcat(first.(B)...) + vector_of_vectors = [matrix[:,i] for i in 1:size(matrix,2)] + input = tuple.(vector_of_vectors, B_out_idxs) + testdiagfunction(k, A, input) end end end - From 90db07da82cef6a5476a1ca8d89850f26130a224 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 13:10:43 +0100 Subject: [PATCH 40/62] With new approach this file is not needed. --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 89a4f7787..4700c8c99 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -52,7 +52,7 @@ using KernelFunctions.TestUtils: test_interface @info "Packages Loaded" include("test_utils.jl") -include("finitedifferences.jl") +# include("finitedifferences.jl") @testset "KernelFunctions" begin include("utils.jl") From 7928803ade39467aa075a21cd29873a4fb79c461 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 15:23:00 +0100 Subject: [PATCH 41/62] Correctly implement @theogf approach to MOkernels AD testing. --- test/test_utils.jl | 101 +++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 59 deletions(-) diff --git a/test/test_utils.jl b/test/test_utils.jl index 2e9ab8fba..af82bcfb0 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -147,10 +147,8 @@ function test_FiniteDiff(kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out x = (rand(rng, dims.in), rand(rng, 1:(dims.out))) y = (rand(rng, dims.in), rand(rng, 1:(dims.out))) - x_out_idx = x[2] - - @test_nowarn gradient(:FiniteDiff, x[1]) do x - k((x, x_out_idx), y) + @test_nowarn gradient(:FiniteDiff, x[1]) do a + k((a, x[2]), y) end ## Testing Kernel Matrices @@ -158,27 +156,30 @@ function test_FiniteDiff(kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out A = [(randn(rng, dims.in), rand(rng, 1:(dims.out))) for i in 1:(dims.obs)] B = [(randn(rng, dims.in), rand(rng, 1:(dims.out))) for i in 1:(dims.obs)] - A_out_idxs = last.(A) - B_out_idxs = last.(B) - - @test_nowarn gradient(:FiniteDiff, first.(A)) do a - testfunction(k, tuple.(a, A_out_idxs)) + @test_nowarn gradient(:FiniteDiff, reduce(hcat, first.(A))) do a + A = tuple.(eachcol(a), last.(A)) + testfunction(k, A) end - @test_nowarn gradient(:FiniteDiff, first.(A)) do a - testfunction(k, tuple.(a, A_out_idxs), B) + @test_nowarn gradient(:FiniteDiff, reduce(hcat, first.(A))) do a + A = tuple.(eachcol(a), last.(A)) + testfunction(k, A, B) end - @test_nowarn gradient(:FiniteDiff, first.(B)) do b - testfunction(k, A, tuple.(b, B_out_idxs)) + @test_nowarn gradient(:FiniteDiff, reduce(hcat, first.(B))) do b + B = tuple.(eachcol(b), last.(B)) + testfunction(k, A, B) end - @test_nowarn gradient(:FiniteDiff, first.(A)) do a - testdiagfunction(k, tuple.(a, A_out_idxs)) + @test_nowarn gradient(:FiniteDiff, reduce(hcat, first.(A))) do a + A = tuple.(eachcol(a), last.(A)) + testdiagfunction(k, A) end - @test_nowarn gradient(:FiniteDiff, first.(A)) do a - testdiagfunction(k, tuple.(a, A_out_idxs), B) + @test_nowarn gradient(:FiniteDiff, reduce(hcat, first.(A))) do a + A = tuple.(eachcol(a), last.(A)) + testdiagfunction(k, A, B) end - @test_nowarn gradient(:FiniteDiff, first.(B)) do b - testdiagfunction(k, A, tuple.(b, B_out_idxs)) + @test_nowarn gradient(:FiniteDiff, reduce(hcat, first.(B))) do b + B = tuple.(eachcol(b), last.(B)) + testdiagfunction(k, A, B) end end end @@ -263,58 +264,40 @@ function test_AD( x = (rand(rng, dims.in), rand(rng, 1:(dims.out))) y = (rand(rng, dims.in), rand(rng, 1:(dims.out))) - x_out_idx = x[2] - y_out_idx = y[2] - - compare_gradient(AD, x[1]) do x - k((x, x_out_idx), y) + compare_gradient(AD, x[1]) do a + k((a, x[2]), y) end - compare_gradient(AD, y[1]) do y - k(x, (y, y_out_idx)) + compare_gradient(AD, y[1]) do b + k(x, (b, y[2])) end # Testing kernel matrices A = [(randn(rng, dims.in), rand(rng, 1:(dims.out))) for i in 1:(dims.obs)] B = [(randn(rng, dims.in), rand(rng, 1:(dims.out))) for i in 1:(dims.obs)] - A_out_idxs = last.(A) - B_out_idxs = last.(B) - - compare_gradient(AD, hcat(first.(A)...)) do a - matrix = hcat(first.(A)...) - vector_of_vectors = [matrix[:,i] for i in 1:size(matrix,2)] - input = tuple.(vector_of_vectors, A_out_idxs) - testfunction(k, input) + compare_gradient(AD, reduce(hcat, first.(A))) do a + A = tuple.(eachcol(a), last.(A)) + testfunction(k, A) end - compare_gradient(AD, hcat(first.(A)...)) do a - matrix = hcat(first.(A)...) - vector_of_vectors = [matrix[:,i] for i in 1:size(matrix,2)] - input = tuple.(vector_of_vectors, A_out_idxs) - testfunction(k, input, B) + compare_gradient(AD, reduce(hcat, first.(A))) do a + A = tuple.(eachcol(a), last.(A)) + testfunction(k, A, B) end - compare_gradient(AD, hcat(first.(B)...)) do b - matrix = hcat(first.(B)...) - vector_of_vectors = [matrix[:,i] for i in 1:size(matrix,2)] - input = tuple.(vector_of_vectors, B_out_idxs) - testfunction(k, A, input) + compare_gradient(AD, reduce(hcat, first.(B))) do b + B = tuple.(eachcol(b), last.(B)) + testfunction(k, A, B) end - compare_gradient(AD, hcat(first.(A)...)) do a - matrix = hcat(first.(A)...) - vector_of_vectors = [matrix[:,i] for i in 1:size(matrix,2)] - input = tuple.(vector_of_vectors, A_out_idxs) - testdiagfunction(k, input) + compare_gradient(AD, reduce(hcat, first.(A))) do a + A = tuple.(eachcol(a), last.(A)) + testdiagfunction(k, A) end - compare_gradient(AD, hcat(first.(A)...)) do a - matrix = hcat(first.(A)...) - vector_of_vectors = [matrix[:,i] for i in 1:size(matrix,2)] - input = tuple.(vector_of_vectors, A_out_idxs) - testdiagfunction(k, input, B) + compare_gradient(AD, reduce(hcat, first.(A))) do a + A = tuple.(eachcol(a), last.(A)) + testdiagfunction(k, A, B) end - compare_gradient(AD, hcat(first.(B)...)) do b - matrix = hcat(first.(B)...) - vector_of_vectors = [matrix[:,i] for i in 1:size(matrix,2)] - input = tuple.(vector_of_vectors, B_out_idxs) - testdiagfunction(k, A, input) + compare_gradient(AD, reduce(hcat, first.(B))) do b + B = tuple.(eachcol(b), last.(B)) + testdiagfunction(k, A, B) end end end From bf1ab84aae062cc873575a9d72217670cad7e7e3 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 15:24:13 +0100 Subject: [PATCH 42/62] Changes tests to use SqExponentialKernel instead of ExponentialKernel because of the latter's AD instability. --- test/mokernels/intrinsiccoregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 9f348dab7..4bfcba83a 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -9,7 +9,7 @@ X = [(rand(dims.in), rand(1:dims.out)) for i in 1:(dims.obs)] - kernel = ExponentialKernel() + kernel = SqExponentialKernel() icoregionkernel = IntrinsicCoregionMOKernel(kernel, B) @test icoregionkernel isa IntrinsicCoregionMOKernel From b5cbab8e2e670947c3ad30511b8fc8b3d73657a0 Mon Sep 17 00:00:00 2001 From: 4aHxKzD <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 15:27:24 +0100 Subject: [PATCH 43/62] Deletes unsused include. This file is not needed anymore. Co-authored-by: David Widmann --- test/runtests.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 4700c8c99..e878d8158 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -52,7 +52,6 @@ using KernelFunctions.TestUtils: test_interface @info "Packages Loaded" include("test_utils.jl") -# include("finitedifferences.jl") @testset "KernelFunctions" begin include("utils.jl") From a627f76edc84029517971aa2e269206bb140d974 Mon Sep 17 00:00:00 2001 From: 4aHxKzD <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 15:30:20 +0100 Subject: [PATCH 44/62] Simplifies docstring. Co-authored-by: David Widmann --- src/mokernels/intrinsiccoregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index 7ea50e6b5..1c97f57bc 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -5,7 +5,7 @@ Kernel associated with the intrinsic coregionalization model. # Definition -For inputs ``x, x'`` and output dimensions ``p_x, p_{x'}'``, the kernel is defined as[^ARL] +For inputs ``x, x'`` and output dimensions ``p, p'``, the kernel is defined as[^ARL] ```math k\big((x, p_x), (x', p_{x'}); B, \tilde{k}\big) = [B]_{p_x, p_{x'}} \tilde{k}\big(x, x'\big), ``` From af05c2a02d24e34f4cd1867dafa37312e5710d7c Mon Sep 17 00:00:00 2001 From: 4aHxKzD <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 15:30:54 +0100 Subject: [PATCH 45/62] Improves error message. Co-authored-by: David Widmann --- src/mokernels/intrinsiccoregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index 1c97f57bc..d5ff0acf5 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -18,7 +18,7 @@ struct IntrinsicCoregionMOKernel{K<:Kernel,T<:AbstractMatrix} <: MOKernel B::T function IntrinsicCoregionMOKernel{K,T}(kernel::K, B::T) where {K,T} - @check_args(IntrinsicCoregionMOKernel, B, (eigmin(B) >= 0), "B is Positive semi-definite") + @check_args(IntrinsicCoregionMOKernel, B, eigmin(B) >= 0, "B has to be positive semi-definite") return new{K,T}(kernel, B) end end From 4965979324da0a6ef6fedab588252d0ee7c5764e Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 15:32:11 +0100 Subject: [PATCH 46/62] Deletes file because we currently pass just the Real part to gradient functions when testing. --- test/finitedifferences.jl | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 test/finitedifferences.jl diff --git a/test/finitedifferences.jl b/test/finitedifferences.jl deleted file mode 100644 index 4602c75b7..000000000 --- a/test/finitedifferences.jl +++ /dev/null @@ -1,7 +0,0 @@ -function FiniteDifferences.to_vec(x::Tuple{T,Int}) where {T} - x_vec, first_x_from_vec = to_vec(first(x)) - function Tuple_from_vec(x_vec::AbstractVector{<:Real}) - return (first_x_from_vec(x_vec), last(x)) - end - return x_vec, Tuple_from_vec -end From cb27119e82b031b9d9d922189d69680793ddcf25 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 16:27:33 +0100 Subject: [PATCH 47/62] Adds kernel and number of outputs when printing Icoregionkernel. --- src/mokernels/intrinsiccoregion.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index d5ff0acf5..e1e4e9ee2 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -31,6 +31,6 @@ function (k::IntrinsicCoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{ return k.B[px, py] * k.kernel(x, y) end -function Base.show(io::IO, ::IntrinsicCoregionMOKernel) - return print(io, "Intrinsic Coregion Multi-Output Kernel") +function Base.show(io::IO, k::IntrinsicCoregionMOKernel) + return print(io, "Intrinsic Coregion Kernel: ", k.kernel, " with ", size(k.B, 1), " outputs") end From 85c86f791d566ef1c1de68b117e0a2bd9fc4dffd Mon Sep 17 00:00:00 2001 From: 4aHxKzD <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 16:30:54 +0100 Subject: [PATCH 48/62] Adds test for different inputs as arguments. Co-authored-by: David Widmann --- test/mokernels/intrinsiccoregion.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 4bfcba83a..ae80529d8 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -17,6 +17,7 @@ @test icoregionkernel isa Kernel @test icoregionkernel(X[1], X[1]) isa Real @test icoregionkernel(X[1], X[1]) ≈ B[X[1][2], X[1][2]] * kernel(X[1][1], X[1][1]) + @test icoregionkernel(X[1], X[end]) ≈ B[X[1][2], X[end][2]] * kernel(X[1][1], X[end][1]) KernelFunctions.TestUtils.test_interface( icoregionkernel, Vector{Tuple{Float64,Int}}, dim_out=dims.out From dbd7f272518a02d5c20530ce53c712f7dcf64955 Mon Sep 17 00:00:00 2001 From: 4aHxKzD <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 16:32:38 +0100 Subject: [PATCH 49/62] Stops infringing character limit. Co-authored-by: David Widmann --- src/mokernels/intrinsiccoregion.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index e1e4e9ee2..66edd85ac 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -9,7 +9,9 @@ For inputs ``x, x'`` and output dimensions ``p, p'``, the kernel is defined as[^ ```math k\big((x, p_x), (x', p_{x'}); B, \tilde{k}\big) = [B]_{p_x, p_{x'}} \tilde{k}\big(x, x'\big), ``` -where ``B`` is a positive semidefinite matrix of size ``m \times m``, with ``m`` being the number of outputs, and ``\tilde{k}`` is a scalar-valued kernel shared by the latent processes. +where ``B`` is a positive semidefinite matrix of size ``m \times m``, with ``m`` being the +number of outputs, and ``\tilde{k}`` is a scalar-valued kernel shared by the latent +processes. [^ARL]: M. Álvarez, L. Rosasco, & N. Lawrence (2012). [Kernels for Vector-Valued Functions: a Review](https://arxiv.org/pdf/1106.6251.pdf). """ From 93e9b2c936ea378f95fe426507e80e2b61a67980 Mon Sep 17 00:00:00 2001 From: 4aHxKzD <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 16:33:17 +0100 Subject: [PATCH 50/62] Further simplify docstring. Co-authored-by: David Widmann --- src/mokernels/intrinsiccoregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index 66edd85ac..e6a004e33 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -7,7 +7,7 @@ Kernel associated with the intrinsic coregionalization model. For inputs ``x, x'`` and output dimensions ``p, p'``, the kernel is defined as[^ARL] ```math -k\big((x, p_x), (x', p_{x'}); B, \tilde{k}\big) = [B]_{p_x, p_{x'}} \tilde{k}\big(x, x'\big), +k\big((x, p), (x', p'); B, \tilde{k}\big) = B_{p, p'} \tilde{k}\big(x, x'\big), ``` where ``B`` is a positive semidefinite matrix of size ``m \times m``, with ``m`` being the number of outputs, and ``\tilde{k}`` is a scalar-valued kernel shared by the latent From 9ab8e6085a57d495a6e2e2fe4f7bd9cf7d1603a3 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 16:39:56 +0100 Subject: [PATCH 51/62] Adds tests. --- test/mokernels/intrinsiccoregion.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index ae80529d8..1a06c963d 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -15,6 +15,8 @@ @test icoregionkernel isa IntrinsicCoregionMOKernel @test icoregionkernel isa MOKernel @test icoregionkernel isa Kernel + @test icoregionkernel.B == B + @test icoregionkernel.kernel == kernel @test icoregionkernel(X[1], X[1]) isa Real @test icoregionkernel(X[1], X[1]) ≈ B[X[1][2], X[1][2]] * kernel(X[1][1], X[1][1]) @test icoregionkernel(X[1], X[end]) ≈ B[X[1][2], X[end][2]] * kernel(X[1][1], X[end][1]) From 16a773b4118d7df811cd4a186993fb35f66e31e8 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 17:00:38 +0100 Subject: [PATCH 52/62] Deletes unuseful tests. --- test/mokernels/intrinsiccoregion.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 1a06c963d..44bb9c071 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -12,12 +12,8 @@ kernel = SqExponentialKernel() icoregionkernel = IntrinsicCoregionMOKernel(kernel, B) - @test icoregionkernel isa IntrinsicCoregionMOKernel - @test icoregionkernel isa MOKernel - @test icoregionkernel isa Kernel @test icoregionkernel.B == B @test icoregionkernel.kernel == kernel - @test icoregionkernel(X[1], X[1]) isa Real @test icoregionkernel(X[1], X[1]) ≈ B[X[1][2], X[1][2]] * kernel(X[1][1], X[1][1]) @test icoregionkernel(X[1], X[end]) ≈ B[X[1][2], X[end][2]] * kernel(X[1][1], X[end][1]) From 160a38e4a993c0422bc7c23200c7664881a145c8 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 17:04:55 +0100 Subject: [PATCH 53/62] Updates print test. --- test/mokernels/intrinsiccoregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 44bb9c071..83cda2789 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -23,5 +23,5 @@ test_ADs(IntrinsicCoregionMOKernel, (kernel, B), dims=dims) - @test string(icoregionkernel) == "Intrinsic Coregion Multi-Output Kernel" + @test string(icoregionkernel) == "Intrinsic Coregion Kernel: $(repr(kernel)) with $(dims.out) outputs" end From bb8adc2bb2c8e756bbc80c8c137a1d2ca4dc4021 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 17:08:45 +0100 Subject: [PATCH 54/62] Stops using interpolations. --- test/mokernels/intrinsiccoregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 83cda2789..6575505eb 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -23,5 +23,5 @@ test_ADs(IntrinsicCoregionMOKernel, (kernel, B), dims=dims) - @test string(icoregionkernel) == "Intrinsic Coregion Kernel: $(repr(kernel)) with $(dims.out) outputs" + @test string(icoregionkernel) == string("Intrinsic Coregion Kernel: ", kernel, " with ", dims.out, " outputs") end From 6fa2404911566911c0f67ffe6945da5ef41f973e Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 17:13:34 +0100 Subject: [PATCH 55/62] Creates constructor with keyword arguments. --- src/mokernels/intrinsiccoregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index e6a004e33..81952ce5c 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -25,7 +25,7 @@ struct IntrinsicCoregionMOKernel{K<:Kernel,T<:AbstractMatrix} <: MOKernel end end -function IntrinsicCoregionMOKernel(kernel::Kernel, B::AbstractMatrix) +function IntrinsicCoregionMOKernel(; kernel::Kernel, B::AbstractMatrix) return IntrinsicCoregionMOKernel{typeof(kernel),typeof(B)}(kernel, B) end From fede68037d154c61c8412fc7b59362195a45e6be Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 17:20:54 +0100 Subject: [PATCH 56/62] Restore constructor. --- src/mokernels/intrinsiccoregion.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index 81952ce5c..d52330c2e 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -25,6 +25,10 @@ struct IntrinsicCoregionMOKernel{K<:Kernel,T<:AbstractMatrix} <: MOKernel end end +function IntrinsicCoregionMOKernel(kernel::Kernel, B::AbstractMatrix) + return IntrinsicCoregionMOKernel{typeof(kernel),typeof(B)}(kernel, B) +end + function IntrinsicCoregionMOKernel(; kernel::Kernel, B::AbstractMatrix) return IntrinsicCoregionMOKernel{typeof(kernel),typeof(B)}(kernel, B) end From a86529bbd688b7ea5838cdc1c28d575d3421216e Mon Sep 17 00:00:00 2001 From: 4aHxKzD <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 22:48:29 +0100 Subject: [PATCH 57/62] Deletes non kwargs outer constructor. Co-authored-by: David Widmann --- src/mokernels/intrinsiccoregion.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index d52330c2e..81952ce5c 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -25,10 +25,6 @@ struct IntrinsicCoregionMOKernel{K<:Kernel,T<:AbstractMatrix} <: MOKernel end end -function IntrinsicCoregionMOKernel(kernel::Kernel, B::AbstractMatrix) - return IntrinsicCoregionMOKernel{typeof(kernel),typeof(B)}(kernel, B) -end - function IntrinsicCoregionMOKernel(; kernel::Kernel, B::AbstractMatrix) return IntrinsicCoregionMOKernel{typeof(kernel),typeof(B)}(kernel, B) end From 169234eb5f3d15969a983d510d9ae01639915ce3 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 23:24:34 +0100 Subject: [PATCH 58/62] Formatting. --- src/mokernels/intrinsiccoregion.jl | 11 +++++++++-- src/test_utils.jl | 2 +- test/mokernels/intrinsiccoregion.jl | 9 +++++---- test/test_utils.jl | 4 ++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index 81952ce5c..18bd111f2 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -20,7 +20,12 @@ struct IntrinsicCoregionMOKernel{K<:Kernel,T<:AbstractMatrix} <: MOKernel B::T function IntrinsicCoregionMOKernel{K,T}(kernel::K, B::T) where {K,T} - @check_args(IntrinsicCoregionMOKernel, B, eigmin(B) >= 0, "B has to be positive semi-definite") + @check_args( + IntrinsicCoregionMOKernel, + B, + eigmin(B) >= 0, + "B has to be positive semi-definite" + ) return new{K,T}(kernel, B) end end @@ -34,5 +39,7 @@ function (k::IntrinsicCoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{ end function Base.show(io::IO, k::IntrinsicCoregionMOKernel) - return print(io, "Intrinsic Coregion Kernel: ", k.kernel, " with ", size(k.B, 1), " outputs") + return print( + io, "Intrinsic Coregion Kernel: ", k.kernel, " with ", size(k.B, 1), " outputs" + ) end diff --git a/src/test_utils.jl b/src/test_utils.jl index e8bf7b072..27d4eba2e 100644 --- a/src/test_utils.jl +++ b/src/test_utils.jl @@ -105,7 +105,7 @@ function test_interface( [(randn(rng, T), rand(rng, 1:dim_out)) for i in 1:51], [(randn(rng, T), rand(rng, 1:dim_out)) for i in 1:51], [(randn(rng, T), rand(rng, 1:dim_out)) for i in 1:50]; - kwargs... + kwargs..., ) end diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 6575505eb..5047c7c9f 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -7,7 +7,7 @@ A = randn(dims.out, rank) B = A * transpose(A) + Diagonal(rand(dims.out)) - X = [(rand(dims.in), rand(1:dims.out)) for i in 1:(dims.obs)] + X = [(rand(dims.in), rand(1:(dims.out))) for i in 1:(dims.obs)] kernel = SqExponentialKernel() icoregionkernel = IntrinsicCoregionMOKernel(kernel, B) @@ -18,10 +18,11 @@ @test icoregionkernel(X[1], X[end]) ≈ B[X[1][2], X[end][2]] * kernel(X[1][1], X[end][1]) KernelFunctions.TestUtils.test_interface( - icoregionkernel, Vector{Tuple{Float64,Int}}, dim_out=dims.out + icoregionkernel, Vector{Tuple{Float64,Int}}; dim_out=dims.out ) - test_ADs(IntrinsicCoregionMOKernel, (kernel, B), dims=dims) + test_ADs(icoregionkernel; dims=dims) - @test string(icoregionkernel) == string("Intrinsic Coregion Kernel: ", kernel, " with ", dims.out, " outputs") + @test string(icoregionkernel) == + string("Intrinsic Coregion Kernel: ", kernel, " with ", dims.out, " outputs") end diff --git a/test/test_utils.jl b/test/test_utils.jl index af82bcfb0..b96c26bab 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -164,7 +164,7 @@ function test_FiniteDiff(kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out A = tuple.(eachcol(a), last.(A)) testfunction(k, A, B) end - @test_nowarn gradient(:FiniteDiff, reduce(hcat, first.(B))) do b + @test_nowarn gradient(:FiniteDiff, reduce(hcat, first.(B))) do b B = tuple.(eachcol(b), last.(B)) testfunction(k, A, B) end @@ -177,7 +177,7 @@ function test_FiniteDiff(kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out A = tuple.(eachcol(a), last.(A)) testdiagfunction(k, A, B) end - @test_nowarn gradient(:FiniteDiff, reduce(hcat, first.(B))) do b + @test_nowarn gradient(:FiniteDiff, reduce(hcat, first.(B))) do b B = tuple.(eachcol(b), last.(B)) testdiagfunction(k, A, B) end From 3f211b2fd0cdde45a74efd3e1ebde5a4a8c77663 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 23:25:01 +0100 Subject: [PATCH 59/62] Updates code to be in consistent with kwargs constructor. --- test/mokernels/intrinsiccoregion.jl | 2 +- test/test_utils.jl | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 5047c7c9f..1bfea6833 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -10,7 +10,7 @@ X = [(rand(dims.in), rand(1:(dims.out))) for i in 1:(dims.obs)] kernel = SqExponentialKernel() - icoregionkernel = IntrinsicCoregionMOKernel(kernel, B) + icoregionkernel = IntrinsicCoregionMOKernel(kernel=kernel, B=B) @test icoregionkernel.B == B @test icoregionkernel.kernel == kernel diff --git a/test/test_utils.jl b/test/test_utils.jl index b96c26bab..69030c451 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -69,9 +69,16 @@ function test_ADs( end end -test_ADs(kernelfunction::Type{<:MOKernel}, args, ADs, dims) = test_ADs( - kernelfunction, args=nothing; - ADs=[:Zygote, :ForwardDiff, :ReverseDiff], dims=(in=3, out=2, obs=3)) +function test_ADs( + k::MOKernel; ADs=[:Zygote, :ForwardDiff, :ReverseDiff], dims=(in=3, out=2, obs=3) + ) + test_fd = test_FiniteDiff(k, dims) + if !test_fd.anynonpass + for AD in ADs + test_AD(AD, k, dims) + end + end +end function test_FiniteDiff(kernelfunction, args=nothing, dims=[3, 3]) # Init arguments : @@ -137,10 +144,7 @@ function test_FiniteDiff(kernelfunction, args=nothing, dims=[3, 3]) end end -function test_FiniteDiff(kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out=2, obs=3)) - # Init arguments : - k = kernelfunction(args...) - +function test_FiniteDiff(k::MOKernel, dims=(in=3, out=2, obs=3)) rng = MersenneTwister(42) @testset "FiniteDifferences" begin ## Testing Kernel Functions @@ -252,12 +256,9 @@ function test_AD(AD::Symbol, kernelfunction, args=nothing, dims=[3, 3]) end function test_AD( - AD::Symbol, kernelfunction::Type{<:MOKernel}, args, dims=(in=3, out=2, obs=3) + AD::Symbol, k::MOKernel, dims=(in=3, out=2, obs=3) ) @testset "$(AD)" begin - # Test kappa function - k = kernelfunction(args...) - rng = MersenneTwister(42) # Testing kernel evaluations From 5db59efb06e623004375943a7a69d9fe52f03bdf Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 23:28:40 +0100 Subject: [PATCH 60/62] Updates docstring. --- src/mokernels/intrinsiccoregion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mokernels/intrinsiccoregion.jl b/src/mokernels/intrinsiccoregion.jl index 18bd111f2..9e38ba09b 100644 --- a/src/mokernels/intrinsiccoregion.jl +++ b/src/mokernels/intrinsiccoregion.jl @@ -1,5 +1,5 @@ @doc raw""" - IntrinsicIntrinsicCoregionMOKernel(kernel::Kernel, B::AbstractMatrix) + IntrinsicIntrinsicCoregionMOKernel(; kernel::Kernel, B::AbstractMatrix) Kernel associated with the intrinsic coregionalization model. From 6d46be5ae3046dc564251714682bd5f5c20da96d Mon Sep 17 00:00:00 2001 From: David Widmann Date: Wed, 2 Jun 2021 00:33:31 +0200 Subject: [PATCH 61/62] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index b49d29fc0..be4600b7c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "KernelFunctions" uuid = "ec8451be-7e33-11e9-00cf-bbf324bd1392" -version = "0.10.1" +version = "0.10.2" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" From b1608103f4b9fd200e01586a1f762d545a665679 Mon Sep 17 00:00:00 2001 From: David <57544666+4aHxKzD@users.noreply.github.com> Date: Tue, 1 Jun 2021 23:39:04 +0100 Subject: [PATCH 62/62] Formatting. --- test/mokernels/intrinsiccoregion.jl | 4 ++-- test/test_utils.jl | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/test/mokernels/intrinsiccoregion.jl b/test/mokernels/intrinsiccoregion.jl index 1bfea6833..2734bda1b 100644 --- a/test/mokernels/intrinsiccoregion.jl +++ b/test/mokernels/intrinsiccoregion.jl @@ -10,7 +10,7 @@ X = [(rand(dims.in), rand(1:(dims.out))) for i in 1:(dims.obs)] kernel = SqExponentialKernel() - icoregionkernel = IntrinsicCoregionMOKernel(kernel=kernel, B=B) + icoregionkernel = IntrinsicCoregionMOKernel(; kernel=kernel, B=B) @test icoregionkernel.B == B @test icoregionkernel.kernel == kernel @@ -24,5 +24,5 @@ test_ADs(icoregionkernel; dims=dims) @test string(icoregionkernel) == - string("Intrinsic Coregion Kernel: ", kernel, " with ", dims.out, " outputs") + string("Intrinsic Coregion Kernel: ", kernel, " with ", dims.out, " outputs") end diff --git a/test/test_utils.jl b/test/test_utils.jl index 69030c451..d2b138077 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -71,7 +71,7 @@ end function test_ADs( k::MOKernel; ADs=[:Zygote, :ForwardDiff, :ReverseDiff], dims=(in=3, out=2, obs=3) - ) +) test_fd = test_FiniteDiff(k, dims) if !test_fd.anynonpass for AD in ADs @@ -255,9 +255,7 @@ function test_AD(AD::Symbol, kernelfunction, args=nothing, dims=[3, 3]) end end -function test_AD( - AD::Symbol, k::MOKernel, dims=(in=3, out=2, obs=3) - ) +function test_AD(AD::Symbol, k::MOKernel, dims=(in=3, out=2, obs=3)) @testset "$(AD)" begin rng = MersenneTwister(42)