-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
189 lines (156 loc) · 7.87 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
from tqdm import tqdm
import torch
from utils import save_model, save_pred, get_pred_prefix, get_model_prefix
from configs.supported import process_outputs_functions
def run_epoch(algorithm, dataset, general_logger, epoch, config, train):
if dataset['verbose']:
general_logger.write(f"\n{dataset['name']}:\n")
if train:
algorithm.train()
else:
algorithm.eval()
# Not preallocating memory is slower
# but makes it easier to handle different types of data loaders
# (which might not return exactly the same number of examples per epoch)
epoch_y_true = []
epoch_y_pred = []
epoch_metadata = []
# Using enumerate(iterator) can sometimes leak memory in some environments (!)
# so we manually increment batch_idx
batch_idx = 0
iterator = tqdm(dataset['loader']) if config.progress_bar else dataset['loader']
for batch in iterator:
if train:
batch_results = algorithm.update(batch)
else:
batch_results = algorithm.evaluate(batch)
# These tensors are already detached, but we need to clone them again
# Otherwise they don't get garbage collected properly in some versions
# The subsequent detach is just for safety
# (they should already be detached in batch_results)
epoch_y_true.append(batch_results['y_true'].clone().detach())
y_pred = batch_results['y_pred'].clone().detach()
if config.process_outputs_function is not None:
y_pred = process_outputs_functions[config.process_outputs_function](y_pred)
epoch_y_pred.append(y_pred)
epoch_metadata.append(batch_results['metadata'].clone().detach())
if train and (batch_idx+1) % config.log_every==0:
log_results(algorithm, dataset, general_logger, epoch, batch_idx)
batch_idx += 1
epoch_y_pred = torch.cat(epoch_y_pred)
epoch_y_true = torch.cat(epoch_y_true)
epoch_metadata = torch.cat(epoch_metadata)
results, results_str = dataset['dataset'].eval(
epoch_y_pred,
epoch_y_true,
epoch_metadata)
if config.scheduler_metric_split == dataset['split']:
algorithm.step_schedulers(
is_epoch=True,
metrics=results,
log_access=(not train))
# log after updating the scheduler in case it needs to access the internal logs
log_results(algorithm, dataset, general_logger, epoch, batch_idx)
results['epoch'] = epoch
dataset['eval_logger'].log(results)
if dataset['verbose']:
general_logger.write('Epoch eval:\n')
general_logger.write(results_str)
return results, epoch_y_pred
def train(algorithm, datasets, general_logger, config, epoch_offset, best_val_metric):
patience = config.early_stopping_patience
for epoch in range(epoch_offset, config.n_epochs):
general_logger.write('\nEpoch [%d]:\n' % epoch)
# First run training
run_epoch(algorithm, datasets['train'], general_logger, epoch, config, train=True)
# Then run val
val_results, y_pred = run_epoch(algorithm, datasets['val'], general_logger, epoch, config, train=False)
curr_val_metric = val_results[config.val_metric]
general_logger.write(f'Validation {config.val_metric}: {curr_val_metric:.3f}\n')
if best_val_metric is None:
is_best = True
else:
if config.val_metric_decreasing:
is_best = curr_val_metric < best_val_metric
else:
is_best = curr_val_metric > best_val_metric
if is_best:
# Reset patience
patience = config.early_stopping_patience
best_epoch = epoch
best_val_metric = curr_val_metric
general_logger.write(f'Epoch {epoch} has the best validation performance so far.\n')
else:
# Decrease patience
patience -= 1
general_logger.write(f'Epoch {epoch} has not improved validation performance (Decrease patience to {patience}).\n')
save_model_if_needed(algorithm, datasets['val'], epoch, config, is_best, best_val_metric)
save_pred_if_needed(y_pred, datasets['val'], epoch, config, is_best)
# Then run everything else
if config.evaluate_all_splits:
additional_splits = [split for split in datasets.keys() if split not in ['train','val']]
else:
additional_splits = config.eval_splits
for split in additional_splits:
_, y_pred = run_epoch(algorithm, datasets[split], general_logger, epoch, config, train=False)
save_pred_if_needed(y_pred, datasets[split], epoch, config, is_best)
general_logger.write('\n')
# Check if patience exhausted
if not patience:
general_logger.write(
f'Early stopping activated. Epoch {best_epoch}/{config.n_epochs} had the best validation performance.\n')
break
def evaluate(algorithm, datasets, epoch, general_logger, config):
algorithm.eval()
for split, dataset in datasets.items():
if (not config.evaluate_all_splits) and (split not in config.eval_splits):
continue
epoch_y_true = []
epoch_y_pred = []
epoch_metadata = []
iterator = tqdm(dataset['loader']) if config.progress_bar else dataset['loader']
for batch in iterator:
batch_results = algorithm.evaluate(batch)
epoch_y_true.append(batch_results['y_true'].clone().detach())
y_pred = batch_results['y_pred'].clone().detach()
if config.process_outputs_function is not None:
y_pred = process_outputs_functions[config.process_outputs_function](y_pred)
epoch_y_pred.append(y_pred)
epoch_metadata.append(batch_results['metadata'].clone().detach())
results, results_str = dataset['dataset'].eval(
torch.cat(epoch_y_pred),
torch.cat(epoch_y_true),
torch.cat(epoch_metadata))
results['epoch'] = epoch
dataset['eval_logger'].log(results)
general_logger.write(f'Eval split {split} at epoch {epoch}:\n')
general_logger.write(results_str)
# Skip saving train preds, since the train loader generally shuffles the data
if split != 'train':
save_pred_if_needed(y_pred, dataset, epoch, config, is_best=False, force_save=True)
def log_results(algorithm, dataset, general_logger, epoch, batch_idx):
if algorithm.has_log:
log = algorithm.get_log()
log['epoch'] = epoch
log['batch'] = batch_idx
dataset['algo_logger'].log(log)
if dataset['verbose']:
general_logger.write(algorithm.get_pretty_log_str())
algorithm.reset_log()
def save_pred_if_needed(y_pred, dataset, epoch, config, is_best, force_save=False):
if config.save_pred:
prefix = get_pred_prefix(dataset, config)
if force_save or (config.save_step is not None and (epoch + 1) % config.save_step == 0):
save_pred(y_pred, prefix + f'epoch:{epoch}_pred.csv')
if config.save_last:
save_pred(y_pred, prefix + f'epoch:last_pred.csv')
if config.save_best and is_best:
save_pred(y_pred, prefix + f'epoch:best_pred.csv')
def save_model_if_needed(algorithm, dataset, epoch, config, is_best, best_val_metric):
prefix = get_model_prefix(dataset, config)
if config.save_step is not None and (epoch + 1) % config.save_step == 0:
save_model(algorithm, epoch, best_val_metric, prefix + f'epoch:{epoch}_model.pth')
if config.save_last:
save_model(algorithm, epoch, best_val_metric, prefix + 'epoch:last_model.pth')
if config.save_best and is_best:
save_model(algorithm, epoch, best_val_metric, prefix + 'epoch:best_model.pth')