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

Update example to new syntax #138

Merged
merged 3 commits into from
Apr 14, 2021
Merged
Changes from all 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
31 changes: 11 additions & 20 deletions examples/regression_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ struct GPLoglikelihood{X,Y}
end

function (ℓ::GPLoglikelihood)(params)
kernel = ScaledKernel(
transform(Matern52Kernel(), ScaleTransform(softplus(params[1]))),
softplus(params[2]),
)
kernel = softplus(params[1]) * (Matern52Kernel() ∘ ScaleTransform(softplus(params[2])))
f = GP(kernel)
fx = f(ℓ.x, 0.1)
return logpdf(fx, ℓ.y)
Expand Down Expand Up @@ -194,7 +191,7 @@ histogram(
xlabel="sample",
ylabel="counts",
layout=2,
title=["inverse length scale" "variance"],
title=["variance" "inverse length scale"],
legend=false,
)
vline!(mean_samples'; linewidth=2)
Expand All @@ -210,9 +207,7 @@ struct GPPosterior{X,Y}
end

function (g::GPPosterior)(p)
kernel = ScaledKernel(
transform(Matern52Kernel(), ScaleTransform(softplus(p[1]))), softplus(p[2])
)
kernel = softplus(p[1]) * (Matern52Kernel() ∘ ScaleTransform(softplus(p[2])))
f = GP(kernel)
return posterior(f(g.x, 0.1), g.y)
end
Expand Down Expand Up @@ -287,7 +282,7 @@ histogram(
xlabel="sample",
ylabel="counts",
layout=2,
title=["inverse length scale" "variance"],
title=["variance" "inverse length scale"],
legend=false,
)
vline!(mean_samples'; linewidth=2)
Expand Down Expand Up @@ -347,7 +342,7 @@ histogram(
xlabel="sample",
ylabel="counts",
layout=2,
title=["inverse length scale" "variance"],
title=["variance" "inverse length scale"],
)
vline!(mean_samples'; layout=2, labels="mean")

Expand Down Expand Up @@ -404,10 +399,7 @@ struct NegativeELBO{X,Y}
end

function (g::NegativeELBO)(params)
kernel = ScaledKernel(
transform(Matern52Kernel(), ScaleTransform(softplus(params[1]))),
softplus(params[2]),
)
kernel = softplus(params[1]) * (Matern52Kernel() ∘ ScaleTransform(softplus(params[2])))
f = GP(kernel)
fx = f(g.x, 0.1)
return -elbo(fx, g.y, f(logistic.(params[3:end])))
Expand All @@ -424,22 +416,21 @@ opt = optimize(NegativeELBO(x_train, y_train), x0, LBFGS())

opt.minimizer

# The optimized value of the inverse lengthscale is
# The optimized value of the variance is

softplus(opt.minimizer[1])

# and of the variance is
# and of the inverse lengthscale is

softplus(opt.minimizer[2])

# We compute the log-likelihood of the test data for the resulting approximate
# posterior. We can observe that there is a significant improvement over the
# log-likelihood with the default kernel parameters of value 1.

opt_kernel = ScaledKernel(
transform(Matern52Kernel(), ScaleTransform(softplus(opt.minimizer[1]))),
softplus(opt.minimizer[2]),
)
opt_kernel =
softplus(opt.minimizer[1]) *
(Matern52Kernel() ∘ ScaleTransform(softplus(opt.minimizer[2])))
opt_f = GP(opt_kernel)
opt_fx = opt_f(x_train, 0.1)
ap = approx_posterior(VFE(), opt_fx, y_train, opt_f(logistic.(opt.minimizer[3:end])))
Expand Down