-
Notifications
You must be signed in to change notification settings - Fork 6
/
citeseer_multirun.py
193 lines (142 loc) · 6.09 KB
/
citeseer_multirun.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
from __future__ import division
from __future__ import print_function
import time
import tensorflow as tf
from utils import *
from models import DSSGCN
from tensorflow import set_random_seed
import matplotlib.pyplot as plt
import scipy.io as sio
import scipy
from scipy.sparse import csr_matrix, lil_matrix
# Settings
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('dataset', 'citeseer', 'Dataset string.')
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
flags.DEFINE_integer('epochs', 100, 'Number of epochs to train.')
flags.DEFINE_list('nfilter', [160,6], 'Number of units in each layer for instance [160, 100,50]')
flags.DEFINE_list('activation_funcs', [tf.nn.relu,lambda x: x], 'Activation functions for hidden+output layers [tf.nn.relu, lambda x: x]')
flags.DEFINE_list('biases', [False,True], 'if apply bias for hidden and output layers')
flags.DEFINE_list('isdroput_inp', [True,True], 'if apply dropout for hidden and output layers'' input')
flags.DEFINE_list('isdroput_kernel', [True,True], 'if apply dropout for hidden and output layers'' kernel')
flags.DEFINE_list('firstDWS_learnable', [True,True], 'if first kernel''s depthwise weights are learnable or not')
flags.DEFINE_list('isdepthwise', [True,True], 'if layer is depthwise or not')
flags.DEFINE_float('dropout', 0.75, 'Dropout rate (1 - keep probability).')
flags.DEFINE_float('weight_decay', 3e-4, 'Weight for L2 loss on embedding matrix.')
flags.DEFINE_float('weight_decay_depthwise', 3e-3, 'Weight for L2 loss on depthwise weigths.')
flags.DEFINE_integer('early_stopping', 400, 'Tolerance for early stopping (# of epochs).')
# Load data
adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask,label = load_data(FLAGS.dataset,fullabel=True)
orgtrain_mask=train_mask.copy()
# train_mask=~(val_mask + test_mask)
# I=train_mask==True
# y_train[I,:]=label[I,:]
# Some preprocessing
#features = preprocess_features(features)
features = justpreprocess_features(features)
feat=features.toarray()
rfile='logs/citeseer.txt'
f=open(rfile,'w')
# Define model evaluation function
def evaluate(features, support, labels, mask, placeholders):
t_test = time.time()
feed_dict_val = construct_feed_dict(features, support, labels, mask, placeholders)
outs_val = sess.run([model.loss, model.accuracy,model.entropy,model.outputs], feed_dict=feed_dict_val)
res=np.argmax(outs_val[3],axis=1)
return outs_val[0], outs_val[1], outs_val[2],res
# cleaning
if FLAGS.dataset=='pubmed':
exit
else:
W=1.0*adj.toarray()
d = W.sum(axis=0)
# normalized Laplacian matrix.
dis=1/np.sqrt(d)
dis[np.isinf(dis)]=0
dis[np.isnan(dis)]=0
D=np.diag(dis)
nL=np.eye(D.shape[0])-(W.dot(D)).T.dot(D)
V1,U1 = np.linalg.eigh(nL)
V1[V1<0]=0
# low pass filter
dbb=(V1.max()-V1)/V1.max()
db=dbb**5
support2 = list()
A0=U1.dot(np.diag(db).dot(U1.T))
A0[np.where(np.abs(A0)<0.001)]=0
support2.append(A0)
# all pass filters
support2.append(np.eye(A0.shape[0]))
# band pass filters
# ff=np.linspace(0,V1.max(),5)
# for f in ff[1:-1]:
# db4=np.exp(-(((V1-f)*1)**2))
# A2=U1.dot(np.diag(db4).dot(U1.T))
# A2[np.where(np.abs(A2)<0.001)]=0
# support2.append(A2)
num_supports = len(support2)
cleangcnn=[]
cleanxentgcnn=[]
semisuper=[];semisuperx=[];semisuper2=[]
for iter in range(0,20):
# Set random seed
seed = iter
np.random.seed(seed)
tf.set_random_seed(seed)
placeholders2 = {
'support': [tf.placeholder(tf.float32) for _ in range(num_supports)],
'features': tf.placeholder(tf.float32, shape=(None, feat.shape[1])),
'labels': tf.placeholder(tf.float32, shape=(None, y_train.shape[1])),
'labels_mask': tf.placeholder(tf.int32),
'dropout': tf.placeholder_with_default(0., shape=()),
'num_features_nonzero': tf.placeholder(tf.int32) # helper variable for sparse dropout
}
model = DSSGCN(placeholders2, input_dim=feat.shape[1], logging=True)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
cost_val = []
bvalacc=0
bvalcost=10000
btestcost=0
btestacc=0
besttrain=0
early=0
# Train model
for epoch in range(FLAGS.epochs):
t = time.time()
# Construct feed dictionary
feed_dict = construct_feed_dict(feat, support2, y_train, train_mask, placeholders2)
feed_dict.update({placeholders2['dropout']: FLAGS.dropout})
# Training step
outs = sess.run([model.opt_op, model.entropy, model.accuracy], feed_dict=feed_dict)
# Validation
cost, acc, valx,predv = evaluate(feat, support2, y_val, val_mask, placeholders2)
cost_val.append(cost)
testacc=(np.argmax(y_test[test_mask],axis=1)==predv[test_mask]).mean()
if bvalacc<acc: #bvalcost>valx:
semipredt=predv.copy()
bvalacc=acc
btestacc=testacc
model.save(sess)
if bvalcost>valx:
bvalcost=valx
#model.save(sess)
f=open(rfile,'a')
msg=str(iter)+ ", Epoch:,"+ '%04d' % (epoch + 1) + ", train_xent=,"+ "{:.5f}".format(outs[1])+", train_acc=,"+ "{:.5f}".format(outs[2]) + ", val_xent=,"+ "{:.5f}".format(valx)+", val_acc=,"+ "{:.5f}".format(acc)+", test_acc=,"+ "{:.5f}".format(testacc)+'\n'
f.writelines(msg)
f.close()
# # Print results
if True:
print("Epoch:", '%04d' % (epoch + 1), "train_xent=", "{:.5f}".format(outs[1]),
"train_acc=", "{:.5f}".format(outs[2]), "val_xent=", "{:.5f}".format(valx),
"val_acc=", "{:.5f}".format(acc), "besttest_acc=", "{:.5f}".format(btestacc),"curr test=", "{:.5f}".format(testacc))
if early>=FLAGS.early_stopping:
print("Early stopping...")
break
cleangcnn.append(btestacc)
#cleanxentgcnn.append(0)
del model
sess.close()
tf.keras.backend.clear_session()
print(iter, " Test acc: ",np.array(cleangcnn).mean()," std:",np.array(cleangcnn).std())