Skip to content

Commit 252b9bc

Browse files
Clean up code
Add requirements.txt
1 parent bf53e1f commit 252b9bc

File tree

6 files changed

+156
-120
lines changed

6 files changed

+156
-120
lines changed

Autoencoder.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import matplotlib.pyplot as plt
22
import numpy as np
3-
from keras.layers import Lambda
43
from keras import Input, Model
54
from keras import backend as K
65
from keras.backend import tf
6+
from keras.layers import Lambda
7+
from pygsp import graphs
78
from spektral.layers import GraphConvSkip
89
from spektral.layers import MinCutPool
910
from spektral.utils.convolution import normalized_adjacency
10-
from utils.misc import sp_matrix_to_sp_tensor_value, get_sw_key
1111
from tqdm import tqdm
12-
from pygsp import graphs
12+
13+
from utils.misc import sp_matrix_to_sp_tensor_value, get_sw_key
1314

1415

1516
def upsampling_from_mask(inputs):
1617
X_, A_, I_, M_ = inputs
1718
S_ = tf.eye(tf.shape(M_)[0])
1819
S_ = tf.boolean_mask(S_, M_)
1920
S_t_ = tf.transpose(S_)
20-
21+
2122
X_out_ = K.dot(S_t_, X_)
2223
A_out_ = K.dot(K.transpose(K.dot(A_, S_)), S_)
2324
I_out_ = K.dot(
@@ -27,32 +28,32 @@ def upsampling_from_mask(inputs):
2728
I_out_ = K.cast(I_out_, tf.int32)
2829
return [X_out_, A_out_, I_out_]
2930

31+
3032
def upsampling_from_matrix(inputs):
3133
X_, A_, I_, S_ = inputs
3234
X_out_ = K.dot(S_, X_)
33-
A_out_ = K.dot(S_, K.transpose(K.dot(S_, A_,)))
35+
A_out_ = K.dot(S_, K.transpose(K.dot(S_, A_, )))
3436
I_out_ = K.dot(
3537
S_,
3638
K.cast(I_[:, None], tf.float32)
3739
)[:, 0]
3840
I_out_ = K.cast(I_out_, tf.int32)
3941
return [X_out_, A_out_, I_out_]
4042

43+
4144
upsampling_from_mask_op = Lambda(upsampling_from_mask)
4245
upsampling_from_matrix_op = Lambda(upsampling_from_matrix)
4346

44-
4547
# HYPERPARAMS
4648
ITER = 10000
4749
ACTIV = 'tanh'
4850
dataset = 'grid'
4951
gnn_channels = 32
5052
es_patience = 1000
5153

52-
5354
# LOAD DATASET
5455
if dataset == 'ring':
55-
G = graphs.Ring(N=200)
56+
G = graphs.Ring(N=200)
5657
elif dataset == 'grid':
5758
G = graphs.Grid2d(N1=30, N2=30)
5859
X = G.coords.astype(np.float32)
@@ -62,7 +63,6 @@ def upsampling_from_matrix(inputs):
6263
n_feat = X.shape[-1]
6364
n_nodes = A.shape[0]
6465

65-
6666
# MODEL DEFINITION
6767
X_in = Input(tensor=tf.placeholder(tf.float32, shape=(None, n_feat), name='X_in'))
6868
A_in = Input(tensor=tf.sparse_placeholder(tf.float32, shape=(None, None)), name='A_in')
@@ -76,7 +76,7 @@ def upsampling_from_matrix(inputs):
7676
X1 = GraphConvSkip(gnn_channels, activation=ACTIV)([X_in, A_in])
7777
X1 = GraphConvSkip(gnn_channels, activation=ACTIV)([X1, A_in])
7878
# pooling
79-
X2, A2, I2, M2 = MinCutPool(k=n_nodes//4, h=gnn_channels)([X1, A_in, I_in])
79+
X2, A2, I2, M2 = MinCutPool(k=n_nodes // 4, h=gnn_channels)([X1, A_in, I_in])
8080
# unpooling
8181
X3, A3, I3 = upsampling_from_matrix_op([X2, A2, I2, M2])
8282
# decoder
@@ -87,7 +87,6 @@ def upsampling_from_matrix(inputs):
8787
model = Model([X_in, A_in, I_in], [X3])
8888
model.compile('adam', 'mse', target_tensors=[X_target])
8989

90-
9190
# TRAINING
9291
sess = K.get_session()
9392
loss = model.total_loss
@@ -134,11 +133,10 @@ def upsampling_from_matrix(inputs):
134133
print('MSE', lss_)
135134
K.clear_session()
136135

137-
138136
# PLOTS
139137
plt.plot(output['loss'])
140138
plt.title('Loss')
141-
plt.figure(figsize=(8, 4 ))
139+
plt.figure(figsize=(8, 4))
142140
pad = 0.1
143141
x_min, x_max = X[:, 0].min() - pad, X[:, 0].max() + pad
144142
y_min, y_max = X[:, 1].min() - pad, X[:, 1].max() + pad
@@ -158,4 +156,4 @@ def upsampling_from_matrix(inputs):
158156
plt.axvline(0, c='k', alpha=0.2)
159157
plt.axhline(0, c='k', alpha=0.2)
160158
plt.tight_layout()
161-
plt.show()
159+
plt.show()

Clustering.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections import OrderedDict
2-
from tqdm import tqdm
2+
33
import matplotlib.pyplot as plt
44
import numpy as np
55
import pandas as pd
@@ -10,15 +10,18 @@
1010
from keras.models import Model
1111
from pygsp import graphs
1212
from sklearn.cluster import spectral_clustering
13-
from sklearn.datasets.samples_generator import make_blobs
13+
from sklearn.datasets import make_blobs
1414
from sklearn.metrics.cluster import v_measure_score, homogeneity_score, completeness_score
1515
from sklearn.neighbors import kneighbors_graph
16+
from spektral.layers import MinCutPool, DiffPool
1617
from spektral.layers.convolutional import GraphConvSkip
1718
from spektral.utils import init_logging
1819
from spektral.utils.convolution import normalized_adjacency
19-
from spektral.layers import MinCutPool, DiffPool
20+
from tqdm import tqdm
21+
2022
from utils import citation
2123
from utils.misc import sp_matrix_to_sp_tensor_value, product_dict
24+
2225
np.random.seed(0) # for reproducibility
2326

2427
PLOTS_ON = True
@@ -36,15 +39,14 @@
3639

3740
# Tunables
3841
tunables = OrderedDict([
39-
('dataset', ['cora']), # 'cora', 'citeseer', 'pubmed'
40-
('method', ['mincut_pool']), # 'mincut_pool', 'diff_pool'
42+
('dataset', ['cora']), # 'cora', 'citeseer', 'pubmed', 'cloud', or 'synth'
43+
('method', ['mincut_pool']), # 'mincut_pool', 'diff_pool'
4144
('H_', [None]),
4245
('n_channels', [16]),
4346
('learning_rate', [5e-4])
4447
])
4548

4649
N_RUNS = 1
47-
dataset = None
4850
df_out = None
4951
for T in product_dict(tunables):
5052
# Update params with current config
@@ -63,18 +65,16 @@
6365
A = sp.csr_matrix(A, dtype=np.float32)
6466
n_clust = y.max() + 1
6567
elif P['dataset'] == 'cloud':
66-
G = graphs.Grid2d(N1=15, N2=10) #Community(N=150, seed=0) #SwissRoll(N=400, seed=0) #Ring(N=100) #TwoMoons() #Cube(nb_pts=500) #Bunny()
68+
G = graphs.Grid2d(N1=15, N2=10) # Community(N=150, seed=0) #SwissRoll(N=400, seed=0) #Ring(N=100) #TwoMoons() #Cube(nb_pts=500) #Bunny()
6769
X = G.coords.astype(np.float32)
6870
A = G.W
6971
y = np.ones(X.shape[0]) # X[:,0] + X[:,1]
7072
n_clust = 5
7173
else:
72-
if dataset != P['dataset']:
73-
dataset = P['dataset']
74-
A, X, _, _, _, _, _, _, y_ohe = citation.load_data(dataset)
75-
y = np.argmax(y_ohe, axis=-1)
76-
X = X.todense()
77-
n_clust = y.max() + 1
74+
A, X, _, _, _, _, _, _, y_ohe = citation.load_data(P['dataset'])
75+
y = np.argmax(y_ohe, axis=-1)
76+
X = X.todense()
77+
n_clust = y.max() + 1
7878

7979
# Sort IDs
8080
if P['dataset'] != 'cloud':
@@ -206,12 +206,12 @@
206206
plt.scatter(X[:, 0], X[:, 1], c=c)
207207
plt.title('GNN-pool clustering')
208208
if P['dataset'] == 'cloud':
209-
fig, ax = plt.subplots(1,1,figsize=(3,3))
210-
G.plot_signal(c, vertex_size=30, plot_name='', colorbar=False,ax=ax)
209+
fig, ax = plt.subplots(1, 1, figsize=(3, 3))
210+
G.plot_signal(c, vertex_size=30, plot_name='', colorbar=False, ax=ax)
211211
ax.set_xticks([])
212212
ax.set_yticks([])
213213
plt.tight_layout()
214-
plt.savefig('logs/grid_mincut.pdf', bbox_inches = 'tight', pad_inches = 0)
214+
plt.savefig('logs/grid_mincut.pdf', bbox_inches='tight', pad_inches=0)
215215
plt.show()
216216

217217
# Spectral clustering
@@ -220,7 +220,8 @@
220220
P['complete_score_sc'] = completeness_score(y, sc)
221221
P['v_score_sc'] = v_measure_score(y, sc)
222222

223-
print('Spectral Clust - HOMO: {:.2}, CS: {:2}, NMI: {:2}'.format(P['homo_score_sc'], P['complete_score_sc'], P['v_score_sc']))
223+
print('Spectral Clust - HOMO: {:.3f}, CS: {:.3f}, NMI: {:.3f}'
224+
.format(P['homo_score_sc'], P['complete_score_sc'], P['v_score_sc']))
224225

225226
if df_out is None:
226227
df_out = pd.DataFrame([P])
@@ -234,10 +235,10 @@
234235
plt.title('Spectral clustering')
235236
plt.show()
236237
if P['dataset'] == 'cloud':
237-
fig, ax = plt.subplots(1,1,figsize=(3,3))
238-
G.plot_signal(sc, vertex_size=30, plot_name='', colorbar=False,ax=ax)
238+
fig, ax = plt.subplots(1, 1, figsize=(3, 3))
239+
G.plot_signal(sc, vertex_size=30, plot_name='', colorbar=False, ax=ax)
239240
ax.set_xticks([])
240241
ax.set_yticks([])
241242
plt.tight_layout()
242-
plt.savefig('logs/grid_spectral.pdf', bbox_inches = 'tight', pad_inches = 0)
243-
K.clear_session()
243+
plt.savefig('logs/grid_spectral.pdf', bbox_inches='tight', pad_inches=0)
244+
K.clear_session()

Graph_Classification.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import itertools
22
import time
33
from collections import OrderedDict
4+
45
import keras.backend as K
56
import numpy as np
67
import pandas as pd
@@ -13,9 +14,10 @@
1314
from sklearn.model_selection import train_test_split
1415
from spektral.layers import GraphConv, GlobalAvgPool, ARMAConv, GraphConvSkip
1516
from spektral.layers import MinCutPool, DiffPool, TopKPool, SAGPool
17+
from spektral.layers.ops import sp_matrix_to_sp_tensor_value
1618
from spektral.utils import batch_iterator, log, init_logging
1719
from spektral.utils.convolution import normalized_adjacency
18-
from spektral.layers.ops import sp_matrix_to_sp_tensor_value
20+
1921
from utils.dataset_loader import get_graph_kernel_dataset
2022

2123

@@ -60,7 +62,7 @@ def evaluate(A_list, X_list, y_list, ops):
6062
# Parameters
6163
P = OrderedDict(
6264
runs=10, # Runs to repeat per config
63-
data_mode='bench', # bench / synth
65+
data_mode='bench', # bench / synth
6466
GNN_type='GCS', # Type of GNN {GCN, GCS, Cheb, ARMA}
6567
n_channels=32, # Channels per layer
6668
activ='relu', # Activation in GNN and mincut
@@ -77,7 +79,7 @@ def evaluate(A_list, X_list, y_list, ops):
7779
# Tunables
7880
tunables = OrderedDict(
7981
dataset_ID=['PROTEINS'],
80-
method=['mincut_pool'] # 'flat', 'dense', 'diff_pool', 'top_k_pool', 'mincut_pool', 'sag_pool'
82+
method=['mincut_pool'] # 'flat', 'dense', 'diff_pool', 'top_k_pool', 'mincut_pool', 'sag_pool'
8183
)
8284
log(tunables)
8385

@@ -99,7 +101,7 @@ def evaluate(A_list, X_list, y_list, ops):
99101
########################################################################
100102
# LOAD DATA
101103
########################################################################
102-
if P['data_mode'] == 'bench':
104+
if P['data_mode'] == 'bench':
103105
A, X, y = get_graph_kernel_dataset(P['dataset_ID'], feat_norm='ohe')
104106
# Train/test split
105107
A_train, A_test, \
@@ -112,12 +114,12 @@ def evaluate(A_list, X_list, y_list, ops):
112114
loaded = np.load('data/hard.npz', allow_pickle=True)
113115
X_train, A_train, y_train = loaded['tr_feat'], list(loaded['tr_adj']), loaded['tr_class']
114116
X_test, A_test, y_test = loaded['te_feat'], list(loaded['te_adj']), loaded['te_class']
115-
X_val, A_val, y_val = loaded['val_feat'], list(loaded['val_adj']), loaded['val_class']
117+
X_val, A_val, y_val = loaded['val_feat'], list(loaded['val_adj']), loaded['val_class']
116118
else:
117119
raise ValueError
118-
120+
119121
# Parameters
120-
F = X_train[0].shape[-1] # Dimension of node features
122+
F = X_train[0].shape[-1] # Dimension of node features
121123
n_out = y_train[0].shape[-1] # Dimension of the target
122124
average_N = np.ceil(np.mean([a.shape[-1] for a in A_train])) # Average number of nodes in dataset
123125

@@ -279,7 +281,8 @@ def evaluate(A_list, X_list, y_list, ops):
279281
ep = int(current_batch / batches_in_epoch)
280282
log('Ep: {:d} - Loss: {:.2f} - Acc: {:.2f} - Val loss: {:.2f} - Val acc: {:.2f}'
281283
.format(ep, model_loss, model_acc, val_loss, val_acc))
282-
log('{} - Average epoch time: {} +- {}'.format(P['method'], np.mean(epoch_time[1:]), np.std(epoch_time[1:])))
284+
log('{} - Average epoch time: {} +- {}'
285+
.format(P['method'], np.mean(epoch_time[1:]), np.std(epoch_time[1:])))
283286
epoch_time.append(0)
284287

285288
if val_loss < best_val_loss:

0 commit comments

Comments
 (0)