【问题标题】:Tensorflow, what is saved with tf.train.Saver?Tensorflow,用 tf.train.Saver 保存了什么?
【发布时间】:2016-10-20 10:31:43
【问题描述】:

我想知道当我在每个训练时期后使用 tf.train.Saver() 保存我的模型时到底保存了什么。与我习惯使用 Keras 模型的文件相比,该文件似乎有点大。现在我的 RNN 每次保存都占用 900 MB。有没有办法告诉保护者只保存可训练的参数?我还想要一种只保存模型的一部分的方法。我知道我可以获取我定义的变量并使用 numpy 格式保存它们,但是当我使用 RNN 类时,我无法直接访问它们的权重,我查看了代码,没有什么像 get_weights 那样我可以看到.

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    如果Saver没有指定var_list,则默认保存所有variables._all_saveable_objects()。

    即Saver默认会保存所有全局变量和可保存变量。

    def _all_saveable_objects():
      """Returns all variables and `SaveableObject`s that must be checkpointed.
    
      Returns:
        A list of `Variable` and `SaveableObject` to be checkpointed
      """
      # TODO(andreasst): make this function public once things are settled.
      return (ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES) +
              ops.get_collection(ops.GraphKeys.SAVEABLE_OBJECTS))
    

    【讨论】:

      【解决方案2】:

      您可以在Saver构造函数中提供要保存的变量列表,即saver=tf.train.Saver(var_list=tf.trainable_variables())

      【讨论】:

      • 这是不完整的,因为某些变量不可训练,但仍由 Saver 保存。
      • 你好,我想知道,如果网络包含BN层,我该怎么办?因为均值和方差是变量,但它们是不可训练的。如果我只保存tf.tf.trainable_variables(),当我恢复模型时,tensorflow找不到BN均值参数……
      猜你喜欢
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      相关资源
      最近更新 更多