【Tensorflow API】Variable与get_variable区别,name_scope与variable_scope区别

1、Variable遇到相同的name时系统会自动重命名

import tensorflow as tf

v1 = tf.Variable(1, name='v1')
v2 = tf.Variable(2, name='v1')
print(v1)
print(v2)

output:

<tf.Variable 'v1:0' shape=() dtype=int32_ref>
<tf.Variable 'v1_1:0' shape=() dtype=int32_ref>

2、get_variable遇到相同的name时直接报错

import tensorflow as tf

v1 = tf.get_variable('v1', [1])
v2 = tf.get_variable('v1', [2])

错误信息:

ValueError: Variable v1 already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

分析:在同一作用域内,Variable遇到同名变量时,不会重复使用,而是直接新建一个变量,get_variable遇到同名变量时,如果没有声明可以重复使用,则直接报错,那么解决get_variable可以重复使用的方法是使用命名空间,variable_scope,前提是要加上reuse = True,并且命名空间的变量名也要相同

import tensorflow as tf

with tf.variable_scope("scope1"):
    w1 = tf.get_variable("w1", shape=[])
    w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope1", reuse=True):
    w1_p = tf.get_variable("w1", shape=[])
    w2_p = tf.Variable(1.0, name="w2")

print(w1)
print(w2)
print(w1_p)
print(w2_p)

output:

<tf.Variable 'scope1/w1:0' shape=() dtype=float32_ref>
<tf.Variable 'scope1/w2:0' shape=() dtype=float32_ref>
<tf.Variable 'scope1/w1:0' shape=() dtype=float32_ref>
<tf.Variable 'scope1_1/w2:0' shape=() dtype=float32_ref>

如果是在不同命名空间下的话,需要将reuse=Ture去掉:

import tensorflow as tf

with tf.variable_scope("scope1"):
    w1 = tf.get_variable("w1", shape=[])
    w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope2"):
    w1_p = tf.get_variable("w1", shape=[])
    w2_p = tf.Variable(1.0, name="w2")

print(w1)
print(w2)
print(w1_p)
print(w2_p)

output:

<tf.Variable 'scope1/w1:0' shape=() dtype=float32_ref>
<tf.Variable 'scope1/w2:0' shape=() dtype=float32_ref>
<tf.Variable 'scope2/w1:0' shape=() dtype=float32_ref>
<tf.Variable 'scope2/w2:0' shape=() dtype=float32_ref>

再看下name_scope和variable_scope区别:

import tensorflow as tf

with tf.name_scope("scope1"):
    w1 = tf.get_variable("w1", shape=[])
    w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope1"):
    w1_p = tf.get_variable("w1", shape=[])
    w2_p = tf.Variable(1.0, name="w2")

print(w1)
print(w2)
print(w1_p)
print(w2_p)

output:

<tf.Variable 'w1:0' shape=() dtype=float32_ref>
<tf.Variable 'scope1/w2:0' shape=() dtype=float32_ref>
<tf.Variable 'scope1/w1:0' shape=() dtype=float32_ref>
<tf.Variable 'scope1_1/w2:0' shape=() dtype=float32_ref>

从以上可以看出,name_scope对get_variable的op不起作用,对Variable的作用同variable_scope一样

猜你喜欢

转载自blog.csdn.net/oMoDao1/article/details/81867115
今日推荐