parser.add_argument里面的参数加--和不加--区别?

def parse_args():
    parser = argparse.ArgumentParser(description='Train a recognizer')
    parser.add_argument('config', help='train config file path')
    parser.add_argument(
        '--validate',
        action='store_true',
        help='whether to evaluate the checkpoint during training')
    parser.add_argument(
        '--test-last',
        action='store_true',
        help='whether to test the checkpoint after training')
    parser.add_argument(
        '--test-best',
        action='store_true',
        help='whether to test the best checkpoint (if applicable) after training')
    parser.add_argument('--seed', type=int, default=None, help='random seed')
    parser.add_argument(
        '--deterministic',
        action='store_true',
        help='whether to set deterministic options for CUDNN backend.')
    parser.add_argument(
        '--launcher',
        choices=['pytorch', 'slurm'],
        default='pytorch',
        help='job launcher')
    parser.add_argument(
        '--compile',
        action='store_true',
        help='whether to compile the model before training / testing (only available in pytorch 2.0)')
    parser.add_argument('--local_rank', type=int, default=-1)
    parser.add_argument('--local-rank', type=int, default=-1)
    args = parser.parse_args()
    if 'LOCAL_RANK' not in os.environ:
        os.environ['LOCAL_RANK'] = str(args.local_rank)

    return args

在 argparse.ArgumentParser 中使用 add_argument 方法时,参数名称前面是否加 -- 有一些区别。

  • 如果参数名称前面没有 --,则该参数被视为位置参数(positional argument)。位置参数是根据参数的位置来进行解析的,命令行中的参数值与位置参数的位置相对应。例如,parser.add_argument('config', help='train config file path') 定义了一个位置参数 config它是必需的,并且需要在命令行中以相应的顺序提供。

  • 如果参数名称前面加了 --,则该参数被视为可选参数(optional argument)。可选参数可以按照任意顺序出现在命令行中,并且可以省略不提供。例如,parser.add_argument('--validate', action='store_true', help='whether to evaluate the checkpoint during training') 定义了一个可选参数 --validate,它不需要在命令行中提供参数值,而是根据是否出现来设置为 True

使用位置参数和可选参数的选择取决于你想要实现的命令行接口的语义。位置参数通常用于必需的参数,而可选参数用于提供更灵活的选项。

请注意,这只是一种约定,你可以根据自己的需求和命令行接口的设计选择使用位置参数或可选参数。

 

def parse_args():
    parser = argparse.ArgumentParser(description='Train a recognizer')
    parser.add_argument('--config',default='../configs/posec3d/c3d_light_gym/joint.py', help='train config file path')
    parser.add_argument(
        '--validate',
        action='store_true',
        help='whether to evaluate the checkpoint during training')
    parser.add_argument(
        '--test-last',
        action='store_true',
        help='whether to test the checkpoint after training')
    parser.add_argument(
        '--test-best',
        action='store_true',
        help='whether to test the best checkpoint (if applicable) after training')
    parser.add_argument('--seed', type=int, default=None, help='random seed')
    parser.add_argument(
        '--deterministic',
        action='store_true',
        help='whether to set deterministic options for CUDNN backend.')
    parser.add_argument(
        '--launcher',
        choices=['pytorch', 'slurm'],
        default='pytorch',
        help='job launcher')
    # parser.add_argument('--local_rank', type=int, default=0)
    args = parser.parse_args()
    # if 'LOCAL_RANK' not in os.environ:
    #     os.environ['LOCAL_RANK'] = str(args.local_rank)
 
    return args

参考:

windows环境下使用单GPU训练pyskl下的模型_# if 'local_rank' not in os.environ: # os.environ[-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值