-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
233 lines (186 loc) · 8.24 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
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
from torch_geometric.datasets import Planetoid, Coauthor, Amazon, WikiCS, CoraFull
from torch_geometric.utils import dropout_adj
import torch.nn.functional as F
import os.path as osp
import os
import numpy as np
# np.random.seed(0)
import torch
import torch.nn as nn
torch.manual_seed(0)
torch.cuda.manual_seed_all(0)
# torch.backends.cudnn.deterministic = True
# torch.backends.cudnn.benchmark = False
from datetime import datetime
def currentTime():
return datetime.now().strftime('%Y-%m-%d %H:%M:%S')
class Augmentation:
def __init__(self, p_f1=0.2, p_f2=0.1, p_e1=0.2, p_e2=0.3):
"""
two simple graph augmentation functions --> "Node feature masking" and "Edge masking"
Random binary node feature mask following Bernoulli distribution with parameter p_f
Random binary edge mask following Bernoulli distribution with parameter p_e
"""
self.p_f1 = p_f1
self.p_f2 = p_f2
self.p_e1 = p_e1
self.p_e2 = p_e2
self.method = "BGRL"
def _feature_masking(self, data, device):
feat_mask1 = torch.FloatTensor(data.x.shape[1]).uniform_() > self.p_f1
feat_mask2 = torch.FloatTensor(data.x.shape[1]).uniform_() > self.p_f2
feat_mask1, feat_mask2 = feat_mask1.to(device), feat_mask2.to(device)
x1, x2 = data.x.clone(), data.x.clone()
x1, x2 = x1 * feat_mask1, x2 * feat_mask2
edge_index1, edge_attr1 = dropout_adj(data.edge_index, data.edge_attr, p=self.p_e1)
edge_index2, edge_attr2 = dropout_adj(data.edge_index, data.edge_attr, p=self.p_e2)
new_data1, new_data2 = data.clone(), data.clone()
new_data1.x, new_data2.x = x1, x2
new_data1.edge_index, new_data2.edge_index = edge_index1, edge_index2
new_data1.edge_attr, new_data2.edge_attr = edge_attr1, edge_attr2
return new_data1, new_data2
def __call__(self, data):
return self._feature_masking(data)
def decide_config(root, dataset):
"""
Create a configuration to download datasets
:param root: A path to a root directory where data will be stored
:param dataset: The name of the dataset to be downloaded
:return: A modified root dir, the name of the dataset class, and parameters associated to the class
"""
dataset = dataset.lower()
if dataset == 'cora' or dataset == 'citeseer' or dataset == "pubmed":
root = osp.join(root, "pyg", "planetoid")
params = {"kwargs": {"root": root, "name": dataset},
"name": dataset, "class": Planetoid, "src": "pyg"}
elif dataset == "computers":
dataset = "Computers"
root = osp.join(root, "pyg")
params = {"kwargs": {"root": root, "name": dataset},
"name": dataset, "class": Amazon, "src": "pyg"}
elif dataset == "photo":
dataset = "Photo"
root = osp.join(root, "pyg")
params = {"kwargs": {"root": root, "name": dataset},
"name": dataset, "class": Amazon, "src": "pyg"}
elif dataset == "cs" :
dataset = "CS"
root = osp.join(root, "pyg")
params = {"kwargs": {"root": root, "name": dataset},
"name": dataset, "class": Coauthor, "src": "pyg"}
elif dataset == "physics":
dataset = "Physics"
root = osp.join(root, "pyg")
params = {"kwargs": {"root": root, "name": dataset},
"name": dataset, "class": Coauthor, "src": "pyg"}
elif dataset == "wikics":
dataset = "WikiCS"
root = osp.join(root, "pyg")
params = {"kwargs": {"root": root},
"name": dataset, "class": WikiCS, "src": "pyg"}
elif dataset == "corafull":
dataset = "CoraFull"
root = osp.join(root, "pyg")
params = {"kwargs": {"root": root},
"name": dataset, "class": CoraFull, "src": "pyg"}
else:
raise Exception(
f"Unknown dataset name {dataset}, name has to be one of the following 'cora', 'citeseer', 'pubmed', 'photo', 'computers', 'cs', 'physics'")
return params
def create_dirs(dirs):
for dir_tree in dirs:
sub_dirs = dir_tree.split("/")
path = ""
for sub_dir in sub_dirs:
path = osp.join(path, sub_dir)
os.makedirs(path, exist_ok=True)
def create_masks(data, name, public = None):
"""
Splits data into training, validation, and test splits in a stratified manner if
it is not already splitted. Each split is associated with a mask vector, which
specifies the indices for that split. The data will be modified in-place
:param data: Data object
:return: The modified data
"""
if public != None :
train_idx = public["train"].numpy()
valid_idx = public["valid"].numpy()
test_idx = public["test"].numpy()
_train_mask = _val_mask = _test_mask = None
for i in range(20):
data_index = np.arange(data.y.shape[0])
train_mask = torch.tensor(np.in1d(data_index, train_idx), dtype=torch.bool)
test_mask = torch.tensor(np.in1d(data_index, test_idx), dtype=torch.bool)
valid_mask = torch.tensor(np.in1d(data_index, valid_idx), dtype=torch.bool)
test_mask = test_mask.reshape(1, -1)
valid_mask = valid_mask.reshape(1, -1)
train_mask = train_mask.reshape(1, -1)
if _train_mask is None:
_train_mask = train_mask
_val_mask = valid_mask
_test_mask = test_mask
else:
_train_mask = torch.cat((_train_mask, train_mask), dim=0)
_val_mask = torch.cat((_val_mask, valid_mask), dim=0)
_test_mask = torch.cat((_test_mask, test_mask), dim=0)
data.train_mask = _train_mask
data.val_mask = _val_mask
data.test_mask = _test_mask
elif name != "WikiCS":
_train_mask = _val_mask = _test_mask = None
for i in range(20):
labels = data.y.numpy()
dev_size = int(labels.shape[0] * 0.1)
test_size = int(labels.shape[0] * 0.8)
perm = np.random.permutation(labels.shape[0])
test_index = perm[:test_size]
dev_index = perm[test_size:test_size + dev_size]
data_index = np.arange(labels.shape[0])
test_mask = torch.tensor(np.in1d(data_index, test_index), dtype=torch.bool)
dev_mask = torch.tensor(np.in1d(data_index, dev_index), dtype=torch.bool)
train_mask = ~(dev_mask + test_mask)
test_mask = test_mask.reshape(1, -1)
dev_mask = dev_mask.reshape(1, -1)
train_mask = train_mask.reshape(1, -1)
if _train_mask is None:
_train_mask = train_mask
_val_mask = dev_mask
_test_mask = test_mask
else:
_train_mask = torch.cat((_train_mask, train_mask), dim=0)
_val_mask = torch.cat((_val_mask, dev_mask), dim=0)
_test_mask = torch.cat((_test_mask, test_mask), dim=0)
data.train_mask = _train_mask
data.val_mask = _val_mask
data.test_mask = _test_mask
else: # in the case of WikiCS
data.train_mask = data.train_mask.T
data.val_mask = data.val_mask.T
return data
class EMA:
def __init__(self, beta, epochs):
super().__init__()
self.beta = beta
self.step = 0
self.total_steps = epochs
def update_average(self, old, new):
if old is None:
return new
self.step += 1
return old * self.beta + (1 - self.beta) * new
def init_weights(m):
if type(m) == nn.Linear:
torch.nn.init.xavier_uniform_(m.weight)
m.bias.data.fill_(0.01)
def update_moving_average(ema_updater, ma_model, current_model):
for current_params, ma_params in zip(current_model.parameters(), ma_model.parameters()):
old_weight, up_weight = ma_params.data, current_params.data
ma_params.data = ema_updater.update_average(old_weight, up_weight)
def set_requires_grad(model, val):
for p in model.parameters():
p.requires_grad = val
class KLD(nn.Module):
def forward(self, inputs, targets):
inputs = F.log_softmax(inputs, dim=1)
targets = F.softmax(targets, dim=1)
return F.kl_div(inputs, targets, reduction='batchmean')