TensorFlow-谷歌深度学习库 存取训练过程中的参数 #tf.train.Saver #checkpoints file

当你一溜十三招训练出了很多参数,如权重矩阵和偏置参数, 当然希望可以通过一种方式把这些参数的值记录下来啊。这很关键,因为如果你把这些值丢弃的话那就前功尽弃了。这很重要啊有木有!!

在TensorFlow中使用tf.train.Saver这个类取不断的存取checkpoints文件从而实现这一目的。

看一下官方说明文档:

class Saver(builtins.object)
Saves and restores variables.for an overview of variables, saving and restoring.
The `Saver` class adds ops to save and restore variables to and from  *checkpoints*.

Checkpoints are binary files in a proprietary format which map variable names to tensor values. The best way to examine the contents of a checkpoint is to load it using a `Saver`.

  __init__(self, var_list=None, reshape=False, sharded=False, max_to_keep=5, keep_checkpoint_every_n_hours=10000.0, name=None, restore_sequentially=False, saver_def=None, builder=None, defer_build=False, allow_empty=False, write_version=2, pad_step_number=False, save_relative_paths=False, filename=None)
 # Creates a `Saver`.
 # The constructor adds ops to save and restore variables.

当需要将参数写入checkpoint时,用save函数

 save(self, sess, save_path, global_step=None, latest_filename=None, meta_graph_suffix='meta', write_meta_graph=True, write_state=True, strip_default_attrs=False)
"""
      Saves variables.
       
       This method runs the ops added by the constructor for saving variables.
       It requires a session in which the graph was launched.  The variables to
       save must also have been initialized.
       
       The method returns the path prefix of the newly created checkpoint files.
       This string can be passed directly to a call to `restore()`.
"""

当要读取参数是,用restore函数

  restore(self, sess, save_path)
"""
 |      Restores previously saved variables.
 |      
 |      This method runs the ops added by the constructor for restoring variables.
 |      It requires a session in which the graph was launched.  The variables to
 |      restore do not have to have been initialized, as restoring is itself a way
 |      to initialize variables.
"""

 

posted @ 2018-03-30 04:06  火山岩上的小红花  阅读(673)  评论(0编辑  收藏  举报