简体   繁体   English

在张量流中设置reuse = True时会发生什么

[英]What happens when setting reuse=True in tensorflow

I would like to know what happens in the background when we set the reuse to True in tensorflow. 我想知道当在tensorflow中将重用设置为True时在后台会发生什么。

Basically, when building models in tensorflow for training and testing, I have to create the model first within a method and then call that within a variable scope as follows: 基本上,当在tensorflow中构建模型以进行训练和测试时,我必须首先在方法中创建模型,然后在可变范围内调用该模型,如下所示:

def model(inputs, return_top=True):
    #.... Here I have several conv layers

    if return_top:
        output = tf.layers.dense(output, units=8, name='outputs')

    return output

with tf.variable_scope('model'):
    output_train = model(inputs_train)
    mse_train = cal_loss(output_train, labels_train) # This is a function that calculates the loss
    train_step = optimize(mse_train)    # This is a function that implements the optimizer

with tf.variable_scope('model', reuse=True):
    output_validation = model(inputs_validation)
    mse_validation = cal_loss(output_validation, labels_validation)

When creating models in tensorflow for training and testing, we usually create one model for training; 在张量流中创建模型进行训练和测试时,我们通常会创建一个模型进行训练; and let's assume that we give it a name "model"; 并假设我们给它起一个名字“ model”; ie we created the whole model under a tf.variable_scope("model", reuse=False) ; 也就是说,我们在tf.variable_scope("model", reuse=False)下创建了整个模型; and then we reused the model for testing where we set reuse to True. 然后重用该模型进行测试,将重用设置为True。 Thus, we use with tf.variable_scope("model", reuse=True) . 因此,我们使用with tf.variable_scope("model", reuse=True) Now if I look into tensorboard, I find two copies for the whole model, one under name "model", and the other under "model_1". 现在,如果我查看张量板,则会发现整个模型有两个副本,一个在名称“ model”下,另一个在“ model_1”下。 Also, I found that "model_1" references "model"; 另外,我发现“ model_1”引用了“ model”; ie, the weights of "model_1" are taken from "model" (That's my assumption; I would like to know if this is true). 即,“ model_1”的权重取自“ model”(这是我的假设;我想知道这是否成立)。 Also, I found that the "model" outputs goes into the optimizer, which is not the case with "model_1". 另外,我发现“模型”输出进入优化器,“模型_1”不是这种情况。 I wonder why. 我想知道为什么。 In other words, if "model_1" references "model"; 换句话说,如果“ model_1”引用“ model”; and the optimizer modifies the weights of "model"; 优化器修改“模型”的权重; should it modify the weights of "model_1"? 它应该修改“ model_1”的权重吗?

Any help is much appreciated!! 任何帮助深表感谢!!

First, reuse and variable scopes in general are deprecated and will be removed in tf2. 首先,一般不建议重用和变量作用域,并将在tf2中将其删除。 They can be very confusing, as you see here. 正如您在此处看到的那样,它们可能会非常令人困惑。 We instead recommend you use the tf.keras layers to build your model, which you can reuse by just reusing the objects. 相反,我们建议您使用tf.keras层来构建模型,只需重用对象即可重用模型。

tf.get_variable and tf.variable_scope together can be used to create and reuse the variables in your model. tf.get_variable和tf.variable_scope一起可用于创建和重用模型中的变量。 Inside a variable_scope, once you've called get_variable with a variable name, calling it again with the same variable name is problematic, in that TF cannot tell whether you mean to create a new variable or to reuse the existing one. 在variable_scope内,使用变量名调用get_variable后,使用相同的变量名再次调用它是有问题的,因为TF无法分辨您是要创建新变量还是重用现有变量。 If you pass reuse=False, the default option, we raise an error. 如果您将默认选项重用= False传递给我们,则会引发错误。 If you pass reuse=True, we give you the same old variable back. 如果您通过了reuse = True,我们将返回相同的旧变量。 However, if you call get_variable with a new variable name and pass reuse=True, we also raise an error since there's no variable to reuse. 但是,如果您使用新的变量名称调用get_variable并传递复用= True,则由于没有变量可复用,我们还会引发错误。 We also have reuse=tf.AUTO_REUSE which never raises an error (returns a variable if it exists and creates if not). 我们还具有reuse = tf.AUTO_REUSE,它永远不会引发错误(如果存在则返回变量,如果不存在则创建变量)。

You can also pass reuse as a parameter to variable scopes, which means you'll pass it implicitly to all get_variable calls in that scope. 您还可以将重用作为参数传递给变量作用域,这意味着您会将其隐式传递给该作用域中的所有get_variable调用。

First, you have namespace conflict for variable_scope. 首先,您在variable_scope上存在名称空间冲突。 Since variable_scope 'model' is already present, the second variable_scope creation needs to be unique. 由于variable_scope“模型”已经存在,因此第二个variable_scope创建必须是唯一的。 Tensorflow automatically uniquify it as 'model_1'. Tensorflow会自动将其统一为“ model_1”。 You try repeating the definition again, if will create 'model_2' variable_scope. 如果会创建'model_2'variable_scope,请尝试再次重复定义。

Second, the reuse=True is not for variable_scope names. 其次,reuse = True不适用于variable_scope名称。 It is for the tf.Variable inside the tensorflow variable_scope. 它用于tensorflow variable_scope中的tf.Variable。

Suppose you want to use a tf.Variable across 2 layers. 假设您要在两层中使用tf.Variable。 In this case, you will use 2 python variables pointing to same TF variable. 在这种情况下,您将使用2个指向相同TF变量的python变量。

Without reuse=True, it will throw an error saying something like Variable already exists. 如果不使用reuse = True,它将抛出一个错误,说明诸如Variable已经存在。 With reuse=True, it gives a pass. 如果reuse = True,则通过。

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

相关问题 在@njit numba 中设置 parallel=True 时实际发生了什么? - What actually happens when setting parallel=True in @njit numba? 什么时候为tensorflow中的多GPU训练设置重用= True? - When to set reuse=True for multi GPU training in tensorflow? Tensorflow重用时进行推断? - Tensorflow reuse when inference? 当我使用 tensorflow ops 编写函数时会发生什么 - what happens when I write a function using tensorflow ops 在列表对象上调用IF语句时会发生什么? 调用什么方法确定是对还是错? - What happens when IF statement is called on a list object? What method is called to determine whether is True or False? 张量流中实现的GAN中的'reuse'标志的目的是什么? - What is the purpose of the 'reuse' flag in GAN implemented in tensorflow? 尽管重用设置为true,Tensorflow仍创建新变量 - Tensorflow creating new variables despite reuse set to true google colab中的Tensorflow网络,当我重新运行脚本时会发生什么 - Tensorflow networks in google colab, what happens when I re-run the script 当你使用 tensorflow.keras.layers.Add 不同的形状时会发生什么? - What happens when you use tensorflow.keras.layers.Add on different shapes? VarScope 中设置reuse=True 或reuse=tf.AUTO_REUSE 是什么意思? - What is mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM