简体   繁体   中英

Python argparse: unrecognized arguments while passing args in command

I have a code in python as follows:

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description='Experiments for optimizer')
    parser.add_argument('list_experiments', type=str, nargs='+',
                        help='List of experiment names. E.g. CDSGD EASGD FASGD SGD Adam --> will run a training session with each optimizer')
    parser.add_argument('--model_name', default='CNN', type=str,
                        help='Model name: CNN, Big_CNN or FCN')
    parser.add_argument('--batch_size', default=128, type=int,
                        help='Batch size')
    parser.add_argument('--nb_epoch', default=30, type=int,
                        help='Number of epochs')
    parser.add_argument('--dataset', type=str, default="cifar10",
                        help='Dataset, cifar10, cifar100 or mnist')
    parser.add_argument('--n_agents', default=5, type=int,
                        help='Number of agents')
    parser.add_argument('--communication_period', default=1, type=int,
                        help='Gap between the communication of the agents')
    parser.add_argument('--sparsity', default=False, type=bool,
                        help='The connection between agents if sparse or not, default: False i.e. fully connected')
    args = parser.parse_args()

I wanna run it with command

python main.py CDSGD -m CNN -b 512 -ep 200 -d cifar10 -n 5 -cp 1 -s 3

but i get the following error:

main.py: error: unrecognized arguments: -m CNN -b 512 -ep 200 -d cifar10 -n 5 -cp 1 -s 3

how can i fix this problem?

A long option can be abbreviated with a unique prefix (eg, --model will be recognized as --model_name , but short options have to be defined explicitly.

For example,

parser.add_argument('--model_name', '-m', default='CNN', type=str,
                    help='Model name: CNN, Big_CNN or FCN')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM