前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[编程经验] 利用TensorFlow和argparse模块解析命令行参数

[编程经验] 利用TensorFlow和argparse模块解析命令行参数

作者头像
用户1622570
发布2018-04-11 15:53:48
1.1K0
发布2018-04-11 15:53:48
举报

今天的内容主要包括如何用TensorFlow解析命令行参数和利用Python自带的argparse模块解析命令行参数。我会分别写一个例子,通过这个例子彻底学会如何在程序中解析命令行参数。

首先什么是命令行?简单理解一下就是在Windows里面命令行就是我们常说的cmd(Command Processor),而在Linux中就是shell。命令行参数就是可以写在命令行中的参数,而怎么让程序知道你在命令行中写了哪些参数,就是命令行参数解析。在Python中,或者说在机器学习中,程序中经常会涉及一些参数的设置,比如学习率,迭代次数等。我们之前在Python入门中提到如何运行Python程序,主要有三种方法。1)利用Python自带的shell,Ctrl+F5运行。2)利用集成的编译器,比如PyCharm,Spyder等,点击run按钮就可以执行程序。3)通过命令行来运行程序。前两种都比较简单,下面举一个如何用命令行来运行程序的栗子。

图1是一段已经写好的简单的程序,图2是通过命令行来执行。程序命名为: cmd_run_test.py, 然后在程序当前所在的文件夹内打开命名行cmd,输入python cmd_run_test.py,回车,得到程序运行结果(图2)。通过这个例子应该能理解什么是通过命令行来执行程序,以及如何来执行。

图1

图2

1. TensorFlow解析命令行参数

下面是程序,Python中单行注释使用#,而多行注释使用三对双引号,引号中间的内容为注释的内容,下面的栗子中,注释的内容为程序当前行的输出!下面不重复说明。

# coding: utf-8

# 导入tensorflow和pprint模块,pprint模块可以是打印输出的更加美观,合理。

import tensorflow as tf

import pprint

# 定义一个pprint.PrettyPrinter类,叫做pp

pp =pprint.PrettyPrinter()

flags =tf.app.flags

print(flags)

"""

<module'tensorflow.python.platform.flags' from

'C:\\Python35\\lib\\site-packages\\tensorflow\\python\\platform\\flags.py'>

"""

这里我们打印了一下flags是什么,flags其实就是一个模块。Python中一切皆对象,所以我们可以把这个模块赋给flags。

flags.DEFINE_integer(flag_name="epoch",default_value=10, docstring="训练轮数")

然后我们利用flags的DEFINE_integer方法,也叫函数,来定义需要解析的参数。函数DEFINE_integer的第一个参数flag_name表示命令行参数的名字,第二参数default_value表示默认的参数值,第三个参数docstring表示命令行参数的说明。也可以不指定参数名,直接写具体的变量值。例如下面这行一样,所不同的是,DEFINE_integer就是定义整数型参数的方法,而DEFINE_float是定义浮点数的方法。

flags.DEFINE_float("learning_rate",0.0002, "学习率")

flags.DEFINE_string("checkpoint_dir","samples", "模型保存路径")

flags.DEFINE_boolean("is_train",True, "如果为True表示训练,False表示测试")

同理DEFINE_string表示定义字符串类型的参数,DEFINE_boolean表示定义布尔值类型的参数。这里定义了4个需要解析的参数,你也可以根据程序的需要定义更多的参数,这个数量是没有限制的。然后使用下面这句,定义一个FLAGS类,它用来调用刚才定义好的命令行参数。

FLAGS =flags.FLAGS

比如我们想利用epoch这个参数,就可以这样,FLAGS.epoch。打印一下它的值,就是10。

print(FLAGS.epoch)

"""

10

"""

# 调用pp的pprint方法

pp.pprint(flags.FLAGS.__flags)

"""

{'checkpoint_dir':'samples',

'epoch': 25,

'is_train': True,

'learning_rate': 0.0002}

"""

这里利用pprint模块来打印出所以的参数,可以看到刚才定义的4个参数都在这里,且以字典的形式输出。

print(type(flags.FLAGS.__flags))

"""

<class'dict'>

"""

打印一下它的类型,是dict字典。

最后我们利用定义好的参数,写一个for循环,也就是如果你后面继续写其他的程序,怎么来调用epoch这个参数。

for i in range(FLAGS.epoch):

print("this is the {}thepoch".format(i))

"""

this is the 0th epoch

this is the 1th epoch

this is the 2th epoch

this is the 3th epoch

this is the 4th epoch

this is the 5th epoch

this is the 6th epoch

this is the 7th epoch

this is the 8th epoch

this is the 9th epoch

"""

最后我们看一下,怎么在命令行来执行这个程序。

前面几行是因为导入了tensorflow而调用的底层库的一下输出,这个不用看,后面是程序运行输出,主要看一下红色框里面是怎么执行程序的:

Python argparse_test.py epoch 10 learning_rate 0.01 is_train True

2. argparse模块解析命令行参数

Argparse模块是Python中非常强大的命令行参数解析库,今天还是通过一个简单的栗子,让大家了解怎么使用,我觉得把这个栗子学会了,以后需要的时候你只需要改下参数名字,基本都是可以用的了。

好了,第一行首先import进来我们需要的模块argparse,和pprint。

import argparse

import pprint

pp =pprint.PrettyPrinter()

parser =argparse.ArgumentParser(description="this is all args in thisprogram")

然后这一句是使用ArgumentParser类定义为parser。ArgumentParser的参数也很多,但是我觉得常用的可能就是description,其他的用的比较少,所以我们就写了这个参数,它表示对命令行参数的一个总体的描述。

parser.add_argument("-epoch",default=20, type=int, required=False, help="epoch times")

然后调用类parser的方法.add_argument,它的参数主要的几个都写在这里了,第一个参数就是命令行参数的名字,第二个表示默认值,第三个是参数类型,第四个表示是否必须输入,如果为False,表示可以不再命令行中输入,如果为True,则表示必须输入,如果没输入则会报错,最后一个help表示参数意义的说明。

同理可得以下三行:

parser.add_argument('--learning_rate',default=0.001, type=int, required=False, help=help)

parser.add_argument('--checkpoint_dir',default=10, type=int, required=False, help=help, )

parser.add_argument("--is_train",default=True, type=bool, required=False, help="train or test")

下面这行就表示将刚才定义的参数解析出来,以便后面使用

args =parser.parse_args()

然后我们打印一下args,得到刚才定义的4个参数。

pp.pprint(args)

"""

Namespace(checkpoint_dir=10,epoch=20, is_train=True, learning_rate=0.001)

"""

然后是一个在程序中调用参数的栗子。

for i in range(args.epoch):

print("this is the {}thepoch".format(i))

"""

this is the 0th epoch

this is the 1th epoch

this is the 2th epoch

this is the 3th epoch

this is the 4th epoch

this is the 5th epoch

this is the 6th epoch

this is the 7th epoch

this is the 8th epoch

this is the 9th epoch

this is the 10th epoch

this is the 11th epoch

this is the 12th epoch

this is the 13th epoch

this is the 14th epoch

this is the 15th epoch

this is the 16th epoch

this is the 17th epoch

this is the 18th epoch

this is the 19th epoch

"""

最后我们看一下,怎么在命令行来执行这个程序。基本和TensorFlow使用方法没有区别,主要在于多了两个减号--:

python argparse_test2.py --epoch 3

今天就讲到这里了,主要介绍了两种解析命令行参数的方法,注意对比使用!

需要完整代码的童鞋请后台回复 argparse 获得!

本文为作者原创,如有雷同,必然是别人抄我的。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-05-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器学习和数学 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档