-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add KernelTensorSum
#507
Open
martincornejo
wants to merge
11
commits into
JuliaGaussianProcesses:master
Choose a base branch
from
martincornejo:kernel-tensor-sum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+189
−3
Open
Add KernelTensorSum
#507
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5a6037d
Add `KernelTensorSum`
martincornejo 2be0d37
Fix `KernelTensorSum` overload test
martincornejo a39f81f
Improve coverage and error message of `validate_domain(::KernelTensor…
martincornejo 03ce5b5
Fix typo
martincornejo 106ef5f
Fix formatting
martincornejo 668fcfd
Fix typo: k -> kernel
martincornejo d1a18b2
Rename `KernelTensorSum` -> `KernelIndependentSum`
martincornejo 36b4d9e
Fix `KernelIndependentSum` pretty printing
martincornejo 8d0da0a
Revert "Rename `KernelTensorSum` -> `KernelIndependentSum`"
martincornejo e016424
Add non-Unicode alternative for ⊕
martincornejo 598fbc7
Fix typo: `kernel_sum` -> `tensor_sum`
martincornejo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
""" | ||
KernelIndependentSum | ||
|
||
Independent sum of kernels. | ||
|
||
# Definition | ||
|
||
For inputs ``x = (x_1, \\ldots, x_n)`` and ``x' = (x'_1, \\ldots, x'_n)``, the tensor | ||
sum of kernels ``k_1, \\ldots, k_n`` is defined as | ||
```math | ||
k(x, x'; k_1, \\ldots, k_n) = \\sum_{i=1}^n k_i(x_i, x'_i). | ||
``` | ||
|
||
# Construction | ||
|
||
The simplest way to specify a `KernelIndependentSum` is to use the `⊕` operator (can be typed by `\\oplus<tab>`). | ||
```jldoctest independentsum | ||
julia> k1 = SqExponentialKernel(); k2 = LinearKernel(); X = rand(5, 2); | ||
|
||
julia> kernelmatrix(k1 ⊕ k2, RowVecs(X)) == kernelmatrix(k1, X[:, 1]) + kernelmatrix(k2, X[:, 2]) | ||
true | ||
``` | ||
|
||
You can also specify a `KernelIndependentSum` by providing kernels as individual arguments | ||
or as an iterable data structure such as a `Tuple` or a `Vector`. Using a tuple or | ||
individual arguments guarantees that `KernelIndependentSum` is concretely typed but might | ||
lead to large compilation times if the number of kernels is large. | ||
```jldoctest independentsum | ||
julia> KernelIndependentSum(k1, k2) == k1 ⊕ k2 | ||
true | ||
|
||
julia> KernelIndependentSum((k1, k2)) == k1 ⊕ k2 | ||
true | ||
|
||
julia> KernelIndependentSum([k1, k2]) == k1 ⊕ k2 | ||
true | ||
``` | ||
""" | ||
struct KernelIndependentSum{K} <: Kernel | ||
kernels::K | ||
end | ||
|
||
function KernelIndependentSum(kernel::Kernel, kernels::Kernel...) | ||
return KernelIndependentSum((kernel, kernels...)) | ||
end | ||
|
||
@functor KernelIndependentSum | ||
|
||
Base.length(kernel::KernelIndependentSum) = length(kernel.kernels) | ||
|
||
function (kernel::KernelIndependentSum)(x, y) | ||
if !((nx = length(x)) == (ny = length(y)) == (nkernels = length(kernel))) | ||
throw( | ||
DimensionMismatch( | ||
"number of kernels ($nkernels) and number of features (x=$nx, y=$ny) are not consistent", | ||
), | ||
) | ||
end | ||
return sum(k(xi, yi) for (k, xi, yi) in zip(kernel.kernels, x, y)) | ||
end | ||
|
||
function validate_domain(k::KernelIndependentSum, x::AbstractVector, y::AbstractVector) | ||
return (dx = dim(x)) == (dy = dim(y)) == (nkernels = length(k)) || error( | ||
"number of kernels ($nkernels) and group of features (x=$dx), y=$dy) are not consistent", | ||
) | ||
end | ||
|
||
function validate_domain(k::KernelIndependentSum, x::AbstractVector) | ||
return validate_domain(k, x, x) | ||
end | ||
|
||
function kernelmatrix(k::KernelIndependentSum, x::AbstractVector) | ||
validate_domain(k, x) | ||
return mapreduce(kernelmatrix, +, k.kernels, slices(x)) | ||
end | ||
|
||
function kernelmatrix(k::KernelIndependentSum, x::AbstractVector, y::AbstractVector) | ||
validate_domain(k, x, y) | ||
return mapreduce(kernelmatrix, +, k.kernels, slices(x), slices(y)) | ||
end | ||
|
||
function kernelmatrix_diag(k::KernelIndependentSum, x::AbstractVector) | ||
validate_domain(k, x) | ||
return mapreduce(kernelmatrix_diag, +, k.kernels, slices(x)) | ||
end | ||
|
||
function kernelmatrix_diag(k::KernelIndependentSum, x::AbstractVector, y::AbstractVector) | ||
validate_domain(k, x, y) | ||
return mapreduce(kernelmatrix_diag, +, k.kernels, slices(x), slices(y)) | ||
end | ||
|
||
function Base.:(==)(x::KernelIndependentSum, y::KernelIndependentSum) | ||
return ( | ||
length(x.kernels) == length(y.kernels) && | ||
all(kx == ky for (kx, ky) in zip(x.kernels, y.kernels)) | ||
) | ||
end | ||
|
||
Base.show(io::IO, kernel::KernelIndependentSum) = printshifted(io, kernel, 0) | ||
|
||
function printshifted(io::IO, kernel::KernelIndependentSum, shift::Int) | ||
print(io, "Tensor sum of ", length(kernel), " kernels:") | ||
for k in kernel.kernels | ||
print(io, "\n") | ||
for _ in 1:(shift + 1) | ||
print(io, "\t") | ||
end | ||
printshifted(io, k, shift + 2) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
@testset "kerneltensorsum" begin | ||
rng = MersenneTwister(123456) | ||
u1 = rand(rng, 10) | ||
u2 = rand(rng, 10) | ||
v1 = rand(rng, 5) | ||
v2 = rand(rng, 5) | ||
|
||
# kernels | ||
k1 = SqExponentialKernel() | ||
k2 = ExponentialKernel() | ||
kernel1 = KernelIndependentSum(k1, k2) | ||
kernel2 = KernelIndependentSum([k1, k2]) | ||
|
||
@test kernel1 == kernel2 | ||
@test kernel1.kernels == (k1, k2) === KernelIndependentSum((k1, k2)).kernels | ||
for (_k1, _k2) in Iterators.product( | ||
(k1, KernelIndependentSum((k1,)), KernelIndependentSum([k1])), | ||
(k2, KernelIndependentSum((k2,)), KernelIndependentSum([k2])), | ||
) | ||
@test kernel1 == _k1 ⊕ _k2 | ||
end | ||
@test length(kernel1) == length(kernel2) == 2 | ||
@test string(kernel1) == ( | ||
"Tensor sum of 2 kernels:\n" * | ||
"\tSquared Exponential Kernel (metric = Euclidean(0.0))\n" * | ||
"\tExponential Kernel (metric = Euclidean(0.0))" | ||
) | ||
@test_throws DimensionMismatch kernel1(rand(3), rand(3)) | ||
|
||
@testset "val" begin | ||
for (x, y) in (((v1, u1), (v2, u2)), ([v1, u1], [v2, u2])) | ||
val = k1(x[1], y[1]) + k2(x[2], y[2]) | ||
|
||
@test kernel1(x, y) == kernel2(x, y) == val | ||
end | ||
end | ||
|
||
# Standardised tests. | ||
TestUtils.test_interface(kernel1, ColVecs{Float64}) | ||
TestUtils.test_interface(kernel1, RowVecs{Float64}) | ||
TestUtils.test_interface( | ||
KernelIndependentSum(WhiteKernel(), ConstantKernel(; c=1.1)), ColVecs{String} | ||
) | ||
test_ADs( | ||
x -> KernelIndependentSum(SqExponentialKernel(), LinearKernel(; c=exp(x[1]))), | ||
rand(1); | ||
dims=[2, 2], | ||
) | ||
types = [ColVecs{Float64,Matrix{Float64}}, RowVecs{Float64,Matrix{Float64}}] | ||
test_interface_ad_perf(2.1, StableRNG(123456), types) do c | ||
KernelIndependentSum(SqExponentialKernel(), LinearKernel(; c=c)) | ||
end | ||
test_params(KernelIndependentSum(k1, k2), (k1, k2)) | ||
|
||
@testset "single kernel" begin | ||
kernel = KernelIndependentSum(k1) | ||
@test length(kernel) == 1 | ||
|
||
@testset "eval" begin | ||
for (x, y) in (((v1,), (v2,)), ([v1], [v2])) | ||
val = k1(x[1], y[1]) | ||
|
||
@test kernel(x, y) == val | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems too generic to be defined and exported from KernelFunctions. Is it not part of TensorCore or some other lightweight interface package? We would also a non-Unicode alias, as for other keyword arguments and functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that, but
⊕
is not part of TensorCore or as far as I know any other lightweight package (https://juliahub.com/ui/Search?q=%E2%8A%95&type=symbols). It is a help constructor for the newKernelTensorSum
/KernelIndependentSum
, so the non-Unicode function is already available.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestions wellcome on how to improve this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no non-Unicode alternative similar to
+
,*
, ortensor
yet as far as I can tell?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! I see, you're right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolve?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This problem is not fixed yet, is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But since
TensorCore.jl
does not define⊕
, what should we do? Here are the packages that use⊕
.Kronecker.jl
is one, but I guess we do not want to add this as a dependency, only to re-use the symbol.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make a PR to TensorCore. I think the operator should not be owned by KernelFunctions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JuliaMath/TensorCore.jl#10