-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathabstractbeliefpropagationcache.jl
More file actions
274 lines (238 loc) · 8.65 KB
/
abstractbeliefpropagationcache.jl
File metadata and controls
274 lines (238 loc) · 8.65 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using Graphs: Graphs
using Adapt
abstract type AbstractBeliefPropagationCache{V} <: AbstractGraph{V} end
#Interface
messages(bp_cache::AbstractBeliefPropagationCache) = not_implemented()
default_messages() = Dictionary{NamedEdge, Union{ITensor, Vector{ITensor}}}()
function rescale_messages!(
bp_cache::AbstractBeliefPropagationCache, edges::Vector{<:AbstractEdge}; kwargs...
)
return not_implemented()
end
function rescale_vertices!(
bp_cache::AbstractBeliefPropagationCache, vertices::Vector; kwargs...
)
return not_implemented()
end
function vertex_scalar(bp_cache::AbstractBeliefPropagationCache, vertex)
incoming_ms = incoming_messages(bp_cache, vertex)
state = bp_factors(bp_cache, vertex)
contract_list = [state; incoming_ms]
sequence = contraction_sequence(contract_list; alg = "optimal")
return contract(contract_list; sequence)[]
end
function edge_scalar(
bp_cache::AbstractBeliefPropagationCache, edge::AbstractEdge; kwargs...
)
return not_implemented()
end
network(bp_cache::AbstractBeliefPropagationCache) = not_implemented()
#Forward onto the network
for f in [
:(Graphs.vertices),
:(Graphs.edges),
:(Graphs.is_tree),
:(NamedGraphs.GraphsExtensions.boundary_edges),
:(bp_factors),
:(default_bp_maxiter),
:(ITensorNetworks.linkinds),
:(ITensorNetworks.underlying_graph),
:(ITensors.datatype),
:(ITensors.scalartype),
:(ITensorNetworks.setindex_preserve_graph!),
:(ITensorNetworks.maxlinkdim),
:(default_message),
]
@eval begin
function $f(bp_cache::AbstractBeliefPropagationCache, args...; kwargs...)
return $f(network(bp_cache), args...; kwargs...)
end
end
end
#Functions derived from the interface
function deletemessage!(bp_cache::AbstractBeliefPropagationCache, e::AbstractEdge)
ms = messages(bp_cache)
delete!(ms, e)
return bp_cache
end
function setmessage!(bp_cache::AbstractBeliefPropagationCache, e::AbstractEdge, message::Union{ITensor, Vector{<:ITensor}})
ms = messages(bp_cache)
set!(ms, e, message)
return bp_cache
end
function message(bp_cache::AbstractBeliefPropagationCache, edge::AbstractEdge; kwargs...)
ms = messages(bp_cache)
return get(() -> default_message(bp_cache, edge; kwargs...), ms, edge)
end
function messages(bp_cache::AbstractBeliefPropagationCache, edges::Vector{<:AbstractEdge})
isempty(edges) && return ITensor[]
return reduce(vcat, [message(bp_cache, e) for e in edges])
end
function setmessages!(bp_cache::AbstractBeliefPropagationCache, edges, messages)
for (e, m) in zip(edges)
setmessage!(bp_cache, e, m)
end
return
end
function deletemessages!(
bp_cache::AbstractBeliefPropagationCache, edges::Vector{<:AbstractEdge} = edges(bp_cache)
)
for e in edges
deletemessage!(bp_cache, e)
end
return bp_cache
end
function vertex_scalars(
bp_cache::AbstractBeliefPropagationCache, vertices = collect(Graphs.vertices(bp_cache)); kwargs...
)
return map(v -> vertex_scalar(bp_cache, v; kwargs...), vertices)
end
function edge_scalars(
bp_cache::AbstractBeliefPropagationCache, edges = Graphs.edges(bp_cache); kwargs...
)
return map(e -> edge_scalar(bp_cache, e; kwargs...), edges)
end
function scalar_factors_quotient(bp_cache::AbstractBeliefPropagationCache)
return vertex_scalars(bp_cache), edge_scalars(bp_cache)
end
function incoming_messages(
bp_cache::AbstractBeliefPropagationCache, vertices::Vector{<:Any}; ignore_edges = []
)
b_edges = NamedGraphs.GraphsExtensions.boundary_edges(bp_cache, vertices; dir = :in)
b_edges = !isempty(ignore_edges) ? setdiff(b_edges, ignore_edges) : b_edges
return messages(bp_cache, b_edges)
end
function incoming_messages(bp_cache::AbstractBeliefPropagationCache, vertex; kwargs...)
return incoming_messages(bp_cache, [vertex]; kwargs...)
end
function updated_message(
alg::Algorithm"contract", bp_cache::AbstractBeliefPropagationCache, edge::NamedEdge
)
vertex = src(edge)
incoming_ms = incoming_messages(
bp_cache, vertex; ignore_edges = typeof(edge)[reverse(edge)]
)
state = bp_factors(bp_cache, vertex)
contract_list = ITensor[incoming_ms; state]
sequence = contraction_sequence(contract_list; alg = alg.kwargs.sequence_alg)
updated_message = contract(contract_list; sequence)
if alg.kwargs.enforce_hermiticity
updated_message = make_hermitian(updated_message)
end
if alg.kwargs.normalize
message_norm = LinearAlgebra.norm(updated_message)
if !iszero(message_norm)
updated_message /= message_norm
end
end
return updated_message
end
function updated_message(
bp_cache::AbstractBeliefPropagationCache,
edge::NamedEdge;
alg = default_message_update_alg(bp_cache),
kwargs...,
)
return updated_message(set_default_kwargs(Algorithm(alg; kwargs...)), bp_cache, edge)
end
"""
Do a sequential update of the message tensors on `edges`
"""
function update_iteration!(
alg::Algorithm"bp",
bpc::AbstractBeliefPropagationCache,
edges::Vector;
(update_diff!) = nothing,
)
for e in edges
prev_message = !isnothing(update_diff!) ? message(bpc, e) : nothing
update_message!(alg.kwargs.message_update_alg, bpc, e)
if !isnothing(update_diff!)
update_diff![] += message_diff(message(bpc, e), prev_message)
end
end
return bpc
end
"""
More generic interface for update, with default params
"""
function update(alg::Algorithm"bp", bpc::AbstractBeliefPropagationCache)
compute_error = !isnothing(alg.kwargs.tolerance)
if isnothing(alg.kwargs.maxiter)
error("You need to specify a number of iterations for BP!")
end
bpc = copy(bpc)
for i in 1:alg.kwargs.maxiter
diff = compute_error ? Ref(0.0) : nothing
update_iteration!(alg, bpc, alg.kwargs.edge_sequence; (update_diff!) = diff)
if compute_error && (diff.x / length(alg.kwargs.edge_sequence)) <= alg.kwargs.tolerance
if alg.kwargs.verbose
println("BP converged to desired precision after $i iterations.")
end
break
end
end
return bpc
end
function update(bpc::AbstractBeliefPropagationCache; alg = default_update_alg(bpc), kwargs...)
return update(set_default_kwargs(Algorithm(alg; kwargs...), bpc), bpc)
end
#Adapt interface for changing device
function map_messages(f, bp_cache::AbstractBeliefPropagationCache, es = keys(messages(bp_cache)))
bp_cache = copy(bp_cache)
for e in es
setmessage!(bp_cache, e, f(message(bp_cache, e)))
end
return bp_cache
end
function map_factors(f, bp_cache::AbstractBeliefPropagationCache, vs = vertices(bp_cache))
bp_cache = copy(bp_cache)
for v in vs
setindex_preserve_graph!(bp_cache, f(network(bp_cache)[v]), v)
end
return bp_cache
end
function adapt_messages(to, bp_cache::AbstractBeliefPropagationCache, args...)
return map_messages(adapt(to), bp_cache, args...)
end
function adapt_factors(to, bp_cache::AbstractBeliefPropagationCache, args...)
return map_factors(adapt(to), bp_cache, args...)
end
function Adapt.adapt_structure(to, bpc::AbstractBeliefPropagationCache)
bpc = adapt_messages(to, bpc)
bpc = adapt_factors(to, bpc)
return bpc
end
function freenergy(bp_cache::AbstractBeliefPropagationCache)
numerator_terms, denominator_terms = scalar_factors_quotient(bp_cache)
if any(t -> real(t) < 0, numerator_terms)
numerator_terms = complex.(numerator_terms)
end
if any(t -> real(t) < 0, denominator_terms)
denominator_terms = complex.(denominator_terms)
end
any(iszero, denominator_terms) && return -Inf
return sum(log.(numerator_terms)) - sum(log.((denominator_terms)))
end
function partitionfunction(bp_cache::AbstractBeliefPropagationCache)
return exp(freenergy(bp_cache))
end
function rescale_messages!(bp_cache::AbstractBeliefPropagationCache, edge::AbstractEdge)
return rescale_messages!(bp_cache, [edge])
end
function rescale_messages!(bp_cache::AbstractBeliefPropagationCache)
return rescale_messages!(bp_cache, edges(bp_cache))
end
function rescale_vertices!(bpc::AbstractBeliefPropagationCache; kwargs...)
return rescale_vertices!(bpc, collect(vertices(bpc)); kwargs...)
end
function rescale!(bpc::AbstractBeliefPropagationCache, args...; kwargs...)
rescale_messages!(bpc)
rescale_vertices!(bpc, args...; kwargs...)
return bpc
end
function rescale(bpc::AbstractBeliefPropagationCache, args...; kwargs...)
bpc = copy(bpc)
rescale!(bpc, args...; kwargs...)
return bpc
end