-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtraining.py
237 lines (200 loc) · 10.6 KB
/
training.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
234
235
236
237
import torch
import tqdm
from sklearn.metrics import f1_score
from train_util import AddEgoIds, extract_param, add_arange_ids, get_loaders, evaluate_homo, evaluate_hetero, save_model, load_model
from models import GINe, PNA, GATe, RGCN
from torch_geometric.data import Data, HeteroData
from torch_geometric.nn import to_hetero, summary
from torch_geometric.utils import degree
import wandb
import logging
def train_homo(tr_loader, val_loader, te_loader, tr_inds, val_inds, te_inds, model, optimizer, loss_fn, args, config, device, val_data, te_data, data_config):
#training
best_val_f1 = 0
for epoch in range(config.epochs):
total_loss = total_examples = 0
preds = []
ground_truths = []
for batch in tqdm.tqdm(tr_loader, disable=not args.tqdm):
optimizer.zero_grad()
#select the seed edges from which the batch was created
inds = tr_inds.detach().cpu()
batch_edge_inds = inds[batch.input_id.detach().cpu()]
batch_edge_ids = tr_loader.data.edge_attr.detach().cpu()[batch_edge_inds, 0]
mask = torch.isin(batch.edge_attr[:, 0].detach().cpu(), batch_edge_ids)
#remove the unique edge id from the edge features, as it's no longer needed
batch.edge_attr = batch.edge_attr[:, 1:]
batch.to(device)
out = model(batch.x, batch.edge_index, batch.edge_attr)
pred = out[mask]
ground_truth = batch.y[mask]
preds.append(pred.argmax(dim=-1))
ground_truths.append(ground_truth)
loss = loss_fn(pred, ground_truth)
loss.backward()
optimizer.step()
total_loss += float(loss) * pred.numel()
total_examples += pred.numel()
pred = torch.cat(preds, dim=0).detach().cpu().numpy()
ground_truth = torch.cat(ground_truths, dim=0).detach().cpu().numpy()
f1 = f1_score(ground_truth, pred)
wandb.log({"f1/train": f1}, step=epoch)
logging.info(f'Train F1: {f1:.4f}')
#evaluate
val_f1 = evaluate_homo(val_loader, val_inds, model, val_data, device, args)
te_f1 = evaluate_homo(te_loader, te_inds, model, te_data, device, args)
wandb.log({"f1/validation": val_f1}, step=epoch)
wandb.log({"f1/test": te_f1}, step=epoch)
logging.info(f'Validation F1: {val_f1:.4f}')
logging.info(f'Test F1: {te_f1:.4f}')
if epoch == 0:
wandb.log({"best_test_f1": te_f1}, step=epoch)
elif val_f1 > best_val_f1:
best_val_f1 = val_f1
wandb.log({"best_test_f1": te_f1}, step=epoch)
if args.save_model:
save_model(model, optimizer, epoch, args, data_config)
return model
def train_hetero(tr_loader, val_loader, te_loader, tr_inds, val_inds, te_inds, model, optimizer, loss_fn, args, config, device, val_data, te_data, data_config):
#training
best_val_f1 = 0
for epoch in range(config.epochs):
total_loss = total_examples = 0
preds = []
ground_truths = []
for batch in tqdm.tqdm(tr_loader, disable=not args.tqdm):
optimizer.zero_grad()
#select the seed edges from which the batch was created
inds = tr_inds.detach().cpu()
batch_edge_inds = inds[batch['node', 'to', 'node'].input_id.detach().cpu()]
batch_edge_ids = tr_loader.data['node', 'to', 'node'].edge_attr.detach().cpu()[batch_edge_inds, 0]
mask = torch.isin(batch['node', 'to', 'node'].edge_attr[:, 0].detach().cpu(), batch_edge_ids)
#remove the unique edge id from the edge features, as it's no longer needed
batch['node', 'to', 'node'].edge_attr = batch['node', 'to', 'node'].edge_attr[:, 1:]
batch['node', 'rev_to', 'node'].edge_attr = batch['node', 'rev_to', 'node'].edge_attr[:, 1:]
batch.to(device)
out = model(batch.x_dict, batch.edge_index_dict, batch.edge_attr_dict)
out = out[('node', 'to', 'node')]
pred = out[mask]
ground_truth = batch['node', 'to', 'node'].y[mask]
preds.append(pred.argmax(dim=-1))
ground_truths.append(batch['node', 'to', 'node'].y[mask])
loss = loss_fn(pred, ground_truth)
loss.backward()
optimizer.step()
total_loss += float(loss) * pred.numel()
total_examples += pred.numel()
pred = torch.cat(preds, dim=0).detach().cpu().numpy()
ground_truth = torch.cat(ground_truths, dim=0).detach().cpu().numpy()
f1 = f1_score(ground_truth, pred)
wandb.log({"f1/train": f1}, step=epoch)
logging.info(f'Train F1: {f1:.4f}')
#evaluate
val_f1 = evaluate_hetero(val_loader, val_inds, model, val_data, device, args)
te_f1 = evaluate_hetero(te_loader, te_inds, model, te_data, device, args)
wandb.log({"f1/validation": val_f1}, step=epoch)
wandb.log({"f1/test": te_f1}, step=epoch)
logging.info(f'Validation F1: {val_f1:.4f}')
logging.info(f'Test F1: {te_f1:.4f}')
if epoch == 0:
wandb.log({"best_test_f1": te_f1}, step=epoch)
elif val_f1 > best_val_f1:
best_val_f1 = val_f1
wandb.log({"best_test_f1": te_f1}, step=epoch)
if args.save_model:
save_model(model, optimizer, epoch, args, data_config)
return model
def get_model(sample_batch, config, args):
n_feats = sample_batch.x.shape[1] if not isinstance(sample_batch, HeteroData) else sample_batch['node'].x.shape[1]
e_dim = (sample_batch.edge_attr.shape[1] - 1) if not isinstance(sample_batch, HeteroData) else (sample_batch['node', 'to', 'node'].edge_attr.shape[1] - 1)
if args.model == "gin":
model = GINe(
num_features=n_feats, num_gnn_layers=config.n_gnn_layers, n_classes=2,
n_hidden=round(config.n_hidden), residual=False, edge_updates=args.emlps, edge_dim=e_dim,
dropout=config.dropout, final_dropout=config.final_dropout
)
elif args.model == "gat":
model = GATe(
num_features=n_feats, num_gnn_layers=config.n_gnn_layers, n_classes=2,
n_hidden=round(config.n_hidden), n_heads=round(config.n_heads),
edge_updates=args.emlps, edge_dim=e_dim,
dropout=config.dropout, final_dropout=config.final_dropout
)
elif args.model == "pna":
if not isinstance(sample_batch, HeteroData):
d = degree(sample_batch.edge_index[1], dtype=torch.long)
else:
index = torch.cat((sample_batch['node', 'to', 'node'].edge_index[1], sample_batch['node', 'rev_to', 'node'].edge_index[1]), 0)
d = degree(index, dtype=torch.long)
deg = torch.bincount(d, minlength=1)
model = PNA(
num_features=n_feats, num_gnn_layers=config.n_gnn_layers, n_classes=2,
n_hidden=round(config.n_hidden), edge_updates=args.emlps, edge_dim=e_dim,
dropout=config.dropout, deg=deg, final_dropout=config.final_dropout
)
elif config.model == "rgcn":
model = RGCN(
num_features=n_feats, edge_dim=e_dim, num_relations=8, num_gnn_layers=round(config.n_gnn_layers),
n_classes=2, n_hidden=round(config.n_hidden),
edge_update=args.emlps, dropout=config.dropout, final_dropout=config.final_dropout, n_bases=None #(maybe)
)
return model
def train_gnn(tr_data, val_data, te_data, tr_inds, val_inds, te_inds, args, data_config):
#set device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#define a model config dictionary and wandb logging at the same time
wandb.init(
mode="disabled" if args.testing else "online",
project="your_proj_name", #replace this with your wandb project name if you want to use wandb logging
config={
"epochs": args.n_epochs,
"batch_size": args.batch_size,
"model": args.model,
"data": args.data,
"num_neighbors": args.num_neighs,
"lr": extract_param("lr", args),
"n_hidden": extract_param("n_hidden", args),
"n_gnn_layers": extract_param("n_gnn_layers", args),
"loss": "ce",
"w_ce1": extract_param("w_ce1", args),
"w_ce2": extract_param("w_ce2", args),
"dropout": extract_param("dropout", args),
"final_dropout": extract_param("final_dropout", args),
"n_heads": extract_param("n_heads", args) if args.model == 'gat' else None
}
)
config = wandb.config
#set the transform if ego ids should be used
if args.ego:
transform = AddEgoIds()
else:
transform = None
#add the unique ids to later find the seed edges
add_arange_ids([tr_data, val_data, te_data])
tr_loader, val_loader, te_loader = get_loaders(tr_data, val_data, te_data, tr_inds, val_inds, te_inds, transform, args)
#get the model
sample_batch = next(iter(tr_loader))
model = get_model(sample_batch, config, args)
if args.reverse_mp:
model = to_hetero(model, te_data.metadata(), aggr='mean')
if args.finetune:
model, optimizer = load_model(model, device, args, config, data_config)
else:
model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=config.lr)
sample_batch.to(device)
sample_x = sample_batch.x if not isinstance(sample_batch, HeteroData) else sample_batch.x_dict
sample_edge_index = sample_batch.edge_index if not isinstance(sample_batch, HeteroData) else sample_batch.edge_index_dict
if isinstance(sample_batch, HeteroData):
sample_batch['node', 'to', 'node'].edge_attr = sample_batch['node', 'to', 'node'].edge_attr[:, 1:]
sample_batch['node', 'rev_to', 'node'].edge_attr = sample_batch['node', 'rev_to', 'node'].edge_attr[:, 1:]
else:
sample_batch.edge_attr = sample_batch.edge_attr[:, 1:]
sample_edge_attr = sample_batch.edge_attr if not isinstance(sample_batch, HeteroData) else sample_batch.edge_attr_dict
logging.info(summary(model, sample_x, sample_edge_index, sample_edge_attr))
loss_fn = torch.nn.CrossEntropyLoss(weight=torch.FloatTensor([config.w_ce1, config.w_ce2]).to(device))
if args.reverse_mp:
model = train_hetero(tr_loader, val_loader, te_loader, tr_inds, val_inds, te_inds, model, optimizer, loss_fn, args, config, device, val_data, te_data, data_config)
else:
model = train_homo(tr_loader, val_loader, te_loader, tr_inds, val_inds, te_inds, model, optimizer, loss_fn, args, config, device, val_data, te_data, data_config)
wandb.finish()