一篇文章弄懂tensorflow的Variab和get_variable,name_scope和variable_scope的区别

Variable与get_variable

直接上代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time       : 2018/11/13 9:18
@Author     : Li Shanlu
@File       : Variable_and_get_variable.py
@Software   : PyCharm
@Description: 解释并验证tensorflow中placeholder、Variable、get_variable的区别
"""
import tensorflow as tf
print("example_1 start ......")
v1 = tf.placeholder(tf.float32, shape=[1])
print(v1.name)
v1 = tf.placeholder(tf.float32, shape=[1], name='variable')
print(v1.name)
v1 = tf.placeholder(tf.float32, shape=[1], name='variable')
print(v1.name)

print(type(v1))
var_set = tf.trainable_variables()
print(len(var_set))
print("example_1 end ......\n")

"""
>> 
Placeholder:0
variable:0
variable_1:0
<class 'tensorflow.python.framework.ops.Tensor'>
0
___________________________________________________________________________
总结:
1.如果没有给placeholder指定名字,那么默认是Placeholder、Placeholder_1。 
2.声明重复名字的placeholder是允许的,但是系统会按照顺序进行编号name、name_1。 
3.placeholder是Tensor类型。 
4.调用存在可训练变量,长度为0,所以placeholder属于不可训练参数。
"""

print("example_2 start ......")
v2 = tf.Variable([1], dtype=tf.float32)
print(v2.name)
v2 = tf.Variable([1], dtype=tf.float32, name='var2')
print(v2.name)
v2 = tf.Variable([1], dtype=tf.float32, name='var2')
print(v2.name)

print(type(v2))
vars = tf.trainable_variables()
for i in vars:
    print(i)
print("example_2 end ......\n")

"""
>> 
Variable:0
var2:0
var2_1:0
<class 'tensorflow.python.ops.variables.Variable'>
<tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'var2:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'var2_1:0' shape=(1,) dtype=float32_ref>
_________________________________________________________________________
总结:
1.如果没有给variable指定名字,那么默认是Variable、Variable_1。 
2.声明重复名字的variable是允许的,但是系统会按照顺序进行编号name、name_1。 
3.variable是Variable类型。 
4.variable是可训练的。 
5.variable创建并赋值的时候,会构建一个对象存储在内存中。
"""

print("example_3 start ......")
"""
v3 = tf.get_variable(shape=[1], name='get_var3')
print(v3.name)
v3 = tf.get_variable(shape=[1], name='get_var3')
print(v3.name)
"""
# 结果会报错: ValueError: Variable get_var3 already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
with tf.variable_scope('test_reuse') as scope:
    v4 = tf.get_variable(shape=[1], name='get_var4')
    print(v4.name)
    scope.reuse_variables()
    v4 = tf.get_variable(shape=[1], name='get_var4')
    print(v4.name)
with tf.variable_scope('test') as scope:
    v5 = tf.get_variable(shape=[1], name='get_var5')
    print(v5.name)
with tf.variable_scope('test1') as scope:
    v5 = tf.get_variable(shape=[1], name='get_var5')
    print(v5.name)
new_vars = tf.trainable_variables()
for i in new_vars:
    print(i)
print("example_3 end ......")

"""
>> 
test_reuse/get_var4:0
test_reuse/get_var4:0
test/get_var5:0
test1/get_var5:0
<tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'var2:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'var2_1:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'test_reuse/get_var4:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'test/get_var5:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'test1/get_var5:0' shape=(1,) dtype=float32_ref>
_________________________________________________________________________________
总结:
1.用get_variable不可以重复创建相同名字的变量。 
2.使用reuse_variable()可以实现变量重用,但是要放在一个变量域中variable_scope,重用之后还是只有一个可训练变量。 
3.可以将两个重名的变量分别放在两个variable_scope中用get_variable定义。也就是说变量是可以被域限定的,这时就是两个可训练变量了。
"""

name_scope与variable_scope

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time       : 2018/11/13 9:14
@Author     : Li Shanlu
@File       : name_scope_and_variable_scope.py
@Software   : PyCharm
@Description: 解释并验证name_scope和variable_scope的区别
"""
import tensorflow as tf

with tf.name_scope('n_s') as ns:
    v1 = tf.Variable([1], name='var1')
    v2 = tf.Variable([1], name='var1')
    v3 = tf.get_variable(shape=[1], name='var3')
    # v4 = tf.get_variable(shape=[1], name='var3')  # 重复定义var3,会出错,ValueError: Variable var3 already exists, disallowed.
    # ns.reuse_variables()   # 报错,AttributeError: 'str' object has no attribute 'reuse_variables'
    # v4 = tf.get_variable(shape=[1], name='var3')

    with tf.variable_scope('v_s') as vs:
        v5 = tf.Variable([1], name='var1')
        v6 = tf.Variable([1], name='var2')
        v7 = tf.Variable([1], name='var2')
        v8 = tf.get_variable(shape=[1], name='var3')
        # v9 = tf.get_variable(shape=[1], name='var3') # 重复定义var3,会出错,ValueError: Variable v_s/var3 already exists, disallowed.
        vs.reuse_variables()   # 这句必须加上才能重复利用上面这个变量,v8和v9其实是一个变量,占用同一块内存
        v9 = tf.get_variable(shape=[1], name='var3')

print('v1 name: ', v1.name)
print('v2 name: ', v2.name)
print('v3 name: ', v3.name)
# print('v4 name: ', v4.name)
print('v5 name: ', v5.name)
print('v6 name: ', v6.name)
print('v7 name: ', v7.name)
print('v8 name: ', v8.name)
print('v9 name: ', v9.name)
var_set = tf.trainable_variables()
for i in var_set:
    print(i)

"""
>>
v1 name:  n_s/var1:0
v2 name:  n_s/var1_1:0
v3 name:  var3:0
v5 name:  n_s/v_s/var1:0
v6 name:  n_s/v_s/var2:0
v7 name:  n_s/v_s/var2_1:0
v8 name:  v_s/var3:0
v9 name:  v_s/var3:0
<tf.Variable 'n_s/var1:0' shape=(1,) dtype=int32_ref>
<tf.Variable 'n_s/var1_1:0' shape=(1,) dtype=int32_ref>
<tf.Variable 'var3:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'n_s/v_s/var1:0' shape=(1,) dtype=int32_ref>
<tf.Variable 'n_s/v_s/var2:0' shape=(1,) dtype=int32_ref>
<tf.Variable 'n_s/v_s/var2_1:0' shape=(1,) dtype=int32_ref>
<tf.Variable 'v_s/var3:0' shape=(1,) dtype=float32_ref>
_____________________________________________________________________
总结:
1.利用get_variable声明变量时,不受name_scope的影响
2.name_scope只影响用tf.Variable形式声明的变量名
3.不在同一个variable_scope的get_variable是互不影响的
4.在name_scope里面不能用get_variable来重复利用变量,想重复利用变量只能在variable_scope里面用get_variable形式声明变量
"""

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值