【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一样

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LoveWeeknd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值