tensorflow点滴

flags的使用:

import tensorflow as tf
flags = tf.flags # flags是tf中的一个模块
FLAGS = flags.FLAGS # FLAGS是一个全局的变量,通过该变量可以访问所有的参数
flags.DEFINE_string("flag_name","default_value","doc_string") # 调用flags模块中的DEFINE_string函数添加optional argument(可选参数)
flags.DEFINE_bool("flag_name","default_value","doc_string")
flags.DEFINE_int("flag_name","default_value","doc_string")
flags.DEFINE_float("flag_name","default_value","doc_string")
# 访问参数的方法
FLAGS.flag_name...

tensorflow中的flags模块的源代码很简单,内部是通过import argparse as _argparse来实现命令行参数的解析。


yield的使用:

相当于关键字return,只不过返回的是生成器generator.

只有在迭代时内部代码才实际执行。下一次迭代时,从yield之后的代码开始执行

https://stackoverflow.com/questions/44516609/tensorflow-what-is-the-relationship-between-ckpt-file-and-ckpt-meta-and-ckp

  • the .ckpt file is the old version output of saver.save(sess), which is the equivalent of your .ckpt-data (see below)

  • the "checkpoint" file is only here to tell some TF functions which is the latest checkpoint file.

  • .ckpt-meta contains the metagraph, i.e. the structure of your computation graph, without the values of the variables (basically what you can see in tensorboard/graph).

  • .ckpt-data contains the values for all the variables, without the structure. To restore a model in python, you'll usually use the meta and data files with (but you can also use the .pb file):

    saver = tf.train.import_meta_graph(path_to_ckpt_meta)
    saver.restore(sess, path_to_ckpt_data)
    
  • I don't know exactly for .ckpt-index, I guess it's some kind of index needed internally to map the two previous files correctly. Anyway it's not really necessary usually, you can restore a model with only .ckpt-meta and .ckpt-data.

  • the .pb file can save your whole graph (meta + data). To load and use (but not train) a graph in c++ you'll usually use it, created with freeze_graph, which creates the .pb file from the meta and data. Be careful, (at least in previous TF versions and for some people) the py function provided by freeze_graph did not work properly, so you'd have to use the script version. Tensorflow also provides a tf.train.Saver.to_proto() method, but I don't know what it does exactly.

    • meta file: describes the saved graph structure, includes GraphDef, SaverDef, and so on; then apply tf.train.import_meta_graph('/tmp/model.ckpt.meta'), will restore Saver and Graph.

    • index file: it is a string-string immutable table(tensorflow::table::Table). Each key is a name of a tensor and its value is a serialized BundleEntryProto. Each BundleEntryProto describes the metadata of a tensor: which of the "data" files contains the content of a tensor, the offset into that file, checksum, some auxiliary data, etc.

    • data file: it is TensorBundle collection, save the values of all variables.


cp $< $@

$< 第一个依赖

$@ 目标


tensorflow模型转成numpy

# c is checkpoint prefix
var_list=tf.contrib.framework.list_variables(c)
reader=tf.contrib.framework.load_checkpoint(c)
args={}
for name,shape in var_list:
  if not name.startswith("global_step") and not name.startswith("train") and not name.startswith("losses"):
    tensor=reader.get_tensor(name)
    args[name]=tensor

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值