farshad1123
farshad1123

Reputation: 325

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?

Upvotes: 1

Views: 1599

Answers (1)

chepner
chepner

Reputation: 531165

A long option can be abbreviated with a unique prefix (e.g., --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')

Upvotes: 3

Related Questions