forked from Fans-xmu/news_popularity_prediction
-
Notifications
You must be signed in to change notification settings - Fork 1
/
testgnn.py
291 lines (264 loc) · 13 KB
/
testgnn.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# encoding: utf-8
'''
@author: Fans
@file: testgnn.py
@time: 2021/6/18 13:25
@desc:
'''
import dgl
import numpy as np
import torch
from module.data_loader_gnn import ExamplegnnSet
from module.embedding import Word_Embedding
from module.vocabulary import Vocab
from tools.logger import *
import logging
import os
import pickle
import argparse
from sklearn.metrics import explained_variance_score,median_absolute_error
import math
def graph_collate_fn(samples):
'''
:param batch: (G, input_pad)
:return:
'''
graphs, index= map(list, zip(*samples))
graph_len = [len(g.filter_nodes(lambda nodes: nodes.data["dtype"] == 1)) for g in graphs] # sent node of graph
sorted_len, sorted_index = torch.sort(torch.LongTensor(graph_len), dim=0, descending=True)
batched_graph = dgl.batch([graphs[idx] for idx in sorted_index])
return batched_graph, [index[idx] for idx in sorted_index]
def setup_eval_MAE(hps,test_loader,new_model):
logger.info("loss:MAE , the acc in test_data:")
# new_model.eval()
test_loss = 0
criterion = torch.nn.L1Loss()
criterion2 = torch.nn.MSELoss()
for i, (G,index) in enumerate(test_loader):
if hps.cuda:
G = G.to(torch.device("cuda"))
index = torch.LongTensor(index).to(torch.device("cuda"))
outputs = new_model.forward(G)
outputs = outputs.squeeze(1)
Nnode_id = G.filter_nodes(lambda nodes: nodes.data["dtype"] == 2)
label = G.ndata["label"][Nnode_id]
label = label.float()
label = label.to(torch.device("cuda"))
# print(label)
loss = criterion(outputs.float(), label.float())
test_loss += loss.item()
logger.info("MAE:{:.6f}".format(test_loss / len(test_loader)))
def setup_eval_MSE(hps,test_loader,new_model):
# new_model.eval()
test_loss = 0
criterion2 = torch.nn.MSELoss()
for i, (G,index) in enumerate(test_loader):
if hps.cuda:
G = G.to(torch.device("cuda"))
index = torch.LongTensor(index).to(torch.device("cuda"))
outputs = new_model.forward(G)
outputs = outputs.squeeze(1)
Nnode_id = G.filter_nodes(lambda nodes: nodes.data["dtype"] == 2)
label = G.ndata["label"][Nnode_id]
label = label.float()
label = label.to(torch.device("cuda"))
loss = criterion2(outputs.float(), label.float())
test_loss += loss.item()
logger.info("MSE:{:.6f}".format(test_loss / len(test_loader)))
def smape(y_true, y_pred):
return 2.0 * np.mean(np.abs(y_pred - y_true) / (np.abs(y_pred) + np.abs(y_true))) * 100
def setup_eval_MedAE(hps, test_loader, new_model):
smape_loss = 0
evs_loss = 0
meadae_loss = 0
for i, (G,index) in enumerate(test_loader):
if hps.cuda:
G = G.to(torch.device("cuda"))
index = torch.LongTensor(index).to(torch.device("cuda"))
outputs = new_model.forward(G)
outputs = outputs.squeeze(1)
Nnode_id = G.filter_nodes(lambda nodes: nodes.data["dtype"] == 2)
label = G.ndata["label"][Nnode_id]
label = label.float()
label = label.to(torch.device("cuda"))
rank_list = outputs.cpu().detach().tolist()
label_list = label.cpu().detach().tolist()
label_list = changezero(label_list, rank_list)
y_pred = np.array(rank_list)
y_true = np.array(label_list)
evs_loss += explained_variance_score(y_true, y_pred)
smape_loss += smape(y_true, y_pred)
meadae_loss += median_absolute_error(y_true, y_pred)
logger.info("SMAPE:{:.3f}".format(smape_loss / len(test_loader)))
logger.info("explained_variance_score:{:.3f}".format(evs_loss / len(test_loader)))
logger.info("meadAE:{:.3f}".format(meadae_loss / len(test_loader)))
def changezero(label_list, rank_list):
label_ls = []
for i in range(len(label_list)):
key = label_list[i]
if key == 0 and rank_list[i] == 0:
key = 1
rank_list[i] = 1
label_ls.append(key)
return label_ls
def setup_eval_RMSE(hps, test_loader, new_model):
criterion2 = torch.nn.MSELoss()
test_loss = 0
for i, (G,index) in enumerate(test_loader):
if hps.cuda:
G = G.to(torch.device("cuda"))
index = torch.LongTensor(index).to(torch.device("cuda"))
outputs = new_model.forward(G)
outputs = outputs.squeeze(1)
Nnode_id = G.filter_nodes(lambda nodes: nodes.data["dtype"] == 2)
label = G.ndata["label"][Nnode_id]
label = label.float()
label = label.to(torch.device("cuda"))
loss = torch.sqrt(criterion2(outputs.float(), label.float()))
test_loss += loss.item()
logger.info("RMSE:{:.6f}".format(test_loss / len(test_loader)))
def setup_eval_R2(hps, test_loader, new_model):
MRR_loss = 0
NDCG_loss = 0
for i, (G, index) in enumerate(test_loader):
if hps.cuda:
G = G.to(torch.device("cuda"))
index = torch.LongTensor(index).to(torch.device("cuda"))
outputs = new_model.forward(G)
outputs = outputs.squeeze(1)
Nnode_id = G.filter_nodes(lambda nodes: nodes.data["dtype"] == 2)
label = G.ndata["label"][Nnode_id]
label = label.float()
label = label.to(torch.device("cuda"))
rank_list = outputs.cpu().detach().tolist()
label_list = label.cpu().detach().tolist()
MRR, NDCG = Seqevalute(rank_list, label_list)
# print("MRR:", MRR)
# print("NDCG", NDCG)
MRR_loss += MRR
NDCG_loss += NDCG
logger.info("NDCG:{:.3f}".format(NDCG_loss / len(test_loader)))
logger.info("MRR:{:.3f}".format(MRR_loss / len(test_loader)))
def Seqevalute(rank_list,label_list):
idlist=[]
for i in range(len(rank_list)):
idlist.append(i)
rank_list_sort=list(zip(idlist,rank_list))
label_list_sort=list(zip(idlist,label_list))
rank_list_sort.sort(key=takeSecond,reverse=True)
label_list_sort.sort(key=takeSecond,reverse=True)
rankid_list=[]
for ids,_ in rank_list_sort:
rankid_list.append(ids)
labelid_list = []
for ids, _ in label_list_sort:
labelid_list.append(ids)
MRR = 0
NDCG=0
for new in rankid_list:
distance=0
for j in labelid_list:
if j!=new:
distance+=1
MRR += 1 / (distance + 1)
NDCG += 1/(math.log2(1+distance+1))
MRR/=len(rank_list)
NDCG/=len(rank_list)
return MRR,NDCG
def takeSecond(elem):
return elem[1]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='ShuaiGat Model')
# Where to find data
parser.add_argument('--data_dir', type=str, default='pkldata/', help='The dataset directory.')
parser.add_argument('--cache_dir', type=str, default='cache/', help='The processed dataset directory')
parser.add_argument('--embedding_path', type=str, default='./pkldata',
help='Path expression to external word embedding.')
# data处理
# Important settings
parser.add_argument('--model', type=str, default='ShuaiGat', help='model structure[HSG|HDSG]')
parser.add_argument('--restore_model', type=str, default='None',
help='Restore model for further training. [bestmodel/bestFmodel/earlystop/None]')
# Where to save output
parser.add_argument('--save_root', type=str, default='save/', help='Root directory for all model.')
parser.add_argument('--log_root', type=str, default='log/', help='Root directory for all logging.')
# Hyperparameters
parser.add_argument('--gpu', type=str, default='5', help='GPU ID to use. [default: 0]')
parser.add_argument('--cuda', action='store_true', default=True, help='GPU or CPU [default: False]')
parser.add_argument('--vocab_size', type=int, default=60000, help='Size of vocabulary. [default: 3000]')
# parser.add_argument('--vocab_size', type=int, default=50000,help='Size of vocabulary. [default: 50000]')
parser.add_argument('--n_epochs', type=int, default=10, help='Number of epochs [default: 20]')
parser.add_argument('--batch_size', type=int, default=128, help='Mini batch size [default: 1]')
parser.add_argument('--n_iter', type=int, default=1, help='iteration hop [default: 1]')
parser.add_argument('--word_embedding', action='store_true', default=True,
help='whether to use Word embedding [default: True]')
parser.add_argument('--word_emb_dim', type=int, default=128, help='Word embedding size [default: 128]')
parser.add_argument('--embed_train', action='store_true', default=False,
help='whether to train Word embedding [default: False]')
parser.add_argument('--feat_embed_size', type=int, default=50, help='feature embedding size [default: 50]')
parser.add_argument('--n_layers', type=int, default=2, help='Number of GAT layers [default: 1]')
parser.add_argument('--lstm_hidden_state', type=int, default=128, help='size of lstm hidden state [default: 128]')
parser.add_argument('--lstm_layers', type=int, default=2, help='Number of lstm layers [default: 2]')
parser.add_argument('--bidirectional', action='store_true', default=True,
help='whether to use bidirectional LSTM [default: True]')
parser.add_argument('--n_feature_size', type=int, default=128, help='size of node feature [default: 128]')
parser.add_argument('--hidden_size', type=int, default=64, help='hidden size [default: 64]')
parser.add_argument('--ffn_inner_hidden_size', type=int, default=512,
help='PositionwiseFeedForward inner hidden size [default: 512]')
parser.add_argument('--n_head', type=int, default=2, help='multihead attention number [default: 8]')
parser.add_argument('--recurrent_dropout_prob', type=float, default=0.1,
help='recurrent dropout prob [default: 0.1]')
parser.add_argument('--atten_dropout_prob', type=float, default=0.1, help='attention dropout prob [default: 0.1]')
parser.add_argument('--ffn_dropout_prob', type=float, default=0.1,
help='PositionwiseFeedForward dropout prob [default: 0.1]')
parser.add_argument('--use_orthnormal_init', action='store_true', default=True,
help='use orthnormal init for lstm [default: True]')
parser.add_argument('--sent_max_len', type=int, default=10,
help='max length of sentences (max source text sentence tokens)')
parser.add_argument('--doc_max_timesteps', type=int, default=5,
help='max length of documents (max timesteps of documents)')
# Training
parser.add_argument('--lr', type=float, default=0.1, help='learning rate')
parser.add_argument('--lr_descent', action='store_true', default=True, help='learning rate descent')
parser.add_argument('--grad_clip', action='store_true', default=False, help='for gradient clipping')
parser.add_argument('--max_grad_norm', type=float, default=1.0,
help='for gradient clipping max gradient normalization')
# parser.add_argument('-m', type=int, default=3, help='decode summary length')
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
torch.set_printoptions(threshold=50000)
# File paths
LOG_PATH = args.log_root
data_file = os.path.join(args.data_dir, "train_event_data.pkl")
valid_file = os.path.join(args.data_dir, "valid_event_data.pkl")
vocab_file = os.path.join(args.data_dir, "event_vocab.pkl")
train_w2s_path = os.path.join(args.cache_dir, "train.w2s.tfidf_event.pkl")
val_w2s_path = os.path.join(args.cache_dir, "val.w2s.tfidf_event.pkl")
embedding_path = os.path.join(args.data_dir, "word_embedding")
test_file = os.path.join(args.data_dir, "test_event_data.pkl")
test_w2s_path = os.path.join(args.cache_dir, "test.w2s.tfidf_event.pkl")
entity_file = os.path.join(args.data_dir, "entitylist_data.pkl")
logger.info("Pytorch %s", torch.__version__)
logger.info("[INFO] Create Vocab, vocab file is %s", vocab_file)
with open(vocab_file, 'rb') as f:
vocab_list = pickle.load(f) # 词库
vocab_num = pickle.load(f) # 词的数量
vocab_size = vocab_num
logger.info("[INFO] vocab_list,vocab_num读取成功!")
#vocab = Vocab(vocab_list, vocab_size)
vocab = Vocab(vocab_list, args.vocab_size)
hps = args
logger.info(hps)
# ----------------------------------test-----------------------------------
test_dataset = ExamplegnnSet(test_file, vocab, hps.doc_max_timesteps, hps.sent_max_len, test_w2s_path)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=hps.batch_size, shuffle=False,
collate_fn=graph_collate_fn)
modelname='bestmodel_weibo_MAE_FHgnn_noentity'
#modelname='earlystop_ShuaiGat_2'
new_model = torch.load('savemodel_entain/'+modelname)
print("The model :" + modelname)
setup_eval_MAE(hps, test_loader, new_model)
# setup_eval_MSE(hps, test_loader, new_model)
setup_eval_RMSE(hps, test_loader, new_model)
setup_eval_MedAE(hps, test_loader, new_model)
#setup_eval_R2(hps, test_loader, new_model)