You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using Distributions, Turing
# LDA example -- copied over from# https://github.com/TuringLang/Turing.jl/issues/668#issuecomment-1153124051function_make_data(D, K, V, N, α, η)
β =Matrix{Float64}(undef, V, K)
for k in1:K
β[:,k] .=rand(Dirichlet(η))
end
θ =Matrix{Float64}(undef, K, D)
z =Vector{Int}(undef, D * N)
w =Vector{Int}(undef, D * N)
doc =Vector{Int}(undef, D * N)
i =0for d in1:D
θ[:,d] .=rand(Dirichlet(α))
for n in1:N
i +=1
z[i] =rand(Categorical(θ[:, d]))
w[i] =rand(Categorical(β[:, z[i]]))
doc[i] = d
endendreturn (D=D, K=K, V=V, N=N, α=α, η=η, z=z, w=w, doc=doc, θ=θ, β=β)
end
data =let D =2, K =2, V =160, N =290_make_data(D, K, V, N, ones(K), ones(V))
end# LDA with vectorization and manual log-density accumulation@modelfunctionLatentDirichletAllocationVectorizedCollapsedMannual(
D, K, V, α, η, w, doc
)
β ~filldist(Dirichlet(η), K)
θ ~filldist(Dirichlet(α), D)
log_product =log.(β * θ)
Turing.@addlogprob!sum(log_product[CartesianIndex.(w, doc)])
# Above is equivalent to below#product = β * θ#dist = [Categorical(product[:,i]) for i in 1:D] #w ~ arraydist([dist[doc[i]] for i in 1:length(doc)])end
model =LatentDirichletAllocationVectorizedCollapsedMannual(
data.D, data.K, data.V, data.α, data.η, data.w, data.doc,
)
ctx = Turing.DefaultContext()
vi = Turing.SimpleVarInfo(model)
vi_linked = Turing.link(vi, model)
As discussed with @torfjelde on slack:
the final line yields
Run on Julia v1.10.0, with
The text was updated successfully, but these errors were encountered: