-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
292 lines (252 loc) · 12.7 KB
/
train.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import copy
import datetime
import json
import os
import time
import warnings
import dgl
import enlighten
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
from utils.early_stop import EarlyStopping
from utils.other import process_metadata, create_model, evaluate, deep_apply, is_single_relation_model, \
get_dataset_name, is_graph_model, time_since, dict2table
warnings.filterwarnings('ignore')
torch.multiprocessing.set_sharing_strategy('file_system')
def train(args, logger):
with open(args.metadata_path) as f:
metadata = json.load(f)
metadata = process_metadata(metadata, args)
logger.info(metadata)
model = create_model(metadata, logger, args).to(args.device)
logger.info(model)
task_to_evaluate = [x['name'] for x in metadata['task'] if x['evaluation']]
model.train()
best_checkpoint_dict = {
task: copy.deepcopy(model.state_dict())
for task in task_to_evaluate
}
best_val_metrics_dict = {}
criterions = {}
if is_single_relation_model(args):
assert len(metadata['task']) == 1
for task in metadata['task']:
# criterions
if task['type'] == 'top-k-recommendation':
original_loss_map = {
'NGCF': 'bpr',
'HET-NGCF': 'bpr',
# TODO
}
if args.model_name in original_loss_map and task['loss'] != original_loss_map[args.model_name]:
logger.warning(
'You are using a different type of loss with the type in the paper'
)
if task['loss'] == 'binary-cross-entropy':
criterions[task['name']] = nn.BCEWithLogitsLoss()
else:
raise NotImplementedError
elif task['type'] == 'interaction-attribute-regression':
raise NotImplementedError
else:
raise NotImplementedError
optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate)
loss_full = []
early_stopping_dict = {
task['name']: EarlyStopping(args.early_stop_patience)
for task in metadata['task']
}
start_time = time.time()
writer = SummaryWriter(log_dir=os.path.join(
args.tensorboard_runs_path,
f'{args.model_name}-{get_dataset_name(args.dataset_path)}',
f"{str(datetime.datetime.now().replace(microsecond=0)).replace(' ', '_').replace(':', '-')}{'-remark-' + os.environ['REMARK'] if 'REMARK' in os.environ else ''}",
))
if args.save_checkpoint:
for task_name in task_to_evaluate:
os.makedirs(os.path.join(
args.checkpoint_path,
f'{args.model_name}-{get_dataset_name(args.dataset_path)}',
task_name,
),
exist_ok=True)
enlighten_manager = enlighten.get_manager()
batch = 0
if is_graph_model(args):
etype2num_neighbors = {
etype: np.clip(
np.quantile(model.graph.in_degrees(etype=etype),
args.neighbors_sampling_quantile,
method='nearest'), # `interpolation` was renamed to `method`
args.min_neighbors_sampled, args.max_neighbors_sampled)
for etype in model.graph.canonical_etypes
}
logger.debug(f'Neighbors sampled {etype2num_neighbors}')
try:
with enlighten_manager.counter(total=args.num_epochs,
desc='Training epochs',
unit='epochs') as epoch_pbar:
for epoch in epoch_pbar(range(1, args.num_epochs + 1)):
if is_graph_model(args):
neighbor_sampler = dgl.dataloading.MultiLayerNeighborSampler(
[etype2num_neighbors] *
(len(args.graph_embedding_dims) - 1))
else:
neighbor_sampler = dgl.dataloading.MultiLayerNeighborSampler([0])
def edge_sampling(etype):
df = pd.DataFrame(
torch.stack(model.graph.edges(etype=etype), dim=1).numpy())
return df.sample(frac=1).drop_duplicates(0).index.values
eid_dict = {
etype: edge_sampling(etype)
for etype in model.primary_etypes
}
# parse reverse_etypes
etypes = copy.deepcopy(model.graph.canonical_etypes)
reverse_etypes = {}
for etype in model.graph.canonical_etypes:
if etype[1].endswith('-by'):
reverse_etype = (etype[2], etype[1][:-len('-by')], etype[0]) # ('user', 'gift', 'room')
reverse_etypes[etype] = reverse_etype # ('room', 'gift-by', 'user'):('user', 'gift', 'room')
reverse_etypes[reverse_etype] = etype # ('user', 'gift', 'room'):('room', 'gift-by', 'user')
etypes.remove(etype)
etypes.remove(reverse_etype)
assert len(etypes) == 0
sampler = dgl.dataloading.as_edge_prediction_sampler(
neighbor_sampler, exclude='reverse_types',
reverse_etypes=reverse_etypes,
negative_sampler=dgl.dataloading.negative_sampler.Uniform(
args.negative_sampling_ratio))
dataloader = dgl.dataloading.DataLoader(
model.graph, eid_dict, sampler,
batch_size=args.batch_size,
shuffle=True,
drop_last=False,
num_workers=args.num_workers
)
with enlighten_manager.counter(total=len(dataloader),
desc='Training batches',
unit='batches',
leave=False) as batch_pbar:
for input_nodes, positive_graph, negative_graph, blocks in batch_pbar(dataloader):
batch += 1
if is_graph_model(args):
if batch == 1:
node_coverage = {
k: len(v) / model.graph.num_nodes(k)
for k, v in input_nodes.items()
}
logger.debug(
f'Node coverage {deep_apply(node_coverage)}'
)
input_nodes = {
k: v.to(args.device)
for k, v in input_nodes.items()
}
positive_graph = positive_graph.to(args.device)
negative_graph = negative_graph.to(args.device)
if is_graph_model(args):
blocks = [block.to(args.device) for block in blocks]
output_embeddings = model.aggregate_embeddings(input_nodes, blocks)
else:
output_embeddings = None
loss = 0
for task in metadata['task']:
positive_index = torch.stack(
positive_graph.edges(etype=task['scheme']))
negative_index = torch.stack(
negative_graph.edges(etype=task['scheme']))
index = torch.cat((positive_index, negative_index),
dim=1)
if not is_graph_model(args):
# map indexs
index[0] = positive_graph.ndata[dgl.NID][
task['scheme'][0]][index[0]]
index[1] = positive_graph.ndata[dgl.NID][
task['scheme'][2]][index[1]]
first = {
'name': task['scheme'][0],
'index': index[0]
}
second = {
'name': task['scheme'][2],
'index': index[1]
}
y_pred = model(first, second, task['name'], output_embeddings)
if task['loss'] == 'binary-cross-entropy':
y_true = torch.cat(
(torch.ones(positive_index.size(1)), torch.zeros(negative_index.size(1)))).to(
args.device)
task_loss = criterions[task['name']](y_pred, y_true)
else:
raise NotImplementedError
if task['filename'] == "user-room-gift.csv":
weight = args.lamb_1
elif task['filename'] == "user-room-chat.csv":
weight = args.lamb_2
else:
weight = 1 - args.lamb_1 - args.lamb_2
loss += task_loss * weight
if len(metadata['task']) > 1:
writer.add_scalar(f"Train/Loss/{task['name']}",
task_loss.item(), batch)
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss_full.append(loss.item())
writer.add_scalar('Train/Loss', loss.item(), batch)
if batch % args.num_batches_show_loss == 0:
logger.info(
f"Time {time_since(start_time)}, epoch {epoch}, batch {batch}, current loss {loss.item():.4f}, average loss {np.mean(loss_full):.4f}, latest average loss {np.mean(loss_full[-10:]):.4f}"
)
if epoch % args.num_epochs_validate == 0:
model.eval()
metrics, overall = evaluate(model, [
x for x in metadata['task']
if x['name'] in task_to_evaluate
], 'val', args)
model.train()
for task_name, values in metrics.items():
for metric, value in values.items():
writer.add_scalar(
f'Validation/{task_name}/{metric}', value,
epoch)
logger.info(
f"Time {time_since(start_time)}, epoch {epoch}, metrics {deep_apply(metrics)}"
)
for task_name in copy.deepcopy(task_to_evaluate):
early_stop, get_better = early_stopping_dict[
task_name](-overall[task_name])
if early_stop:
task_to_evaluate.remove(task_name)
logger.info(f'Task {task_name} early stopped')
elif get_better:
best_checkpoint_dict[task_name] = copy.deepcopy(
model.state_dict())
best_val_metrics_dict[task_name] = copy.deepcopy(
metrics[task_name])
if args.save_checkpoint:
torch.save(
{'model_state_dict': model.state_dict()},
os.path.join(
args.checkpoint_path,
f'{args.model_name}-{get_dataset_name(args.dataset_path)}',
task_name, f'ckpt-{epoch}.pt'))
if not task_to_evaluate:
logger.info('All tasks early stopped')
break
except KeyboardInterrupt:
logger.info('Stop in advance')
logger.info(
f'Best metrics on validation set\n{dict2table(best_val_metrics_dict)}')
test_metrics_dict = {}
for task_name, checkpoint in best_checkpoint_dict.items():
model.load_state_dict(checkpoint)
model.eval()
metrics, _ = evaluate(
model, [x for x in metadata['task'] if x['name'] == task_name], 'test', args)
test_metrics_dict[task_name] = metrics[task_name]
logger.info(f'Metrics on test set\n{dict2table(test_metrics_dict)}')