-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentropy.jl
177 lines (170 loc) · 5.93 KB
/
entropy.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import Gen
using Statistics: mean
using Gen:
assess,
complement,
project,
generate,
GenerativeFunction,
get_choices,
get_score,
get_selected,
Selection,
simulate
function logmeanexp(arr::AbstractArray{T}) where {T <: Real}
return Gen.logsumexp(arr) - log(length(arr))
end
"""Entropy lower bound using custom proposal."""
function entropy_lower_bound(
model::GenerativeFunction,
model_args::Tuple,
proposal::GenerativeFunction,
proposal_args::Tuple,
targets::Selection,
N::Integer,
M::Integer;
model_traces::Vector{Gen.Trace}=Gen.Trace[])
wi_list = []
for i=1:N
# Sample observations from model.
tr_p = isempty(model_traces) ? simulate(model, model_args) : model_traces[i]
observations = get_selected(get_choices(tr_p), targets)
wj_list = []
for j=1:M
# Sample latents from proposal and compute score.
tr_q = simulate(proposal, (proposal_args..., observations,))
log_q = get_score(tr_q)
# Compute score of latents + observations under model.
choices = merge(observations, get_choices(tr_q))
_, log_p = generate(model, model_args, choices)
# Compute importance weight.
log_w = log_p - log_q
push!(wj_list, log_w)
end
wj_avg = mean(wj_list)
push!(wi_list, wj_avg)
end
return mean(wi_list)
end
"""Entropy upper bound using custom proposal."""
function entropy_upper_bound(
model::GenerativeFunction,
model_args::Tuple,
proposal::GenerativeFunction,
proposal_args::Tuple,
targets::Selection,
N::Integer,
M::Integer;
model_traces::Vector{Gen.Trace}=Gen.Trace[])
@assert(M == 1)
wi_list = []
for i=1:N
# Sample latents + observations from model and compute score.
tr_p = isempty(model_traces) ? simulate(model, model_args) : model_traces[i]
wj_list = []
for j=1:M
# Compute score of latents + observations under model.
log_p = get_score(tr_p)
# Compute score of latents under proposal.
observations = get_selected(get_choices(tr_p), targets)
latents = get_selected(get_choices(tr_p), complement(targets))
_, log_q = generate(proposal, (proposal_args..., observations,), latents)
# Compute importance weight.
log_w = log_q - log_p
push!(wj_list, log_w)
# TODO: MCMC step iterating the latents in tr_p
# if j < M
# tr_p = metropolis_hastings(tr_p, complement(targets);
# check=true, observations=observations)
# end
end
wj_avg = -mean(wj_list)
push!(wi_list, wj_avg)
end
return mean(wi_list)
end
"""Entropy lower bound using default proposal."""
function entropy_lower_bound(
model::GenerativeFunction,
model_args::Tuple,
targets::Selection,
N::Integer,
M::Integer,
K::Integer;
model_traces::Vector{Gen.Trace}=Gen.Trace[],
return_weights::Bool=false)
wi_list = []
for i=1:N
# Sample observations from model.
tr_p = isempty(model_traces) ? simulate(model, model_args) : model_traces[i]
observations = get_selected(get_choices(tr_p), targets)
wj_list = []
for j=1:M
wk_list::Vector{Float64} = []
for k=1:K
# Sample latents (particle k) from default proposal
# and retrieve weight w_k,
# which is p(observations | latents)
tr_q, log_w = generate(model, model_args, observations)
push!(wk_list, log_w)
end
# Compute overall log importance weight,
# which is log [1/K sum w_k]
wk_avg = logmeanexp(wk_list)
!isinf(wk_avg) || throw("Invalid proposal (infinite log weight)")
push!(wj_list, wk_avg)
end
wj_avg = mean(wj_list)
push!(wi_list, wj_avg)
end
return return_weights ? wi_list : mean(wi_list)
end
"""Entropy upper bound using default proposal."""
function entropy_upper_bound(
model::GenerativeFunction,
model_args::Tuple,
targets::Selection,
N::Integer,
M::Integer,
K::Integer;
model_traces::Vector{Gen.Trace}=Gen.Trace[],
return_weights::Bool=false)
@assert(M == 1)
wi_list = []
for i=1:N
# Sample latents + observations from model.
tr_p = isempty(model_traces) ? simulate(model, model_args) : model_traces[i]
observations = get_selected(get_choices(tr_p), targets)
wj_list = []
for j=1:M
tr_pk = tr_p
wk_list::Vector{Float64} = []
# Retrieve weight of exact posterior sample (particle 1)
# using default proposal, which is
# p(observations | latents)
log_w = Gen.project(tr_pk, targets)
push!(wk_list, log_w)
for k=2:K
# Sample latents (particle k) from default proposal
# and retrieve weight w_k,
# which is p(observations | latents)
tr_pk, log_w = generate(model, model_args, observations)
push!(wk_list, log_w)
end
# Compute overall log importance weight, which is
# log [1 / (1/K sum w_k)]
# = - log [1/K sum w_k]
wk_avg = -logmeanexp(wk_list)
@assert !isinf(wk_avg)
push!(wj_list, wk_avg)
# TODO: MCMC step iterating the latents in tr_p
# if j < M
# tr_p = metropolis_hastings(tr_p, complement(targets);
# check=true, observations=observations)
# end
end
wj_avg = -mean(wj_list)
push!(wi_list, wj_avg)
end
return return_weights ? wi_list : mean(wi_list)
end