-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
71 lines (52 loc) · 2 KB
/
run.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
import tensorflow as tf
from tensorflow.python.util import deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False
from sklearn.kernel_approximation import RBFSampler, PolynomialCountSketch, Nystroem
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.neighbors import kneighbors_graph
from time import time
from scipy import sparse
from sklearn.metrics import silhouette_score
from utils import *
flags = tf.compat.v1.flags
FLAGS = flags.FLAGS
# Parameters
flags.DEFINE_string('dataset', 'acm', 'Name of the graph dataset (`acm`, `dblp`, `arxiv`, `pubmed` or `wiki`).')
flags.DEFINE_integer('power', 2, 'Propagation order.')
flags.DEFINE_integer('runs', 5, 'Number of runs per power.')
dataset = flags.FLAGS.dataset
p = flags.FLAGS.power
n_runs = flags.FLAGS.runs
adj, features, labels, n_classes = datagen(dataset)
norm_adj, features = preprocess_dataset(adj, features,
tf_idf=True,
sparse=True)
features = features.toarray()
n, d = features.shape
k = n_classes
metrics = {}
metrics['acc'] = []
metrics['nmi'] = []
metrics['ari'] = []
metrics['time'] = []
norm_adj = convert_sparse_matrix_to_sparse_tensor(norm_adj.astype('float64'))
features = tf.convert_to_tensor(features.astype('float64'))
x = features
for run in range(n_runs):
features = x
t0 = time()
features = convolve(features, norm_adj, p)
P, Q = run_model(features, c=2**-.5, k=k)
metrics['time'].append(time()-t0)
metrics['acc'].append(clustering_accuracy(labels, P)*100)
metrics['nmi'].append(nmi(labels, P)*100)
metrics['ari'].append(ari(labels, P)*100)
results = {
'mean': {k:(np.mean(v)).round(2) for k,v in metrics.items() },
'std': {k:(np.std(v)).round(2) for k,v in metrics.items()}
}
means = results['mean']
std = results['std']
print(f"{dataset} {p}")
print(f"{means['acc']}±{std['acc']} & {means['nmi']}±{std['nmi']} & {means['ari']}±{std['ari']}", sep=',')
print(f"{means['time']}±{std['time']}")