参数解析

import argparse

## 一般有两种参数,
# 一种是数值型参数,终端输入指定数值,则按指定数值运行
# 另一种是激活型参数,终端输入相关 激活指令,则该参数解析为true,否则为false,如下面--resume 参数

# def argsparse():
#     parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
#     parser.add_argument('--lr', default=0.1, type=float, help='learning rate')
#     parser.add_argument('--resume', '-r', action='store_true',default=False,help='resume from checkpoint')

#     args = parser.parse_args()
#     return args

class Arg:
    def __init__(self):
        parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
        parser.add_argument('--lr', default=0.1, type=float, help='learning rate')
        parser.add_argument('--resume', '-r', action='store_true',default=False,help='resume from checkpoint')
        args = parser.parse_args()   
        self.args= parser.parse_args()

if __name__ == "__main__":

    ## way1
    # args = argsparse()

    ## way2
    t = Arg()
    print(t.args.lr)
    if t.args.resume:
        print("resume")
    if not t.args.resume:
        print("no resume")

猜你喜欢

转载自www.cnblogs.com/yeran/p/10587976.html
今日推荐