Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions src/elbo.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
function maximize_elbo(rng, logp, dists, ndraws, executor)
μ = dists[1].μ
N = length(μ)
u = Random.randn!(rng, similar(μ, N, ndraws))
EE = Core.Compiler.return_type(
elbo_and_samples, Tuple{typeof(rng),typeof(logp),eltype(dists),Int}
elbo_and_samples!, Tuple{typeof(rng),typeof(u),typeof(logp),eltype(dists),Int}
)
estimates = similar(dists, EE)
isempty(estimates) && return 0, estimates
Folds.map!(estimates, dists, executor) do dist
Copy link
Member

Choose a reason for hiding this comment

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

Since u is being overwritten here, I think we cannot use Transducers, or different threads could be writing to it at the same time. This will probably just need to be made a simple map

return elbo_and_samples(rng, logp, dist, ndraws)
return elbo_and_samples!(rng, u, logp, dist, ndraws)
end
_, iteration_opt = _findmax(estimates |> Transducers.Map(est -> est.value))
return iteration_opt, estimates
end

function elbo_and_samples(rng, logp, dist, ndraws)
ϕ, logqϕ = rand_and_logpdf(rng, dist, ndraws)
function elbo_and_samples!(rng, u, logp, dist, ndraws)
Copy link
Member

Choose a reason for hiding this comment

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

draws will need to be removed from ELBOEstimate as well.

ϕ, logqϕ = rand_and_logpdf!(rng, u, dist, ndraws)
logpϕ = similar(logqϕ)
logpϕ .= logp.(eachcol(ϕ))
logr = logpϕ - logqϕ
Expand Down
4 changes: 2 additions & 2 deletions src/mvnormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ function fit_mvnormals(θs, ∇logpθs; kwargs...)
end

# faster than computing `logpdf` and `rand` independently
function rand_and_logpdf(rng, dist::Distributions.MvNormal, ndraws)
function rand_and_logpdf!(rng, u, dist::Distributions.MvNormal, ndraws)
μ = dist.μ
Σ = dist.Σ
N = length(μ)

# draw points
u = Random.randn!(rng, similar(μ, N, ndraws))
Random.randn!(rng, u)
unormsq = vec(sum(abs2, u; dims=1))
x = PDMats.unwhiten!(u, Σ, u)
x .+= μ
Expand Down