TensorFlow中关于tf.app.flags命令行参数解析模块

作者:打工人小飞 时间:2021-10-17 03:40:40 

tf.app.flags命令行参数解析模块

说道命令行参数解析,就不得不提到 python 的 argparse 模块,详情可参考我之前的一篇文章:python argparse 模块命令行参数用法及说明。

在阅读相关工程的源码时,很容易发现 tf.app.flags 模块的身影。其作用与 python 的 argparse 类似。

直接上代码实例,新建一个名为 test_flags.py 的文件,内容如下:

#coding:utf-8
import tensorflow as tf

FLAGS = tf.app.flags.FLAGS
# tf.app.flags.DEFINE_string("param_name", "default_val", "description")
tf.app.flags.DEFINE_string("train_data_path", "/home/feige", "training data dir")
tf.app.flags.DEFINE_string("log_dir", "./logs", " the log dir")
tf.app.flags.DEFINE_integer("train_batch_size", 128, "batch size of train data")
tf.app.flags.DEFINE_integer("test_batch_size", 64, "batch size of test data")
tf.app.flags.DEFINE_float("learning_rate", 0.001, "learning rate")

def main(unused_argv):
   train_data_path = FLAGS.train_data_path
   print("train_data_path", train_data_path)
   train_batch_size = FLAGS.train_batch_size
   print("train_batch_size", train_batch_size)
   test_batch_size = FLAGS.test_batch_size
   print("test_batch_size", test_batch_size)
   size_sum = tf.add(train_batch_size, test_batch_size)
   with tf.Session() as sess:
       sum_result = sess.run(size_sum)
       print("sum_result", sum_result)

# 使用这种方式保证了,如果此文件被其他文件 import的时候,不会执行main 函数
if __name__ == '__main__':
   tf.app.run()   # 解析命令行参数,调用main 函数 main(sys.argv)

上述代码已给出较为详细的注释,在此不再赘述。

该文件的调用示例以及运行结果如下所示

TensorFlow中关于tf.app.flags命令行参数解析模块

如果需要修改默认参数的值,则在命令行传入自定义参数值即可,若全部使用默认参数值,则可直接在命令行运行该 python 文件。

读者可能会对 tf.app.run() 有些疑问,在上述注释中也有所解释,但要真正弄清楚其运行原理

还需查阅其源代码

def run(main=None, argv=None):
 """Runs the program with an optional 'main' function and 'argv' list."""
 f = flags.FLAGS

# Extract the args from the optional `argv` list.
 args = argv[1:] if argv else None

# Parse the known flags from that list, or from the command
 # line otherwise.
 # pylint: disable=protected-access
 flags_passthrough = f._parse_flags(args=args)
 # pylint: enable=protected-access

main = main or sys.modules['__main__'].main

# Call the main function, passing through any arguments
 # to the final program.
 sys.exit(main(sys.argv[:1] + flags_passthrough))

flags_passthrough=f._parse_flags(args=args)这里的_parse_flags就是我们tf.app.flags源码中用来解析命令行参数的函数。

所以这一行就是解析参数的功能;

下面两行代码也就是 tf.app.run 的核心意思:执行程序中 main 函数,并解析命令行参数!

来源:https://huangfei.blog.csdn.net/article/details/81090227

标签:TensorFlow,tf.app.flags,命令行,参数
0
投稿

猜你喜欢

  • Python编程基础之输入与输出

    2021-03-03 10:06:07
  • 基于PHP做个图片防盗链

    2023-05-25 00:27:30
  • asp如何对多个条件进行判断?

    2009-11-20 18:28:00
  • python自动化操作之动态验证码、滑动验证码的降噪和识别

    2023-03-26 02:48:28
  • python深度学习tensorflow训练好的模型进行图像分类

    2023-02-20 20:40:37
  • 深入浅析python的第三方库pandas

    2021-06-05 03:13:03
  • asp如何用组件实现自动发送电子邮件?

    2010-06-16 09:56:00
  • PHP实现用户认证及管理完全源码

    2023-11-19 20:26:19
  • 兼容低版本 IE 的 JScript 5.5 实现

    2008-07-01 12:48:00
  • python 详解如何写flask文件下载接口

    2023-05-04 21:07:06
  • 几个常用的js小函数

    2007-09-19 12:59:00
  • python如何实现从视频中提取每秒图片

    2023-10-15 11:36:25
  • MacOS中 VSCode 安装 GO 插件失败问题的快速解决方法

    2023-06-18 02:32:32
  • Python爬取视频(其实是一篇福利)过程解析

    2021-09-21 17:52:11
  • python-itchat 获取微信群用户信息的实例

    2022-11-09 18:15:41
  • python进行两个表格对比的方法

    2021-12-15 20:31:57
  • Python中staticmethod和classmethod的作用与区别

    2022-03-12 21:30:00
  • python中的变量命名规则详情

    2022-05-25 17:49:06
  • 怎样解决MySQL 5.0.16的乱码问题

    2008-10-13 12:47:00
  • 浅谈python中scipy.misc.logsumexp函数的运用场景

    2023-11-10 17:10:56
  • asp之家 网络编程 m.aspxhome.com