-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
165 lines (119 loc) · 3.86 KB
/
train.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
import tensorflow as tf
import numpy as np
import networkx as nx
import os
import csv
from gcn import *
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.utils import shuffle
from sklearn.preprocessing import LabelEncoder
data = []
edges = []
with open(os.path.join("cora","cora.content")) as tsv:
for line in csv.reader(tsv, delimiter="\t"):
data.append(line)
with open(os.path.join("cora","cora.cites")) as tsv:
for line in csv.reader(tsv, delimiter="\t"):
edges.append(line)
data = shuffle(data,random_state=1)
labels = []
nodes = []
features = []
for row in data:
labels.append(row[-1])
features.append(row[1:-1])
nodes.append(row[0])
features = np.array(features,dtype=int)
edge_list=[]
for edge in edges:
edge_list.append((edge[0],edge[1]))
num_nodes = features.shape[0]
num_labels = len(labels)
def get_index(num_labels, num_train_class=20, num_test=1000, num_val=500):
label_count = {}
train_index = []
for i, label in enumerate(labels):
if not label in label_count:
label_count[label] = 1
train_index.append(i)
else:
if label_count[label] >= num_train_class:
continue
else:
label_count[label] += 1
train_index.append(i)
test_index = []
count=0
for i in range(num_labels):
if count >= num_test:
break
if i not in train_index:
test_index.append(i)
count += 1
val_index = []
count=0
for i in range(num_labels):
if count >= num_val:
break
if i not in train_index and i not in test_index:
val_index.append(i)
count += 1
return train_index, test_index, val_index
num_nodes = features.shape[0]
test = 1000
val = 500
train = 20
# train_index, test_index, val_index = get_index(num_labels, train, test, val)
index = [i for i in range(num_nodes)]
index = shuffle(index,random_state=1)
train_index = index[:(num_nodes-test-val)]
val_index = index[(num_nodes-test-val):(num_nodes-test)]
test_index = index[(num_nodes-test):]
len(train_index), len(val_index), len(test_index)
train_mask = np.zeros((num_nodes,),dtype=bool)
train_mask[train_index] = True
val_mask = np.zeros((num_nodes,),dtype=bool)
val_mask[val_index] = True
test_mask = np.zeros((num_nodes,),dtype=bool)
test_mask[test_index] = True
def encode_label(labels):
label_encoder = LabelEncoder()
labels = label_encoder.fit_transform(labels)
labels = tf.keras.utils.to_categorical(labels)
return labels, label_encoder.classes_
G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edge_list)
A = nx.convert_matrix.to_numpy_matrix(G)
A = tf.convert_to_tensor(A, tf.float32)
print('Graph info: ', nx.info(G))
A = norm_adjacency_matrix(A)
l2 = 5e-4
rate = 0.5
epochs = 200
learning_rate = 1e-2
labels_encoded, classes = encode_label(labels)
inp = tf.keras.Input((features.shape[1],))
out_1 = GraphConvolutionLayer(16, A, rate=rate, l2=l2)(inp)
out = GraphConvolutionLayer( 7, A, tf.nn.sigmoid, rate)(out_1)
model = tf.keras.Model(inputs= inp, outputs=out, name="graph_convolution")
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer,
loss='categorical_crossentropy',
weighted_metrics=['acc'])
print(model.summary())
validation_data = (features, labels_encoded, val_mask)
model.fit(features,
labels_encoded,
sample_weight=train_mask,
epochs=epochs,
batch_size=num_nodes,
validation_data=validation_data,
shuffle=False)
features_test = features[test_mask]
A_test = np.array(A)[test_mask,:][:,test_mask]
y_test = labels_encoded[test_mask]
y_pred = model.predict(features, batch_size=num_nodes)
report = classification_report(np.argmax(y_test,axis=1), np.argmax(y_pred[test_mask],axis=1), target_names=classes)
print('GCN Classification Report: \n {}'.format(report))