作  者:marsggbo
出  处:https://www.cnblogs.com/marsggbo
版权声明:署名 - 非商业性使用 - 禁止演绎,协议普通文本 | 协议法律文本

---------------------------------------------------------------------------------------------------------------

Tensorflow       tf.app  &  tf.app.flags    用法介绍

1.     tf.app.flags

下面介绍 tf.app.flags.FLAGS的使用,主要是在用命令行执行程序时,需要传些参数,其实也就可以理解成对argparse库进行的封装,示例代码如下

#coding:utf-8 

# 学习使用 tf.app.flags 使用,全局变量
# 可以再命令行中运行也是比较方便,如果只写 python app_flags.py 则代码运行时默认程序里面设置的默认设置
# 若 python app_flags.py --train_data_path <绝对路径 train.txt> --max_sentence_len 100
# --embedding_size 100 --learning_rate 0.05 代码再执行的时候将会按照上面的参数来运行程序 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", "/desktop/train.txt", "training data dir")
tf.app.flags.DEFINE_string("log_dir", "./logs", " the log dir")
tf.app.flags.DEFINE_integer("max_sentence_len", 80, "max num of tokens per query")
tf.app.flags.DEFINE_integer("embedding_size", 50, "embedding size") 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)
print("*" * 30)
max_sentence_len = FLAGS.max_sentence_len
print("max_sentence_len", max_sentence_len)
print("*" * 30)
embdeeing_size = FLAGS.embedding_size
print("embedding_size", embdeeing_size)
print("*" * 30)
abc = tf.add(max_sentence_len, embdeeing_size) init = tf.global_variables_initializer() with tf.Session() as sess:
sess.run(init)
print("abc", sess.run(abc)) # 使用这种方式保证了,如果此文件被其他文件 import的时候,不会执行main 函数
if __name__ == '__main__':
tf.app.run() # 解析命令行参数,调用main 函数 main(sys.argv)

两种调用方式:

方式一:

  1. python tf_app_flag.py

结果如下:

方式二:

python app_flags.py --train_data_path ./test.py --max_sentence_len 100 --embedding_size 100 --learning_rate 0.05

 3.         tf.app.run()

该函数一般都是出现在这种代码中:

if __name__ == '__main__':
tf.app.run()

上述第一行代码表示如果当前是从其它模块调用的该模块程序,则不会运行main函数!而如果就是直接运行的该模块程序,则会运行main函数。

具体第二行的功能从源码开始分析,源码如下:

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

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






参考:

----------------------------------------------------------------------------------------------------

【转载】 TensorFlow tf.app&tf.app.flags用法介绍的更多相关文章

  1. TensorFlow tf.app&tf.app.flags用法介绍

    TensorFlow tf.app&tf.app.flags用法介绍 TensorFlow tf.app argparse  tf.app.flags 下面介绍 tf.app.flags.FL ...

  2. [转载]tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定

    tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...

  3. 【TensorFlow】tf.nn.embedding_lookup函数的用法

    tf.nn.embedding_lookup函数的用法主要是选取一个张量里面索引对应的元素.tf.nn.embedding_lookup(tensor, id):tensor就是输入张量,id就是张量 ...

  4. 【转载】 TensorFlow函数:tf.Session()和tf.Session().as_default()的区别

    原文地址: https://blog.csdn.net/Enchanted_ZhouH/article/details/77571939 ------------------------------- ...

  5. tensorflow中使用变量作用域及tf.variable(),tf,getvariable()与tf.variable_scope()的用法

    一 .tf.variable() 在模型中每次调用都会重建变量,使其存储相同变量而消耗内存,如: def repeat_value(): weight=tf.variable(tf.random_no ...

  6. TensorFlow高级API(tf.contrib.learn)及可视化工具TensorBoard的使用

    一.TensorFlow高层次机器学习API (tf.contrib.learn) 1.tf.contrib.learn.datasets.base.load_csv_with_header 加载cs ...

  7. Tensorflow中的tf.argmax()函数

    转载请注明出处:http://www.cnblogs.com/willnote/p/6758953.html 官方API定义 tf.argmax(input, axis=None, name=None ...

  8. tf.concat, tf.stack和tf.unstack的用法

    tf.concat, tf.stack和tf.unstack的用法 tf.concat相当于numpy中的np.concatenate函数,用于将两个张量在某一个维度(axis)合并起来,例如: a ...

  9. tf.nn.embedding_lookup函数的用法

    关于np.random.RandomState.np.random.rand.np.random.random.np.random_sample参考https://blog.csdn.net/lanc ...

随机推荐

  1. TI DSP数据长度

    环境CCS7.2 平台C6748 结果: sizeof short int is 2sizeof int is 4sizeof long is 4sizeof unsigned long is 4si ...

  2. Nginx 配置及参数详解

    Nginx 配置及参数详解 Nginx Location 指令语法 如下就是常用的 location 配置的语法格式,其中modifier是可选的,location_match就是制定 URI 应该去 ...

  3. windows系统获取进程的pid号并终止

    ,,,* delims= " %a in ('tasklist ^| findstr "AutodeskDesktopApp.exe"') do (set commitd ...

  4. xtrabackup 使用

    创建具有完全备份所需的最小权限的数据库用户的SQL示例如下 mysql> CREATE USER 'bkpuser'@'%' IDENTIFIED BY 's3cret';mysql> G ...

  5. jmeter针对websocket协议的压测

    之前一直没有接触过websocket协议,所以一直对websocket的压测存在疑惑,在网上参考文章并不断尝试之后,终于有所得:第一次用jmeter的websoket插件,用的ws非加密协议,请求都能 ...

  6. ‘cmake' 不是内部或外部命令 也不是可运行的程序 或批处理文

    在 Win7下的命令行模式下,输入cmake相关命令,出现如下错误: ’cmake' 不是内部或外部命令 也不是可运行的程序 或批处理文件 解决方法: 在环境变量中添加cmake的文件路径. 计算机( ...

  7. UiAutomatorViewer无法获取手机截图进行元素定位的解决办法

    问题描述 本来想使用UIAutomatorView定位app页面元素的,最开始我使用的是夜神模拟器,打开UIAutomatorView连接模拟器没有问题,但是后来我使用真机时发现无法连接到真机获取真机 ...

  8. Linux输入输出重定向练习

    1.date >> 123 date > 123 2.abc 2>123 abc 2>>123 abc 2>/dev/null  标准输出重定向到回收站 3. ...

  9. Buy Fruits-(构造)

    https://ac.nowcoder.com/acm/contest/847/C 在blueland上有 n n个水果店,它们的编号依次为 0,1,2...n−1 0,1,2...n−1.奇妙的是, ...

  10. scala 学习笔记--集合

    1.scala集合的null 是nil 而不是null 2.set的三个方法union,intersect,diff union--合并去重 intersect--交集 diff--a减去(a和b交集 ...