-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
250 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
function coreVIdiag(logp::Function, μ₀::AbstractArray{T, 1}, Σ₀diag::AbstractArray{T, 1}; gradlogp = gradlogp, seed = seed, S = S, test_every = test_every, optimiser = optimiser, iterations = iterations, numerical_verification = numerical_verification, Stest = Stest, show_every = show_every) where T | ||
|
||
D = length(μ₀) | ||
|
||
#---------------------------------------------------- | ||
# generate latent variables | ||
#---------------------------------------------------- | ||
|
||
Ztrain = generatelatentZ(S = S, D = D, seed = seed) | ||
|
||
|
||
#---------------------------------------------------- | ||
# Auxiliar function for handling parameters | ||
#---------------------------------------------------- | ||
|
||
function unpack(param) | ||
|
||
@assert(length(param) == D+D) | ||
|
||
local μ = param[1:D] | ||
|
||
local Cdiag = reshape(param[D+1:D+D], D) | ||
|
||
return μ, Cdiag | ||
|
||
end | ||
|
||
|
||
#---------------------------------------------------- | ||
# Objective and gradient functions for Optim.optimize | ||
#---------------------------------------------------- | ||
|
||
function minauxiliary(param) | ||
|
||
local μ, Cdiag = unpack(param) | ||
|
||
local ℓ = elbo(μ, Cdiag, Ztrain) | ||
|
||
update!(trackELBO; newelbo = ℓ, μ = μ, C = Cdiag) | ||
|
||
return -1.0 * ℓ # Optim.optimise is minimising | ||
|
||
end | ||
|
||
|
||
function minauxiliary_grad(param) | ||
|
||
local μ, Cdiag = unpack(param) | ||
|
||
return -1.0 * elbo_grad(μ, Cdiag, Ztrain) # Optim.optimise is minimising | ||
|
||
end | ||
|
||
|
||
#---------------------------------------------------- | ||
# Functions for covariance and covariance root | ||
#---------------------------------------------------- | ||
|
||
|
||
function getcov(Cdiag) | ||
|
||
Diagonal(Cdiag.^2) | ||
|
||
end | ||
|
||
|
||
function getcovroot(Cdiag) | ||
|
||
return Cdiag | ||
|
||
end | ||
|
||
|
||
#---------------------------------------------------- | ||
# Approximate evidence lower bound and its gradient | ||
#---------------------------------------------------- | ||
|
||
function elbo(μ, Cdiag, Z) | ||
|
||
local aux = z -> logp(μ .+ Cdiag.*z) | ||
|
||
Transducers.foldxt(+, Map(aux), Z) / length(Z) + GaussianVariationalInference.entropy(Cdiag) | ||
|
||
end | ||
|
||
|
||
function partial_elbo_grad(μ, Cdiag, z) | ||
|
||
local g = gradlogp(μ .+ Cdiag.*z) | ||
|
||
[g; vec(g.*z)] | ||
|
||
end | ||
|
||
|
||
function elbo_grad(μ, Cdiag, Z) | ||
|
||
local aux = z -> partial_elbo_grad(μ, Cdiag, z) | ||
|
||
local gradμCdiag = Transducers.foldxt(+, Map(aux), Z) / length(Z) | ||
|
||
# entropy contribution to covariance | ||
|
||
gradμCdiag[D+1:end] .+= vec(1.0 ./ Cdiag) | ||
|
||
return gradμCdiag | ||
|
||
end | ||
|
||
|
||
# Package Optim requires that function for gradient has following signature | ||
|
||
gradhelper(storage, param) = copyto!(storage, minauxiliary_grad(param)) | ||
|
||
|
||
#---------------------------------------------------- | ||
# Numerically verify gradient | ||
#---------------------------------------------------- | ||
|
||
numerical_verification ? verifygradient(μ₀, Σ₀diag, elbo, minauxiliary_grad, unpack, Ztrain) : nothing | ||
|
||
|
||
#---------------------------------------------------- | ||
# Define callback function called at each iteration | ||
#---------------------------------------------------- | ||
|
||
# We want to keep track of the best variational | ||
# parameters encountered during the optimisation of | ||
# the elbo. Unfortunately, the otherwise superb | ||
# package Optim.jl does not provide a consistent way | ||
# accross different optimisers to do this. | ||
|
||
|
||
trackELBO = RecordELBOProgress(; μ = zeros(D), C = zeros(D), | ||
Stest = Stest, | ||
show_every = show_every, | ||
test_every = test_every, | ||
elbo = elbo, seed = seed) | ||
|
||
|
||
|
||
#---------------------------------------------------- | ||
# Call optimiser to minimise *negative* elbo | ||
#---------------------------------------------------- | ||
|
||
options = Optim.Options(extended_trace = false, store_trace = false, show_every = 1, show_trace = false, iterations = iterations, g_tol = 1e-6, callback = trackELBO) | ||
|
||
result = Optim.optimize(minauxiliary, gradhelper, [μ₀; vec(sqrt.(Σ₀diag))], optimiser, options) | ||
|
||
μopt, Copt = unpack(result.minimizer) | ||
|
||
|
||
#---------------------------------------------------- | ||
# Return results | ||
#---------------------------------------------------- | ||
|
||
Σopt = getcov(Copt) | ||
|
||
return MvNormal(μopt, Σopt), elbo(μopt, Copt, Ztrain), Copt | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.