-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernels.py
More file actions
213 lines (201 loc) · 7.78 KB
/
kernels.py
File metadata and controls
213 lines (201 loc) · 7.78 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
from re import S
import numpy as np
from scipy.linalg import expm
from scipy.sparse.linalg import cg
from graph import Graph
from tqdm import tqdm
class BhattKernel:
def __init__(self, t, num_bins, graphs=None, y=None, r_lambda=0, use_labels=False, label_pairs=None, calcWeights=False):
self.t = t
self.numT = len(t)
self.num_bins = num_bins
self.graphs = graphs
self.y = y
self.r_lambda = r_lambda
self.use_labels = use_labels
if self.use_labels and label_pairs is None:
raise Exception("No label pairs provided")
self.label_pairs = label_pairs
if not self.graphs is None:
if self.numT == 1:
self.binnedGraphs = [self.graphBinnedSingle(g) for g in self.graphs]
else:
self.binnedGraphs = [self.graphBinned(g) for g in self.graphs]
if not calcWeights:
self.weights = np.zeros(self.num_bins)
else:
self.weights, err = self.calculateWeights()
print("Train Set Average Absolute Error:", err)
def predictGraph(self, g):
if self.numT == 1:
return self.weights@np.sqrt(self.graphBinnedSingle(g))
else:
return self.weights@np.sqrt(self.graphBinned(g))
def graphBinnedSingle(self, g):
L = g.getLaplacian()
h = expm(-self.t[0]*L)
if self.use_labels:
bins = {}
for p in self.label_pairs:
bins[p] = [0 for _ in range(self.num_bins)]
it = np.nditer(h, flags=['multi_index'])
while not it.finished:
v = it[0]
x, y = g.vert_feats[min(it.multi_index)], g.vert_feats[max(it.multi_index)]
x, y = min(x,y), max(x,y)
if v >= 1:
bins[(x,y)][self.num_bins-1] += 1
else:
bins[(x,y)][int(v*self.num_bins)] += 1
it.iternext()
pi = []
for l in bins.values():
pi += l
return pi
else:
pi = [0 for _ in range(self.num_bins)]
for v in np.nditer(h):
if v >= 1:
pi[self.num_bins-1] += 1
else:
pi[int(v*self.num_bins)] += 1
return pi
def graphBinned(self, g):
L = g.getLaplacian()
# h = expm(-(self.t/g.vertN)*L)
w, v = np.linalg.eigh(L)
pi_t = []
for i, B in enumerate(self.t):
h = v@np.diag(np.exp(-B*w))@v.T
if self.use_labels:
bins = {}
for p in self.label_pairs:
bins[p] = [0 for _ in range(self.num_bins)]
it = np.nditer(h, flags=['multi_index'])
while not it.finished:
entry = it[0]
x, y = g.vert_feats[min(it.multi_index)], g.vert_feats[max(it.multi_index)]
x, y = min(x,y), max(x,y)
if entry >= 1:
bins[(x,y)][self.num_bins-1] += 1
else:
# print(entry)
bins[(x,y)][int(entry*self.num_bins)] += 1
it.iternext()
pi = []
for l in bins.values():
pi += l
pi_t += pi
else:
pi = [0 for _ in range(self.num_bins)]
for entry in np.nditer(h):
if entry >= 1:
pi[self.num_bins-1] += 1
else:
pi[int(entry*self.num_bins)] += 1
pi_t += pi
return pi_t
def calculateWeights(self):
phi = np.sqrt(self.binnedGraphs)
# alpha = np.linalg.solve((phi@phi.T) + self.r_lambda*np.eye(len(self.graphs)), self.y)
# w = phi.T@alpha
w, _ = cg((phi.T@phi) + self.r_lambda*np.eye(phi.shape[1]), phi.T@self.y)
return w, np.mean(np.abs(phi@w - self.y))
class BhattKernelNodes:
def __init__(self, t, num_bins, graphs=None, ys=None, r_lambda=0, use_labels=False, label_types=None, calcWeights=False):
self.t = t
self.numT = len(t)
self.num_bins = num_bins
self.graphs = graphs
self.ys = ys
self.r_lambda = r_lambda
self.use_labels = use_labels
if self.use_labels and label_types is None:
raise Exception("No label pairs provided")
self.label_types = label_types
self.binnedGraphs = []
self.y = []
if not self.graphs is None:
if self.numT == 1:
for g, nodeY in zip(self.graphs, self.ys):
for node, y in nodeY:
self.binnedGraphs += [self.graphBinnedSingle(g, node)]
self.y += [y]
else:
for g, nodeY in tqdm(zip(self.graphs, self.ys)):
for node, y in nodeY:
self.binnedGraphs += [self.graphBinned(g, node)]
self.y += [y]
if not calcWeights:
self.weights = np.zeros(self.num_bins)
else:
self.weights, err = self.calculateWeights()
print("Train Set Average Absolute Error:", err)
def predictNode(self, g, node):
if self.numT == 1:
return self.weights@np.sqrt(self.graphBinnedSingle(g, node))
else:
return self.weights@np.sqrt(self.graphBinned(g, node))
def graphBinnedSingle(self, g, node):
L = g.getLaplacian()
h = expm(-self.t[0]*L)
if self.use_labels:
bins = {}
for p in self.label_types:
bins[p] = [0 for _ in range(self.num_bins)]
for it, v in enumerate(h[node]):
x = g.vert_feats[it]
if v >= 1:
bins[x][self.num_bins-1] += 1
else:
bins[x][int(v*self.num_bins)] += 1
pi = []
for l in bins.values():
pi += l
return pi
else:
pi = [0 for _ in range(self.num_bins)]
for v in h[node]:
if v >= 1:
pi[self.num_bins-1] += 1
else:
pi[int(v*self.num_bins)] += 1
return pi
def graphBinned(self, g, node):
L = g.getLaplacian()
# h = expm(-(self.t/g.vertN)*L)
w, v = g.w, g.v
pi_t = []
for i, B in enumerate(self.t):
h = v@np.diag(np.exp(-B*w))@v.T
if self.use_labels:
bins = {}
for p in self.label_types:
bins[p] = [0 for _ in range(self.num_bins)]
for it, entry in enumerate(h[node]):
x = g.vert_feats[it]
if entry >= 1:
bins[x][self.num_bins-1] += 1
else:
bins[x][int(entry*self.num_bins)] += 1
pi = []
for l in bins.values():
pi += l
pi_t += pi
else:
pi = [0 for _ in range(self.num_bins)]
for entry in h[node]:
if entry >= 1:
pi[self.num_bins-1] += 1
else:
pi[int(entry*self.num_bins)] += 1
pi_t += pi
return pi_t
def calculateWeights(self):
phi = np.sqrt(self.binnedGraphs)
# print(phi.shape)
w, _ = cg((phi.T@phi) + self.r_lambda*np.eye(phi.shape[1]), phi.T@self.y)
# alpha = cg((phi@phi.T) + self.r_lambda*np.eye(len(self.y)), self.y)
# alpha = np.linalg.solve((phi@phi.T) + self.r_lambda*np.eye(len(self.graphs)), self.y)
# w = phi.T@alpha
return w, np.mean(np.abs(phi@w - self.y))