forked from PaddlePaddle/PGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_class.py
271 lines (226 loc) · 9.48 KB
/
multi_class.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
263
264
265
266
267
268
269
270
271
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import time
import os
import math
import glob
import numpy as np
import paddle
from easydict import EasyDict as edict
import pgl
import yaml
from paddle.optimizer import Adam
import tqdm
from pgl.utils.logger import log
from sklearn.metrics import f1_score
from dataset import ShardedDataset
def load(name):
if name == 'cora':
dataset = pgl.dataset.CoraDataset()
elif name == "pubmed":
dataset = pgl.dataset.CitationDataset("pubmed", symmetry_edges=True)
elif name == "citeseer":
dataset = pgl.dataset.CitationDataset("citeseer", symmetry_edges=True)
elif name == "BlogCatalog":
dataset = pgl.dataset.BlogCatalogDataset()
else:
raise ValueError(name + " dataset doesn't exists")
dataset.graph.indegree()
dataset.graph.outdegree()
dataset.graph = dataset.graph.to_mmap()
return dataset
class Model(paddle.nn.Layer):
def __init__(self, num_nodes, embed_size=16, num_classes=39):
super(Model, self).__init__()
self.num_nodes = num_nodes
embed_init = paddle.nn.initializer.Uniform(
low=-1. / math.sqrt(embed_size), high=1. / math.sqrt(embed_size))
emb_attr = paddle.ParamAttr(name="node_embedding")
self.emb = paddle.nn.Embedding(
num_nodes, embed_size, weight_attr=emb_attr)
self.linear = paddle.nn.Linear(embed_size, num_classes)
def forward(self, node_ids):
node_emb = self.emb(node_ids)
node_emb.stop_gradient = True
logits = self.linear(node_emb)
return logits
def node_classify_generator(graph,
all_nodes=None,
batch_size=512,
epoch=1,
shuffle=True):
if all_nodes is None:
all_nodes = np.arange(graph.num_nodes)
def batch_nodes_generator(shuffle=shuffle):
perm = np.arange(len(all_nodes), dtype=np.int64)
if shuffle:
np.random.shuffle(perm)
start = 0
while start < len(all_nodes):
yield all_nodes[perm[start:start + batch_size]]
start += batch_size
def wrapper():
for _ in range(epoch):
for batch_nodes in batch_nodes_generator():
# batch_nodes_expanded = np.expand_dims(batch_nodes,
# -1).astype(np.int64)
batch_labels = graph.node_feat['group_id'][batch_nodes].astype(
np.float32)
yield [batch_nodes, batch_labels]
return wrapper
def topk_f1_score(labels,
probs,
topk_list=None,
average="macro",
threshold=None):
assert topk_list is not None or threshold is not None, "one of topklist and threshold should not be None"
if threshold is not None:
preds = probs > threshold
else:
preds = np.zeros_like(labels, dtype=np.int64)
for idx, (prob, topk) in enumerate(zip(np.argsort(probs), topk_list)):
preds[idx][prob[-int(topk):]] = 1
return f1_score(labels, preds, average=average)
def train(model, data_loader, optim, log_per_step=1000, threshold=0.3):
model.train()
total_loss = 0.
total_sample = 0
bce_loss = paddle.nn.BCEWithLogitsLoss()
test_probs_vals, test_labels_vals, test_topk_vals = [], [], []
for batch, (node, labels) in enumerate(data_loader):
num_samples = len(node)
node = paddle.to_tensor(node)
labels = paddle.to_tensor(labels)
logits = model(node)
probs = paddle.nn.functional.sigmoid(logits)
loss = bce_loss(logits, labels)
loss.backward()
optim.step()
optim.clear_grad()
topk = labels.sum(-1)
test_probs_vals.append(probs.numpy())
test_labels_vals.append(labels.numpy())
test_topk_vals.append(topk.numpy())
total_loss += loss.numpy()[0] * num_samples
total_sample += num_samples
test_probs_array = np.concatenate(test_probs_vals)
test_labels_array = np.concatenate(test_labels_vals)
test_topk_array = np.concatenate(test_topk_vals)
test_macro_f1 = topk_f1_score(test_labels_array, test_probs_array,
test_topk_array, "macro", threshold)
test_micro_f1 = topk_f1_score(test_labels_array, test_probs_array,
test_topk_array, "micro", threshold)
test_loss_val = total_loss / total_sample
log.info("Train Loss: %f " % test_loss_val + "Train Macro F1: %f " %
test_macro_f1 + "Train Micro F1: %f " % test_micro_f1)
return total_loss / total_sample
@paddle.no_grad()
def test(model, data_loader, log_per_step=1000, threshold=0.3):
model.eval()
total_loss = 0.
total_sample = 0
bce_loss = paddle.nn.BCEWithLogitsLoss()
test_probs_vals, test_labels_vals, test_topk_vals = [], [], []
for batch, (node, labels) in enumerate(data_loader):
num_samples = len(node)
node = paddle.to_tensor(node)
labels = paddle.to_tensor(labels)
logits = model(node)
probs = paddle.nn.functional.sigmoid(logits)
loss = bce_loss(logits, labels)
topk = labels.sum(-1)
test_probs_vals.append(probs.numpy())
test_labels_vals.append(labels.numpy())
test_topk_vals.append(topk.numpy())
total_loss += loss.numpy()[0] * num_samples
total_sample += num_samples
test_probs_array = np.concatenate(test_probs_vals)
test_labels_array = np.concatenate(test_labels_vals)
test_topk_array = np.concatenate(test_topk_vals)
test_macro_f1 = topk_f1_score(test_labels_array, test_probs_array,
test_topk_array, "macro", threshold)
test_micro_f1 = topk_f1_score(test_labels_array, test_probs_array,
test_topk_array, "micro", threshold)
test_loss_val = total_loss / total_sample
log.info("\t\tTest Loss: %f " % test_loss_val + "Test Macro F1: %f " %
test_macro_f1 + "Test Micro F1: %f " % test_micro_f1)
return test_loss_val, test_macro_f1, test_micro_f1
def load_from_files(model_dir):
files = glob.glob(
os.path.join(model_dir, "node_embedding_txt",
"node_embedding.block*.txt"))
emb_table = dict()
for filename in files:
for line in open(filename):
key, value = line.strip(",\n").split("\t")
key = int(key)
value = [float(v) for v in value.split(",")]
emb_table[key] = value
emb_list = [emb_table[key] for key in range(len(emb_table))]
emb_arr = np.array(emb_list, dtype=np.float32)
emb_arr = emb_arr[:, :(emb_arr.shape[1] - 3) // 3]
return {'emb.weight': emb_arr}
def main(args):
if not args.use_cuda:
paddle.set_device("cpu")
if paddle.distributed.get_world_size() > 1:
paddle.distributed.init_parallel_env()
dataset = load(args.dataset)
graph = dataset.graph
model = Model(graph.num_nodes, args.embed_size, dataset.num_groups)
model = paddle.DataParallel(model)
batch_size = len(dataset.train_index)
train_steps = int(len(dataset.train_index) / batch_size) * args.epoch
scheduler = paddle.optimizer.lr.PolynomialDecay(
learning_rate=args.multiclass_learning_rate,
decay_steps=train_steps,
end_lr=0.0001)
optim = Adam(learning_rate=scheduler, parameters=model.parameters())
if args.load_from_static:
model.set_state_dict(load_from_files("./model"))
else:
model.set_state_dict(paddle.load("model.pdparams"))
train_data_loader = node_classify_generator(
graph, dataset.train_index, batch_size=batch_size, epoch=1)
test_data_loader = node_classify_generator(
graph, dataset.test_index, batch_size=batch_size, epoch=1)
best_test_macro_f1 = -1
for epoch in tqdm.tqdm(range(args.epoch)):
train_loss = train(model, train_data_loader(), optim)
test_loss, test_macro_f1, test_micro_f1 = test(model,
test_data_loader())
best_test_macro_f1 = max(best_test_macro_f1, test_macro_f1)
log.info("Best test macro f1 is %s." % best_test_macro_f1)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Deepwalk')
parser.add_argument(
"--dataset",
type=str,
default="BlogCatalog",
help="dataset (cora, pubmed, BlogCatalog)")
parser.add_argument("--use_cuda", action='store_true', help="use_cuda")
parser.add_argument(
"--conf",
type=str,
default="./config.yaml",
help="config file for models")
parser.add_argument("--epoch", type=int, default=1000, help="Epoch")
parser.add_argument(
"--load_from_static", action='store_true', help="use_cuda")
args = parser.parse_args()
# merge user args and config file
config = edict(yaml.load(open(args.conf), Loader=yaml.FullLoader))
config.update(vars(args))
main(config)