-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlogger.py
285 lines (251 loc) · 11 KB
/
logger.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
import logging
import time
import numpy as np
import torch
from scipy.stats import stats
from sklearn.metrics import accuracy_score, precision_score, recall_score, \
f1_score, roc_auc_score, mean_absolute_error, mean_squared_error
from sklearn.metrics import r2_score
from torch_geometric.graphgym import get_current_gpu_usage
from torch_geometric.graphgym.config import cfg
from torch_geometric.graphgym.logger import infer_task, Logger
from torch_geometric.graphgym.utils.io import dict_to_json, dict_to_tb
from torchmetrics.functional import auroc
import graphgps.metrics_ogb as metrics_ogb
from graphgps.metric_wrapper import MetricWrapper
class CustomLogger(Logger):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Whether to run comparison tests of alternative score implementations.
self.test_scores = False
# basic properties
def basic(self):
stats = {
'loss': round(self._loss / self._size_current, max(8, cfg.round)),
'lr': round(self._lr, max(8, cfg.round)),
'params': self._params,
'time_iter': round(self.time_iter(), cfg.round),
}
gpu_memory = get_current_gpu_usage()
if gpu_memory > 0:
stats['gpu_memory'] = gpu_memory
return stats
# task properties
def classification_binary(self):
true = torch.cat(self._true).squeeze(-1)
pred_score = torch.cat(self._pred)
pred_int = self._get_pred_int(pred_score)
if true.shape[0] < 1e7: # AUROC computation for very large datasets is too slow.
# TorchMetrics AUROC on GPU if available.
auroc_score = auroc(pred_score.to(torch.device(cfg.device)),
true.to(torch.device(cfg.device)),
pos_label=1)
if self.test_scores:
# SK-learn version.
try:
r_a_score = roc_auc_score(true.cpu().numpy(),
pred_score.cpu().numpy())
except ValueError:
r_a_score = 0.0
assert np.isclose(float(auroc_score), r_a_score)
else:
auroc_score = 0.
reformat = lambda x: round(float(x), cfg.round)
return {
'accuracy': reformat(accuracy_score(true, pred_int)),
'precision': reformat(precision_score(true, pred_int)),
'recall': reformat(recall_score(true, pred_int)),
'f1': reformat(f1_score(true, pred_int)),
'auc': reformat(auroc_score),
}
def classification_multi(self):
true, pred_score = torch.cat(self._true), torch.cat(self._pred)
pred_int = self._get_pred_int(pred_score)
reformat = lambda x: round(float(x), cfg.round)
res = {
'accuracy': reformat(accuracy_score(true, pred_int)),
'f1': reformat(f1_score(true, pred_int,
average='macro', zero_division=0)),
}
if true.shape[0] < 1e7:
# AUROC computation for very large datasets runs out of memory.
# TorchMetrics AUROC on GPU is much faster than sklearn for large ds
res['auc'] = reformat(auroc(pred_score.to(torch.device(cfg.device)),
true.to(torch.device(cfg.device)).squeeze(),
num_classes=pred_score.shape[1],
average='macro'))
if self.test_scores:
# SK-learn version.
sk_auc = reformat(roc_auc_score(true, pred_score.exp(),
average='macro',
multi_class='ovr'))
assert np.isclose(sk_auc, res['auc'])
return res
def classification_multilabel(self):
true, pred_score = torch.cat(self._true), torch.cat(self._pred)
reformat = lambda x: round(float(x), cfg.round)
# Send to GPU to speed up TorchMetrics if possible.
true = true.to(torch.device(cfg.device))
pred_score = pred_score.to(torch.device(cfg.device))
acc = MetricWrapper(metric='accuracy',
target_nan_mask='ignore-mean-label',
threshold=0.,
cast_to_int=True)
ap = MetricWrapper(metric='averageprecision',
target_nan_mask='ignore-mean-label',
pos_label=1,
cast_to_int=True)
auroc = MetricWrapper(metric='auroc',
target_nan_mask='ignore-mean-label',
pos_label=1,
cast_to_int=True)
results = {
'accuracy': reformat(acc(pred_score, true)),
'ap': reformat(ap(pred_score, true)),
'auc': reformat(auroc(pred_score, true)),
}
if self.test_scores:
# Compute metric by OGB Evaluator methods.
true = true.cpu().numpy()
pred_score = pred_score.cpu().numpy()
ogb = {
'accuracy': reformat(metrics_ogb.eval_acc(
true, (pred_score > 0.).astype(int))['acc']),
'ap': reformat(metrics_ogb.eval_ap(true, pred_score)['ap']),
'auc': reformat(
metrics_ogb.eval_rocauc(true, pred_score)['rocauc']),
}
assert np.isclose(ogb['accuracy'], results['accuracy'])
assert np.isclose(ogb['ap'], results['ap'])
assert np.isclose(ogb['auc'], results['auc'])
return results
def subtoken_prediction(self):
from ogb.graphproppred import Evaluator
evaluator = Evaluator('ogbg-code2')
seq_ref_list = []
seq_pred_list = []
for seq_pred, seq_ref in zip(self._pred, self._true):
seq_ref_list.extend(seq_ref)
seq_pred_list.extend(seq_pred)
input_dict = {"seq_ref": seq_ref_list, "seq_pred": seq_pred_list}
result = evaluator.eval(input_dict)
result['f1'] = result['F1']
del result['F1']
return result
def regression(self):
true, pred = torch.cat(self._true), torch.cat(self._pred)
reformat = lambda x: round(float(x), cfg.round)
return {
'mae': reformat(mean_absolute_error(true, pred)),
'r2': reformat(r2_score(true, pred, multioutput='uniform_average')),
'spearmanr': reformat(eval_spearmanr(true.numpy(),
pred.numpy())['spearmanr']),
'mse': reformat(mean_squared_error(true, pred)),
'rmse': reformat(mean_squared_error(true, pred, squared=False)),
}
def update_stats(self, true, pred, loss, lr, time_used, params,
dataset_name=None, **kwargs):
if dataset_name == 'ogbg-code2':
assert true['y_arr'].shape[1] == len(pred) # max_seq_len (5)
assert true['y_arr'].shape[0] == pred[0].shape[0] # batch size
batch_size = true['y_arr'].shape[0]
# Decode the predicted sequence tokens, so we don't need to store
# the logits that take significant memory.
from graphgps.loader.ogbg_code2_utils import idx2vocab, \
decode_arr_to_seq
arr_to_seq = lambda arr: decode_arr_to_seq(arr, idx2vocab)
mat = []
for i in range(len(pred)):
mat.append(torch.argmax(pred[i].detach(), dim=1).view(-1, 1))
mat = torch.cat(mat, dim=1)
seq_pred = [arr_to_seq(arr) for arr in mat]
seq_ref = [true['y'][i] for i in range(len(true['y']))]
pred = seq_pred
true = seq_ref
else:
assert true.shape[0] == pred.shape[0]
batch_size = true.shape[0]
self._iter += 1
self._true.append(true)
self._pred.append(pred)
self._size_current += batch_size
self._loss += loss * batch_size
self._lr = lr
self._params = params
self._time_used += time_used
self._time_total += time_used
for key, val in kwargs.items():
if key not in self._custom_stats:
self._custom_stats[key] = val * batch_size
else:
self._custom_stats[key] += val * batch_size
def write_epoch(self, cur_epoch):
start_time = time.perf_counter()
basic_stats = self.basic()
if self.task_type == 'regression':
task_stats = self.regression()
elif self.task_type == 'classification_binary':
task_stats = self.classification_binary()
elif self.task_type == 'classification_multi':
task_stats = self.classification_multi()
elif self.task_type == 'classification_multilabel':
task_stats = self.classification_multilabel()
elif self.task_type == 'subtoken_prediction':
task_stats = self.subtoken_prediction()
else:
raise ValueError('Task has to be regression or classification')
epoch_stats = {'epoch': cur_epoch,
'time_epoch': round(self._time_used, cfg.round)}
eta_stats = {'eta': round(self.eta(cur_epoch), cfg.round),
'eta_hours': round(self.eta(cur_epoch) / 3600, cfg.round)}
custom_stats = self.custom()
if self.name == 'train':
stats = {
**epoch_stats,
**eta_stats,
**basic_stats,
**task_stats,
**custom_stats
}
else:
stats = {
**epoch_stats,
**basic_stats,
**task_stats,
**custom_stats
}
# print
logging.info('{}: {}'.format(self.name, stats))
# json
dict_to_json(stats, '{}/stats.json'.format(self.out_dir))
# tensorboard
if cfg.tensorboard_each_run:
dict_to_tb(stats, self.tb_writer, cur_epoch)
self.reset()
if cur_epoch < 3:
logging.info(f"...computing epoch stats took: "
f"{time.perf_counter() - start_time:.2f}s")
return stats
def create_logger():
"""
Create logger for the experiment
Returns: List of logger objects
"""
loggers = []
names = ['train', 'val', 'test']
for i, dataset in enumerate(range(cfg.share.num_splits)):
loggers.append(CustomLogger(name=names[i], task_type=infer_task()))
return loggers
def eval_spearmanr(y_true, y_pred):
"""Compute Spearman Rho averaged across tasks.
"""
res_list = []
if y_true.ndim == 1:
res_list.append(stats.spearmanr(y_true, y_pred)[0])
else:
for i in range(y_true.shape[1]):
# ignore nan values
is_labeled = ~np.isnan(y_true[:, i])
res_list.append(stats.spearmanr(y_true[is_labeled, i],
y_pred[is_labeled, i])[0])
return {'spearmanr': sum(res_list) / len(res_list)}