-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
30 lines (25 loc) · 1.01 KB
/
utils.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
import torch
import math
import numpy as np
def index_to_mask(index, size):
mask = torch.zeros(size, dtype=torch.bool)
mask[index] = 1
return mask
def random_planetoid_splits(data, num_classes, percls_trn=20, val_lb=500, seed=12134):
index=[i for i in range(0,data.y.shape[0])]
train_idx=[]
rnd_state = np.random.RandomState(seed)
for c in range(num_classes):
class_idx = np.where(data.y.cpu() == c)[0]
if len(class_idx)<percls_trn:
train_idx.extend(class_idx)
else:
train_idx.extend(rnd_state.choice(class_idx, percls_trn,replace=False))
rest_index = [i for i in index if i not in train_idx]
val_idx=rnd_state.choice(rest_index,val_lb,replace=False)
test_idx=[i for i in rest_index if i not in val_idx]
#print(test_idx)
data.train_mask = index_to_mask(train_idx,size=data.num_nodes)
data.val_mask = index_to_mask(val_idx,size=data.num_nodes)
data.test_mask = index_to_mask(test_idx,size=data.num_nodes)
return data