SauravMaheshkar
commited on
Commit
•
0f3cb5b
1
Parent(s):
1666cdc
feat: update link generation scritp
Browse files- link_gen.py +56 -0
link_gen.py
CHANGED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import dgl
|
2 |
+
import torch
|
3 |
+
import pickle
|
4 |
+
from ogb.nodeproppred import DglNodePropPredDataset
|
5 |
+
|
6 |
+
dataset = DglNodePropPredDataset(name="ogbn-arxiv")
|
7 |
+
g = dataset[0][0]
|
8 |
+
total_edges = torch.stack(g.edges()).t()
|
9 |
+
|
10 |
+
num_val_links = 30000
|
11 |
+
num_test_links = 60000
|
12 |
+
num_train_links = 210000
|
13 |
+
|
14 |
+
assert num_train_links + num_test_links + num_val_links < len(
|
15 |
+
total_edges
|
16 |
+
), "Not enough edges to sample "
|
17 |
+
|
18 |
+
# *100 here means we can set negative ratio for upto 100 when training for link prediciton downstream task
|
19 |
+
negatives = torch.stack(
|
20 |
+
g.global_uniform_negative_sampling(
|
21 |
+
num_val_links + num_test_links + num_train_links * 100
|
22 |
+
)
|
23 |
+
).t()
|
24 |
+
test_edges_false, val_edges_false, train_edges_false = (
|
25 |
+
negatives[:num_test_links].numpy(),
|
26 |
+
negatives[num_test_links : num_test_links + num_val_links].numpy(),
|
27 |
+
negatives[num_test_links + num_val_links :].numpy(),
|
28 |
+
)
|
29 |
+
|
30 |
+
indices = torch.randperm(len(total_edges)).numpy()
|
31 |
+
total_edges = total_edges[indices]
|
32 |
+
test_edges, val_edges, train_edges = (
|
33 |
+
total_edges[:num_test_links].numpy(),
|
34 |
+
total_edges[num_test_links : num_test_links + num_val_links].numpy(),
|
35 |
+
total_edges[
|
36 |
+
num_test_links
|
37 |
+
+ num_val_links : num_test_links
|
38 |
+
+ num_val_links
|
39 |
+
+ num_train_links
|
40 |
+
].numpy(),
|
41 |
+
)
|
42 |
+
|
43 |
+
tvt_edges_file = "links/arxiv_tvtEdges.pkl"
|
44 |
+
pickle.dump(
|
45 |
+
(
|
46 |
+
train_edges,
|
47 |
+
train_edges_false,
|
48 |
+
val_edges,
|
49 |
+
val_edges_false,
|
50 |
+
test_edges,
|
51 |
+
test_edges_false,
|
52 |
+
),
|
53 |
+
open(tvt_edges_file, "wb"),
|
54 |
+
)
|
55 |
+
node_assignment = dgl.metis_partition_assignment(g, 10)
|
56 |
+
torch.save(node_assignment, "pretrain_labels/metis_label_arxiv.pt")
|