当前位置:网站首页>常用编程记录——parser = argparse.ArgumentParser()
常用编程记录——parser = argparse.ArgumentParser()
2022-04-23 05:44:00 【umbrellalalalala】
在很多pytorch写的人工智能项目中,都能看到这样一句:parser = argparse.ArgumentParser(),本文就简单记录下它们的使用方法
部分内容参考:
https://blog.csdn.net/lizhiyuanbest/article/details/104975848
方式1—单个参数
创建a.py:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number", type=int)
args = parser.parse_args()
s = args.square ** 2
print(s)
命令行执行:
$ python a.py 2
4
方式2—多个参数
创建b.py:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number", type=int)
parser.add_argument("add1", help="display a sum of two given numbers", type=int)
parser.add_argument("add2", help="display a sum of two given numbers", type=int)
args = parser.parse_args()
s = args.square ** 2
a = args.add1 + args.add2
print(s)
print(a)
命令行执行结果:
$ python b.py 2 10 20
4
30
可见是将三个整数按顺序赋给三个参数
方式3—乱序赋值
创建c.py:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--square", help="display a square of a given number", type=int)
parser.add_argument("--add1", help="display a sum of two given numbers", type=int)
parser.add_argument("--add2", help="display a sum of two given numbers", type=int)
args = parser.parse_args()
s = args.square ** 2
a = args.add1 + args.add2
print(s)
print(a)
️注意参数前面有--
命令行运行方式1:
$ python c.py --square 2 --add1 10 --add2 20
4
30
命令行运行方式2(打乱顺序):
$ python c.py --add1 2 --add2 10 --square 20
400
12
敲pytorch的同学掌握这些应该就足够使用了
版权声明
本文为[umbrellalalalala]所创,转载请带上原文链接,感谢
https://blog.csdn.net/umbrellalalalala/article/details/123804616
边栏推荐
- 关于二叉树的遍历
- Pytorch learning record (XII): learning rate attenuation + regularization
- 一文读懂当前常用的加密技术体系(对称、非对称、信息摘要、数字签名、数字证书、公钥体系)
- PyEMD安装及简单使用
- Ora: 28547 connection to server failed probable Oracle net admin error
- JVM系列(3)——内存分配与回收策略
- PyQy5学习(二):QMainWindow+QWidget+QLabel
- Package mall system based on SSM
- 图解numpy数组矩阵
- Meta annotation (annotation of annotation)
猜你喜欢

图解HashCode存在的意义

多线程与高并发(3)——synchronized原理

JDBC连接数据库

Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.

opensips(1)——安装opensips详细流程

PyQy5学习(四):QAbstractButton+QRadioButton+QCheckBox

lambda表达式

Opensips (1) -- detailed process of installing opensips

Pytorch学习记录(十三):循环神经网络((Recurrent Neural Network)

线程的底部实现原理—静态代理模式
随机推荐
Duplicate key update in MySQL
无监督去噪——[TMI2022]ISCL: Interdependent Self-Cooperative Learning for Unpaired Image Denoising
The attendance client date of K / 3 wise system can only be selected to 2019
创建线程的三种方式
Multithreading and high concurrency (1) -- basic knowledge of threads (implementation, common methods, state)
redhat实现目录下特定文本类型内关键字查找及vim模式下关键字查找
关于二叉树的遍历
DWSurvey是一个开源的调查问卷系统。解决无法运行问题,修改bug。
MySql基础狂神说
The list attribute in the entity is empty or null, and is set to an empty array
多线程与高并发(1)——线程的基本知识(实现,常用方法,状态)
Pytorch学习记录(十):数据预处理+Batch Normalization批处理(BN)
PyQy5学习(三):QLineEdit+QTextEdit
Record a project experience and technologies encountered in the project
Pytorch learning record (XI): data enhancement, torchvision Explanation of various functions of transforms
Excel obtains the difference data of two columns of data
interviewter:介绍一下MySQL日期函数
治疗TensorFlow后遗症——简单例子记录torch.utils.data.dataset.Dataset重写时的图片维度问题
开发环境 EAS登录 license 许可修改
创建二叉树