Skip to content
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

reactivate AD tests: FiniteGP #314

Merged
merged 14 commits into from
Apr 7, 2022
146 changes: 58 additions & 88 deletions test/finite_gp_projection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,57 +102,29 @@ end
@test mean(abs.(Σ′ - cov(f))) < 1e-2
end
end
# @testset "rand (gradients)" begin
# rng, N, S = MersenneTwister(123456), 10, 3
# x = collect(range(-3.0, stop=3.0, length=N))
# Σy = 1e-12

# # Check that the gradient w.r.t. the samples is correct (single-sample).
# adjoint_test(
# x->rand(MersenneTwister(123456), GP(sin, EQ(), GPC())(x, Σy)),
# randn(rng, N),
# x;
# atol=1e-9, rtol=1e-9,
# )

# # Check that the gradient w.r.t. the samples is correct (multisample).
# adjoint_test(
# x->rand(MersenneTwister(123456), GP(sin, EQ(), GPC())(x, Σy), S),
# randn(rng, N, S),
# x;
# atol=1e-9, rtol=1e-9,
# )
# end
# @testset "tr_Cf_invΣy" begin
# N = 11
# x = collect(range(-3.0, 3.0; length=N))
# @testset "dense" begin
# rng = MersenneTwister(123456)
# A = randn(rng, N, N - 2)
# adjoint_test(
# (x, A)->begin
# f = GP(sin, EQ(), GPC())
# Σy = _to_psd(A)
# C = cholesky(Σy)
# return tr_Cf_invΣy(f(x, Σy), Σy, C)
# end,
# randn(rng), x, A,
# )
# end
# @testset "Diagonal" begin
# rng = MersenneTwister(123456)
# a = 0.01 .* randn(rng, N)
# adjoint_test(
# (x, a)->begin
# f = GP(sin, EQ(), GPC())
# Σy = Diagonal(exp.(a .+ 1))
# C = cholesky(Σy)
# return tr_Cf_invΣy(f(x, Σy), Σy, C)
# end,
# randn(rng), x, a,
# )
# end
# end
@testset "rand (gradients)" begin
rng, N, S = MersenneTwister(123456), 10, 3
x = collect(range(-3.0; stop=3.0, length=N))
Σy = 1e-12

# Check that the gradient w.r.t. the samples is correct (single-sample).
adjoint_test(
x -> rand(MersenneTwister(123456), GP(sin, SqExponentialKernel())(x, Σy)),
randn(rng, N),
x;
atol=1e-9,
rtol=1e-9,
)

# Check that the gradient w.r.t. the samples is correct (multisample).
adjoint_test(
x -> rand(MersenneTwister(123456), GP(sin, SqExponentialKernel())(x, Σy), S),
randn(rng, N, S),
x;
atol=1e-9,
rtol=1e-9,
)
end
@testset "logpdf / loglikelihood" begin
rng = MersenneTwister(123456)
N = 10
Expand All @@ -177,43 +149,41 @@ end
@test logpdf(y, Ŷ) ≈ [logpdf(y, Ŷ[:, n]) for n in 1:S]
@test loglikelihood(y, Ŷ) == sum(logpdf(y, Ŷ))

# # Check gradient of logpdf at mean is zero for `f`.
# adjoint_test(ŷ->logpdf(fx, ŷ), 1, ones(size(ŷ)))
# lp, back = Zygote.pullback(ŷ->logpdf(fx, ŷ), ones(size(ŷ)))
# @test back(randn(rng))[1] == zeros(size(ŷ))

# # Check that gradient of logpdf at mean is zero for `y`.
# adjoint_test(ŷ->logpdf(y, ŷ), 1, ones(size(ŷ)))
# lp, back = Zygote.pullback(ŷ->logpdf(y, ŷ), ones(size(ŷ)))
# @test back(randn(rng))[1] == zeros(size(ŷ))

# # Check that gradient w.r.t. inputs is approximately correct for `f`.
# x, l̄ = randn(rng, N), randn(rng)
# adjoint_test(
# x->logpdf(f(x, 1e-3), ones(size(x))),
# l̄, collect(x);
# atol=1e-8, rtol=1e-8,
# )
# adjoint_test(
# x->sum(logpdf(f(x, 1e-3), ones(size(Ŷ)))),
# l̄, collect(x);
# atol=1e-8, rtol=1e-8,
# )

# # Check that the gradient w.r.t. the noise is approximately correct for `f`.
# σ_ = randn(rng)
# adjoint_test((σ_, ŷ)->logpdf(f(x, exp(σ_)), ŷ), l̄, σ_, ŷ)
# adjoint_test((σ_, Ŷ)->sum(logpdf(f(x, exp(σ_)), Ŷ)), l̄, σ_, Ŷ)

# # Check that the gradient w.r.t. a scaling of the GP works.
# adjoint_test(
# α->logpdf((α * f)(x, 1e-1), ŷ), l̄, randn(rng);
# atol=1e-8, rtol=1e-8,
# )
# adjoint_test(
# α->sum(logpdf((α * f)(x, 1e-1), Ŷ)), l̄, randn(rng);
# atol=1e-8, rtol=1e-8,
# )
# Check gradient of logpdf at mean is zero for `f`.
adjoint_test(ŷ -> logpdf(fx, ŷ), 1, ones(size(ŷ)))
lp, back = Zygote.pullback(ŷ -> logpdf(fx, ŷ), ones(size(ŷ)))
@test back(randn(rng))[1] == zeros(size(ŷ))

# Check that gradient of logpdf at mean is zero for `y`.
adjoint_test(ŷ -> logpdf(y, ŷ), 1, ones(size(ŷ)))
lp, back = Zygote.pullback(ŷ -> logpdf(y, ŷ), ones(size(ŷ)))
@test back(randn(rng))[1] == zeros(size(ŷ))

# Check that gradient w.r.t. inputs is approximately correct for `f`.
x, l̄ = randn(rng, N), randn(rng)
adjoint_test(
x -> logpdf(f(x, 1e-3), ones(size(x))), l̄, collect(x); atol=1e-8, rtol=1e-8
)
adjoint_test(
x -> sum(logpdf(f(x, 1e-3), ones(size(Ŷ)))),
l̄,
collect(x);
atol=1e-8,
rtol=1e-8,
)

# Check that the gradient w.r.t. the noise is approximately correct for `f`.
σ_ = randn(rng)
adjoint_test((σ_, ŷ) -> logpdf(f(x, exp(σ_)), ŷ), l̄, σ_, ŷ)
adjoint_test((σ_, Ŷ) -> sum(logpdf(f(x, exp(σ_)), Ŷ)), l̄, σ_, Ŷ)

# Check that the gradient w.r.t. a scaling of the GP works.
st-- marked this conversation as resolved.
Show resolved Hide resolved
adjoint_test(
α -> logpdf((α * f)(x, 1e-1), ŷ), l̄, randn(rng); atol=1e-8, rtol=1e-8
)
adjoint_test(
α -> sum(logpdf((α * f)(x, 1e-1), Ŷ)), l̄, randn(rng); atol=1e-8, rtol=1e-8
)
end
@testset "Type Stability - $T" for T in [Float64, Float32]
rng = MersenneTwister(123456)
Expand Down
32 changes: 31 additions & 1 deletion test/sparse_approximations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,41 @@ end
σ² = 1.234
Σy_Diagonal = Diagonal(Fill(σ², N))
Σy_ScalMat = ScalMat(N, σ²)
Σy_dense = _to_psd(randn(N, N - 2))
f = GP(SqExponentialKernel())

for Σy in (Σy_Diagonal, Σy_ScalMat)
for Σy in (Σy_Diagonal, Σy_ScalMat, Σy_dense)
fx = f(x, Σy)
Cf = cov(f, x)
@test AbstractGPs.tr_Cf_invΣy(fx, Σy) ≈ tr(Cf / Matrix(Σy))
end

@testset "adjoint_test" begin
N = 11
x = collect(range(-3.0, 3.0; length=N))
@testset "dense" begin
rng = MersenneTwister(123456)
A = randn(rng, N, N - 2)
adjoint_test(
(x, A) -> begin
f = GP(sin, SqExponentialKernel())
Σy = _to_psd(A)
C = cholesky(Σy)
return AbstractGPs.tr_Cf_invΣy(f(x, Σy), Σy, C)
end, randn(rng), x, A
st-- marked this conversation as resolved.
Show resolved Hide resolved
)
end
@testset "Diagonal" begin
rng = MersenneTwister(123456)
a = 0.01 .* randn(rng, N)
adjoint_test(
(x, a) -> begin
f = GP(sin, SqExponentialKernel())
Σy = Diagonal(exp.(a .+ 1))
C = cholesky(Σy)
return AbstractGPs.tr_Cf_invΣy(f(x, Σy), Σy, C)
end, randn(rng), x, a
st-- marked this conversation as resolved.
Show resolved Hide resolved
)
end
end
end