forked from liugangcode/GREA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
173 lines (153 loc) · 7.95 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
import torch
import argparse
from sklearn.metrics import r2_score
def get_args():
parser = argparse.ArgumentParser(description='Graph rationalization with Environment-based Augmentation')
parser.add_argument('--device', type=int, default=0,
help='which gpu to use if any (default: 0)')
# model
parser.add_argument('--gnn', type=str, default='gin-virtual',
help='GNN gin, gin-virtual, or gcn, or gcn-virtual (default: gin-virtual)')
parser.add_argument('--drop_ratio', type=float, default=0.5,
help='dropout ratio (default: 0.5)')
parser.add_argument('--num_layer', type=int, default=5,
help='number of GNN message passing layers (default: 5)')
parser.add_argument('--emb_dim', type=int, default=128,
help='dimensionality of hidden units in GNNs (default: 128)')
parser.add_argument('--use_linear_predictor', default=False, action='store_true',
help='Use Linear predictor')
parser.add_argument('--gamma', type=float, default=0.4,
help='size ratio to regularize the rationale subgraph (default: 0.4)')
# training
parser.add_argument('--batch_size', type=int, default=256,
help='input batch size for training (default: 256)')
parser.add_argument('--epochs', type=int, default=200,
help='number of epochs to train (default: 200)')
parser.add_argument('--patience', type=int, default=50,
help='patience for early stop (default: 50)')
parser.add_argument('--lr', type=float, default=1e-2,
help='Learning rate (default: 1e-2)')
parser.add_argument('--l2reg', type=float, default=5e-6,
help='L2 norm (default: 5e-6)')
parser.add_argument('--use_lr_scheduler', default=False, action='store_true',
help='Use learning rate scheduler CosineAnnealingLR')
parser.add_argument('--use_clip_norm', default=False, action='store_true',
help='Use learning rate clip norm')
parser.add_argument('--path_list', nargs="+", default=[1,4],
help='path for alternative optimization')
parser.add_argument('--initw_name', type=str, default='default',
choices=['default','orthogonal','normal','xavier','kaiming'],
help='method name to initialize neural weights')
parser.add_argument('--dataset', type=str, default="ogbg-molbbbp",
help='dataset name (default: ogbg-molhiv)')
parser.add_argument('--trails', type=int, default=5,
help='numer of experiments (default: 5)')
parser.add_argument('--by_default', default=False, action='store_true',
help='use default configuration for hyperparameters')
args = parser.parse_args()
return args
cls_criterion = torch.nn.BCEWithLogitsLoss()
reg_criterion = torch.nn.MSELoss()
def train(args, model, device, loader, optimizers, task_type, optimizer_name):
optimizer = optimizers[optimizer_name]
model.train()
if optimizer_name == 'predictor':
set_requires_grad([model.graph_encoder, model.predictor], requires_grad=True)
set_requires_grad([model.separator], requires_grad=False)
if optimizer_name == 'separator':
set_requires_grad([model.separator], requires_grad=True)
set_requires_grad([model.graph_encoder,model.predictor], requires_grad=False)
for step, batch in enumerate(loader):
batch = batch.to(device)
if batch.x.shape[0] == 1 or batch.batch[-1] == 0:
pass
else:
optimizer.zero_grad()
pred = model(batch)
if "classification" in task_type:
criterion = cls_criterion
else:
criterion = reg_criterion
if args.dataset.startswith('plym'):
if args.plym_prop == 'density':
batch.y = torch.log(batch[args.plym_prop])
else:
batch.y = batch[args.plym_prop]
target = batch.y.to(torch.float32)
is_labeled = batch.y == batch.y
loss = criterion(pred['pred_rem'].to(torch.float32)[is_labeled], target[is_labeled])
target_rep = batch.y.to(torch.float32).repeat_interleave(batch.batch[-1]+1,dim=0)
is_labeled_rep = target_rep == target_rep
loss += criterion(pred['pred_rep'].to(torch.float32)[is_labeled_rep], target_rep[is_labeled_rep])
if optimizer_name == 'separator':
loss += pred['loss_reg']
loss.backward()
if args.use_clip_norm:
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
def eval(args, model, device, loader, evaluator):
model.eval()
y_true = []
y_pred = []
for step, batch in enumerate(loader):
batch = batch.to(device)
if batch.x.shape[0] == 1:
pass
else:
with torch.no_grad():
pred = model.eval_forward(batch)
if args.dataset.startswith('plym'):
if args.plym_prop == 'density' :
batch.y = torch.log(batch[args.plym_prop])
else:
batch.y = batch[args.plym_prop]
y_true.append(batch.y.view(pred.shape).detach().cpu())
y_pred.append(pred.detach().cpu())
y_true = torch.cat(y_true, dim = 0).numpy()
y_pred = torch.cat(y_pred, dim = 0).numpy()
input_dict = {"y_true": y_true, "y_pred": y_pred}
if args.dataset.startswith('plym'):
return [evaluator.eval(input_dict)['rmse'], r2_score(y_true, y_pred)]
elif args.dataset.startswith('ogbg'):
return [evaluator.eval(input_dict)['rocauc']]
def init_weights(net, init_type='normal', init_gain=0.02):
"""Initialize network weights.
Parameters:
net (network) -- network to be initialized
init_type (str) -- the name of an initialization method: normal | xavier | kaiming | orthogonal
init_gain (float) -- scaling factor for normal, xavier and orthogonal.
"""
def init_func(m): # define the initialization function
classname = m.__class__.__name__
if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
if init_type == 'normal':
torch.nn.init.normal_(m.weight.data, 0.0, init_gain)
elif init_type == 'xavier':
torch.nn.init.xavier_normal_(m.weight.data, gain=init_gain)
elif init_type == 'kaiming':
torch.nn.init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif init_type == 'orthogonal':
torch.nn.init.orthogonal_(m.weight.data, gain=init_gain)
elif init_type == 'default':
pass
else:
raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
if hasattr(m, 'bias') and m.bias is not None:
torch.nn.init.constant_(m.bias.data, 0.0)
elif classname.find('BatchNorm2d') != -1: # BatchNorm Layer's weight is not a matrix; only normal distribution applies.
torch.nn.init.normal_(m.weight.data, 1.0, init_gain)
torch.nn.init.constant_(m.bias.data, 0.0)
print('initialize network with %s' % init_type)
net.apply(init_func) # apply the initialization function <init_func>
def set_requires_grad(nets, requires_grad=False):
"""Set requies_grad=Fasle for all the networks to avoid unnecessary computations
Parameters:
nets (network list) -- a list of networks
requires_grad (bool) -- whether the networks require gradients or not
"""
if not isinstance(nets, list):
nets = [nets]
for net in nets:
if net is not None:
for param in net.parameters():
param.requires_grad = requires_grad