-
Notifications
You must be signed in to change notification settings - Fork 7
/
tagcn-bak.py
262 lines (183 loc) · 7.45 KB
/
tagcn-bak.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
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
259
260
261
262
import tensorflow as tf
import numpy as np
from utils import *
from metrics import *
from inits import *
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('dataset', 'cora', 'Dataset string.') # 'cora', 'citeseer', 'pubmed'
flags.DEFINE_string('model', 'gcn', 'Model string.') # 'gcn', 'gcn_cheby', 'dense'
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
flags.DEFINE_integer('epochs', 200, 'Number of epochs to train.')
flags.DEFINE_integer('hidden1', 16, 'Number of units in hidden layer 1.')
flags.DEFINE_float('dropout', 0.5, 'Dropout rate (1 - keep probability).')
flags.DEFINE_float('weight_decay', 5e-4, 'Weight for L2 loss on embedding matrix.')
flags.DEFINE_integer('early_stopping', 10, 'Tolerance for early stopping (# of epochs).')
flags.DEFINE_integer('max_degree', 3, 'Maximum Chebyshev polynomial degree.')
adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(FLAGS.dataset)
features = features.todense()
# g = tf.get_default_graph()
# [op.name for op in g.get_operations()]
path_weight_matrix = np.load("path_weights.dat")
path_weight_matrix = path_weight_matrix.astype('float32')
from inits import *
# var_gs = {}
name = '01'
Fl = 8
Kl = 2
Cl = features.shape[1]
Nl = features.shape[0]
from tensorflow.python.framework.ops import reset_default_graph
reset_default_graph()
# define weights, biases
vars_F={}
with tf.variable_scope(name + '_vars'):
for k in range(Kl):
vars_F['weights_' + str(0) + '_' + str(k)] = tf.get_variable(name=('weights_' + str(0) + '_' + str(k)),
initializer=tf.contrib.layers.xavier_initializer(),
shape=[Cl,Fl])
initial = tf.zeros([Nl,Fl], dtype=tf.float32)
vars_F['bias_' + str(0)] = tf.Variable(initial, name='bias')
for k in range(Kl):
vars_F['weights_' + str(1) + '_' + str(k)] = tf.get_variable(name=('weights_' + str(1) + '_' + str(k)),
initializer=tf.contrib.layers.xavier_initializer(),
shape=[Fl,Fl]) # prev [Fl,Fl]
initial = tf.zeros([Nl,Fl], dtype=tf.float32) # prev [Nl,Fl]
vars_F['bias_' + str(1)] = tf.Variable(initial, name='bias')
vars_F['weights_' + str(2)] = tf.get_variable(name=('weights_' + str(2)),
initializer=tf.contrib.layers.xavier_initializer(),
shape=[Fl,7])
initial = tf.zeros([Nl,7], dtype=tf.float32)
vars_F['bias_' + str(2)] = tf.Variable(initial, name='bias')
features_m = tf.placeholder(tf.float32, shape=[features.shape[0], features.shape[1]])
pwm = tf.placeholder(tf.float32, [Nl, Nl, Kl])
outputs=[]
# def layer(input_t,output_dim,num):
#
# # droupout
# input_t = tf.nn.dropout(input_t, 1-FLAGS.dropout)
# conv = np.zeros(output_dim,dtype=np.float32)
#
# for k in range(Kl):
#
# w_k = pwm[:,:,k]
#
# s = tf.matmul(w_k,input_t)
#
# G_k = vars_F['weights_' + str(num) + '_' + str(k)]
#
# res = tf.matmul(s,G_k)
#
# conv = tf.add(conv,res)
#
# print(conv.shape)
#
# # add bias
# bias = vars_F['bias_' + str(0)]
#
# res = tf.add(conv,bias)
#
# # apply non-linearity
# res = tf.nn.relu(conv)
#
# return conv
#
# res = layer(features_m,output_dim=[Nl,Fl],num=0)
# conv = layer(res,output_dim=[Nl,Fl],num=1)
conv = np.zeros([Nl,Fl],dtype=np.float32)
#tf.placeholder(tf.float32, shape=[Nl,Fl])
# droupout
features_m = tf.nn.dropout(features_m, 1-FLAGS.dropout)
# layer 1
for k in range(Kl):
w_k = pwm[:,:,k]
s = tf.matmul(w_k,features_m)
G_k = vars_F['weights_' + str(0) + '_' + str(k)]
res = tf.matmul(s,G_k)
outputs.append(res)
conv = tf.add(conv,res)
print(conv.shape)
outputs.append(conv)
#res = conv
# add bias
bias = vars_F['bias_' + str(0)]
conv = tf.add(conv,bias)
# apply non-linearity
conv = tf.nn.relu(conv)
# droupout
conv = tf.nn.dropout(conv, 1-FLAGS.dropout)
# layer 2
#conv = np.zeros([Fl,Fl],dtype=np.float32)
for k in range(Kl):
w_k = pwm[:,:,k]
s = tf.matmul(w_k,conv)
G_k = vars_F['weights_' + str(1) + '_' + str(k)]
res = tf.matmul(s,G_k)
outputs.append(res)
conv = tf.add(conv,res)
print(conv.shape)
outputs.append(conv)
# add bias
bias = vars_F['bias_' + str(1)]
conv = tf.add(conv,bias)
# apply non-linearity
conv = tf.nn.relu(conv)
# output layer
# droupout
conv = tf.nn.dropout(conv, 1-FLAGS.dropout)
w_k = pwm[:,:,k]
s = tf.matmul(w_k,conv)
G_k = vars_F['weights_' + str(2)]
conv = tf.matmul(s,G_k)
print(conv.shape)
# add bias
bias = vars_F['bias_' + str(2)]
conv = tf.add(conv,bias)
# apply non-linearity
conv = tf.nn.relu(conv)
# for training
accuracy1 = masked_accuracy(conv,y_train, train_mask)
loss1= 0
# weight decay loss
loss1 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['weights_' + str(0) + '_' + str(0)])
loss1 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['weights_' + str(0) + '_' + str(1)])
loss1 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['bias_' + str(0)])
# loss1 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['weights_' + str(1) + '_' + str(0)])
# loss1 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['weights_' + str(1) + '_' + str(1)])
# loss1 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['bias_' + str(1)])
# Cross entropy error
loss1 += masked_softmax_cross_entropy(conv, y_train, train_mask)
optimizer = tf.train.AdamOptimizer(learning_rate=FLAGS.learning_rate)
opt_op = optimizer.minimize(loss1)
# for testing
accuracy2 = masked_accuracy(conv,y_test, test_mask)
loss2= 0
# weight decay loss
loss2 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['weights_' + str(0) + '_' + str(0)])
loss2 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['weights_' + str(0) + '_' + str(1)])
loss2 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['bias_' + str(0)])
# loss2 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['weights_' + str(1) + '_' + str(0)])
# loss2 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['weights_' + str(1) + '_' + str(1)])
# loss2 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['bias_' + str(1)])
# Cross entropy error
loss2 += masked_softmax_cross_entropy(conv, y_test, test_mask)
# for validation
accuracy3 = masked_accuracy(conv,y_val, val_mask)
loss3= 0
# weight decay loss
loss3 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['weights_' + str(0) + '_' + str(0)])
loss3 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['weights_' + str(0) + '_' + str(1)])
loss3 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['bias_' + str(0)])
# loss3 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['weights_' + str(1) + '_' + str(0)])
# loss3 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['weights_' + str(1) + '_' + str(1)])
# loss3 += FLAGS.weight_decay * tf.nn.l2_loss(vars_F['bias_' + str(1)])
# Cross entropy error
loss3 += masked_softmax_cross_entropy(conv, y_val, val_mask)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for epoch in range(FLAGS.epochs):
evals = sess.run([opt_op,loss1,accuracy1],feed_dict={features_m:features,pwm:path_weight_matrix})
evals2 = sess.run([loss3,accuracy3],feed_dict={features_m:features,pwm:path_weight_matrix})
print("Test",evals[1], evals[2],"Validation",evals2[0], evals2[1])
outs_val = sess.run([loss2, accuracy2], feed_dict={features_m:features,pwm:path_weight_matrix})
print(outs_val[0], outs_val[1])