Tensorflow - tf.variable_scope ,重用 GAN 的参数
Tensorflow - tf.variable_scope , reuse argument for GAN
我正在尝试为一个项目构建一个 GAN,我真的很想了解 tensorflow variable_scope 中的变量共享是如何工作的。
为了构建 GAN,我有一个生成器网络和两个鉴别器网络:
一个鉴别器输入真实图像,另一个鉴别器输入由生成器创建的假图像。重要的是,输入真实图像的鉴别器和输入假图像的鉴别器需要共享相同的权重。为此,我需要共享权重。
我得到了鉴别器和生成器的定义,假设:
def discriminator(images, reuse=False):
with variable_scope("discriminator", reuse=reuse):
#.... layer definitions, not important here
#....
logits = tf.layers.dense(X, 1)
logits = tf.identity(logits, name="logits")
out = tf.sigmoid(logits, name="out")
# 14x14x64
return logits, out
def generator(input_z, reuse=False):
with variable_scope("generator", reuse=reuse):
#.. not so important
out = tf.tanh(logits)
return out
现在正在调用生成器和鉴别器函数:
g_model = generator(input_z)
d_model_real, d_logits_real = discriminator(input_real)
#Here , reuse=True should produce the weight sharing between d_model_real, d_logits_real
#and d_model_fake and d_logits_fake.. why?
d_model_fake, d_logits_fake = discriminator(g_model, reuse=True)
为什么 second 调用中的 reuse=True 语句会产生权重共享?据我了解,您需要在第一次调用时决定变量的重用,以便稍后在程序的某个地方使用它们。
如果有人能向我解释这一点,我将非常高兴,我没有找到该主题的良好来源,而且它对我来说真的很混乱和复杂。
谢谢!
在底层,变量是使用 tf.get_variable()
创建的。
此函数将在变量名前加上作用域,并在创建新变量之前检查它是否存在。
例如,如果您在范围 "fc"
中并调用 tf.get_variable("w", [10,10])
,变量名称将为 "fc/w:0"
。
现在,当您第二次执行此操作时,如果 reuse=True
,范围将再次为 "fc"
,并且 get_variable 将重用变量 "fc/w:0"
.
但是如果reuse=False
,你会得到一个错误,因为变量"fc/w:0"
已经存在,提示你使用不同的名字或者使用reuse=True
.
示例:
In [1]: import tensorflow as tf
In [2]: with tf.variable_scope("fc"):
...: v = tf.get_variable("w", [10,10])
...:
In [3]: v
Out[3]: <tf.Variable 'fc/w:0' shape=(10, 10) dtype=float32_ref>
In [4]: with tf.variable_scope("fc"):
...: v = tf.get_variable("w", [10,10])
...:
ValueError: Variable fc/w already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
In [5]: with tf.variable_scope("fc", reuse=True):
...: v = tf.get_variable("w", [10,10])
...:
In [6]: v
Out[6]: <tf.Variable 'fc/w:0' shape=(10, 10) dtype=float32_ref>
请注意,您可以只实例化一个鉴别器,而不是共享权重。然后,您可以决定使用 placeholder_with_default.
向其提供真实数据或生成的数据
我正在尝试为一个项目构建一个 GAN,我真的很想了解 tensorflow variable_scope 中的变量共享是如何工作的。
为了构建 GAN,我有一个生成器网络和两个鉴别器网络: 一个鉴别器输入真实图像,另一个鉴别器输入由生成器创建的假图像。重要的是,输入真实图像的鉴别器和输入假图像的鉴别器需要共享相同的权重。为此,我需要共享权重。
我得到了鉴别器和生成器的定义,假设:
def discriminator(images, reuse=False):
with variable_scope("discriminator", reuse=reuse):
#.... layer definitions, not important here
#....
logits = tf.layers.dense(X, 1)
logits = tf.identity(logits, name="logits")
out = tf.sigmoid(logits, name="out")
# 14x14x64
return logits, out
def generator(input_z, reuse=False):
with variable_scope("generator", reuse=reuse):
#.. not so important
out = tf.tanh(logits)
return out
现在正在调用生成器和鉴别器函数:
g_model = generator(input_z)
d_model_real, d_logits_real = discriminator(input_real)
#Here , reuse=True should produce the weight sharing between d_model_real, d_logits_real
#and d_model_fake and d_logits_fake.. why?
d_model_fake, d_logits_fake = discriminator(g_model, reuse=True)
为什么 second 调用中的 reuse=True 语句会产生权重共享?据我了解,您需要在第一次调用时决定变量的重用,以便稍后在程序的某个地方使用它们。
如果有人能向我解释这一点,我将非常高兴,我没有找到该主题的良好来源,而且它对我来说真的很混乱和复杂。 谢谢!
在底层,变量是使用 tf.get_variable()
创建的。
此函数将在变量名前加上作用域,并在创建新变量之前检查它是否存在。
例如,如果您在范围 "fc"
中并调用 tf.get_variable("w", [10,10])
,变量名称将为 "fc/w:0"
。
现在,当您第二次执行此操作时,如果 reuse=True
,范围将再次为 "fc"
,并且 get_variable 将重用变量 "fc/w:0"
.
但是如果reuse=False
,你会得到一个错误,因为变量"fc/w:0"
已经存在,提示你使用不同的名字或者使用reuse=True
.
示例:
In [1]: import tensorflow as tf
In [2]: with tf.variable_scope("fc"):
...: v = tf.get_variable("w", [10,10])
...:
In [3]: v
Out[3]: <tf.Variable 'fc/w:0' shape=(10, 10) dtype=float32_ref>
In [4]: with tf.variable_scope("fc"):
...: v = tf.get_variable("w", [10,10])
...:
ValueError: Variable fc/w already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
In [5]: with tf.variable_scope("fc", reuse=True):
...: v = tf.get_variable("w", [10,10])
...:
In [6]: v
Out[6]: <tf.Variable 'fc/w:0' shape=(10, 10) dtype=float32_ref>
请注意,您可以只实例化一个鉴别器,而不是共享权重。然后,您可以决定使用 placeholder_with_default.
向其提供真实数据或生成的数据