tensorflow--variable_scope

1.with tf.variable_scope(name , reuse = reuse)

(1)创建variable scope

with tf.variable_scope("foo"):
    with tf.variable_scope("bar"):
        v = tf.get_variable("v", [1])
        assert v.name == "foo/bar/v:0"

(2)共享变量

with tf.variable_scope("foo"):
    v = tf.get_variable("v", [1])
with tf.variable_scope("foo", reuse=True):
    v1 = tf.get_variable("v", [1])
assert v1 == v

(3)在一个变量域中,不允许重用变量

with tf.variable_scope("foo"):
    v = tf.get_variable("v", [1])
    v1 = tf.get_variable("v", [1])
    #  Raises ValueError("... v already exists ...").

(4)当试图获取在重用模式中不存在的变量时,我们会引发异常

with tf.variable_scope("foo", reuse=True):
    v = tf.get_variable("v", [1])
    #  Raises ValueError("... v does not exists ...").

 

posted @ 2018-11-23 20:12  陈柯成  阅读(212)  评论(0编辑  收藏  举报