victoresque / pytorch-template

PyTorch deep learning projects made easy.
MIT License
4.75k stars 1.09k forks source link

Customizable CLI options #48

Closed SunQpark closed 5 years ago

SunQpark commented 5 years ago

This PR contains a new feature: configurable CLI options. Following is an example of adding two flags with this, which is included in train.py.

# simple class-like object having 3 attributes, `flags`, `type`, `target`.
CustomArgs = collections.namedtuple('CustomArgs', 'flags type target')
options = [
    CustomArgs(['--lr', '--learning_rate'], type=float, target=('optimizer', 'args', 'lr')),
    CustomArgs(['--bs', '--batch_size'], type=int, target=('data_loader', 'args', 'batch_size'))
    # options added here can be modified by command line flags.
]

target here is a sequence of keys which are required to access that option in the config dict. In this example, target of learning rate option is ('optimizer', 'args', 'lr') because config['optimizer']['args']['lr'] points to the learning rate. python3 train.py -c config.json --bs 256 runs training with options given in config.json except for the batch size which is increased to 256 by command line options.