脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - tensorflow 使用flags定义命令行参数的方法

tensorflow 使用flags定义命令行参数的方法

2021-02-04 00:53一个人的场域 Python

本篇文章主要介绍了tensorflow 使用flags定义命令行参数的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

tf定义了tf.app.flags,用于支持接受命令行传递参数,相当于接受argv。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import tensorflow as tf
 
#第一个是参数名称,第二个参数是默认值,第三个是参数描述
tf.app.flags.DEFINE_string('str_name', 'def_v_1',"descrip1")
tf.app.flags.DEFINE_integer('int_name', 10,"descript2")
tf.app.flags.DEFINE_boolean('bool_name', False, "descript3")
 
FLAGS = tf.app.flags.FLAGS
 
#必须带参数,否则:'TypeError: main() takes no arguments (1 given)';  main的参数名随意定义,无要求
def main(_):
  print(FLAGS.str_name)
  print(FLAGS.int_name)
  print(FLAGS.bool_name)
 
if __name__ == '__main__':
  tf.app.run() #执行main函数

执行:

[root@AliHPC-G41-211 test]# python tt.py
def_v_1
10
False
[root@AliHPC-G41-211 test]# python tt.py --str_name test_str --int_name 99 --bool_name True
test_str
99
True

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/leiting_imecas/article/details/72367937

延伸 · 阅读

精彩推荐