current position:Home>[Python] How to use argparse.ArgumentParser()
[Python] How to use argparse.ArgumentParser()
2022-08-06 10:55:39【Xiao Ming 2766】
argparse.ArgumentParser()
argparse是一个Python模块:命令行选项、参数和子命令解析器.
argparse 模块可以让人轻松编写用户友好的命令行接口.程序定义它需要的参数,然后 argparse 将弄清如何从 sys.argv 解析出那些参数. argparse 模块还会自动生成帮助和使用手册,并在用户给程序传入无效参数时报出错误信息.
使用流程
1 创建解析器
parser = argparse.ArgumentParser()
使用 argparse
的第一步是创建一个 ArgumentParser
对象.
ArgumentParser
对象包含将命令行解析成 Python 数据类型所需的全部信息.
2 添加参数
argparser.add_argument('--epoch', type=int, help='epoch number', default=60000)
argparser.add_argument('--n_way', type=int, help='n way', default=5)
argparser.add_argument('--k_spt', type=int, help='k shot for support set', default=1)
argparser.add_argument('--k_qry', type=int, help='k shot for query set', default=15)
argparser.add_argument('--imgsz', type=int, help='imgsz', default=84)
argparser.add_argument('--imgc', type=int, help='imgc', default=3)
argparser.add_argument('--task_num', type=int, help='meta batch size, namely task num', default=4)
argparser.add_argument('--meta_lr', type=float, help='meta-level outer learning rate', default=1e-3)
argparser.add_argument('--update_lr', type=float, help='task-level inner update learning rate', default=0.01)
argparser.add_argument('--update_step', type=int, help='task-level inner update steps', default=5)
argparser.add_argument('--update_step_test', type=int, help='update steps for finetunning', default=10)
给一个 ArgumentParser
添加程序参数信息是通过调用 add_argument()
方法完成的.
3 解析参数
args = argparser.parse_args()
print(args)
ArgumentParser
通过 parse_args()
方法解析参数.
打印结果如下:
Namespace(epoch=60000, imgc=3, imgsz=84, k_qry=15, k_spt=1, meta_lr=0.001, n_way=5, task_num=4, update_lr=0.01, update_step=5, update_step_test=10)
在学习MAML的pytorch实现时,I found this usage:
maml = Meta(args, config)
再看看Meta类的实现:
class Meta(nn.Module):
""" Meta Learner """
def __init__(self, args, config):
""" :param args: """
super(Meta, self).__init__()
self.update_lr = args.update_lr
self.meta_lr = args.meta_lr
self.n_way = args.n_way
self.k_spt = args.k_spt
self.k_qry = args.k_qry
self.task_num = args.task_num
self.update_step = args.update_step
self.update_step_test = args.update_step_test
self.net = Learner(config, args.imgc, args.imgsz)
self.meta_optim = optim.Adam(self.net.parameters(), lr=self.meta_lr)
#...
可以看出来,argparserThe parsed result is passed directly to Meta的初始化函数.Learned this usage today.
参考文献
copyright notice
author[Xiao Ming 2766],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/218/202208061050315221.html
The sidebar is recommended
- Python's common modules of threading and Thread modules The first stage of thread implementation
- Blender Python Programming: Creating Emitting Materials
- Python multiprocessing
- How does python implement image edge detection
- Django paging method
- django regex
- How does Python represent time?2 modules, 3 ways, 1 text to understand~
- Modify column name and row index in pandas DataFrame
- [python pandas groupby]
- Python Daily Practice (New Question Bank of Niu Ke) - Day 20: Dictionary Practice
guess what you like
[LeetCode brush questions] Hash table - some questions are implemented with built-in functions (with Python code)
[LeetCode brush questions] Linked list topic (1) (with Python code)
[Small case of python learning] Simulation system invasion to enhance interest
Getting Started with Python Basics - Essential Knowledge for Getting Started
How does Python represent time?2 modules, 3 ways, 1 text to get it~
Python office software automation, master openpyxl operation in 5 minutes
Introduction to Python Basics - Variables, Strings
[python2] remove the u in front of the unicode string
How to use the Python Color class to draw text
How to use Asyncio Python web programming
Random recommended
- 27 Python artificial intelligence libraries have been sorted out, it is recommended to collect!
- [Python] Word2Vec predicts what will be left if I subtract 'love' from my 'life'
- When I export a pandas package, there is a problem. If I don't import it, there is no problem. Is this not enough memory?
- Python version 3.7.4 How can I install locust successfully?
- How does python use pyinstaller to package music into exe, that is, play background music when running the packaged program?
- Python use pyinstaller how to wrap up music exe, is to run a packaged program play background music?
- Rescue 007 of graph traversal application, detailed analysis of python version
- 27 Python artificial intelligence libraries have been sorted out, it is recommended to collect~
- pandas DataFrame data filtering (2)
- Python is how to represent time?- two modules, three ways, complete the ~ 1
- The definition of pycharm writing python code
- Problems defining functions in Python
- Python Socket Network Programming
- Django server running error
- Python image processing notes - image matching based on Harris corners (skimage)
- (Thirteen--1) Concurrent programming of python (thread, queue, process, coroutine, process thread coroutine comparison)
- (12) Python's memory management mechanism
- Python crawler entry case day07: Hippopx
- Django reports an error ModuleNotFoundError: No module named 'mysqlclient'
- Python study notes
- How to upgrade python? python3 version and install pip3
- [Python] 4-word summary for amateurs, PyCharm2022.2 Professional Edition improves development efficiency, basic settings and common shortcut keys (super practical) & basic knowledge of Python - CSDN 21-day learning challenge
- [Python Data Science Quick Start Series | 03] Playing with Data Extraction: Numpy Indexing and Slicing
- Python draws a curve experiment with matplotlib
- How to represent unequally spaced values with equally spaced values in Python?
- The principle of python decorator
- Python dictionary usage summary
- How to install python, configure environment variables, and change sources for third-party libraries
- [Conda] python data analysis/deep learning environment configuration windows+conda+jupyter+pytorch
- 【Conda】python environment setup , windows+conda+jupyter+pytorch (English Version)
- [Pandas] A primer on Pandas processing csv file datasets (neural network/machine learning algorithm data preprocessing)
- 100 days proficient in python (basic version) -- the first day: python and vscode environment installation
- How does python solve high concurrency
- Using the python library geopy method calculating the distance between the multiple sets of longitude and latitude
- JavaScript, Python 8x, 29x slower than C++?
- python---SSH connection to linux service
- [reprint] Python - list comprehension
- Python Computer Vision Programming - Chapter 2 Local Image Descriptors
- "Compared to Excel, easy learning Python data analysis," reading notes -- -- -- -- -- - data operation
- Why is a function in the math module opened in python or in the IDE of pycharm, only the definition is not implemented?