tf.name_scope&tf.variable scope

轉載自:http://blog.csdn.net/u012436149/article/details/53081454

在tensorflow中,有兩個scope, 一個是name_scope一個是variable_scope,這兩個scope到底有什麼區別呢?
先看第一個程序:

with tf.name_scope("hello") as name_scope:
    arr1 = tf.get_variable("arr1", shape=[2,10],dtype=tf.float32)

    print name_scope
    print arr1.name
    print "scope_name:%s " % tf.get_variable_scope().original_name_scope

輸出爲:
hello/
arr1:0
scope_name:

可以看出:

  • tf.name_scope() 返回的是 一個string,”hello/”
  • 在name_scope中定義的variable的name並沒有 “hello/”前綴
  • tf.get_variable_scope()的original_name_scope 是空

第二個程序:

with tf.variable_scope("hello") as variable_scope:
    arr1 = tf.get_variable("arr1", shape=[2, 10], dtype=tf.float32)

    print variable_scope
    print variable_scope.name #打印出變量空間名字
    print arr1.name
    print tf.get_variable_scope().original_name_scope
    #tf.get_variable_scope() 獲取的就是variable_scope

    with tf.variable_scope("xixi") as v_scope2:
        print tf.get_variable_scope().original_name_scope
        #tf.get_variable_scope() 獲取的就是v _scope2

輸出爲:

<tensorflow.python.ops.variable_scope.VariableScope object at 0x7fbc09959210>
hello
hello/arr1:0
hello/
hello/xixi/

可以看出:

  • tf.variable_scope() 返回的是一個 op對象
  • variable_scope中定義的variable 的name加上了”hello/”前綴
  • tf.get_variable_scope()的original_name_scope 是 嵌套後的scope name

第三個程序:

with 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:0

可以看出:variable scopename scope都會給opname加上前綴

對比三個個程序可以看出:

  • name_scope對 get_variable()創建的變量 的名字不會有任何影響,而創建的op會被加上前綴.
  • tf.get_variable_scope() 返回的只是 variable_scope,不管 name_scope.所以以後我們在使用tf.get_variable_scope().reuse_variables() 時可以無視name_scope

其它

with tf.name_scope("scope1") as scope1:
    with tf.name_scope("scope2") as scope2:
        print scope2
#輸出:scope1/scope2/

import tensorflow as tf
with tf.variable_scope("scope1") as scope1:
    with tf.variable_scope("scope2") as scope2:
        print scope2.name
#輸出:scope1/scope2

name_scope可以用來幹什麼

典型的 TensorFlow 可以有數以千計的節點,如此多而難以一下全部看到,甚至無法使用標準圖表工具來展示。爲簡單起見,我們爲op/tensor名劃定範圍,並且可視化把該信息用於在圖表中的節點上定義一個層級。默認情況下, 只有頂層節點會顯示。下面這個例子使用tf.name_scope在hidden命名域下定義了三個操作:

import tensorflow as tf

with tf.name_scope('hidden') as scope:
  a = tf.constant(5, name='alpha')
  W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0), name='weights')
  b = tf.Variable(tf.zeros([1]), name='biases')
  print a.name
  print W.name
  print b.name

結果是得到了下面三個操作名:

hidden/alpha
hidden/weights
hidden/biases
name_scope 是給op_name加前綴, variable_scope是給get_variable()創建的變量的名字加前綴。

tf.variable_scope有時也會處理命名衝突

import tensorflow as tf
def test(name=None):
    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:0
#可以看出,如果只是使用default_name這個屬性來創建variable_scope
#的時候,會處理命名衝突

其它

  • 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

variable_scope可以用來幹什麼

variable_scope 用來管理 variable 詳見variable_scope

總結

簡單來看
1. 使用tf.Variable()的時候,tf.name_scope()tf.variable_scope() 都會給 Variableopname屬性加上前綴。
2. 使用tf.get_variable()的時候,tf.name_scope()就不會給 tf.get_variable()創建出來的Variable加前綴。

發佈了63 篇原創文章 · 獲贊 22 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章