-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First-class support of SKCE for probability vectors and boolean targe…
…ts (#85)
- Loading branch information
Showing
7 changed files
with
191 additions
and
54 deletions.
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 |
---|---|---|
@@ -1,51 +1,15 @@ | ||
@testset "kernels.jl" begin | ||
# alternative implementation of white kernel | ||
struct WhiteKernel2 <: Kernel end | ||
(::WhiteKernel2)(x, y) = x == y | ||
kernel = TVExponentialKernel() | ||
|
||
# alternative implementation TensorProductKernel | ||
struct TensorProduct2{K1<:Kernel,K2<:Kernel} <: Kernel | ||
kernel1::K1 | ||
kernel2::K2 | ||
end | ||
function (kernel::TensorProduct2)((x1, x2), (y1, y2)) | ||
return kernel.kernel1(x1, y1) * kernel.kernel2(x2, y2) | ||
end | ||
# traits | ||
@test KernelFunctions.metric(kernel) === TotalVariation() | ||
|
||
@testset "TVExponentialKernel" begin | ||
kernel = TVExponentialKernel() | ||
# simple evaluation | ||
x, y = rand(10), rand(10) | ||
@test kernel(x, y) == exp(-totalvariation(x, y)) | ||
|
||
# traits | ||
@test KernelFunctions.metric(kernel) === TotalVariation() | ||
|
||
# simple evaluation | ||
x, y = rand(10), rand(10) | ||
@test kernel(x, y) == exp(-totalvariation(x, y)) | ||
|
||
# transformations | ||
@test (kernel ∘ ScaleTransform(0.1))(x, y) == exp(-0.1 * totalvariation(x, y)) | ||
ard = rand(10) | ||
@test (kernel ∘ ARDTransform(ard))(x, y) == exp(-totalvariation(ard .* x, ard .* y)) | ||
end | ||
|
||
@testset "unsafe_skce_eval" begin | ||
kernel = SqExponentialKernel() | ||
kernel1 = kernel ⊗ WhiteKernel() | ||
kernel2 = kernel ⊗ WhiteKernel2() | ||
kernel3 = TensorProduct2(kernel, WhiteKernel()) | ||
|
||
x1, x2 = rand(10), rand(1:10) | ||
|
||
@test CalibrationErrors.unsafe_skce_eval(kernel1, x1, x2, x1, x2) ≈ | ||
CalibrationErrors.unsafe_skce_eval(kernel2, x1, x2, x1, x2) | ||
@test CalibrationErrors.unsafe_skce_eval(kernel1, x1, x2, x1, x2) ≈ | ||
CalibrationErrors.unsafe_skce_eval(kernel3, x1, x2, x1, x2) | ||
|
||
y1, y2 = rand(10), rand(1:10) | ||
|
||
@test CalibrationErrors.unsafe_skce_eval(kernel1, x1, x2, y1, y2) ≈ | ||
CalibrationErrors.unsafe_skce_eval(kernel2, x1, x2, y1, y2) | ||
@test CalibrationErrors.unsafe_skce_eval(kernel1, x1, x2, y1, y2) ≈ | ||
CalibrationErrors.unsafe_skce_eval(kernel3, x1, x2, y1, y2) | ||
end | ||
# transformations | ||
@test (kernel ∘ ScaleTransform(0.1))(x, y) == exp(-0.1 * totalvariation(x, y)) | ||
ard = rand(10) | ||
@test (kernel ∘ ARDTransform(ard))(x, y) == exp(-totalvariation(ard .* x, ard .* y)) | ||
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
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,60 @@ | ||
@testset "generic.jl" begin | ||
# alternative implementation of white kernel | ||
struct WhiteKernel2 <: Kernel end | ||
(::WhiteKernel2)(x, y) = x == y | ||
|
||
# alternative implementation TensorProductKernel | ||
struct TensorProduct2{K1<:Kernel,K2<:Kernel} <: Kernel | ||
kernel1::K1 | ||
kernel2::K2 | ||
end | ||
function (kernel::TensorProduct2)((x1, x2), (y1, y2)) | ||
return kernel.kernel1(x1, y1) * kernel.kernel2(x2, y2) | ||
end | ||
|
||
@testset "binary classification" begin | ||
# probabilities and boolean targets | ||
p, p̃ = rand(2) | ||
y, ỹ = rand(Bool, 2) | ||
scale = rand() | ||
kernel = SqExponentialKernel() ∘ ScaleTransform(scale) | ||
val = unsafe_skce_eval(kernel ⊗ WhiteKernel(), p, y, p̃, ỹ) | ||
@test unsafe_skce_eval(kernel ⊗ WhiteKernel2(), p, y, p̃, ỹ) ≈ val | ||
@test unsafe_skce_eval(TensorProduct2(kernel, WhiteKernel()), p, y, p̃, ỹ) ≈ val | ||
@test unsafe_skce_eval(TensorProduct2(kernel, WhiteKernel2()), p, y, p̃, ỹ) ≈ val | ||
|
||
# corresponding values and kernel for full categorical distribution | ||
pfull = [p, 1 - p] | ||
yint = y ? 1 : 2 | ||
p̃full = [p̃, 1 - p̃] | ||
ỹint = ỹ ? 1 : 2 | ||
kernelfull = SqExponentialKernel() ∘ ScaleTransform(scale / sqrt(2)) | ||
|
||
@test unsafe_skce_eval(kernelfull ⊗ WhiteKernel(), pfull, yint, p̃full, ỹint) ≈ val | ||
@test unsafe_skce_eval(kernelfull ⊗ WhiteKernel2(), pfull, yint, p̃full, ỹint) ≈ | ||
val | ||
@test unsafe_skce_eval( | ||
TensorProduct2(kernelfull, WhiteKernel()), pfull, yint, p̃full, ỹint | ||
) ≈ val | ||
@test unsafe_skce_eval( | ||
TensorProduct2(kernelfull, WhiteKernel2()), pfull, yint, p̃full, ỹint | ||
) ≈ val | ||
end | ||
|
||
@testset "multi-class classification" begin | ||
n = 10 | ||
p = rand(n) | ||
p ./= sum(p) | ||
y = rand(1:n) | ||
p̃ = rand(n) | ||
p̃ ./= sum(p̃) | ||
ỹ = rand(1:n) | ||
|
||
kernel = SqExponentialKernel() ∘ ScaleTransform(rand()) | ||
val = unsafe_skce_eval(kernel ⊗ WhiteKernel(), p, y, p̃, ỹ) | ||
|
||
@test unsafe_skce_eval(kernel ⊗ WhiteKernel2(), p, y, p̃, ỹ) ≈ val | ||
@test unsafe_skce_eval(TensorProduct2(kernel, WhiteKernel()), p, y, p̃, ỹ) ≈ val | ||
@test unsafe_skce_eval(TensorProduct2(kernel, WhiteKernel2()), p, y, p̃, ỹ) ≈ val | ||
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
a140a02
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.
@JuliaRegistrator register
a140a02
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.
Registration pull request created: JuliaRegistries/General/35591
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: