-
Notifications
You must be signed in to change notification settings - Fork 147
/
NodeClassificationDataset.py
1195 lines (1053 loc) · 55.8 KB
/
NodeClassificationDataset.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import dgl
import dgl.function as fn
import torch as th
import numpy as np
from dgl.data.rdf import AIFBDataset, MUTAGDataset, BGSDataset, AMDataset
from dgl.data.utils import load_graphs, save_graphs
from dgl.data import CoraGraphDataset,CiteseerGraphDataset,PubmedGraphDataset
from dgl import sparse as dglsp
# from dgl.data import TexasDataset,CornellDataset
import scipy.sparse as sp
from ogb.nodeproppred import DglNodePropPredDataset
from . import load_acm_raw
from . import BaseDataset, register_dataset
from . import AcademicDataset, HGBDataset, OHGBDataset,IMDB4MAGNN_Dataset
from .utils import sparse_mx_to_torch_sparse_tensor, to_symmetric, row_norm
from ..utils import add_reverse_edges
import os
from dgl.data.utils import download, extract_archive
from abc import ABC
from collections import defaultdict
import torch
######################## add dataset here
@register_dataset('common_dataset')
class Common_Dataset(BaseDataset):
def __init__(self, dataset_name, *args, **kwargs):
super(Common_Dataset, self).__init__(*args, **kwargs)
assert dataset_name in ['BPHGNN_dataset','acm4HGMAE','hgprompt_acm_dblp','acm4FedHGNN']
if dataset_name == 'acm4HGMAE':
# 这是从云盘上下载下来的 本地zip文件
self.zip_file = os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset',f'{dataset_name}.zip' )
#本地base_dir文件夹.
self.base_dir = os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset',f'{dataset_name}'+'_dir' )
# 云端的zip文件
self.url = f'https://s3.cn-north-1.amazonaws.com.cn/dgl-data/dataset/openhgnn/{dataset_name}.zip'
if os.path.exists(self.zip_file):
pass
else:
os.makedirs( os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset') ,exist_ok= True)
download(self.url,
path=os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset')
)
if os.path.exists( self.base_dir ):
pass
else:
os.makedirs( os.path.join( self.base_dir ) ,exist_ok= True )
extract_archive(self.zip_file, self.base_dir) # 把graph.bin 解压到 base_dir文件夹中
self.g = dgl.load_graphs( os.path.join(self.base_dir,f'{dataset_name}.bin') )[0][0]
self.category = 'paper'
self.num_classes = 3
self.meta_paths_dict = {} # 元路径
self.has_feature = True # 是否有初始特征
###### add dataset here
elif dataset_name == 'BPHGNN_dataset':
# 这是从云盘上下载下来的 本地zip文件
self.zip_file = os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset',f'{dataset_name}.zip' )
#本地base_dir文件夹.
self.base_dir = os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset',f'{dataset_name}'+'_dir' )
# 云端的zip文件
self.url = f'https://s3.cn-north-1.amazonaws.com.cn/dgl-data/dataset/openhgnn/{dataset_name}.zip'
if os.path.exists(self.zip_file):
pass
else:
os.makedirs( os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset') ,exist_ok= True)
download(self.url,
path= os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset')
)
if os.path.exists( self.base_dir ):
pass
else:
os.makedirs( os.path.join( self.base_dir ) ,exist_ok= True )
extract_archive(self.zip_file, self.base_dir)
elif dataset_name == 'hgprompt_acm_dblp':
self.zip_file = os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset',f'{dataset_name}.zip' )
self.base_dir = os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset',f'{dataset_name}'+'_dir' )
self.url = f'https://s3.cn-north-1.amazonaws.com.cn/dgl-data/dataset/openhgnn/{dataset_name}.zip'
if os.path.exists(self.zip_file):
pass
else:
os.makedirs( os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset') ,exist_ok= True)
download(self.url,
path= os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset')
)
if os.path.exists( self.base_dir ):
pass
else:
os.makedirs( os.path.join( self.base_dir ) ,exist_ok= True )
extract_archive(self.zip_file, self.base_dir)
elif dataset_name == 'acm4FedHGNN':
self.zip_file = os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset',f'{dataset_name}.zip' )
self.base_dir = os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset',f'{dataset_name}'+'_dir' )
self.url = f'https://s3.cn-north-1.amazonaws.com.cn/dgl-data/dataset/openhgnn/{dataset_name}.zip'
if os.path.exists(self.zip_file):
pass
else:
os.makedirs( os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset') ,exist_ok= True)
download(self.url,
path= os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset')
)
if os.path.exists( self.base_dir ):
pass
else:
os.makedirs( os.path.join( self.base_dir ) ,exist_ok= True )
extract_archive(self.zip_file, self.base_dir)
self.acm_mat_file = os.path.join(self.base_dir,'acm4FedHGNN.mat')
import scipy.io as sio
self.data = sio.loadmat(self.acm_mat_file)
elif dataset_name == 'acm4FedHGNN':
self.zip_file = os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset',f'{dataset_name}.zip' )
self.base_dir = os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset',f'{dataset_name}'+'_dir' )
self.url = f'https://s3.cn-north-1.amazonaws.com.cn/dgl-data/dataset/openhgnn/{dataset_name}.zip'
if os.path.exists(self.zip_file):
pass
else:
os.makedirs( os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset') ,exist_ok= True)
download(self.url,
path= os.path.join( kwargs.get('args').openhgnn_dir ,'dataset','Common_Dataset')
)
if os.path.exists( self.base_dir ):
pass
else:
os.makedirs( os.path.join( self.base_dir ) ,exist_ok= True )
extract_archive(self.zip_file, self.base_dir)
self.acm_mat_file = os.path.join(self.base_dir,'acm4FedHGNN.mat')
import scipy.io as sio
self.data = sio.loadmat(self.acm_mat_file)
@register_dataset('GraphBolt_Dataset')
class GraphBolt_Dataset(BaseDataset):
def __init__(self, dataset_name, *args, **kwargs):
super(GraphBolt_Dataset, self).__init__(*args, **kwargs)
assert dataset_name in ['imdb4GTN','HGBl-amazon']
self.zip_path = f'./openhgnn/dataset/GraphBolt_Dataset/{dataset_name}_base_dir.zip'
self.base_dir = './openhgnn/dataset/GraphBolt_Dataset/' + dataset_name + '_base_dir'
self.url = f'https://s3.cn-north-1.amazonaws.com.cn/dgl-data/dataset/openhgnn/{dataset_name}_base_dir.zip'
import os
from dgl.data.utils import download, extract_archive
if os.path.exists(self.zip_path):
pass
else:
os.makedirs( os.path.join('./openhgnn/dataset/GraphBolt_Dataset/') ,exist_ok= True)
download(self.url,
path=os.path.join('./openhgnn/dataset/GraphBolt_Dataset/')
)
if os.path.exists( self.base_dir ):
pass
else:
extract_archive(self.zip_path, os.path.join('./openhgnn/dataset/GraphBolt_Dataset/'))
@register_dataset('node_classification')
class NodeClassificationDataset(BaseDataset):
r"""
The class *NodeClassificationDataset* is a base class for datasets which can be used in task *node classification*.
So its subclass should contain attributes such as graph, category, num_classes and so on.
Besides, it should implement the functions *get_labels()* and *get_split()*.
Attributes
-------------
g : dgl.DGLHeteroGraph
The heterogeneous graph.
category : str
The category(or target) node type need to be predict. In general, we predict only one node type.
num_classes : int
The target node will be classified into num_classes categories.
has_feature : bool
Whether the dataset has feature. Default ``False``.
multi_label : bool
Whether the node has multi label. Default ``False``. For now, only HGBn-IMDB has multi-label.
"""
def __init__(self, *args, **kwargs):
super(NodeClassificationDataset, self).__init__(*args, **kwargs)
self.g = None
self.category = None
self.num_classes = None
self.has_feature = False
self.multi_label = False
self.meta_paths_dict =None
# self.in_dim = None
def get_labels(self):
r"""
The subclass of dataset should overwrite the function. We can get labels of target nodes through it.
Notes
------
In general, the labels are th.LongTensor.
But for multi-label dataset, they should be th.FloatTensor. Or it will raise
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 target' in call to _thnn_nll_loss_forward
return
-------
labels : torch.Tensor
"""
if 'labels' in self.g.nodes[self.category].data:
labels = self.g.nodes[self.category].data.pop('labels').long()
elif 'label' in self.g.nodes[self.category].data:
labels = self.g.nodes[self.category].data.pop('label').long()
else:
raise ValueError('Labels of nodes are not in the hg.nodes[category].data.')
labels = labels.float() if self.multi_label else labels
return labels
def get_split(self, validation=True):
r"""
Parameters
----------
validation : bool
Whether to split dataset. Default ``True``. If it is False, val_idx will be same with train_idx.
We can get idx of train, validation and test through it.
return
-------
train_idx, val_idx, test_idx : torch.Tensor, torch.Tensor, torch.Tensor
"""
if 'train_mask' not in self.g.nodes[self.category].data:
self.logger.dataset_info("The dataset has no train mask. "
"So split the category nodes randomly. And the ratio of train/test is 8:2.")
num_nodes = self.g.number_of_nodes(self.category)
n_test = int(num_nodes * 0.2)
n_train = num_nodes - n_test
train, test = th.utils.data.random_split(range(num_nodes), [n_train, n_test])
train_idx = th.tensor(train.indices)
test_idx = th.tensor(test.indices)
if validation:
self.logger.dataset_info("Split train into train/valid with the ratio of 8:2 ")
random_int = th.randperm(len(train_idx))
valid_idx = train_idx[random_int[:len(train_idx) // 5]]
train_idx = train_idx[random_int[len(train_idx) // 5:]]
else:
self.logger.dataset_info("Set valid set with train set.")
valid_idx = train_idx
train_idx = train_idx
else:
train_mask = self.g.nodes[self.category].data.pop('train_mask')
test_mask = self.g.nodes[self.category].data.pop('test_mask')
train_idx = th.nonzero(train_mask, as_tuple=False).squeeze()
test_idx = th.nonzero(test_mask, as_tuple=False).squeeze()
if validation:
if 'val_mask' in self.g.nodes[self.category].data:
val_mask = self.g.nodes[self.category].data.pop('val_mask')
valid_idx = th.nonzero(val_mask, as_tuple=False).squeeze()
elif 'valid_mask' in self.g.nodes[self.category].data:
val_mask = self.g.nodes[self.category].data.pop('valid_mask').squeeze()
valid_idx = th.nonzero(val_mask, as_tuple=False).squeeze()
else:
# RDF_NodeClassification has train_mask, no val_mask
self.logger.dataset_info("Split train into train/valid with the ratio of 8:2 ")
random_int = th.randperm(len(train_idx))
valid_idx = train_idx[random_int[:len(train_idx) // 5]]
train_idx = train_idx[random_int[len(train_idx) // 5:]]
else:
self.logger.dataset_info("Set valid set with train set.")
valid_idx = train_idx
train_idx = train_idx
self.train_idx = train_idx
self.valid_idx = valid_idx
self.test_idx = test_idx
# Here set test_idx as attribute of dataset to save results of HGB
return self.train_idx, self.valid_idx, self.test_idx
@register_dataset('hga_node_classification')
class HGA_NodeClassification(NodeClassificationDataset):
def __init__(self, dataset_name, *args, **kwargs):
super(HGA_NodeClassification, self).__init__(*args, **kwargs)
assert dataset_name in ['acm4HGA','dblp4HGA']
if dataset_name == 'acm4HGA':
self.zip_file = f'./openhgnn/dataset/Common_Dataset/{dataset_name}.zip'
self.base_dir = './openhgnn/dataset/Common_Dataset/' + dataset_name + '_dir'
self.url = f'https://s3.cn-north-1.amazonaws.com.cn/dgl-data/dataset/openhgnn/{dataset_name}.zip'
if os.path.exists(self.zip_file):
pass
else:
os.makedirs( os.path.join('./openhgnn/dataset/Common_Dataset/') ,exist_ok= True)
download(self.url,
path=os.path.join('./openhgnn/dataset/Common_Dataset/')
)
if os.path.exists( self.base_dir ):
pass
else:
os.makedirs( os.path.join( self.base_dir ) ,exist_ok= True )
extract_archive(self.zip_file, self.base_dir)
self.g = dgl.load_graphs( os.path.join(self.base_dir,f'{dataset_name}.bin') )[0][0].long()
self.category = '1'
self.num_classes = 4
self.in_dim = self.g.ndata['h'][self.category].shape[1]
self.meta_paths_dict = {
'131': [('1', '2', '3'), ('3', '2', '1')],
'121': [('1', '1', '2'), ('2', '1', '1')],
}
self.has_feature = True
elif dataset_name == 'dblp4HGA':
self.zip_file = f'./openhgnn/dataset/Common_Dataset/{dataset_name}.zip'
self.base_dir = './openhgnn/dataset/Common_Dataset/' + dataset_name + '_dir'
self.url = f'https://s3.cn-north-1.amazonaws.com.cn/dgl-data/dataset/openhgnn/{dataset_name}.zip'
if os.path.exists(self.zip_file):
pass
else:
os.makedirs( os.path.join('./openhgnn/dataset/Common_Dataset/') ,exist_ok= True)
download(self.url,
path=os.path.join('./openhgnn/dataset/Common_Dataset/')
)
if os.path.exists( self.base_dir ):
pass
else:
os.makedirs( os.path.join( self.base_dir ) ,exist_ok= True )
extract_archive(self.zip_file, self.base_dir)
self.g = dgl.load_graphs( os.path.join(self.base_dir,f'{dataset_name}.bin') )[0][0].long()
self.category = '1'
self.num_classes = 4
self.in_dim = self.g.ndata['h'][self.category].shape[1]
self.meta_paths_dict = {
'131': [('1', '2', '3'), ('3', '2', '1')],
'121': [('1', '1', '2'), ('2', '1', '1')],
}
self.has_feature = True
@register_dataset('rhine_node_classification')
class RHINE_NodeClassification(NodeClassificationDataset):
def __init__(self, dataset_name, *args, **kwargs):
super(RHINE_NodeClassification, self).__init__(*args, **kwargs)
self.dataset_name = dataset_name
assert self.dataset_name in['dblp4RHINE']
self.zip_file = f'./openhgnn/dataset/Common_Dataset/{dataset_name}.zip'
self.base_dir = './openhgnn/dataset/Common_Dataset/' + dataset_name + '_dir'
self.url = f'https://s3.cn-north-1.amazonaws.com.cn/dgl-data/dataset/openhgnn/{dataset_name}.zip'
if os.path.exists(self.zip_file):
pass
else:
os.makedirs( os.path.join('./openhgnn/dataset/Common_Dataset/') ,exist_ok= True)
download(self.url,
path=os.path.join('./openhgnn/dataset/Common_Dataset/')
)
if os.path.exists( self.base_dir ):
pass
else:
os.makedirs( os.path.join( self.base_dir ) ,exist_ok= True )
extract_archive(self.zip_file, self.base_dir)
self.graph_file = os.path.join(self.base_dir,'dblp4RHINE.bin')
self.load_rhine_data()
def load_rhine_data(self):
if self.dataset_name == 'dblp4RHINE':
if os.path.exists(self.graph_file):
self.g=dgl.load_graphs(self.graph_file)[0][0].long()
self.node_types = {'a': 'author', 'p': 'paper', 't': 'term', 'c': 'conf'}
self.IRs = ['ap', 'pt', 'apt']
self.ARs = ['pc', 'apc']
self.category='paper'
self.meta_paths_dict={
'ap':[('author', 'writes', 'paper'),('paper','written_by','author')],
'pt':[('paper', 'has_term', 'term'),('term','term_of','paper')],
'apt':[('author', 'writes', 'paper'), ('paper', 'has_term', 'term'),('term','term_of','paper'),('paper','written_by','author')],
'pc':[('paper', 'published_in', 'conf'), ('conf', 'publish', 'paper')],
'apc':[('author', 'writes', 'paper'), ('paper', 'published_in', 'conf'), ('conf', 'publish', 'paper'),('paper','written_by','author')]
}
test_mask = [i!=-1 for i in self.g.nodes['paper'].data['label']]
self.g.nodes['paper'].data['label_mask']=th.tensor(test_mask)
self.train_id = th.tensor(range(self.g.num_nodes('paper')))
self.pred_id=self.test_id=self.valid_id = th.tensor(range(self.g.num_nodes('paper')))[test_mask]
class MHGCN_Base_Dataset(BaseDataset):
def __init__(self, *args, **kwargs):
# train_percent = kwargs.pop('train_percent', 0.1)
super(MHGCN_Base_Dataset, self).__init__(*args, **kwargs)
_dataset_list = ['dblp4MHGCN','imdb4MHGCN','alibaba4MHGCN']
self.data_path = ""
self.name = args[0]
if not self.name in _dataset_list:
raise ValueError("Unsupported dataset name {}".format(self.name))
self.data_path = 'openhgnn/dataset/data/{}'.format(self.name)
self.url = 'https://s3.cn-north-1.amazonaws.com.cn/dgl-data/dataset/openhgnn/{}.zip'.format(self.name)
self.process()
self.multi_label = False
def process(self):
if not os.path.exists(self.data_path) or not os.path.exists(os.path.join(self.data_path,self.name+".zip")):
self.download()
self.load_data()
def _multiplex_relation_aggregation(self,coo_list,etype_count):
'''
Multiplex relation aggregation (1)
In order to decrease the consumption of time , we divide multiplex relation aggregation into two parts.
The first part is to aggregate all relations into one relation , which will be run only once.
The second part is to update the weight of the relations , which will be run in every epochs.
Codes below are the first part.
'''
adj_list = []
for i in range(etype_count):
adj = sp.coo_matrix(coo_list[i]).todense()
adj_list.append(torch.tensor(adj,dtype=torch.float32))
weight = torch.linspace(0,etype_count - 1,etype_count)
weight = [2**i for i in weight]
weight = torch.tensor(weight,dtype=torch.float32)
adj = torch.stack(adj_list,dim=2)
adj = adj + adj.transpose(0,1)
aggr_adj = torch.matmul(adj,weight)
sparse_aggr_adj = sp.coo_matrix(aggr_adj.detach().cpu())
self.g = dgl.from_scipy(sparse_aggr_adj)
self.etype_num = etype_count
etag = torch.zeros(sparse_aggr_adj.data.shape[0],etype_count,dtype=torch.float32)
for i in range(sparse_aggr_adj.data.shape[0]):
etag[i] = adj[sparse_aggr_adj.row[i],sparse_aggr_adj.col[i]]
self.g.edata['tag'] = etag.clone()
def _multiplex_relation_aggregation_old(self,coo_list,etype_count):
'''
Multiplex relation aggregation (1)
In order to decrease the consumption of time , we divide multiplex relation aggregation into two parts.
The first part is to aggregate all relations into one relation , which will be run only once.
The second part is to update the weight of the relations , which will be run in every epochs.
Codes below are the first part.
'''
adj_list = []
for i in range(etype_count):
adj = sp.coo_matrix(coo_list[i]).todense()
adj_list.append(torch.tensor(adj,dtype=torch.float32))
weight = torch.linspace(0,etype_count - 1,etype_count)
weight = [2**i for i in weight]
weight = torch.tensor(weight,dtype=torch.float32)
adj = torch.stack(adj_list,dim=2)
aggr_adj = torch.matmul(adj,weight)
aggr_adj = aggr_adj + aggr_adj.transpose(0,1)
sparse_aggr_adj = sp.coo_matrix(aggr_adj.detach().cpu())
data_val = sparse_aggr_adj.data
data_val = torch.tensor(data_val,dtype=torch.int32)
self.g = dgl.from_scipy(sparse_aggr_adj)
self.etype_num = etype_count
etag = torch.zeros(data_val.shape[0],etype_count,dtype=torch.float32)
for index,val in enumerate(data_val):
for i in range(etype_count):
if val & 2**i != 0:
etag[index][i] = 1
self.g.edata['tag'] = etag
def load_data(self):
if self.name == 'dblp4MHGCN':
self.labels = np.load(os.path.join(self.data_path,"labels_mat.npy"))
self.edges_paper_author = sp.load_npz(os.path.join(self.data_path,'edge_paper_author.npz'))
self.edges_paper_term = sp.load_npz(os.path.join(self.data_path,'edge_paper_term.npz'))
self.edges_paper_venue = sp.load_npz(os.path.join(self.data_path,'edge_paper_venue.npz'))
self.features = np.load(os.path.join(self.data_path,'features.npy'))
self.train_val_test_idx = np.load(os.path.join(self.data_path,'train_val_test_idx.npz'))
self.train_idx = self.train_val_test_idx['test_idx']
self.val_idx = self.train_val_test_idx['val_idx']
self.test_idx = self.train_val_test_idx['train_idx']
self._multiplex_relation_aggregation([self.edges_paper_author,self.edges_paper_term,self.edges_paper_venue],3)
self.etype_num = 3
self.num_classes = self.labels.shape[1]
elif self.name == 'imdb4MHGCN':
self.labels = np.load(os.path.join(self.data_path,"labels.npy"))
self.edges_A = sp.load_npz(os.path.join(self.data_path,'edges_A.npz'))
self.edges_B = sp.load_npz(os.path.join(self.data_path,'edges_B.npz'))
self.features = np.load(os.path.join(self.data_path,'features.npy'))
self.train_val_test_idx = np.load(os.path.join(self.data_path,'train_val_test_idx.npz'))
self.train_idx = self.train_val_test_idx['train_idx']
self.val_idx = self.train_val_test_idx['val_idx']
self.test_idx = self.train_val_test_idx['test_idx']
self._multiplex_relation_aggregation([self.edges_A,self.edges_B],2)
self.etype_num = 2
self.num_classes = self.labels.shape[1]
elif self.name == 'alibaba4MHGCN':
self.labels = np.load(os.path.join(self.data_path,"labels.npy"))
self.edges_A = sp.load_npz(os.path.join(self.data_path,'edges_A.npz'))
self.edges_B = sp.load_npz(os.path.join(self.data_path,'edges_B.npz'))
self.edges_C = sp.load_npz(os.path.join(self.data_path,'edges_C.npz'))
self.edges_D = sp.load_npz(os.path.join(self.data_path,'edges_D.npz'))
self.features = np.load(os.path.join(self.data_path,'features.npy'))
self.train_val_test_idx = np.load(os.path.join(self.data_path,'train_val_test_idx.npz'))
self.train_idx = self.train_val_test_idx['train_idx']
self.val_idx = self.train_val_test_idx['val_idx']
self.test_idx = self.train_val_test_idx['test_idx']
self._multiplex_relation_aggregation([self.edges_A,self.edges_B,self.edges_C,self.edges_D],4)
self.etype_num = 4
self.num_classes = self.labels.shape[1]
def download(self):
# download raw data to local disk
try:
if os.path.exists(self.data_path): # pragma: no cover
pass
else:
file_path = os.path.join(self.data_path,self.name+".zip")
# download file
download(self.url, path=file_path)
extract_archive(os.path.join(self.data_path, self.name+".zip"),self.data_path)
except Exception as e:
os.removedirs(os.path.join(self.data_path))
raise e
@register_dataset('mhgcn_node_classification')
class MHGCN_NC_Dataset(MHGCN_Base_Dataset):
def __init__(self, *args, **kwargs):
super(MHGCN_NC_Dataset, self).__init__(*args, **kwargs)
def get_split(self):
return self.train_idx, self.val_idx, self.test_idx
def get_labels(self):
return torch.argmax(torch.tensor(self.labels),dim=1)
@register_dataset('mhgcn_link_prediction')
class MHGCN_LP_Dataset(MHGCN_Base_Dataset):
def __init__(self, *args, **kwargs):
# train_percent = kwargs.pop('train_percent', 0.1)
super(MHGCN_LP_Dataset, self).__init__(*args, **kwargs)
def load_training_data(self):
f_name = os.path.join(self.data_path,'train.txt')
edges_src = list()
edges_dst = list()
edges_type = list()
with open(f_name, 'r') as f:
for line in f:
words = line[:-1].split()
x = int(words[1])
y = int(words[2])
type = int(words[0])
edges_src.append(x)
edges_dst.append(y)
edges_type.append(type)
g = dgl.graph((edges_src,edges_dst),num_nodes=self.num_nodes)
g.edata['type'] = torch.tensor(edges_type)
return g
def load_testing_data(self,is_val:bool):
f_name = os.path.join(self.data_path,'valid.txt') if is_val else os.path.join(self.data_path,'test.txt')
true_edges_src = list()
true_edges_dst = list()
true_edges_type = list()
false_edges_src = list()
false_edges_dst = list()
false_edges_type = list()
with open(f_name, 'r') as f:
for line in f:
# words = line[:-1].split('\t')
words = line[:-1].split()
x = int(words[1])
y = int(words[2])
type = int(words[0])
if int(words[3]) == 1:
true_edges_src.append(x)
true_edges_dst.append(y)
true_edges_type.append(type)
else:
false_edges_src.append(x)
false_edges_dst.append(y)
false_edges_type.append(type)
true_g = dgl.graph((true_edges_src,true_edges_dst),num_nodes=self.num_nodes)
true_g.edata['type'] = torch.tensor(true_edges_type)
false_g = dgl.graph((false_edges_src,false_edges_dst),num_nodes=self.num_nodes)
false_g.edata['type'] = torch.tensor(false_edges_type)
return true_g,false_g
def load_data(self):
super().load_data()
self.num_nodes = self.g.num_nodes()
self.train_g = self.load_training_data()
self.val_g,self.val_neg_g = self.load_testing_data(is_val=True)
self.test_g,self.test_neg_g = self.load_testing_data(is_val=False)
def get_split(self):
return self.train_g, self.val_g, self.test_g, self.val_neg_g, self.test_neg_g
def get_labels(self):
return torch.argmax(torch.tensor(self.labels),dim=1)
####################################
@register_dataset('rdf_node_classification')
class RDF_NodeClassification(NodeClassificationDataset):
r"""
The RDF dataset will be used in task *entity classification*.
Dataset Name : aifb/ mutag/ bgs/ am.
We download from dgl and process it, refer to
`RDF datasets <https://docs.dgl.ai/api/python/dgl.data.html#rdf-datasets>`_.
Notes
------
They are all have no feature.
"""
def __init__(self, dataset_name, *args, **kwargs):
super(RDF_NodeClassification, self).__init__(*args, **kwargs)
self.g, self.category, self.num_classes = self.load_RDF_dgl(dataset_name)
self.has_feature = False
def load_RDF_dgl(self, dataset):
# load graph data
if dataset == 'aifb':
kg_dataset = AIFBDataset()
elif dataset == 'mutag':
kg_dataset = MUTAGDataset()
elif dataset == 'bgs':
kg_dataset = BGSDataset()
elif dataset == 'am':
kg_dataset = AMDataset()
else:
raise ValueError()
# Load from hetero-graph
kg = kg_dataset[0]
category = kg_dataset.predict_category
num_classes = kg_dataset.num_classes
return kg, category, num_classes
@register_dataset('hin_node_classification')
class HIN_NodeClassification(NodeClassificationDataset):
r"""
The HIN dataset are all used in different papers. So we preprocess them and store them as form of dgl.DGLHeteroGraph.
The dataset name combined with paper name through 4(for).
Dataset Name :
acm4NSHE/ acm4GTN/ acm4NARS/ acm_han_raw/ academic4HetGNN/ dblp4MAGNN/ imdb4MAGNN/ ...
"""
def __init__(self, dataset_name, *args, **kwargs):
super(HIN_NodeClassification, self).__init__(*args, **kwargs)
if 'args' in kwargs:
self.args = kwargs['args']
else:
self.args = None
self.g, self.category, self.num_classes = self.load_HIN(dataset_name)
def load_HIN(self, name_dataset):
if name_dataset == 'demo_graph':
data_path = './openhgnn/dataset/demo_graph.bin'
category = 'author'
num_classes = 4
g, _ = load_graphs(data_path)
g = g[0].long()
self.in_dim = g.ndata['h'][category].shape[1]
elif name_dataset == 'acm4NSHE':
dataset = AcademicDataset(name='acm4NSHE', raw_dir='')
category = 'paper'
g = dataset[0].long()
num_classes = 3
self.in_dim = g.ndata['h'][category].shape[1]
elif name_dataset == 'dblp4MAGNN':
dataset = AcademicDataset(name='dblp4MAGNN', raw_dir='')
category = 'A'
g = dataset[0].long()
num_classes = 4
self.meta_paths_dict = {
'APVPA': [('A', 'A-P', 'P'), ('P', 'P-V', 'V'), ('V', 'V-P', 'P'), ('P', 'P-A', 'A')],
'APA': [('A', 'A-P', 'P'), ('P', 'P-A', 'A')],
}
self.meta_paths = [(('A', 'A-P', 'P'), ('P', 'P-V', 'V'), ('V', 'V-P', 'P'), ('P', 'P-A', 'A')),
(('A', 'A-P', 'P'), ('P', 'P-A', 'A'))]
self.in_dim = g.ndata['h'][category].shape[1]
elif name_dataset == 'imdb4MAGNN':
if self.args:
if self.args.use_database == True:
dataset = IMDB4MAGNN_Dataset(name='imdb4MAGNN',args = self.args)
else:
dataset = AcademicDataset(name='imdb4MAGNN', raw_dir='')
category = 'M'
g = dataset[0].long()
num_classes = 3
self.in_dim = g.ndata['h'][category].shape[1]
elif name_dataset == 'imdb4GTN':
dataset = AcademicDataset(name='imdb4GTN', raw_dir='')
category = 'movie'
g = dataset[0].long()
num_classes = 3
self.in_dim = g.ndata['h'][category].shape[1]
elif name_dataset == 'acm4GTN':
dataset = AcademicDataset(name='acm4GTN', raw_dir='')
category = 'paper'
g = dataset[0].long()
num_classes = 3
self.meta_paths_dict = {'PAPSP': [('paper', 'paper-author', 'author'), ('author', 'author-paper', 'paper'),
('paper', 'paper-subject', 'subject'),
('subject', 'subject-paper', 'paper')],
'PAP': [('paper', 'paper-author', 'author'), ('author', 'author-paper', 'paper')],
'PSP': [('paper', 'paper-subject', 'subject'),
('subject', 'subject-paper', 'paper')]
}
# self.meta_paths = [(('paper', 'paper-author', 'author'), ('author', 'author-paper', 'paper'),
# ('paper', 'paper-subject', 'subject'), ('subject', 'subject-paper', 'paper'))]
self.in_dim = g.ndata['h'][category].shape[1]
elif name_dataset == 'acm4NARS':
dataset = AcademicDataset(name='acm4NARS', raw_dir='')
g = dataset[0].long()
num_classes = 3
# g, labels, num_classes, train_nid, val_nid, test_nid = load_acm_nars()
category = 'paper'
elif name_dataset == 'acm4HeCo':
dataset = AcademicDataset(name='acm4HeCo', raw_dir='')
pos = sp.load_npz("./openhgnn/dataset/acm4HeCo/pos.npz")
self.pos = sparse_mx_to_torch_sparse_tensor(pos)
g = dataset[0].long()
num_classes = 3
category = 'paper'
elif name_dataset == 'academic4HetGNN':
# which is used in HetGNN
dataset = AcademicDataset(name='academic4HetGNN', raw_dir='')
category = 'author'
g = dataset[0].long()
num_classes = 4
elif name_dataset == 'yelp4HeGAN':
# which is used in HeGAN
dataset = AcademicDataset(name='yelp4HeGAN', raw_dir='')
category = 'business'
g = dataset[0].long()
num_classes = 3
elif name_dataset == 'yelp4HGSL':
# yelp used for HGSL
dataset = AcademicDataset(name = 'yelp4HGSL', raw_dir='')
category = 'b'
g = dataset[0].long()
num_classes = 4
self.meta_paths_dict = {'bub': [('b', 'b-u', 'u'), ('u', 'u-b', 'b')],
'bsb': [('b', 'b-s', 's'), ('s', 's-b', 'b')],
'bublb': [('b', 'b-u', 'u'), ('u', 'u-b', 'b'),
('b', 'b-l', 'l'), ('l', 'l-b', 'b')],
'bubsb': [('b', 'b-u', 'u'), ('u', 'u-b', 'b'),
('b', 'b-s', 's'), ('s', 's-b', 'b')]
}
elif name_dataset == 'HNE-PubMed':
# which is used in HeGAN
dataset = AcademicDataset(name='HNE-PubMed', raw_dir='')
category = 'DISEASE'
g = dataset[0].long()
num_classes = 8
g = add_reverse_edges(g)
self.meta_paths_dict = {'DCD': [('DISEASE', 'CHEMICAL-in-DISEASE-rev', 'CHEMICAL'), ('CHEMICAL', 'CHEMICAL-in-DISEASE', 'DISEASE')],
'DDD': [('DISEASE', 'DISEASE-and-DISEASE', 'DISEASE'), ('DISEASE', 'DISEASE-and-DISEASE-rev', 'DISEASE')],
'DGD': [('DISEASE', 'GENE-causing-DISEASE-rev', 'GENE'), ('GENE', 'GENE-causing-DISEASE', 'DISEASE')],
'DSD': [('DISEASE', 'SPECIES-with-DISEASE-rev', 'SPECIES'), ('SPECIES', 'SPECIES-with-DISEASE', 'DISEASE')]
}
elif name_dataset in ['acm_han', 'acm_han_raw']:
if name_dataset == 'acm_han':
pass
elif name_dataset == 'acm_han_raw':
g, category, num_classes, self.in_dim = load_acm_raw(False)
self.meta_paths_dict = {'PAP': [('paper', 'pa', 'author'), ('author', 'ap', 'paper')],
'PFP': [('paper', 'pf', 'field'), ('field', 'fp', 'paper')]
}
else:
return NotImplementedError('Unsupported dataset {}'.format(name_dataset))
return g, category, num_classes
elif name_dataset in ['demo']:
data_path = './openhgnn/dataset/graph.bin'
category = 'author'
num_classes = 4
g, _ = load_graphs(data_path)
g = g[0].long()
self.in_dim = g.ndata['h'][category].shape[1]
# g, _ = load_graphs(data_path)
# g = g[0]
return g, category, num_classes
@register_dataset('ohgb_node_classification')
class OHGB_NodeClassification(NodeClassificationDataset):
def __init__(self, dataset_name, *args, **kwargs):
super(OHGB_NodeClassification, self).__init__(*args, **kwargs)
self.dataset_name = dataset_name
self.has_feature = True
if dataset_name == 'ohgbn-Freebase':
dataset = OHGBDataset(name=dataset_name, raw_dir='')
g = dataset[0].long()
category = 'BOOK'
num_classes = 8
g = add_reverse_edges(g)
self.meta_paths_dict = {'BB': [('BOOK', 'BOOK-and-BOOK', 'BOOK')],
'BFB': [('BOOK', 'BOOK-to-FILM', 'FILM'), ('FILM', 'BOOK-to-FILM-rev', 'BOOK')],
'BOFB': [('BOOK', 'BOOK-about-ORGANIZATION', 'ORGANIZATION'),
('ORGANIZATION', 'ORGANIZATION-in-FILM', 'FILM'),
('FILM', 'BOOK-to-FILM-rev', 'BOOK')],
'BLMB': [('BOOK', 'BOOK-on-LOCATION', 'LOCATION'),
('LOCATION', 'MUSIC-on-LOCATION-rev', 'MUSIC'),
('MUSIC', 'MUSIC-in-BOOK', 'BOOK')],
'BPB': [('BOOK', 'PEOPLE-to-BOOK-rev', 'PEOPLE'),
('PEOPLE', 'PEOPLE-to-BOOK', 'BOOK')],
'BPSB': [('BOOK', 'PEOPLE-to-BOOK-rev', 'PEOPLE'),
('PEOPLE', 'PEOPLE-to-SPORTS', 'SPORTS'),
('SPORTS', 'BOOK-on-SPORTS-rev', 'BOOK')],
'BBuB': [('BOOK', 'BUSINESS-about-BOOK-rev', 'BUSINESS'),
('BUSINESS', 'BUSINESS-about-BOOK', 'BOOK')],
}
elif dataset_name == 'ohgbn-yelp2':
dataset = OHGBDataset(name=dataset_name, raw_dir='')
g = dataset[0].long()
g = add_reverse_edges(g)
category = 'business'
num_classes = 16
self.multi_label = True
elif dataset_name == 'ohgbn-acm':
dataset = OHGBDataset(name=dataset_name, raw_dir='')
g = dataset[0].long()
category = 'paper'
num_classes = 3
self.meta_paths_dict = {
'PAP': [('paper', 'paper-author', 'author'), ('author', 'author-paper', 'paper')],
'PSP': [('paper', 'paper-subject', 'subject'),
('subject', 'subject-paper', 'paper')]
}
elif dataset_name == 'ohgbn-imdb':
dataset = OHGBDataset(name=dataset_name, raw_dir='')
category = 'movie'
g = dataset[0].long()
num_classes = 3
self.meta_paths_dict = {
'MAM': [('movie', 'movie-actor', 'actor'), ('actor', 'actor-movie', 'movie')],
'MDM': [('movie', 'movie-director', 'director'), ('director', 'director-movie', 'movie')]}
self.g, self.category, self.num_classes = g, category, num_classes
@register_dataset('HGBn_node_classification')
class HGB_NodeClassification(NodeClassificationDataset):
r"""
The HGB dataset will be used in task *node classification*.
Dataset Name :
HGBn-ACM/HGBn-DBLP/HGBn-Freebase/HGBn-IMDB
So if you want to get more information, refer to
`HGB datasets <https://github.com/THUDM/HGB>`_
"""
def __init__(self, dataset_name, *args, **kwargs):
super(HGB_NodeClassification, self).__init__(*args, **kwargs)
self.dataset_name = dataset_name
self.has_feature = True
if dataset_name == 'HGBn-ACM':
dataset = HGBDataset(name=dataset_name, raw_dir='')
g = dataset[0].long()
category = 'paper'
num_classes = 3
# graph: dgl graph object, label: torch tensor of shape (num_nodes, num_tasks)
self.meta_paths_dict = {'PAP': [('paper', 'paper-author', 'author'), ('author', 'author-paper', 'paper')],
'PSP': [('paper', 'paper-subject', 'subject'),
('subject', 'subject-paper', 'paper')],
'PcPAP': [('paper', 'paper-cite-paper', 'paper'),
('paper', 'paper-author', 'author'),
('author', 'author-paper', 'paper')],
'PcPSP': [('paper', 'paper-cite-paper', 'paper'),
('paper', 'paper-subject', 'subject'),
('subject', 'subject-paper', 'paper')],
'PrPAP': [('paper', 'paper-ref-paper', 'paper'),
('paper', 'paper-author', 'author'),
('author', 'author-paper', 'paper')],
'PrPSP': [('paper', 'paper-ref-paper', 'paper'),
('paper', 'paper-subject', 'subject'),
('subject', 'subject-paper', 'paper')]
}
elif dataset_name == 'HGBn-DBLP':
dataset = HGBDataset(name=dataset_name, raw_dir='')
g = dataset[0].long()
category = 'author'
num_classes = 4
self.meta_paths_dict = {'APA': [('author', 'author-paper', 'paper'), ('paper', 'paper-author', 'author')],
'APTPA': [('author', 'author-paper', 'paper'), ('paper', 'paper-term', 'term'),
('term', 'term-paper', 'paper'), ('paper', 'paper-author', 'author')],
'APVPA': [('author', 'author-paper', 'paper'), ('paper', 'paper-venue', 'venue'),
('venue', 'venue-paper', 'paper'), ('paper', 'paper-author', 'author')],
}
# graph: dgl graph object, label: torch tensor of shape (num_nodes, num_tasks)
elif dataset_name == 'HGBn-Freebase':
dataset = HGBDataset(name=dataset_name, raw_dir='')
g = dataset[0].long()
category = 'BOOK'
num_classes = 7
self.has_feature = False
g = add_reverse_edges(g)
self.meta_paths_dict = {'BB': [('BOOK', 'BOOK-and-BOOK', 'BOOK')],
'BFB': [('BOOK', 'BOOK-to-FILM', 'FILM'), ('FILM', 'BOOK-to-FILM-rev', 'BOOK')],
'BOFB': [('BOOK', 'BOOK-about-ORGANIZATION', 'ORGANIZATION'),
('ORGANIZATION', 'ORGANIZATION-in-FILM', 'FILM'),
('FILM', 'BOOK-to-FILM-rev', 'BOOK')],
'BLMB': [('BOOK', 'BOOK-on-LOCATION', 'LOCATION'),
('LOCATION', 'MUSIC-on-LOCATION-rev', 'MUSIC'),
('MUSIC', 'MUSIC-in-BOOK', 'BOOK')],
'BPB': [('BOOK', 'PEOPLE-to-BOOK-rev', 'PEOPLE'),
('PEOPLE', 'PEOPLE-to-BOOK', 'BOOK')],
'BPSB': [('BOOK', 'PEOPLE-to-BOOK-rev', 'PEOPLE'),
('PEOPLE', 'PEOPLE-to-SPORTS', 'SPORTS'),
('SPORTS', 'BOOK-on-SPORTS-rev', 'BOOK')],
'BBuB': [('BOOK', 'BUSINESS-about-BOOK-rev', 'BUSINESS'),
('BUSINESS', 'BUSINESS-about-BOOK', 'BOOK')],
# 'BOMB': [('BOOK', 'BOOK-about-ORGANIZATION', 'ORGANIZATION'),
# ('ORGANIZATION', 'ORGANIZATION-to-MUSIC', 'MUSIC'),
# ('MUSIC', 'MUSIC-in-BOOK', 'BOOK')],
# 'BOBuB': [('BOOK', 'BOOK-about-ORGANIZATION', 'ORGANIZATION'),
# ('ORGANIZATION', 'ORGANIZATION-for-BUSINESS', 'BUSINESS'),
# ('BUSINESS', 'BUSINESS-about-BOOK', 'BOOK')]
}
# graph: dgl graph object, label: torch tensor of shape (num_nodes, num_tasks)
elif dataset_name == 'HGBn-IMDB':
dataset = HGBDataset(name=dataset_name, raw_dir='')
g = dataset[0].long()
category = 'movie'
num_classes = 5
self.meta_paths_dict = {
'MAM': [('movie', 'movie->actor', 'actor'), ('actor', 'actor->movie', 'movie')],
'MDM': [('movie', 'movie->director', 'director'), ('director', 'director->movie', 'movie')],
'MKM': [('movie', 'movie->keyword', 'keyword'), ('keyword', 'keyword->movie', 'movie')],
'DMD': [('director', 'director->movie', 'movie'), ('movie', 'movie->director', 'director')],
'DMAMD': [('director', 'director->movie', 'movie'), ('movie', 'movie->actor', 'actor'),
('actor', 'actor->movie', 'movie'), ('movie', 'movie->director', 'director')],
'AMA': [('actor', 'actor->movie', 'movie'), ('movie', 'movie->actor', 'actor')],
'AMDMA': [('actor', 'actor->movie', 'movie'), ('movie', 'movie->director', 'director'),
('director', 'director->movie', 'movie'), ('movie', 'movie->actor', 'actor')]
}
# RuntimeError: result type Float can't be cast to the desired output type Long
self.multi_label = True
else:
raise ValueError
self.g, self.category, self.num_classes = g, category, num_classes
def save_results(self, logits, file_path):
r"""
To save test results of HGBn.
Parameters
----------
logits: th.Tensor
The prediction of target nodes.
file_path : str
The path to save file.
"""
test_logits = logits[self.test_idx]
if self.dataset_name == 'HGBn-IMDB':
pred = (test_logits.cpu().numpy() > 0).astype(int)
multi_label = []
for i in range(pred.shape[0]):
label_list = [str(j) for j in range(pred[i].shape[0]) if pred[i][j] == 1]
multi_label.append(','.join(label_list))
pred = multi_label
elif self.dataset_name in ['HGBn-ACM', 'HGBn-DBLP', 'HGBn-Freebase']: