当前位置:网站首页>Deep understanding of add in argparse module_ Argument parameters (such as action)

Deep understanding of add in argparse module_ Argument parameters (such as action)

2022-06-11 08:11:00 Interval

Easy to use

import argparse


parser = argparse.ArgumentParser(description="Deep Gaussian Processes on MNIST")
parser.add_argument("-n", "--num-epochs", default=5, type=int)
parser.add_argument("-t", "--num-iters", default=60, type=int)
parser.add_argument("-b", "--batch-size", default=1000, type=int)
parser.add_argument("-lr", "--learning-rate", default=0.01, type=float)


args = parser.parse_args()
print(args.num_epochs)

Output results :

5

User specified parameters

Next, let's take the example above , Suppose this python The name of the file is main.py. If we need to Runtime Change the value of the parameter , Can be as follows :

python main.py -n 6 -t 100# The parameter n,t The values of are changed from the default values to 6,100.
python main.py --num-epochs 6 --num-iters 100# Equivalent form 

This is not the first example , It is special , But it will be used later .

python main.py -x

You can see , Its special feature is that it is not followed by a value that needs to be specified . Let's reveal it first , In fact, this is not uncommon , To recall linux The style of .

Parameters

We can see from above add_argument There are many interface options , for example default,type wait , What are their respective uses ?

  1. first "-x", Just for python Abbreviation when the system reads the input .
  2. the second "–xxx", As the real name of this parameter , That is, when this parameter is used, it is args.xxx, instead of args.x.
  3. default, The default value of this parameter .
  4. type, Data type of parameter , for example int,float,str,list etc. .
  5. required, The default is false, If true, And there is no default value , Indicates that this parameter must be specified by the user , Otherwise, the program reports an error .
  6. action,action=‘store_true’/‘store_false’. The argument to use this option must be a boolean variable . among store_true Express : The user specified this parameter , So this parameter is true. What does a user specify ? For example, the special example above ,python main.py -x, It designates x, that x Will become True. Empathy store_false.
  7. choice, That is, the value range of the parameter , Can help you automatically determine whether to cross the line . for example [1,2], Indicates that only values can be taken 1 perhaps 2.
  8. help, Interpretation of parameters , It is equivalent to annotation , I'm afraid I forget .
  9. nargs, The value can be ["*","+", Positive integer ] etc. , That is, this parameter is a list ,"*" Represents any parameter .“+” Represents at least one parameter . A positive integer represents so many parameters . Suppose this parameter is called people, And nargs=2. So the order is as follows :python main.py --people x y. that people=[“x”,“y”].

This article only talks about some common parameters and functions .

In fact, there are some other parameters , Here's not a list , Another example nargs The value of can also be “?". What does this mean , And examples , It is highly recommended that you refer to python Official documents of https://docs.python.org/3/. This thing is always the best .

 Insert picture description here

 Insert picture description here

原网站

版权声明
本文为[Interval]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020514399097.html