tf.variable_scope()和tf.train.Saver()有什么区别?

时间:2018-03-13 22:24:27

标签: python tensorflow neural-network

我正在建立一个神经网络。我使用tf.variable_scope()作为列车部件,并使用相同的范围作为测试部件(reset = True)。我想知道如果我使用tf.train.Saver()来保存列车部分中的变量并使用tf.train.restore()来恢复测试部分的变量,我是否会得到不同的结果? 基本上,我想确保将训练过的变量复制到测试部分。

例如,在下面的示例中,变量v1和v_1都具有相同的值,因此,在这种情况下,是否需要使用tf.train.Saver()来获得正确的结果?     导入张量流为tf     tf.reset_default_graph()

# Create some variables.
with tf.variable_scope("first_model"):
    v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
    v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer)

    inc_v1 = v1.assign(v1+2)
    dec_v2 = v2.assign(v2-1)



with tf.variable_scope("first_model", reuse=True):
    # Create some variables.
    v_1 = tf.get_variable("v1", shape=[3])
    v_2 = tf.get_variable("v2", shape=[5])

    inc_v_1 = v1.assign(v1+2)
    dec_v_2 = v2.assign(v2-1)

 # Add an op to initialize the variables.
init_op = tf.global_variables_initializer()

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, initialize the variables, do some work, and save the
# variables to disk.
with tf.Session() as sess:
  sess.run(init_op)
  # Do some work with the model.
  inc_v1.op.run()
  dec_v2.op.run()

  inc_v_1.op.run()
  dec_v_2.op.run()
  # Save the variables to disk.
  save_path = saver.save(sess, "/tmp/model_2.ckpt")
  print("Model saved in path: %s" % save_path)


  print("v1 : %s" % v1.eval())
  print("v2 : %s" % v2.eval())

  print("v_1 : %s" % v_1.eval())
  print("v_2 : %s" % v_2.eval())

1 个答案:

答案 0 :(得分:0)

tf.variable_scope()更改您的变量名称。

例如,考虑使用和不使用variable_scope创建变量:

x = tf.Variable(1.0, name='myvar')
<tf.Variable 'myvar:0' shape=() dtype=float32_ref>

请注意,变量名为myvar:0,当您使用tf.train.Saver()保存检查点时,这将是此变量的名称。使用tf.train.restore()恢复检查点时,最好在图表中使用名为myvar:0的变量,否则无法知道将变量恢复到的位置。

现在与tf.variable_scope()相同:

with tf.variable_scope('newscope'):
  x = tf.Variable(1.0, name='myvar')

<tf.Variable 'newscope/myvar:0' shape=() dtype=float32_ref>

请注意,该名称现在为newscope/myvar:0。变量的名称已更改。这允许您保持变量命名空间的有序性。它主要用于调试和显示张量板中的东西。

保存和恢复模型时,您可以采用2种方法。

基本检查点只保存与模型相关的数据(这似乎是您在问题中引用的内容)。在这个范例中,您需要在加载检查点之前重新创建图形中的所有变量,并且您的名称更好地匹配(如果您在训练中使用variable_scope,则最好在测试中完成相同的操作)。这是我通常遵循的方法,我建议您将所有tensorflow操作放在一个名为build_graph()的函数中。然后重新构建用于训练或测试的图形,只需调用该函数即可,图形相同,保存/恢复功能按预期工作。

您可能还会注意到,您可以保存meta_graph,不仅可以恢复变量,还可以恢复图形定义(您正在执行的实际操作)。在这个范例中,您首先加载meta_graph定义,然后加载检查点,您不需要在测试环境中重新定义图形。我没有太多使用这种方法,所以我不会详细了解它。