-
Notifications
You must be signed in to change notification settings - Fork 1
/
proactive_train.py
85 lines (73 loc) · 4 KB
/
proactive_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
import argparse
import collections
import torch
import numpy as np
import data_loader.data_loaders as module_data
import model.loss as module_loss
import model.metric as module_metric
import model.model as module_arch
from parse_config import ConfigParser
from trainer import ProactiveTrainer, SparseTrainer
from utils import str2bool
# fix random seeds for reproducibility
SEED = 123
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(SEED)
def main(config):
logger = config.get_logger('train')
# setup data_loader instances
data_loader = config.init_obj('data_loader', module_data)
valid_data_loader = config.init_obj('valid_data_loader', module_data)
# build model architecture, then print to console
model = config.init_obj('arch', module_arch)
logger.info(model)
# get function handles of loss and metrics
criterion = getattr(module_loss, config['loss'])
metrics = [getattr(module_metric, met) for met in config['metrics']]
# build optimizer, learning rate scheduler. delete every lines containing lr_scheduler for disabling scheduler
trainable_params = filter(lambda p: p.requires_grad, model.parameters())
optimizer = config.init_obj('optimizer', torch.optim, trainable_params)
print("Config sparse_train", config["sparse_train"])
if config["sparse_train"]:
logger.debug("Starting sparse proactive trainer...")
trainer = SparseTrainer(model, criterion, metrics, optimizer,
config=config,
data_loader=data_loader, valid_data_loader=valid_data_loader)
else:
logger.debug("Starting simple proactive trainer...")
trainer = ProactiveTrainer(model, criterion, metrics, optimizer,
config=config,
data_loader=data_loader)
trainer.train()
if __name__ == '__main__':
args = argparse.ArgumentParser(description='PyTorch Template')
args.add_argument('-c', '--config', default=None, type=str,
help='config file path (default: None)')
args.add_argument('-r', '--resume', default=None, type=str,
help='path to latest checkpoint (default: None)')
args.add_argument('-d', '--device', default=None, type=str,
help='indices of GPUs to enable (default: all)')
# custom cli options to modify configuration from default values given in json file.
CustomArgs = collections.namedtuple('CustomArgs', 'flags type target')
options = [
CustomArgs(['--opt', '--optimizer'], type=str, target='optimizer'),
CustomArgs(['--lr', '--learning_rate'], type=float, target='optimizer;args;lr'),
CustomArgs(['--include_new'], type=str2bool, target='data_loader;args;always_include_new'),
CustomArgs(['--keep_probs'], type=str2bool, target='data_loader;args;keep_probs'),
CustomArgs(['--bs', '--batch_size'], type=int, target='data_loader;args;batch_size'),
CustomArgs(['--ts', '--trigger_size'], type=int, target='data_loader;args;trigger_size'),
CustomArgs(['--ws', '--window_size'], type=int, target='data_loader;args;window_size'),
CustomArgs(['--n', '--name'], type=str, target='name'),
CustomArgs(['--dr', '--deployment_ratio'], type=float, target='deployment_ratio'),
CustomArgs(['--st', '--sparse_train'], type=str2bool, target='sparse_train'),
CustomArgs(['--o', '--online_training'], type=str2bool, target='online_training'),
CustomArgs(['--tu', '--train_until'], type=int, target='trainer;epochs'),
CustomArgs(['--ct', '--compress_type'], type=str, target='compress_type'),
CustomArgs(['--cmem', '--compress_memory'], type=str2bool, target='compress_memory'),
CustomArgs(['--model_name'], type=str, target='arch;args;model_name'),
CustomArgs(['--history_end'], type=int, target='data_loader;args;history_end'),
]
config = ConfigParser.from_args(args, options)
main(config)