tensorflow中name_scope和variable_scope变量的使用
1. variable_scope的使用
首先,使用variable_scope可以很方便的管理get_varibale。
如何確定 get_variable 的 prefixed name?
1.1 variable scope是可以嵌套的:
import tensorflow as tfwith tf.variable_scope("tet1"):var3 = tf.get_variable("var3",shape=[2],dtype=tf.float32)print (var3.name)with tf.variable_scope("tet2"):var4 = tf.get_variable("var4",shape=[2],dtype=tf.float32)print (var4.name)輸出:
tet1/var3:0 tet1/tet2/var4:0可以看到變量嵌套命名,類似于查找文件時一個完整的路徑。
1.2 get_varibale.name 以創建變量的 scope 作為名字的prefix
示例1:
import tensorflow as tfdef te2():with tf.variable_scope("te2"):var2 = tf.get_variable("var2",shape=[2], dtype=tf.float32)print ('var2.name:',var2.name)def te1():with tf.variable_scope("te1"):var1 = tf.get_variable("var1", shape=[2], dtype=tf.float32)return var1return te1() #在scope te2 內調用的res = te2() print ('res.name:',res.name)輸出:
var2.name: te2/var2:0 res.name: te2/te1/var1:0示例2:
import tensorflow as tfdef te2():with tf.variable_scope("te2"):var2 = tf.get_variable("var2",shape=[2], dtype=tf.float32)print ('var2.name:',var2.name)def te1():with tf.variable_scope("te1"):var1 = tf.get_variable("var1", shape=[2], dtype=tf.float32)return var1return te1() #在scope te2 外調用的res = te2() print ('res.name:',res.name)輸出:
var2.name: te2/var2:0 res.name: te1/var1:01.3 tf.variable_scope(“name”) 與 tf.variable_scope(scope)的區別
import tensorflow as tfwith tf.variable_scope("scope"):print (tf.get_variable("w",shape=[1]))#這個變量的name是 scope/wwith tf.variable_scope("scope"):# 這個變量的name是 scope/scope/w,這兩個變量的名字是不一樣的,所以不會產生沖突print( tf.get_variable("w", shape=[1]) )print('........................................')with tf.variable_scope("yin"):print(tf.get_variable("w",shape=[1]))scope = tf.get_variable_scope()#這個變量的name是 yin/wprint('scope:',scope)with tf.variable_scope(scope):#這種方式設置的scope,是用的外部的scopeprint(tf.get_variable("w", shape=[1]))#這個變量的name也是 yin/w,兩個變量的名字一樣,會報錯 <tf.Variable 'scope/w:0' shape=(1,) dtype=float32_ref> <tf.Variable 'scope/scope/w:0' shape=(1,) dtype=float32_ref> ........................................ <tf.Variable 'yin/w:0' shape=(1,) dtype=float32_ref> scope: <tensorflow.python.ops.variable_scope.VariableScope object at 0x12060cd68> Traceback (most recent call last):File "<ipython-input-1-cb0c968e524c>", line 1, in <module>runfile('/Users/lilong/Desktop/tensorflow_test/tensorflow_8/variable_use.py', wdir='/Users/lilong/Desktop/tensorflow_test/tensorflow_8')........ValueError: Variable yin/w already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:...1.4 共享變量
共享變量的前提是,變量的名字是一樣的,變量的名字是由變量名和其scope前綴一起構成, tf.get_variable_scope().reuse_variables() 是允許共享當前scope下的所有變量。reused_variables可以看同一個節點。
tf.get_variable_scope() :獲取當前scope
tf.get_variable_scope().reuse_variables():共享變量
輸出:
<tf.Variable 'level1/w:0' shape=(1,) dtype=float32_ref> scope: <tensorflow.python.ops.variable_scope.VariableScope object at 0x12b470b00> <tf.Variable 'level1/level2/w:0' shape=(1,) dtype=float32_ref> ................... <tf.Variable 'level1/w:0' shape=(1,) dtype=float32_ref> scope: <tensorflow.python.ops.variable_scope.VariableScope object at 0x12b470940> <tf.Variable 'level1/level2/w:0' shape=(1,) dtype=float32_ref>1.5 tf.variable_scope有時也會處理命名沖突
import tensorflow as tfdef test(name=None):# 可以看出,如果只是使用default_name這個屬性來創建variable_scope的時候,會處理命名沖突with tf.variable_scope(name, default_name="scope") as scope:w = tf.get_variable("w", shape=[2, 10])test() test() ws = tf.trainable_variables() for w in ws:print(w.name)輸出:
scope/w:0 scope_1/w:02 name_scope的使用
2.1 在tensorflow中,有兩個scope, 一個是name_scope一個是variable_scope,下面通過簡單的示例說明:
示例1:
import tensorflow as tfwith tf.name_scope("hello") as name_scope:arr1 = tf.get_variable("arr1", shape=[2,10],dtype=tf.float32)print ('h1:',name_scope)print ('h2:',arr1.name)print ("scope_name:%s " % tf.get_variable_scope().original_name_scope)運行:
h1: hello/ h2: arr1:0 scope_name:示例2:
import tensorflow as tf;with tf.variable_scope("hello") as variable_scope:arr1 = tf.get_variable("arr1", shape=[2, 10], dtype=tf.float32)print ('p1:',variable_scope) # 輸出變量命令空間對象print ('p2:',variable_scope.name) # 打印出變量空間名字print ('p3:',arr1.name) # 輸出變量名:hello/arr1:0print ('p4:',tf.get_variable_scope().original_name_scope) # 獲得原始命名空間print ('p6:',tf.get_variable_scope()) # 獲取的就是variable_scope對象with tf.variable_scope("xixi") as v_scope2:print ('p7:',tf.get_variable_scope().original_name_scope)print ('p8:',tf.get_variable_scope()) # 獲取的就是v _scope2,這是另一個命名空間輸出:
p1: <tensorflow.python.ops.variable_scope.VariableScope object at 0x12ccd4b38> p2: hello p3: hello/arr1:0 p4: hello/ p6: <tensorflow.python.ops.variable_scope.VariableScope object at 0x12ccd4b38> p7: hello/xixi/ p8: <tensorflow.python.ops.variable_scope.VariableScope object at 0x12ccd4dd8>示例3:
import tensorflow as tfwith tf.name_scope("name1"):with tf.variable_scope("var1"):w = tf.get_variable("w",shape=[2])res = tf.add(w,[3])print (w.name) print (res.name)輸出:
var1/w:0 name1/var1/Add:0variable_scope和name_scope都會給op的name加上前綴
這實際上是因為 創建 variable_scope 時內部會創建一個同名的 name_scope
對比三個個程序可以看出:
- name_scope對 get_variable()創建的變量 的名字不會有任何影響,而創建的op會被加上前綴.
- tf.get_variable_scope() 返回的只是 variable_scope,不管 name_scope. 所以以后在使用tf.get_variable_scope().reuse_variables() 時可以不用管name_scope
2.2 name_scope作用
name_scope 是給op_name加前綴, variable_scope是給get_variable()創建的變量的名字加前綴
典型的 TensorFlow 可以有數以千計的節點,如此多而難以一下全部看到,甚至無法使用標準圖表工具來展示。為簡單起見,我們為op/tensor名劃定范圍,并且可視化把該信息用于在圖表中的節點上定義一個層級,默認情況下, 只有頂層節點會顯示。
下面這個例子使用tf.name_scope在hidden命名域下定義了三個操作:
輸出:
hidden/alpha hidden/weights hidden/biases2.3 tf.name_scope(None) 有清除name scope的作用
import tensorflow as tf with tf.name_scope("hehe"):w1 = tf.Variable(1.0)with tf.name_scope(None):w2 = tf.Variable(2.0) print(w1.name) print(w2.name)#hehe/Variable:0 #Variable:0小小總結:
1. 使用tf.Variable()的時候,tf.name_scope()和tf.variable_scope() 都會給 Variable 和 op 的 name屬性加上前綴。
2. 使用tf.get_variable()的時候,tf.name_scope()就不會給 tf.get_variable()創建出來的Variable加前綴。但是 tf.Variable() 創建出來的就會受到 name_scope 的影響
參考:
https://blog.csdn.net/u012436149/article/details/53018924/
https://blog.csdn.net/u012436149/article/details/53081454
總結
以上是生活随笔為你收集整理的tensorflow中name_scope和variable_scope变量的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 爱购平台是什么公司
- 下一篇: 即分期超g会员上征信吗