-
Notifications
You must be signed in to change notification settings - Fork 7
/
message.py
218 lines (159 loc) · 5 KB
/
message.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
from __future__ import division, print_function
import torch
from torch.autograd import Variable
import numpy as np
import pdb
import sys
np.set_printoptions(threshold=np.nan)
class FactorGraph:
"""
Class defining the Factor Graph
"""
def __init__(self, T, batch_size, gpu=True):
self.vars = []
self.factors = []
self.var2idx = {}
self.var2factor = {}
self.T = T
self.batch_size = batch_size
self.gpu = gpu
def __iter__():
for var in self.vars:
yield(var)
def iterFactors(self):
for factor in self.factors:
yield(factor)
def addFactor(self, kind, var1, var2):
# var1 and var2 are Var objects
self.factors.append(Factor(kind, var1, var2, self.batch_size, self.gpu))
if var1 not in self.var2factor:
self.var2factor[var1] = []
if var2 not in self.var2factor:
self.var2factor[var2] = []
self.var2factor[var1].append(len(self.factors)-1)
self.var2factor[var2].append(len(self.factors)-1)
# self.factors.append(Factor(kind, var1, var2))
def addVariable(self, tag, label, timestep):
newVar = Var(tag, label, timestep, self.batch_size, self.gpu)
self.vars.append(newVar)
def getVarsByTimestep(self, t, tagSize):
var_list = []
for idx in range(tagSize):
var_list.append(self.getVarByTimestepnTag(t, idx))
return var_list
def getVarsByFactor(self, factor):
return factor.var1, factor.var2
def getVarsByTag(self, tagIdx):
var_list = []
for t in range(self.T):
var_list.append(self.getVarByTimestepnTag(t, tagIdx))
return var_list
def getVarByTimestepnTag(self, t, tagIdx):
idx = tagIdx * self.T + t
return self.vars[idx]
def getFactorByVars(self, var1, var2=None):
if var1==var2:
sys.exit("Error => var1 and var2 are equal!")
if var2!=None:
idx = list(set(self.var2factor[var1]).intersection(self.var2factor[var2]))[0]
return self.factors[idx]
else:
neighbor_factor_idxs = self.var2factor[var1]
neighbor_factors = []
for idx in neighbor_factor_idxs:
neighbor_factors.append(self.factors[idx])
return neighbor_factors
class Factor:
"""
Class for Factors
"""
def __init__(self, kind, var1, var2, batch_size=1, gpu=True):
# Kind -> pair or trans or lstm
self.kind = kind
self.var1 = var1
self.var2 = var2
if self.kind=="lstm":
self.belief = None
else:
self.belief = Variable(torch.zeros((batch_size, var1.tag.size(), var2.tag.size())), requires_grad=True)
if gpu:
self.belief = self.belief.cuda()
def __hash__(self):
return hash((self.var1, self.var2, self.kind))
def updateBelief(self, value):
self.belief = value
def getOtherVar(self, var):
if self.var1==var:
return self.var2
else:
return self.var1
class Var:
"""
Class for Variables
"""
def __init__(self, tag, label, timestep, batch_size=1, gpu=True):
self.tag = tag
self.label = label
self.timestep = timestep
self.belief = Variable(torch.zeros(batch_size, tag.size()), requires_grad=True)
if gpu:
self.belief = self.belief.cuda()
def __hash__(self):
return hash((self.tag.name, self.timestep))
def updateValue(self, value):
self.belief = value
class Messages:
def __init__(self, graph, batch_size=1, test=False):
self.messages = {}
count = 0
self.batch_size = batch_size
for var in graph.vars:
self.messages[var] = {}
for factor in graph.factors:
self.messages[factor] = {}
# Initialize messages to the uniform distribution
for var in graph.vars:
for factor in graph.getFactorByVars(var):
self.messages[factor][var] = {}
self.addMessage(factor, var, np.full((batch_size, var.tag.size()), np.log(1./var.tag.size())))
# self.addMessage(factor, var, np.repeat(1./var.tag.size(), var.tag.size()))
count += 1
for factor in graph.factors:
# Don't add messages from variable to LSTM unary factor
if factor.kind!="lstm":
for var in graph.getVarsByFactor(factor):
self.messages[var][factor] = {}
self.addMessage(var, factor, np.full((batch_size, var.tag.size()), np.log(1./var.tag.size())))
# self.addMessage(var, factor, np.repeat(1./var.tag.size(), var.tag.size()))
count += 1
def __iter__(self):
for var in self.messages.keys():
for msg in self.messages[var].values():
yield(msg)
def __copy__(self, graph):
msgsCopy = Messages(graph)
for var in graph.vars:
for factor in graph.getFactorByVars(var):
msgsCopy.updateMessage(factor, var, self.getMessage(factor, var).value)
for factor in graph.factors:
for var in graph.getVarsByFactor(factor):
msgsCopy.updateMessage(var, factor, self.getMessage(var, factor).values)
return msgsCopy
def addMessage(self, frm, to, value):
self.messages[frm][to] = Message(frm, to, value)
def updateMessage(self, frm, to, value):
existing_msg = self.getMessage(frm, to)
existing_msg.updateValue(value)
def getMessage(self, frm, to):
return self.messages[frm][to]
class Message:
def __init__(self, frm, to, value):
"""
Message from variable to factor
or factor to variable
"""
self.frm = frm
self.to = to
self.value = value
def updateValue(self, value):
self.value = value