-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix_tree_theorem.py
212 lines (122 loc) · 5.97 KB
/
matrix_tree_theorem.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
import networkx as nx
import numpy as np
import math
import pandas as pd
from networkx.drawing.nx_agraph import graphviz_layout
import matplotlib
from time import gmtime, strftime
import scipy
margFeat=[]
def getAdjacency(theta):
adjacency=np.exp(theta)
#print "adjacency max "+str(np.amax(adjacency))
np.fill_diagonal(adjacency,0)
return adjacency
def getmLaplacian(adjacency,root_theta):
laplacian=-adjacency
for i in range (len(laplacian)):
laplacian[i,i]=sum(adjacency[:,i])[0,0]
z=np.zeros((root_theta.shape[1],root_theta.shape[1]))
np.fill_diagonal(z,np.exp(root_theta))
laplacian[0]=np.exp(root_theta)
return laplacian
def getMarginal(laplacian,adjacency,root_theta):
delta=np.zeros((len(laplacian),len(laplacian)))
np.fill_diagonal(delta,1)
inv_laplacian=np.linalg.inv(laplacian)
marg_p=np.zeros((len(laplacian),len(laplacian)))
for h in range (len(laplacian)):
for m in range (len(laplacian)):
#print str(h) + " " + str(m )
marg_p[h,m]=(1-delta[0,m])*adjacency[h,m]*inv_laplacian[m,m]- (1-delta[h,0])*adjacency[h,m]*inv_laplacian[m,h]
root_marg=np.zeros((1,len(laplacian)))
for m in range (len(laplacian)):
root_marg[0,m]=np.exp(root_theta[0,m])*inv_laplacian[m,0]
return marg_p,root_marg
def computeTheta(w,train_vec,node_docs,labels=""):
theta_list=(train_vec*w.T)
ptr=0;
theta_doc=[]
root_theta_doc=[]
theta_active_sum=[]
for doc in range (len(node_docs)):
theta_active_sum.append(0)
nodeDoc=node_docs[doc]
rootIndex=nodeDoc.mention.index("ROOT")
nodes=len(nodeDoc.mention)
thetas=np.asmatrix(np.zeros((nodes,nodes)))
root_thetas=np.asmatrix(np.zeros((1,nodes)))
for h in range(len(nodeDoc.mention)):
for m in range(len(nodeDoc.mention)):
if (h!=m and m!=rootIndex):
thetas[h,m]=theta_list[ptr]
if labels!="" and labels[ptr]=='1':
theta_active_sum[doc]+=theta_list[ptr]
ptr+=1
root_thetas=np.zeros((1,len(thetas)))
theta_doc.append(thetas)
root_theta_doc.append(root_thetas)
return theta_doc,root_theta_doc,theta_active_sum,theta_list
def computeMtx(train_vec,node_docs,theta_doc,root_theta_doc):
adjacency_doc=[]
laplacian_doc=[]
partitionLog=[]
marginal_doc=[]
root_marg_doc=[]
for doc in range (len(theta_doc)):
adjacency_doc.append(getAdjacency(theta_doc[doc]))
laplacian_doc.append(getmLaplacian(adjacency_doc[doc],root_theta_doc[doc]))
(sign, logdet) = np.linalg.slogdet(laplacian_doc[doc])
partitionLog.append(sign*logdet)
#partitionLog.append(np.log(np.linalg.det(laplacian_doc[doc])))
marg=getMarginal(laplacian_doc[doc],adjacency_doc[doc],root_theta_doc[doc])
marginal_doc.append(marg[0])
root_marg_doc.append(marg[1])
ptr=0;
margs=[]
for doc in range (len(marginal_doc)):
marginal=marginal_doc[doc]
root_marg=root_marg_doc[doc]
for h in range(len(marginal)):
for m in range(len(marginal)):
marg=marginal[h,m]
if (h!=m and m<len(marginal)-1):
margs.append(marg)
ptr+=1
margs=np.asarray(margs)
margs = scipy.sparse.csr_matrix(margs)
margFeat=margs.T.multiply(train_vec)
return adjacency_doc,laplacian_doc,partitionLog,marginal_doc,root_marg_doc,margFeat
def printTime(messageBefore):
print messageBefore+" : "+strftime("%Y-%m-%d %H:%M:%S")
def L(w,train_vec,node_docs,labels,C,featuresSum):
w=np.matrix(w)
theta=computeTheta(w,train_vec,node_docs,labels)
theta_doc=theta[0]
root_theta_doc=theta[1]
theta_active_sum=theta[2]
logSum=0
mtx=computeMtx(train_vec,node_docs,theta_doc,root_theta_doc)
adjacency_doc=mtx[0]
laplacian_doc=mtx[1]
partitionLog=mtx[2]
marginal_doc=mtx[3]
root_marg_doc=mtx[4]
global margFeat
margFeat=mtx[5]
for doc in range (len(theta_doc)):
logSum+=theta_active_sum[doc]-partitionLog[doc]
L=-C*logSum+0.5*math.pow(np.linalg.norm(w),2)
if logSum>0:
raise ValueError('--Log likelihood is positive --')
print "----------------------------------------------------------"
print "Objective: "+str(L)+ " Likelihood: " + str(logSum)
print "----------------------------------------------------------"
return L
def gradL(w,train_vec,node_docs,labels,C,featuresSum):
w=np.matrix(w)
sumMargFeat=np.zeros((1,train_vec.shape[1])).astype('double')
sumMargFeat=scipy.sparse.csr_matrix.sum(margFeat,axis=0)#np.sum(margFeat,axis=0)
dL=w-C*featuresSum+C*sumMargFeat
dL=np.squeeze(np.asarray(dL))
return dL