forked from ChenJY-Count/PolyGCL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
50 lines (44 loc) · 1.37 KB
/
utils.py
File metadata and controls
50 lines (44 loc) · 1.37 KB
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
import torch
import math
import numpy as np
import random
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def cheby(i,x):
if i==0:
return 1
elif i==1:
return x
else:
T0=1
T1=x
for ii in range(2,i+1):
T2=2*x*T1-T0
T0,T1=T1,T2
return T2
def index_to_mask(index, size):
mask = torch.zeros(size, dtype=torch.bool)
mask[index] = 1
return mask
def random_splits(label, num_classes, percls_trn, val_lb, seed=42):
num_nodes = label.shape[0]
index=[i for i in range(num_nodes)]
train_idx=[]
rnd_state = np.random.RandomState(seed)
for c in range(num_classes):
class_idx = np.where(label.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]
train_mask = index_to_mask(train_idx,size=num_nodes)
val_mask = index_to_mask(val_idx,size=num_nodes)
test_mask = index_to_mask(test_idx,size=num_nodes)
return train_mask, val_mask, test_mask