-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactor_graph.py
273 lines (239 loc) · 9.6 KB
/
factor_graph.py
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
import networkx as nx
import matplotlib.pyplot as plt
from random import choice
from typing import List
from node import Node, FactorNode
import numpy as np
class FactorGraph(nx.Graph):
INF = 1
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.var_nodes = []
self.factor_nodes = []
def add_var_nodes(self, nodes: List[Node]):
self.var_nodes = nodes
super().add_nodes_from(nodes)
def add_factor_nodes(self, nodes: List[Node]):
self.factor_nodes = nodes
super().add_nodes_from(nodes)
def _clear_messages(self):
for node in self.var_nodes:
node.reset_belief()
node.clear_messages()
def _init_messages(self):
for node in self.var_nodes:
for factor in node.get_neighbors():
node.messages_out[factor] = node.belief
def sum_product(self, node=None):
"""
This function runs the sum product algorithm
on the graph and sets the message for every nodes.
:param node: The root node in the graph
:return: None
"""
self._clear_messages()
if node is None:
node = choice(self.nodes)
elif node not in self.nodes:
raise IndexError("the requested node not found")
backward = list(nx.dfs_edges(self, source=node))
forward = reversed(backward)
for (v, u) in forward:
u.sum_product(v)
for (u, v) in backward:
u.sum_product(v)
for var in self.var_nodes:
var.update_belief()
'''
print(f"The beliefs :")
for var in self.var_nodes:
print(f"variable: {var.name} has prob: {var.belief}")
'''
def max_product(self, node=None):
self._clear_messages()
if node is None:
node = choice(self.nodes)
elif node not in self.nodes:
raise IndexError("the requested node not found")
backward = list(nx.dfs_edges(self, source=node))
forward = reversed(backward)
for (v, u) in forward:
u.max_product(v)
for (u, v) in backward:
u.max_product(v)
for var in self.var_nodes:
var.update_belief()
print(f"The beliefs :")
for var in self.var_nodes:
print(f"variable: {var.name} has prob: {var.belief}")
pass
def max_sum(self, node=None):
self._clear_messages()
if node is None:
node = choice(self.nodes)
elif node not in self.nodes:
raise IndexError("the requested node not found")
backward = list(nx.dfs_edges(self, source=node))
forward = reversed(backward)
for (v, u) in forward:
u.max_sum(v)
for (u, v) in backward:
u.max_sum(v)
for var in self.var_nodes:
var.update_log_belief()
print(f"The beliefs :")
for var in self.var_nodes:
print(f"variable: {var.name} has prob: {var.belief}")
pass
def loopy_sum_product(self, iterations=10, epsilon=1e-2, plot_errors=False):
self._clear_messages()
self._init_messages()
mu = []
errors = []
for var in self.var_nodes:
mu.append(np.full_like(var.belief, FactorGraph.INF))
for i in range(iterations):
print(f"iteration:{i}")
# updating factor to variable message
for factor in self.factor_nodes:
adj_variables = factor.get_neighbors()
for var in adj_variables:
factor.sum_product(var, normalize=True)
# updating variable to factor message
for var in self.var_nodes:
adj_factors = var.get_neighbors()
for factor in adj_factors:
var.sum_product(factor, normalize=True)
for var in self.var_nodes:
var.update_belief()
new_mu = [var.belief.copy() for var in self.var_nodes]
error = [np.linalg.norm(m - nm) for m, nm in zip(mu, new_mu)]
error = sum(error) / len(error)
errors.append(error)
if error < epsilon:
break
mu = [var.copy() for var in new_mu]
'''
print("The messages are as follows:")
for var in self.var_nodes:
for factor in var.messages_out:
print(f"{var.name} -> {factor.name}: {var.messages_out[factor]}")
for factor in var.messages_in:
print(f"{factor.name} -> {var.name}: {var.messages_in[factor]}")
print(f"The beliefs after iteration {i}:")
for var in self.var_nodes:
print(f"variable: {var.name} has prob: {var.belief}")
print()
'''
if plot_errors:
FactorGraph.plot_errors(errors, 'sum_product_error')
pass
def loopy_max_product(self, iterations=10, epsilon=1e-2, plot_errors=False):
self._clear_messages()
self._init_messages()
mu = []
for var in self.var_nodes:
mu.append(np.full_like(var.belief, FactorGraph.INF))
errors = []
for i in range(iterations):
# updating factor to variable message
for factor in self.factor_nodes:
adj_variables = factor.get_neighbors()
for var in adj_variables:
factor.max_product(var, normalize=True)
# updating variable to factor message
for var in self.var_nodes:
adj_factors = var.get_neighbors()
for factor in adj_factors:
var.max_product(factor, normalize=True)
for var in self.var_nodes:
var.update_belief()
new_mu = [var.belief.copy() for var in self.var_nodes]
error = [np.linalg.norm(m - nm) for m, nm in zip(mu, new_mu)]
error = sum(error) / len(error)
errors.append(error)
if error < epsilon:
break
mu = [var.copy() for var in new_mu]
if plot_errors:
FactorGraph.plot_errors(errors, 'max_product_error')
@staticmethod
def plot_errors(errors, fig_name="error"):
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(211)
ax.plot(errors)
ax.set_xlabel('Num of iterations')
ax.set_ylabel('convergence error')
# plt.ylim(0, 0.01)
# plt.show()
ax2 = fig.add_subplot(212)
ax2.plot(np.log(errors))
ax2.set_xlabel('Num of iterations')
ax2.set_ylabel('convergence error (log scale)')
plt.savefig(fig_name)
def loopy_max_sum(self, iterations=10, epsilon=1e-2, plot_errors=False):
self._clear_messages()
self._init_messages()
mu = []
errors = []
for var in self.var_nodes:
mu.append(np.full_like(var.belief, FactorGraph.INF))
for i in range(iterations):
# updating factor to variable message
for factor in self.factor_nodes:
adj_variables = factor.get_neighbors()
for var in adj_variables:
factor.max_sum(var, normalize=True)
# updating variable to factor message
for var in self.var_nodes:
adj_factors = var.get_neighbors()
for factor in adj_factors:
var.max_sum(factor, normalize=True)
for var in self.var_nodes:
var.update_log_belief()
new_mu = [var.belief.copy() for var in self.var_nodes]
error = [np.linalg.norm(m - nm) for m, nm in zip(mu, new_mu)]
error = sum(error) / len(error)
errors.append(error)
if error < epsilon:
break
mu = [var.copy() for var in new_mu]
if plot_errors:
FactorGraph.plot_errors(errors, 'max_sum_error')
pass
def draw_graph(self, num=None, pos=None):
# if position not provided get the positions of nodes from networkx spring_layout
if pos is None:
pos = nx.spring_layout(self, scale=10)
fig = plt.figure(num=num)
ax = fig.add_subplot(111)
# drawing the variable nodes of the graph which are of shape 'circle'
for node in self.var_nodes:
ax.annotate(node.name, xy=(pos[node][0], pos[node][1] + .0045))
plt.scatter(pos[node][0], pos[node][1], marker='o', s=800, facecolors='r', edgecolors='r')
# drawing the factor nodes of the graph which are of shape 'square'
for node in self.factor_nodes:
ax.annotate(node.name, xy=(pos[node][0], pos[node][1] + .0045))
# annotating the midpoint of the edge with the message provided as a list
x1, y1 = pos[node]
k = 0.5
ax.annotate(node.cpd,
xy=(x1, y1),
xytext=(x1 + k, y1 + k),
# arrowprops=dict(arrowstyle="->")
)
plt.scatter(pos[node][0], pos[node][1], marker='s', s=500, facecolors='r')
# drawing the edges
for (u, v) in self.edges.data(False):
x, y = zip(*[tuple(pos[u]), tuple(pos[v])])
plt.plot(x, y, 'r-', linewidth=2)
plt.axis('off')
return
def save_graph_fig(self, num=None, pos=None, fig_name="graph"):
self.draw_graph(num=num, pos=pos)
plt.savefig(fig_name)
pass
def get_beliefs(self):
return {node.name:node.belief for node in self.var_nodes}
def transform_junction(self):
raise NotImplementedError("WIP: not implemented yet")