简体   繁体   English

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

[英]What is the difference between tf.variable_scope() and tf.train.Saver()?

I am building a neural network. 我正在建立一个神经网络。 I am using tf.variable_scope() for the train part and use the same scope for the test part (reset=True). 我将tf.variable_scope()用于火车零件,并将相同的作用域用于测试零件(reset = True)。 I am wondering whether I will get different results if I use tf.train.Saver() to save the variables in the train part and use tf.train.restore() to restore the variables for the test part? 我想知道如果我使用tf.train.Saver()将变量保存在训练部分中,并使用tf.train.restore()恢复测试部分的变量是否会得到不同的结果? Basically, I want to make sure the trained variables are copied to the test part. 基本上,我想确保将训练后的变量复制到测试部分。

For instance, in the example below, both variables v1 and v_1 have the same values, so, in this case, do I need to use tf.train.Saver() to get correct results? 例如,在下面的示例中,变量v1和v_1具有相同的值,因此,在这种情况下,是否需要使用tf.train.Saver()获得正确的结果? import tensorflow as tf tf.reset_default_graph() 将tensorflow导入为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())

tf.variable_scope() changes your variable names. tf.variable_scope()更改变量名。

For example, consider creating a variable with and without variable_scope : 例如,考虑创建一个带有和不带有variable_scope

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

Note the variable is named myvar:0 , when you save the checkpoint using tf.train.Saver() this will be the name of this variable. 请注意,该变量名为myvar:0 ,当您使用tf.train.Saver()保存检查点时,它将是此变量的名称。 When you restore your checkpoint with tf.train.restore() you better have a variable named myvar:0 in your graph otherwise it won't know where to restore the variable to. 当您使用tf.train.restore()恢复检查点时,最好在图形中有一个名为myvar:0的变量,否则它将不知道将变量恢复到的位置。

Now the same thing with tf.variable_scope() : 现在与tf.variable_scope()相同:

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

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

Notice that the name is now newscope/myvar:0 . 注意,该名称现在为newscope/myvar:0 The name of the variable has changed. 变量的名称已更改。 this allows you to keep your variable namespace organized. 这样可以使变量名称空间井井有条。 It's useful for debugging and for visualizing things in tensorboard mainly. 主要用于调试和可视化tensorboard中的东西。

When you save and restore a model you have 2 approaches you can take. 保存和还原模型时,可以采用两种方法。

The basic checkpoint just saves the data associated with the model (this appears to be what you've referenced in your question). 基本检查点只是保存与模型关联的数据(这似乎是您在问题中引用的内容)。 In this paradigm, you need to re-create all of the variables in your graph before loading the checkpoint, and your names better all match (if you used variable_scope in training you better have done the same in test). 在此范例中,您需要在加载检查点之前重新创建图形中的所有变量,并且您的名称最好完全匹配(如果您在训练中使用了variable_scope,则最好在测试中也做同样的事情)。 This is the method I typically follow and I recommend you put all of your tensorflow ops in a function called build_graph() . 这是我通常遵循的方法,建议您将所有tensorflow ops放入一个名为build_graph()的函数中。 Then to re-build the graph for your training or test, you just call that function and voila, the graph is identical and the save/restore functions work as expected. 然后,要重新构建用于训练或测试的图,只需调用该函数并瞧,图是相同的,并且保存/恢复功能按预期工作。

You might also note that you can save the meta_graph and restore not only the variables, but also the graph definitions (the actual operations you're performing). 您可能还注意到,您可以保存meta_graph,不仅可以还原变量,还可以还原图定义(您正在执行的实际操作)。 In this paradigm you first load the meta_graph definitions, then the checkpoint, you don't need to re-define the graph in your test environment. 在此范例中,您首先加载meta_graph定义,然后加载检查点,而无需在测试环境中重新定义图。 I don't use this method much so I won't go into any more detail about it. 我没有使用太多这种方法,因此我将不对其进行任何详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM