Skip to content

Commit b428774

Browse files
authored
Refactoring in (almost) state-less form (#106)
* Formatting and more * Stateless updates * Small clean up * Renamed AbstractGPs.jl * Updates on the likelihood * Compilable version * Removed joinpath and others * More changes * More changes * more changes duh * gp classification passing! * Last updates * Formatting * Modify multiclass * Moar changes * Online example working * Actually fixed online GP * Fixing the sampling * Last tutorial working! * More fixes * Passing examples for regression * Fix hyperparameters update online * Fixed inference issues * Bayesian SVM test passing * Fixed the prior means * Lots of likelihoods passing tests! * Fixes Gaussian tests * More fixes * Fixed multiclass * Fixes softmax * All the fixes in da house * Make them int * Fix ambiguity * Add some formatting * Better typing
1 parent 4edc232 commit b428774

File tree

132 files changed

+2594
-2965
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+2594
-2965
lines changed

Project.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
name = "AugmentedGaussianProcesses"
22
uuid = "38eea1fd-7d7d-5162-9d08-f89d0f2e271e"
33
authors = ["Theo Galy-Fajou <theo.galyfajou@gmail.com>"]
4-
version = "0.10.5"
4+
version = "0.11.0"
55

66
[deps]
77
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
8-
AdvancedHMC = "0bf59076-c3b1-5ca4-86bd-e02cd72cde3d"
98
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
109
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1110
FastGaussQuadrature = "442a2c76-b920-505d-bb47-c5924d526838"
12-
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
1311
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1412
InducingPoints = "b4bd816d-b975-4295-ac05-5f2992945579"
1513
KernelFunctions = "ec8451be-7e33-11e9-00cf-bbf324bd1392"
@@ -28,11 +26,9 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
2826

2927
[compat]
3028
AbstractMCMC = "2, 3"
31-
AdvancedHMC = "0.2.13, 0.3"
3229
ChainRulesCore = "0.9, 1"
3330
Distributions = "0.21.5, 0.22, 0.23, 0.24, 0.25"
3431
FastGaussQuadrature = "0.4"
35-
Flux = "0.10, 0.11, 0.12"
3632
ForwardDiff = "0.10"
3733
InducingPoints = "0.2"
3834
KernelFunctions = "0.8, 0.9, 0.10"

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ A complete documentation is available [in the docs](https://theogf.github.io/Aug
7272
```julia
7373
using AugmentedGaussianProcesses;
7474
using KernelFunctions
75-
model = SVGP(X_train, Y_train, SqExponentialKernel(), LogisticLikelihood(),AnalyticSVI(100), 64)
76-
train!(model, 100)
77-
Y_predic = predict_y(model, X_test) #For getting the label directly
78-
Y_predic_prob, Y_predic_prob_var = proba_y(model,X_test) #For getting the likelihood (and likelihood uncertainty) of predicting class 1
75+
model = SVGP(SqExponentialKernel(), LogisticLikelihood(), AnalyticSVI(100), 64)
76+
train!(model, X_train, Y_train, 100)
77+
Y_predic = predict_y(model, X_test) # For getting the label directly
78+
Y_predic_prob, Y_predic_prob_var = proba_y(model, X_test) # For getting the likelihood (and likelihood uncertainty) of predicting class 1
7979
```
8080

8181
Both [documentation](https://theogf.github.io/AugmentedGaussianProcesses.jl/stable/) and [examples/tutorials](https://nbviewer.jupyter.org/github/theogf/AugmentedGaussianProcesses.jl/tree/master/examples/) are available.

docs/examples/gpclassification.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ data = CSV.read(data.body, DataFrame)
1515
data.Class[data.Class .== 2] .= -1
1616
data = Matrix(data)
1717
X = data[:, 1:2]
18-
Y = data[:, end];
18+
Y = Int.(data[:, end]);
1919

2020
# ### We create a function to visualize the data
2121

@@ -28,21 +28,19 @@ plot_data(X, Y; size=(500, 500))
2828

2929
# ### Run sparse classification with increasing number of inducing points
3030
Ms = [4, 8, 16, 32, 64]
31-
models = Vector{AbstractGP}(undef, length(Ms) + 1)
31+
models = Vector{AbstractGPModel}(undef, length(Ms) + 1)
3232
kernel = SqExponentialKernel() ScaleTransform(1.0)
3333
for (i, num_inducing) in enumerate(Ms)
3434
@info "Training with $(num_inducing) points"
35-
m = SVGP(
36-
X,
37-
Y,
35+
global m = SVGP(
3836
kernel,
3937
LogisticLikelihood(),
4038
AnalyticVI(),
41-
num_inducing;
39+
inducingpoints(KmeansAlg(num_inducing), X);
4240
optimiser=false,
4341
Zoptimiser=false,
4442
)
45-
@time train!(m, 20)
43+
@time train!(m, X, Y, 20)
4644
models[i] = m
4745
end
4846
# ### Running the full model

docs/examples/gpevents.jl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# ### Loading necessary packages
66
using Plots
77
using AugmentedGaussianProcesses
8-
const AGP = AugmentedGaussianProcesses
98
using Distributions
109

1110
# ## Creating some random data
@@ -58,17 +57,6 @@ function plot_model(model, X, Y, title=nothing)
5857
lab="",
5958
linewidth=3.0,
6059
)
61-
if model isa SVGP # Plot the inducing points as well
62-
Plots.plot!(
63-
p,
64-
vec(model.f[1].Z),
65-
zeros(dim(model.f[1]));
66-
msize=2.0,
67-
color="black",
68-
t=:scatter,
69-
lab="",
70-
)
71-
end
7260
return p
7361
end;
7462

docs/examples/gpregression.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#
33
# ### Loading necessary packages
44
using AugmentedGaussianProcesses
5-
const AGP = AugmentedGaussianProcesses
65
using Distributions
76
using Plots
87

@@ -27,23 +26,21 @@ scatter(X, Y; lab="")
2726

2827
Ms = [4, 8, 16, 32, 64];
2928
# Create an empty array of GPs
30-
models = Vector{AbstractGP}(undef, length(Ms) + 1);
29+
models = Vector{AbstractGPModel}(undef, length(Ms) + 1);
3130
# Chose a kernel
3231
kernel = SqExponentialKernel();# + PeriodicKernel()
3332
# And Run sparse classification with an increasing number of inducing points
3433
for (index, num_inducing) in enumerate(Ms)
3534
@info "Training with $(num_inducing) points"
3635
m = SVGP(
37-
X,
38-
Y, # First arguments are the input and output
3936
kernel, # Kernel
4037
GaussianLikelihood(σ), # Likelihood used
4138
AnalyticVI(), # Inference usede to solve the problem
42-
num_inducing; # Number of inducing points used
39+
inducingpoints(KmeansAlg(num_inducing), X); # Inducing points initialized with kmeans
4340
optimiser=false, # Keep kernel parameters fixed
4441
Zoptimiser=false, # Keep inducing points locations fixed
4542
)
46-
@time train!(m, 100) # Train the model for 100 iterations
43+
@time train!(m, X, Y, 100) # Train the model for 100 iterations
4744
models[index] = m # Save the model in the array
4845
end
4946

@@ -123,7 +120,7 @@ Plots.plot(
123120
likelihoods = [
124121
StudentTLikelihood(3.0), LaplaceLikelihood(3.0), HeteroscedasticLikelihood(1.0)
125122
]
126-
ngmodels = Vector{AbstractGP}(undef, length(likelihoods) + 1)
123+
ngmodels = Vector{AbstractGPModel}(undef, length(likelihoods) + 1)
127124
for (i, l) in enumerate(likelihoods)
128125
@info "Training with the $(l)" # We need to use VGP
129126
m = VGP(

docs/examples/multiclassgp.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ end
3939
# ### And a function to plot the data
4040
function plot_data(X, Y, σ)
4141
p = Plots.plot(size(300, 500); lab="", title="sigma = ")
42-
ys = unique(Y)
4342
Plots.scatter!(eachcol(X)...; group=Y, msw=0.0, lab="")
4443
return p
4544
end
@@ -48,21 +47,21 @@ plot([plot_data(generate_mixture_data(σ)..., σ) for σ in σs]...)
4847

4948
# ## Model training
5049
# ### Run sparse multiclass classification with different level of noise
51-
models = Vector{AbstractGP}(undef, length(σs))
50+
models = Vector{AbstractGPModel}(undef, length(σs))
5251
kernel = SqExponentialKernel()
5352
num_inducing = 50
5453
for (i, σ) in enumerate(σs)
5554
@info "Training with data with noise "
55+
X, y = generate_mixture_data(σ)
5656
m = SVGP(
57-
generate_mixture_data(σ)...,
5857
kernel,
5958
LogisticSoftMaxLikelihood(n_class),
6059
AnalyticVI(),
61-
num_inducing;
60+
inducingpoints(KmeansAlg(num_inducing), X);
6261
optimiser=false,
6362
Zoptimiser=false,
6463
)
65-
@time train!(m, 20)
64+
@time train!(m, X, y, 20)
6665
models[i] = m
6766
end
6867

docs/examples/onlinegp.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ IP_alg = OIPS(0.8);
3535
model = OnlineSVGP(k, GaussianLikelihood(σ), AnalyticVI(), IP_alg; optimiser=false)
3636
anim = Animation()
3737
size_batch = 100
38-
for (i, (X_batch, y_batch)) in enumerate(eachbatch((X_train, y_train); obsdim=1, size=size_batch))
39-
train!(model, X_batch, y_batch; iterations=3)
40-
plot_model(model, X, X_test, X_train[1:(i * size_batch)], y_train[1:(i * size_batch)])
41-
frame(anim)
38+
let state = nothing
39+
for (i, (X_batch, y_batch)) in
40+
enumerate(eachbatch((X_train, y_train); obsdim=1, size=size_batch))
41+
_, state = train!(model, X_batch, y_batch, state; iterations=5)
42+
plot_model(
43+
model, X, X_test, X_train[1:(i * size_batch)], y_train[1:(i * size_batch)]
44+
)
45+
frame(anim)
46+
end
4247
end
4348
gif(anim; fps=4)
4449
# This works just as well with any likelihood! Just try it out!

docs/examples/sampling.jl

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,41 @@ using AugmentedGaussianProcesses
99
using Distributions
1010
using LinearAlgebra
1111

12-
# ### Loading the banana dataset from OpenML
12+
# ### Generating some random data
1313
kernel = SqExponentialKernel()
14-
x = range(0, 10, length=50)
14+
x = range(0, 10; length=50)
1515
K = kernelmatrix(kernel, x)
1616
f = rand(MvNormal(K + 1e-8I)) # Sample a random GP
1717
y = rand.(Bernoulli.(AGP.logistic.(f)))
18-
y_sign = sign.(y .- 0.5)
19-
18+
y_sign = Int.(sign.(y .- 0.5))
2019

2120
# ### We create a function to visualize the data
2221

23-
function plot_data(x, y; size=(300,500))
24-
Plots.scatter(x,
25-
y,
26-
alpha=0.2,
27-
markerstrokewidth=0.0,
28-
lab="",
29-
size=size
30-
)
22+
function plot_data(x, y; size=(300, 500))
23+
return Plots.scatter(x, y; alpha=0.2, markerstrokewidth=0.0, lab="", size=size)
3124
end
32-
plot_data(x, y; size = (500, 500))
25+
plot_data(x, y; size=(500, 500))
3326

3427
# ### Run the variational gaussian process approximation
3528
@info "Running full model"
36-
mfull = VGP(x, y_sign,
37-
kernel,
38-
LogisticLikelihood(),
39-
AnalyticVI(),
40-
optimiser = false
41-
)
29+
mfull = VGP(x, y_sign, kernel, LogisticLikelihood(), AnalyticVI(); optimiser=false)
4230
@time train!(mfull, 5)
4331

4432
# ### We can also create a sampling based model
4533
@info "Sampling from model"
46-
mmcmc = MCGP(x, y,
47-
kernel,
48-
LogisticLikelihood(),
49-
GibbsSampling(),
50-
optimiser = false
51-
)
34+
mmcmc = MCGP(x, y, kernel, LogisticLikelihood(), GibbsSampling(); optimiser=false)
5235
m = mmcmc
5336
@time samples = sample(mmcmc, 1000)
5437

5538
# ### We can now visualize the results of both models
5639

5740
# ### We first plot the latent function f (truth, the VI estimate, the samples)
58-
p1 = plot(x, f, label="true f")
59-
plot!(x, samples, label="", color=:black, alpha=0.02, lab="")
60-
plot!(x, mean(mfull[1]), ribbon=sqrt.(var(mfull[1])), label="VI")
41+
p1 = plot(x, f; label="true f")
42+
plot!(x, samples; label="", color=:black, alpha=0.02, lab="")
43+
plot!(x, mean(mfull[1]); ribbon=sqrt.(var(mfull[1])), label="VI")
6144
# ### And we can also plot the predictions vs the data
62-
p2 = plot_data(x, y; size=(600,400))
45+
p2 = plot_data(x, y; size=(600, 400))
6346
μ_vi, σ_vi = proba_y(mfull, x)
6447
plot!(x, μ_vi; ribbon=σ_vi, label="VI")
6548
μ_mcmc, σ_mcmc = proba_y(mmcmc, x)
66-
plot!(x, μ_mcmc; ribbon=σ_mcmc, label="MCMC")
49+
plot!(x, μ_mcmc; ribbon=σ_mcmc, label="MCMC")

docs/make.jl

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,24 @@ end
2828

2929
# Make the docs
3030

31-
makedocs(modules = [AugmentedGaussianProcesses],
32-
format = Documenter.Writers.HTMLWriter.HTML(
33-
assets = ["assets/icon.ico"],
34-
analytics = "UA-129106538-2",
35-
),
36-
sitename= "AugmentedGaussianProcesses",
37-
authors = "Théo Galy-Fajou",
38-
pages = [
39-
"Home" => "index.md",
40-
"Background" => "background.md",
41-
"User Guide" => "userguide.md",
42-
"Kernels" => "kernel.md",
43-
"Examples" => joinpath.("examples", filter(x -> endswith(x, ".md"), readdir(MD_OUTPUT))),
44-
"Julia GP Packages" => "comparison.md",
45-
"API" => "api.md"
46-
]
47-
)
31+
makedocs(;
32+
modules=[AugmentedGaussianProcesses],
33+
format=Documenter.Writers.HTMLWriter.HTML(;
34+
assets=["assets/icon.ico"], analytics="UA-129106538-2"
35+
),
36+
sitename="AugmentedGaussianProcesses",
37+
authors="Théo Galy-Fajou",
38+
pages=[
39+
"Home" => "index.md",
40+
"Background" => "background.md",
41+
"User Guide" => "userguide.md",
42+
"Kernels" => "kernel.md",
43+
"Examples" =>
44+
joinpath.("examples", filter(x -> endswith(x, ".md"), readdir(MD_OUTPUT))),
45+
"Julia GP Packages" => "comparison.md",
46+
"API" => "api.md",
47+
],
48+
)
4849

4950
# Deploy the docs
5051

docs/src/examples/gpclassification.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Run sparse classification with increasing number of inducing points
2020

2121
```@example gpclassification
2222
Ms = [4, 8, 16, 32, 64]
23-
models = Vector{AbstractGP}(undef, length(Ms) + 1)
23+
models = Vector{AbstractGPModel}(undef, length(Ms) + 1)
2424
kernel = transform(SqExponentialKernel(), 1.0)
2525
for (i, num_inducing) in enumerate(Ms)
2626
@info "Training with $(num_inducing) points"

0 commit comments

Comments
 (0)