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

[WIP] Equivalence tests #6

Merged
merged 6 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[deps]
AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
119 changes: 119 additions & 0 deletions test/equivalences.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
@testset "equivalences" begin
rng, N = MersenneTwister(654321), 20
x = rand(rng, N) * 10
y = sin.(x) + 0.9 * cos.(x * 1.6) + 0.4 * rand(rng, N)

z = copy(x) # Set inducing inputs == training inputs

# Create a kernel from parameters k
kernel(k) = softplus(k[1]) * (SqExponentialKernel() ∘ ScaleTransform(softplus(k[2])))
k_init = [0.2, 0.6] # initial kernel parameters

lik_noise = 0.1 # The (fixed) Gaussian likelihood noise
jitter = 1e-5

## FIRST - define the models
# GPR - Exact GP regression
struct GPRModel
k # kernel parameters
end
@Flux.functor GPRModel

function (m::GPRModel)(x)
f = GP(kernel(m.k))
fx = f(x, lik_noise)
return fx
end

# # SGPR - Sparse GP regression (Titsias 2009)
# struct SGPRModel
# k # kernel parameters
# z # inducing points
# end
# @Flux.functor SGPRModel (k,) # Don't train the inducing inputs

# function (m::SGPRModel)(x)
# f = GP(kernel(m.k))
# fx = f(x, lik_noise)
# fz = f(m.z, lik_noise)
# return fx, fz
# end

# SVGP - Sparse variational GP regression (Hensman 2014)
struct SVGPModel
k # kernel parameters
z # inducing points
m # variational mean
A # variational covariance sqrt (Σ = A'A)
end
@Flux.functor SVGPModel (k, m, A,) # Don't train the inducing inputs

function (m::SVGPModel)(x)
f = GP(kernel(m.k))
q = MvNormal(m.m, m.A'm.A)
fx = f(x, lik_noise)
fz = f(m.z, jitter)
return fx, fz, q
end

## SECOND - create the models and associated training losses
gpr = GPRModel(copy(k_init))
function GPR_loss(x, y)
fx = gpr(x)
return -logpdf(fx, y)
end

# sgpr = SGPRModel(copy(k_init), copy(z))
# function SGPR_loss(x, y)
# fx, fz = sgpr(x)
# return -AbstractGPs.elbo(fx, y, fz)
# end

m, A = rand(rng, N), rand(rng, N, N)/2 # initialise the variational parameters
svgp = SVGPModel(copy(k_init), copy(z), m, A)
function SVGP_loss(x, y)
fx, fz, q = svgp(x)
return -SparseGPs.elbo(fx, y, fz, q)
end

## THIRD - train the models
data = [(x, y)]
opt = ADAM(0.01)

svgp_ps = Flux.params(svgp)
delete!(svgp_ps, svgp.k) # Don't train the kernel parameters

# Flux.train!((x, y) -> GPR_loss(x, y), Flux.params(gpr), ncycle(data, 3000), opt)
# Flux.train!((x, y) -> SGPR_loss(x, y), Flux.params(sgpr), ncycle(data, 3000), opt)
Flux.train!((x, y) -> SVGP_loss(x, y), svgp_ps, ncycle(data, 9000), opt)

## FOURTH - construct the posteriors
function posterior(m::GPRModel, x, y)
f = GP(kernel(m.k))
fx = f(x, lik_noise)
return AbstractGPs.posterior(fx, y)
end

# function posterior(m::SGPRModel, x, y)
# f = GP(kernel(m.k))
# fx = f(x, lik_noise)
# fz = f(m.z)
# return AbstractGPs.approx_posterior(VFE(), fx, y, fz)
# end

function posterior(m::SVGPModel)
f = GP(kernel(m.k))
fz = f(m.z, jitter)
q = MvNormal(m.m, m.A'm.A)
return SparseGPs.approx_posterior(SVGP(), fz, q)
end
gpr_post = posterior(gpr, x, y)
# sgpr_post = posterior(sgpr, x, y)
svgp_post = posterior(svgp)

## FIFTH - test equivalences
@test all(isapprox.(mean(gpr_post, x), mean(svgp_post, x), atol=1e-3))
@test all(isapprox.(cov(gpr_post, x), cov(svgp_post, x), atol=1e-3))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current state of this is:

Testing equivalence of the exact and sparse mean and cov works (with a fairly large tolerance of 1e-3) when only optimising the sparse variational posterior. Optimising kernel parameters at the same time does not give consistent enough behaviour - possibly need to use a different optimiser?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before investigating this further, how about one step simpler - set z=x and then we can determine analytically what we need to set q to for it to be exactly equivalent to GPR (then it should be equal within a very tight tolerance at most ~1e-8 or so in double precision).


end

22 changes: 22 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Random
using Test
using SparseGPs
using Flux
using IterTools
using AbstractGPs
using Distributions

const GROUP = get(ENV, "GROUP", "All")
const PKGDIR = dirname(dirname(pathof(SparseGPs)))

include("test_utils.jl")

@testset "SparseGPs" begin
include("svgp.jl")
println(" ")
@info "Ran svgp tests"

include("equivalences.jl")
println(" ")
@info "Ran equivalences tests"
end
4 changes: 4 additions & 0 deletions test/svgp.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@testset "svgp" begin
x = 4
@test x == 4
end
Empty file added test/test_utils.jl
Empty file.