TensorFlow 学习总结

1. TensorFlow  生成的  .ckpt 和  .pb 都有什么用?


The .ckpt is the model given by tensorflow which includes all the 
weights/parameters in the model.  The .pb file stores the computational 
graph.  To make tensorflow work we need both the graph and the 
parameters.  There are two ways to get the graph: 
(1) use the python program that builds it in the first place (tensorflowNetworkFunctions.py).
(2) Use a .pb file (which would have to be generated by tensorflowNetworkFunctions.py). 

.ckpt file is were all the intelligence is.

2. TensorFlow saving into/loading a graph from a file

       

       正好看到 StackOverflow 上有位仁兄问过相关的问题,整理的不错

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

        From what I've gathered so far, there are several different ways of dumping a TensorFlow graph
into a file and then loading it into another program, but I haven't been able to find clear examples/information on how they work. What I already know is this:

  1. Save the model's variables into a checkpoint file (.ckpt) using a tf.train.Saver() and restore them later (source)
  2. Save a model into a .pb file and load it back in using tf.train.write_graph() and tf.import_graph_def() (source)
  3. Load in a model from a .pb file, retrain it, and dump it into a new .pb file using Bazel (source)
  4. Freeze the graph to save the graph and weights together (source)
  5. Use as_graph_def() to save the model, and for weights/variables, map them into constants (source)

However, I haven't been able to clear up several questions regarding these different methods:

  1. Regarding checkpoint files, do they only save the trained weights of a model? Could checkpoint files be loaded into a new program, and be used to run the model, or do they simply serve as ways to save the weights in a model at a certain time/stage?
  2. Regarding tf.train.write_graph(), are the weights/variables saved as well?
  3. Regarding Bazel, can it only save into/load from .pb files for retraining? Is there a simple Bazel command just to dump a graph into a .pb?
  4. Regarding freezing, can a frozen graph be loaded in using tf.import_graph_def()?
  5. The Android demo for TensorFlow loads in Google's Inception model from a .pb file. If I wanted to substitute my own .pb file, how would I go about doing that? Would I need to change any native code/methods?
  6. In general, what exactly is the difference between all these methods? Or more broadly, what is the difference between as_graph_def()/.ckpt/.pb?

In short, what I'm looking for is a method to save both a graph (as in, the various operations and such) and its weights/variables into a file, which can then be used to load the graph and weights into another program, for use (not necessarily continuing/retraining).

Documentation about this topic isn't very straightforward, so any answers/information would be greatly appreciated.

1 Answer

up vote
down vote accepted

There are many ways to approach the problem of saving a model in TensorFlow, which can make it a bit confusing. The documentation on this topic is taking shape, but doesn't cover all of the details in your question. Taking each of your sub-questions in turn:

  1. The checkpoint files (produced e.g. by calling saver.save() on a tf.train.Saver object) contain only the weights, and any other variables defined in the same program. To use them in another program, you must re-create the associated graph structure (e.g. by running code to build it again, or calling tf.import_graph_def()), which tells TensorFlow what to do with those weights. Note that calling saver.save() also produces a file containing a MetaGraphDef, which contains a graph and details of how to associate the weights from a checkpoint with that graph. See the tutorial for more details.

  2. tf.train.write_graph() only writes the graph structure; not the weights.

  3. Bazel is unrelated to reading or writing TensorFlow graphs. (Perhaps I misunderstand your question: feel free to clarify it in a comment.)

  4. A frozen graph can be loaded using tf.import_graph_def(). In this case, the weights are (typically) embedded in the graph, so you don't need to load a separate checkpoint.

  5. The main change would be to update the names of the tensor(s) that are fed into the model, and the names of the tensor(s) that are fetched from the model. In the TensorFlow Android demo, this would correspond to the inputName and outputName strings that are passed to TensorFlowClassifier.initializeTensorFlow().

  6. The GraphDef is the program structure, which typically does not change through the training process. The checkpoint is a snapshot of the state of a training process, which typically changes at every step of the training process. As a result, TensorFlow uses different storage formats for these types of data, and the low-level API provides different ways to save and load them. Higher-level libraries, such as the MetaGraphDef libraries, Keras, and skflow build on these mechanisms to provide more convenient ways to save and restore an entire model.

share improve this answer
 
 
Does this mean that the C++ API documentation lies, when it says that you can load the graph saved withtf.train.write_graph() and then execute it? –  mnicky  yesterday 
 
The C++ API documentation does not lie, but it is missing a few details. The most important detail is that, in addition to the GraphDef saved by tf.train.write_graph(), you also need to remember the names of the tensors that you want to feed and fetch when executing the graph (item 5 above). –  mrry  yesterday






















  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorFlow中,我们可以使用tf.train模块中的Optimizer类和learning_rate参数来控制学习率。学习率是在模型训练过程中决定参数更新步长的重要超参数。 首先,我们可以使用tf.train模块中的Optimizer类来定义一个优化器对象。例如,可以使用tf.train.GradientDescentOptimizer来定义一个梯度下降优化器对象。 然后,在定义一个optimization步骤时,我们可以通过设置learning_rate参数来指定所需的学习率。例如,可以使用optimizer.minimize(loss, var_list=variables, learning_rate=0.01)来指定学习率为0.01。 如果想在训练过程中动态修改学习率,可以通过在定义optimization步骤时将学习率设置为一个Tensor而不是一个固定的常数。这样,我们可以使用tf.placeholder来创建一个占位符,并在每次执行训练步骤时,将实时的学习率值传递给占位符。 例如,可以使用learning_rate = tf.placeholder(tf.float32, shape=[])来创建一个学习率的占位符。然后,在每次执行训练步骤时,可以通过feed_dict参数来传递实时的学习率值,例如feed_dict={learning_rate: 0.01}。 这样,在每次训练步骤中,我们就可以动态地修改学习率。例如,可以通过设置不同学习率值来实现学习率衰减或动态调整学习率的算法。 总结起来,要在TensorFlow中动态修改学习率,我们可以通过创建一个学习率的占位符,并在每次执行训练步骤时,通过feed_dict参数传递实时的学习率值。这样就可以实现学习率的动态修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值