import dgl import torch import pickle from ogb.nodeproppred import DglNodePropPredDataset dataset = DglNodePropPredDataset(name="ogbn-arxiv") g = dataset[0][0] total_edges = torch.stack(g.edges()).t() num_val_links = 30000 num_test_links = 60000 num_train_links = 210000 assert num_train_links + num_test_links + num_val_links < len( total_edges ), "Not enough edges to sample " # *100 here means we can set negative ratio for upto 100 when training for link prediciton downstream task negatives = torch.stack( g.global_uniform_negative_sampling( num_val_links + num_test_links + num_train_links * 100 ) ).t() test_edges_false, val_edges_false, train_edges_false = ( negatives[:num_test_links].numpy(), negatives[num_test_links : num_test_links + num_val_links].numpy(), negatives[num_test_links + num_val_links :].numpy(), ) indices = torch.randperm(len(total_edges)).numpy() total_edges = total_edges[indices] test_edges, val_edges, train_edges = ( total_edges[:num_test_links].numpy(), total_edges[num_test_links : num_test_links + num_val_links].numpy(), total_edges[ num_test_links + num_val_links : num_test_links + num_val_links + num_train_links ].numpy(), ) tvt_edges_file = "links/arxiv_tvtEdges.pkl" pickle.dump( ( train_edges, train_edges_false, val_edges, val_edges_false, test_edges, test_edges_false, ), open(tvt_edges_file, "wb"), ) node_assignment = dgl.metis_partition_assignment(g, 10) torch.save(node_assignment, "pretrain_labels/metis_label_arxiv.pt")