-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkPrediction.py
More file actions
258 lines (179 loc) · 7.57 KB
/
linkPrediction.py
File metadata and controls
258 lines (179 loc) · 7.57 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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
# import stellargraph as sg
# from stellargraph.data import EdgeSplitter
# from stellargraph.mapper import FullBatchLinkGenerator
# from stellargraph.layer import GCN, LinkEmbedding
#
# from tensorflow import keras
# from sklearn import preprocessing, feature_extraction, model_selection
#
# import networkx as nx
def df_metrics_per_seed(models, trains_flow, tests_flow, seeds):
train_loss, train_acc = [], []
test_loss, test_acc = [], []
for i in range(len(seeds)):
train_metrics = models[i].evaluate(trains_flow[i])
test_metrics = models[i].evaluate(tests_flow[i])
train_loss.append(train_metrics[0])
train_acc.append(train_metrics[1])
test_loss.append(test_metrics[0])
test_acc.append(test_metrics[1])
return train_loss, train_acc, test_loss, test_acc
def df_seeds(seeds, train_loss, train_acc, test_loss, test_acc):
df = pd.DataFrame(
{
'seeds': seeds,
'train_loss': train_loss,
'train_acc': train_acc,
'test_loss': test_loss,
'test_acc': test_acc
}
)
df.set_index('seeds', inplace=True)
return df
def plot_metrics_all_seeds(df, seeds):
plt.subplots(figsize=(8,6))
plt.plot(df)
plt.legend(['train_loss', 'train_acc', 'test_loss', 'test_acc'])
plt.xlabel('Seeds')
plt.title('Metrics')
plt.xticks(seeds)
plt.show()
return
def gcn_seeds_metrics_and_plots(G, seeds, test_keep_connected=True):
hist, models, trains_flow, tests_flow = gcn_seeds(G, seeds, test_keep_connected=test_keep_connected)
plot_history_all_seeds(hist)
train_loss, train_acc, test_loss, test_acc = df_metrics_per_seed(models, trains_flow, tests_flow, seeds)
df = df_seeds(seeds, train_loss, train_acc, test_loss, test_acc)
print(df)
print(df.mean())
plot_metrics_all_seeds(df, seeds)
def plot_history_all_seeds(hist):
fig, axs = plt.subplots(2,2, figsize=(12,10))
for i in hist:
axs[0,0].plot(i.history['binary_accuracy'])
axs[0,0].set_title('binary_accuracy')
axs[0,1].plot(i.history['loss'])
axs[0,1].set_title('loss')
axs[1,0].plot(i.history['val_binary_accuracy'])
axs[1,0].set_title('val_binary_accuracy')
axs[1,1].plot(i.history['val_loss'])
axs[1,1].set_title('val_loss')
plt.show()
def gcn_seeds(G, seeds, test_keep_connected=True):
'''
Parameters:
----------
G : StellarGraph
seeds : list
'''
hist = []
models = []
trains_flow = []
tests_flow = []
for seed in tqdm(seeds):
print('-'*10, "Seed: ", seed, '-'*10)
edge_splitter_test = EdgeSplitter(G)
G_test, edge_ids_test, edge_labels_test = edge_splitter_test.train_test_split(
p=0.1, method="global", keep_connected=test_keep_connected, seed=seed)
edge_splitter_train = EdgeSplitter(G_test)
G_train, edge_ids_train, edge_labels_train = edge_splitter_train.train_test_split(
p=0.1, method="global", keep_connected=False, seed=seed)
epochs = 50
train_gen = FullBatchLinkGenerator(G_train, method="gcn")
train_flow = train_gen.flow(edge_ids_train, edge_labels_train)
test_gen = FullBatchLinkGenerator(G_test, method="gcn")
test_flow = train_gen.flow(edge_ids_test, edge_labels_test)
gcn = GCN(
layer_sizes=[16, 16], activations=["relu", "relu"], generator=train_gen, dropout=0.3
)
x_inp, x_out = gcn.in_out_tensors()
prediction = LinkEmbedding(activation="relu", method="ip")(x_out)
prediction = keras.layers.Reshape((-1,))(prediction)
model = keras.Model(inputs=x_inp, outputs=prediction)
model.compile(
optimizer=keras.optimizers.Adam(learning_rate=0.01),
loss=keras.losses.binary_crossentropy,
metrics=["binary_accuracy"],
)
history = model.fit(
train_flow, epochs=epochs, validation_data=test_flow, verbose=2, shuffle=False
)
hist.append(history)
models.append(model)
trains_flow.append(train_flow)
tests_flow.append(test_flow)
return hist, models, trains_flow, tests_flow
def edges_to_df(KG, rel='relation'):
df_edges_linguistic = pd.DataFrame()
source, target, relation = [], [], []
for edge in KG.edges(data=True):
source.append(edge[0])
target.append(edge[1])
relation.append(edge[2][rel])
df_edges_linguistic['source'] = source
df_edges_linguistic['relation'] = relation
df_edges_linguistic['target'] = target
return df_edges_linguistic
def matrix_adj(KG):
adj_matrix = nx.adjacency_matrix(KG).todense()
df_adj_matrix = pd.DataFrame(adj_matrix)
df_adj_matrix['index'] = list(KG.nodes)
df_adj_matrix.set_index('index', inplace=True)
df_adj_matrix.columns = list(KG.nodes)
return df_adj_matrix
def create_stellargraph(KG):
df = edges_to_df(KG)
matrix = matrix_adj(KG)
G = sg.StellarGraph(matrix, df[['source', 'target']])
return G
def gcn_function(SG, seed=42, epochs=50, show_metrics_untrained=False, show_metrics_trained=True, show_history=True):
edge_splitter_test = EdgeSplitter(SG)
G_test, edge_ids_test, edge_labels_test = edge_splitter_test.train_test_split(
p=0.1, method="global", keep_connected=False, seed=seed)
edge_splitter_train = EdgeSplitter(G_test)
G_train, edge_ids_train, edge_labels_train = edge_splitter_train.train_test_split(
p=0.1, method="global", keep_connected=False, seed=seed)
train_gen = FullBatchLinkGenerator(G_train, method="gcn")
train_flow = train_gen.flow(edge_ids_train, edge_labels_train)
test_gen = FullBatchLinkGenerator(G_test, method="gcn")
test_flow = train_gen.flow(edge_ids_test, edge_labels_test)
gcn = GCN(
layer_sizes=[16, 16], activations=["relu", "relu"], generator=train_gen, dropout=0.3)
x_inp, x_out = gcn.in_out_tensors()
prediction = LinkEmbedding(activation="relu", method="ip")(x_out)
prediction = keras.layers.Reshape((-1,))(prediction)
model = keras.Model(inputs=x_inp, outputs=prediction)
model.compile(
optimizer=keras.optimizers.Adam(lr=0.01),
loss=keras.losses.binary_crossentropy,
metrics=["binary_accuracy"],)
init_train_metrics = model.evaluate(train_flow)
init_test_metrics = model.evaluate(test_flow)
if show_metrics_untrained == True:
print("\nTrain Set Metrics of the initial (untrained) model:")
for name, val in zip(model.metrics_names, init_train_metrics):
print("\t{}: {:0.4f}".format(name, val))
print("\nTest Set Metrics of the initial (untrained) model:")
for name, val in zip(model.metrics_names, init_test_metrics):
print("\t{}: {:0.4f}".format(name, val))
history = model.fit(
train_flow, epochs=epochs, validation_data=test_flow, verbose=2, shuffle=False
)
if show_history == True:
print(sg.utils.plot_history(history))
train_metrics = model.evaluate(train_flow)
test_metrics = model.evaluate(test_flow)
if show_metrics_trained == True:
print("\nTrain Set Metrics of the trained model:")
for name, val in zip(model.metrics_names, train_metrics):
print("\t{}: {:0.4f}".format(name, val))
train_acc = val
print("\nTest Set Metrics of the trained model:")
for name, val in zip(model.metrics_names, test_metrics):
print("\t{}: {:0.4f}".format(name, val))
test_acc = val
return round(train_acc, 5), round(test_acc, 5)