TensorFlow可以将变量保存在某个variable_scope中吗?

时间:2017-10-03 16:13:06

标签: tensorflow

我知道TensoFlow可以通过显式指定变量列表来保存某些变量,如下所示:

var1 = tf.get_variable('var_1', shape, dtype)
var2 = tf.get_variable('var_2', shape, dtype)
saver = tf.train.Saver([var1, var2])
saver.save(sess, path)

但我不知道TF是否可以将变量保存在某个variable_scope 中,因为如果我想预先训练模型并且参数都在某个variable_scope中,那么将会很方便保存variable_scope中的所有变量并将其恢复。 如果TF现在无法实现这个功能,请给我一些关于如何预训模型和保存预训练模型参数的建议。 非常感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用tf.train.Saver为前缀为scope_prefix的变量范围创建tf.get_collection(),如下所示:

saver = tf.train.Saver(
    tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope_prefix))