Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion committed Jan 11, 2021
1 parent 99e1bf1 commit fce0c64
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/deprecated.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# TODO: remove tests when removed
@deprecate MahalanobisKernel(; P::AbstractMatrix{<:Real}) transform(SqExponentialKernel(), LinearTransform(cholesky(P).U))
@deprecate MahalanobisKernel(; P::AbstractMatrix{<:Real}) transform(SqExponentialKernel(), LinearTransform(sqrt(2) .* cholesky(P).U))

# TODO: remove keyword argument `maha` when removed
@deprecate PiecewisePolynomialKernel{V}(A::AbstractMatrix{<:Real}) where V transform(PiecewisePolynomialKernel{V}(size(A, 1)), LinearTransform(cholesky(A).U))
38 changes: 3 additions & 35 deletions test/basekernels/maha.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@testset "maha" begin
rng = MersenneTwister(123456)
x = 2 * rand(rng)
D_in = 3
v1 = rand(rng, D_in)
v2 = rand(rng, D_in)
Expand All @@ -9,40 +8,10 @@
P = Matrix(Cholesky(U, 'U', 0))
@assert isposdef(P)

k = MahalanobisKernel(; P=P)

@test kappa(k, x) == exp(-x)
k = @test_deprecated MahalanobisKernel(; P=P)
@test k isa TransformedKernel{SqExponentialKernel,<:LinearTransform}
@test k.transform.A sqrt(2) .* U
@test k(v1, v2) exp(-sqmahalanobis(v1, v2, P))
@test kappa(ExponentialKernel(), x) == kappa(k, x)
@test repr(k) == "Mahalanobis Kernel (size(P) = $(size(P)))"

M1, M2 = rand(rng, 3, 2), rand(rng, 3, 2)

function FiniteDifferences.to_vec(dist::SqMahalanobis)
return vec(dist.qmat), x -> SqMahalanobis(reshape(x, size(dist.qmat)...))
end
a = rand()

function test_mahakernel(U::UpperTriangular, v1::AbstractVector, v2::AbstractVector)
return MahalanobisKernel(; P=Array(U' * U))(v1, v2)
end

@test all(
FiniteDifferences.j′vp(FDM, test_mahakernel, a, U, v1, v2)[1] .≈
UpperTriangular(Zygote.pullback(test_mahakernel, U, v1, v2)[2](a)[1]),
)

function test_sqmaha(U::UpperTriangular, v1::AbstractVector, v2::AbstractVector)
return SqMahalanobis(Array(U' * U))(v1, v2)
end

@test all(
FiniteDifferences.j′vp(FDM, test_sqmaha, a, U, v1, v2)[1] .≈
UpperTriangular(Zygote.pullback(test_sqmaha, U, v1, v2)[2](a)[1]),
)

# test_ADs(U -> MahalanobisKernel(P=Array(U' * U)), U, ADs=[:Zygote])
@test_broken "Nothing passes (problem with Mahalanobis distance in Distances)"

# Standardised tests.
@testset "ColVecs" begin
Expand All @@ -57,5 +26,4 @@
x2 = RowVecs(randn(2, D_in))
TestUtils.test_interface(k, x0, x1, x2)
end
test_params(k, (P,))
end
19 changes: 12 additions & 7 deletions test/basekernels/piecewisepolynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@
v2 = rand(D)
maha = Matrix{Float64}(I, D, D)
v = 3
k = PiecewisePolynomialKernel{v}(maha)

k2 = PiecewisePolynomialKernel(; v=v, maha=maha)
k = PiecewisePolynomialKernel(; v=v, d=D)
k2 = PiecewisePolynomialKernel{v}(D)
k3 = @test_deprecated PiecewisePolynomialKernel{v}(maha)
k4 = @test_deprecated PiecewisePolynomialKernel(; v=v, maha=maha)

@test k2(v1, v2) k(v1, v2) atol = 1e-5
@test k2(v1, v2) == k(v1, v2)
@test k3(v1, v2) k(v1, v2)
@test k4(v1, v2) k(v1, v2)

@test_throws ErrorException PiecewisePolynomialKernel{4}(maha)
@test_throws ErrorException PiecewisePolynomialKernel{4}(D)
@test_throws ErrorException PiecewisePolynomialKernel{v}(-1)

@test repr(k) == "Piecewise Polynomial Kernel (v = $(v), size(maha) = $(size(maha)))"
@test repr(k) == "Piecewise Polynomial Kernel (v = $(v), ⌊d/2⌋ = $(div(D, 2)))"

# Standardised tests.
TestUtils.test_interface(k, ColVecs{Float64}; dim_in=2)
TestUtils.test_interface(k, RowVecs{Float64}; dim_in=2)
# test_ADs(maha-> PiecewisePolynomialKernel(v=2, maha = maha), maha)
@test_broken "Nothing passes (problem with Mahalanobis distance in Distances)"
test_ADs(() -> PiecewisePolynomialKernel{v}(D))

test_params(k, (maha,))
test_params(k, ())
end

0 comments on commit fce0c64

Please sign in to comment.