怎么在tensorflow中使用flags定义命令行参数

2024-03-26,

这期内容当中小编将会给大家带来有关怎么在tensorflow中使用flags定义命令行参数,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

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

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

上述就是小编为大家分享的怎么在tensorflow中使用flags定义命令行参数了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注本站行业资讯频道。

《怎么在tensorflow中使用flags定义命令行参数.doc》

下载本文的Word格式文档,以方便收藏与打印。