-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdyngem.py
221 lines (173 loc) · 7.5 KB
/
dyngem.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
import networkx as nx
import keras
from keras.models import Model, Sequential, load_model
from keras.layers import Dense, Input, Embedding, Reshape, Lambda
from keras import backend as K, regularizers
import numpy as np
from functools import reduce
dynamic_series = []
activation_fn = 'relu'
activation_fn_embedding_layer='relu'
loss_function = 'binary_crossentropy'
#Loss Function for preserving First and Second Order
def build_reconstruction_loss(beta):
"""
return the loss function for 2nd order proximity
beta: the definition below Equation 3"""
assert beta > 1
def reconstruction_loss(true_y, pred_y):
diff = K.square(true_y - pred_y)
# borrowed from https://github.com/suanrong/SDNE/blob/master/model/sdne.py#L93
weight = true_y * (beta - 1) + 1
weighted_diff = diff * weight
return K.mean(K.sum(weighted_diff, axis=1)) # mean square error
return reconstruction_loss
def edge_wise_loss(true_y, embedding_diff):
"""1st order proximity
"""
# true_y supposed to be None
# we don't use it
return K.mean(K.sum(K.square(embedding_diff), axis=1)) # mean square error
#Loading Snapshot Pickled With networkx 1.11
def loadRealGraphSeries(file_prefix, startId, endId):
graphs = []
for file_id in range(startId, endId + 1):
graph_file = file_prefix + str(file_id) + '_graph.gpickle'
graphs.append(nx.read_gpickle(graph_file))
return graphs
def get_encoder(model, input_name):
#Encoder Model
model = model.model
encoder = Sequential()
flag = 0 # Keep Adding layer to Sequential
for layer in model.layers:
if flag == 1:
break
if layer.name == 'embedding-layer':
flag = 1
encoder.add(layer)
encoder = Model(encoder.input, encoder.get_layer('embedding-layer').output)
return encoder
def get_decoder(model):
decoder = None
return decoder
def link_prediction():
return None
def get_embedding(encoder, graph_name, output_name):
g = nx.read_edgelist(graph_name, create_using=nx.Graph())
g = nx.convert_node_labels_to_integers(g)
graph = g
N = graph.number_of_nodes()
adj_mat = nx.adjacency_matrix(graph).toarray()
embedding = encoder.predict(adj_mat)
np.savetxt(output_name,embedding)
return embedding
def build_model():
#Main Function
embedding_dim = 0 # 1/4 of number of nodes
# Encoding Layer Dims
encoding_dim = []
# Decoding Layer Dims
decoding_dim = []
encoding_layers = []
decoding_layers = []
dynamic_series = loadRealGraphSeries('snapshots/s',1,7)
count = 0
node_count = []
#Initial Values
beta=2
alpha=2
l2_param=1e-3
print("Builiding Model.................")
final_model = None
for g in dynamic_series:
N = g.number_of_nodes()
node_count.append(N)
count = count + 1
adj_mat = nx.adjacency_matrix(g).toarray()
edges = np.array(list(g.edges_iter()))
weights = [ g[u][v].get('weight',1.0) for u,v in g.edges_iter() ]
if count == 1 :
# Create Model from Scratch
embedding_dim = (int) (N/4) # 1/4
# Embedding Layers
i = embedding_dim
while ((i+embedding_dim) < N):
i = i + embedding_dim
encoding_dim.append(i)
encoding_dim.append(N)
decoding_dim = encoding_dim
encoding_dim = encoding_dim[::-1]
print("Number Of Nodes: ", end=" ")
print(N)
print("Decoding Dimensions : ", end=" ")
print(decoding_dim)
print("Encoding Dimensions : ", end=" ")
print(encoding_dim)
print("Embedding Dimension : ", end=" ")
print(embedding_dim)
#Initializing Model
model = Sequential()
i = 0
for dim in encoding_dim:
i = i + 1
layer = Dense(dim, activation=activation_fn, kernel_regularizer=regularizers.l2(l2_param), name='encoding-layer-{}'.format(i))
model.add(layer)
model.add(Dense(embedding_dim, activation=activation_fn_embedding_layer, kernel_regularizer=regularizers.l2(l2_param), name='embedding-layer'))
i = 0
decoding_dim.append(N)
for dim in decoding_dim:
i = i + 1
layer = Dense(dim, activation=activation_fn, kernel_regularizer=regularizers.l2(l2_param), name='decoding-layer-{}'.format(i))
model.add(layer)
model.compile(loss=loss_function, optimizer='adam', metrics=['acc','mae'])
model.fit(adj_mat,adj_mat,epochs=300)
model_name = 'models/prev_model_{}.h5'.format(count)
model.summary()
model.save(model_name)
print("SDNE Initial Model Built Completed ... ")
final_model = model # Saving The Final Model
else:
# Create Model from Scratch
print("Adding More Dynamic Layers {}".format(count))
prev_N = node_count[count - 2]
N = g.number_of_nodes()
prev_model_name = 'models/prev_model_{}.h5'.format(count-1)
curr_model_name = 'models/prev_model_{}.h5'.format(count)
if prev_N == N:
prev_model = load_model(prev_model_name)
prev_model.save(curr_model_name)
continue
if prev_N < N:
prev_model = load_model(prev_model_name)
input_layer = Dense(N,input_dim=N,activation=activation_fn, kernel_regularizer=regularizers.l2(l2_param), name='dynamic-encoding-layer-{}'.format(count))
input_layer_dummy = Dense(prev_N, activation=activation_fn, kernel_regularizer=regularizers.l2(l2_param), name='dynamic-encoding-layer-support-{}'.format(count))
output_layer = Dense(N, activation=activation_fn, kernel_regularizer=regularizers.l2(l2_param), name='dynamic-decoding-layer-{}'.format(count))
curr_model = Sequential()
#Adding the New Input Layer
curr_model.add(input_layer)
curr_model.add(input_layer_dummy)
#Adding the Existing Layers
model_api = prev_model.model
skip_input_layer=0 # This is a flag to Skip the Previous input Layer
for layer in model_api.layers:
if skip_input_layer == 0:
skip_input_layer = 1
continue
curr_model.add(layer)
#Adding the Existing Layer
curr_model.add(output_layer)
curr_model.compile(loss=loss_function, optimizer='adam', metrics=['mae','acc'])
curr_model.fit(adj_mat,adj_mat,epochs=300)
#Sequential Before Saving
curr_model.summary()
curr_model.save(curr_model_name)
print("Dynamic Layer {} Addition Completed .".format(count))
#Assigning Final Model
final_model = curr_model
return final_model,'dynamic-encoding-layer-{}'.format(count) #Returning the final model
f_model, input_layer_name = build_model()
f_model.summary()
print("******************* Embedding ************************")
# encoder = get_encoder(f_model, input_layer_name)
# get_embedding(encoder,"graphs/final_graph.txt", "embedding/final_output.embedding")