TensorFlow tf.app&tf.app.flags用法介绍
TensorFlow tf.app&tf.app.flags用法介绍
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)
两种调用方式:
方式一:
- 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

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用法介绍的更多相关文章
- 【转载】 TensorFlow tf.app&tf.app.flags用法介绍
作 者:marsggbo 出 处:https://www.cnblogs.com/marsggbo版权声明:署名 - 非商业性使用 - 禁止演绎,协议普通文本 | 协议法律文本. ---------- ...
- 【TensorFlow】tf.nn.embedding_lookup函数的用法
tf.nn.embedding_lookup函数的用法主要是选取一个张量里面索引对应的元素.tf.nn.embedding_lookup(tensor, id):tensor就是输入张量,id就是张量 ...
- tensorflow中使用变量作用域及tf.variable(),tf,getvariable()与tf.variable_scope()的用法
一 .tf.variable() 在模型中每次调用都会重建变量,使其存储相同变量而消耗内存,如: def repeat_value(): weight=tf.variable(tf.random_no ...
- TensorFlow高级API(tf.contrib.learn)及可视化工具TensorBoard的使用
一.TensorFlow高层次机器学习API (tf.contrib.learn) 1.tf.contrib.learn.datasets.base.load_csv_with_header 加载cs ...
- tf.nn.embedding_lookup函数的用法
关于np.random.RandomState.np.random.rand.np.random.random.np.random_sample参考https://blog.csdn.net/lanc ...
- tf.concat, tf.stack和tf.unstack的用法
tf.concat, tf.stack和tf.unstack的用法 tf.concat相当于numpy中的np.concatenate函数,用于将两个张量在某一个维度(axis)合并起来,例如: a ...
- Tensorflow中的tf.argmax()函数
转载请注明出处:http://www.cnblogs.com/willnote/p/6758953.html 官方API定义 tf.argmax(input, axis=None, name=None ...
- Tensorflow 学习笔记 -----tf.where
TensorFlow函数:tf.where 在之前版本对应函数tf.select 官方解释: tf.where(input, name=None)` Returns locations of true ...
- python3 zip 与tf.data.Data.zip的用法
###python自带的zip函数 与 tf.data.Dataset.zip函数 功能用法相似 ''' zip([iterator1,iterator2,]) 将可迭代对象中对应的元素打包成一个元祖 ...
随机推荐
- keepalived初次安装体验
keepalived主要有两大功能,一个是LB,一个是VRRP+failover,其中LB功能和LVS的功能类似,都是通过在LB上配置RS,监控RS的状态,将从client来的请求发送给对应算法的RS ...
- exgcd证明和最基础应用
如何求解这个方程:\(ax + by = gcd (a, b)\)? \(∵gcd(a, b) = gcd (b, a \% b)\) \(∴\)易证 $ gcd(a, b)$ 总是可以化为 \(gc ...
- 看我如何未授权登陆某APP任意用户(token泄露实例)
转载:https://www.nosafe.org/thread-333-1-1.html 先来看看这个. 首先,我在登陆时候截取返回包修改id值是无效的,因为有一个token验证,经过多次登陆 ...
- Linux如何修改和查询时区时间
Linux如何修改和查询时区时间 我在日常工作中,最近遇到了在解压源码包的时候,提示时间比较旧,解压安装出现问题.原因是,租用的vps所在时区和自己所需要的时区不一致,于是在网上找了相关资料.并亲自实 ...
- BZOJ2006 ST表 + 堆
https://www.lydsy.com/JudgeOnline/problem.php?id=2006 题意:在长度N的序列中求K段长度在L到R之间的区间,使得他们的和最大 很容易想到要求一个前缀 ...
- LightGBM 调参方法(具体操作)
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- u-boot(六)小结
目录 u-boot(六)小结 概述 内存分布 内核交互参数 title: u-boot(六)小结 tags: linux date: 2018-09-27 23:23:05 --- u-boot(六) ...
- Compiling R-3.4.3 on CentOS 6.10 with GNU
If you are compiling CentOS 6, you will notice that the R source will not compile without a updated ...
- centos之Too many open files问题-修改linux最大文件句柄数
linux服务器大并发调优时,往往需要预先调优linux参数,其中修改linux最大文件句柄数是最常修改的参数之一. 在linux中执行ulimit -a 即可查询linux相关的参数,如下所示: [ ...
- Docker: 安装配置入门[二]
一.安装配置启动 1.环境 [root@docker1 ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@d ...