tensorflow里面共享变量、name_scope, variable_scope等如何理解

tensorflow里面共享变量、name_scope, variable_scope等如何理解

  • name_scope, variable_scope目的:1 减少训练参数的个数。 2 区别同名变量

  • 为什么要共享变量?我举个简单的例子:例如,当我们研究生成对抗网络GAN的时候,判别器的任务是,如果接收到的是生成器生成的图像,判别器就尝试优化自己的网络结构来使自己输出0,如果接收到的是来自真实数据的图像,那么就尝试优化自己的网络结构来使自己输出1。也就是说,生成图像和真实图像经过判别器的时候,要共享同一套变量,所以TensorFlow引入了变量共享机制。

    来源:http://www.cnblogs.com/Charles-Wan/p/6200446.html

四个tf.Variable(), tf.get_variable(), tf.Variable_scope(), tf.name_scope()的区别:

  1. tf.Variable()和 tf.get_variable() :

    • tf.Variable()会自动检测命名冲突并自行处理。tf.get_variable()有一个变量检测机制,会检测已经存在的变量时否设置为共享变量,如果已经存在该变量且没有被设置为共享变量,则TensorFlow运行到第二个变量时会报。

    • tf.Variable()和 tf.get_variable()这两种方式都用在一个name_scope下面获取或创建一个变量的两种方式的区别在于:tf.Variable()用于创建一个新变量,在同一个name_scope下可以创建相同名字的变量,底层实现会自动引入别名机制,两次调用产生两个不同的变量。tf.get_variable()用于获取一个变量,并且不受name_scope的约束,当这个变量已经存在,则自动获取,如果不存在,则自动创建一个变量。

    • code解析:

作者:C Li
链接:https://www.zhihu.com/question/54513728/answer/181819324
来源:知乎

'''
1 在tf.name_scope下时,tf.get_variable()创建的变量名不受name_scope的影响,而在未指定共享变量时,如果重名就会报错,tf.Variable()会自动检测有没有变量重名,如果有则会自行处理。
'''
import tensorflow as tf
with tf.name_scope('name_scope_x'):
    var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)
    var3 = tf.Variable(name='var2', initial_value=[2], dtype=tf.float32)
    var4 = tf.Variable(name='var2', initial_value=[2], dtype=tf.float32)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(var1.name, sess.run(var1))
    print(var3.name, sess.run(var3))
    print(var4.name, sess.run(var4))
# 输出结果:
# var1:0 [-0.30036557]   可以看到前面不含有指定的'name_scope_x'
# name_scope_x/var2:0 [ 2.]
# name_scope_x/var2_1:0 [ 2.]  可以看到变量名自行变成了'var2_1',避免了和'var2'冲突

'''
2 使用tf.get_variable()创建变量,且没有设置共享变量,重名时会报错。
'''
import tensorflow as tf
with tf.name_scope('name_scope_1'):
    var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)
    var2 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(var1.name, sess.run(var1))
    print(var2.name, sess.run(var2))

# ValueError: Variable var1 already exists, disallowed. Did you mean 
# to set reuse=True in VarScope? Originally defined at:
# var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)

'''
3 共享变量方法,(要共享变量就要使用tf.get_variable(<variable_name>)
'''
import tensorflow as tf
with tf.variable_scope('variable_scope_y') as scope:
    var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)
    scope.reuse_variables()  # 设置共享变量
    var1_reuse = tf.get_variable(name='var1')
    var2 = tf.Variable(initial_value=[2.], name='var2', dtype=tf.float32)
    var2_reuse = tf.Variable(initial_value=[2.], name='var2', dtype=tf.float32)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(var1.name, sess.run(var1))
    print(var1_reuse.name, sess.run(var1_reuse))
    print(var2.name, sess.run(var2))
    print(var2_reuse.name, sess.run(var2_reuse))
# 输出结果:
# variable_scope_y/var1:0 [-1.59682846]
# variable_scope_y/var1:0 [-1.59682846]   可以看到变量var1_reuse重复使用了var1
# variable_scope_y/var2:0 [ 2.]
# variable_scope_y/var2_1:0 [ 2.]

'''
或者
'''
with tf.variable_scope('foo') as foo_scope:
    v = tf.get_variable('v', [1])
with tf.variable_scope('foo', reuse=True):
    v1 = tf.get_variable('v')
assert v1 == v

'''
或者
'''
with tf.variable_scope('foo') as foo_scope:
    v = tf.get_variable('v', [1])
with tf.variable_scope(foo_scope, reuse=True):
    v1 = tf.get_variable('v')
assert v1 == v
  1. tf.name_scope()与tf.variable_scope():

    • tf.name_scope()主要用于管理一个图里的各种op,返回的是一个以scope_name命名的context manager。一个graph会维护一个name_space的堆,每一个namespace下面可以定义各种op或者子namespace,实现一种层次化有条理的管理,避免各个op之间命名冲突。

    • tf.variable_scope()一般与tf.name_scope()配合使用,用于管理一个graph中变量的名字,避免变量之间的命名冲突,tf.variable_scope()允许在一个variable_scope下面共享变量。variable_scope的reuse的默认值为False。

    • 通常情况下,tf.variable_scope和tf.name_scope配合,能画出非常漂亮的流程图,但是他们两个之间又有着细微的差别,那就是name_scope只能管住操作ops的名字,而管不住变量Variables的名字。

    with tf.variable_scope("foo"):
       with tf.name_scope("bar"):
           v = tf.get_variable("v", [1])
           x = 1.0 + v
    assert v.name == "foo/v:0"
    assert x.op.name == "foo/bar/add"
原文地址:https://www.cnblogs.com/adong7639/p/9230642.html