作  者: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. Kubernetes学习之路(28)之镜像仓库Harbor部署

    Harbor的部署 官方文档 Harbor有两种安装的方式: 在线安装:直接从Docker Hub下载Harbor的镜像,并启动. 离线安装:在官网上下载离线安装包其地址为:https://githu ...

  2. docker启动报错 docker: Error response from daemon: OCI runtime create failed: container_linux.go:348

    问题描述 doker启动时,报错:docker: Error response from daemon: OCI runtime create failed: container_linux.go:3 ...

  3. Windows系统下安装VirtualBox及安装Ubuntu16.04

    1.软件介绍 VirtualBox VirtualBox 是一款免费的开源虚拟机软件,所谓虚拟机软件,就是能够提供各种模拟的硬件环境,并且在其上安装各种操作系统,目前支持Window,Linux,Ma ...

  4. Kali linux network is unreachable

    使用树莓派4 安装完kali linux后,配置好ip,能ping通上级路由. 但ping公网地址显示网络不可达. Ping: connect: network is unreachable 解决方法 ...

  5. Spring Boot Configuration Annotation Proessor not found in classpath解决办法

    From: https://www.cnblogs.com/whtgjy/p/9438317.html 出现spring boot Configuration Annotation Proessor ...

  6. Elasticsearch 概念理解

    官方文档地址 Filebeat: https://www.elastic.co/cn/products/beats/filebeat https://www.elastic.co/guide/en/b ...

  7. Spring AOP 知识点入门

    一.基本知识点 1.AOP概念 AOP(Aspect-Oriented Programming), 即 面向切面编程, 它与 OOP( Object-Oriented Programming, 面向对 ...

  8. 如果wordpress分类只有一篇文章则直接跳转到文章页

    每个项目的需求都不一样,比如最近ytkah的客户提出如果wordpress分类只有一篇文章则直接跳转到文章页,这个实现起来不会很麻烦,几行代码就能搞定,下面就来一起看看吧.打开主题的function. ...

  9. 牛客网CSP-S提高组赛前集训营Round4

    牛客网CSP-S提高组赛前集训营 标签(空格分隔): 题解 算法 模拟赛 题目 描述 做法 \(BSOJ6377\) 求由\(n\)长度的数组复制\(k\)次的数组里每个连续子序列出现数字种类的和 对 ...

  10. A1136 | 字符串处理、大整数运算

    题目链接: https://www.patest.cn/contests/pat-a-practise/1136 今天是12月17号.最近这几天都有点不在状态.已经整整一周没有练算法了,自从12.3考 ...