tf.name_scope()和tf.variable_scope()

來源:互聯網
上載者:User

標籤:為什麼   float   連結   命名   ons   art   提取   直接   舉例   

tf.name_scope()和tf.variable_scope()是兩個範圍,一般與兩個建立/調用變數的函數tf.variable() 和tf.get_variable()搭配使用。

tf.name_scope和 variable_scope也是個作為上下文管理器的角色,下文管理器:意思就是,在這個管理器下做的事情,會被這個管理器管著。

一.name_scope 和 variable_scope的用途:
name_scope 和 variable_scope 主要是因為 變數共用 的需求。

變數共用主要涉及兩個函數:tf.variable() 和tf.get_variable();即就是必須要在tf.variable_scope的範圍下使用tf.get_variable()函數。這裡用tf.get_variable( ) 而不用tf.Variable( ),是因為前者擁有一個變數檢查機制,會檢測已經存在的變數是否設定為共用變數,如果已經存在的變數沒有設定為共用變數,TensorFlow 運行到第二個擁有相同名字的變數的時候,就會報錯。

兩個建立變數的方式。如果使用tf.Variable() 的話每次都會建立變數。但是大多數時候我們是希望重用一些變數,所以就用到了get_variable(),它會去搜尋變數名,有就直接用,沒有再建立。名字域。既然用到變數名了,就涉及到了名字域的概念。通過不同的域來區別變數名,畢竟讓我們給所有變數都取不同名字還是很辛苦。這就是為什麼會有scope 的概念。name_scope 作用於操作,variable_scope 可以通過設定reuse 標誌以及初始化方式來影響域下的變數,因為想要達到變數共用的效果, 就要在 tf.variable_scope()的範圍下使用 tf.get_variable() 這種方式產生和提取變數. 不像 tf.Variable() 每次都會產生新的變數, tf.get_variable() 如果遇到了已經存在名字的變數時, 它會單純的提取這個同樣名字的變數,如果不存在名字的變數再建立.

舉例:

with tf.variable_scope(‘V1‘,reuse=True):      a1 = tf.get_variable(name=‘a1‘, shape=[1], initializer=tf.constant_initializer(1))      a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name=‘a2‘)  with tf.variable_scope(‘V2‘,reuse=True):      a3 = tf.get_variable(name=‘a1‘, shape=[1],initializer=tf.constant_initializer(1))      a4 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name=‘a2‘)      with tf.Session() as sess:      sess.run(tf.initialize_all_variables())      print (a1.name)      print (a2.name)       print (a3.name)       print (a4.name) 

輸出:

V1/a1:0V1_14/a2:0V2/a1:0V2_2/a2:0  在tf.name_scope()中則沒有resuse這個參數,無法實現這種操作。

二.TensorFlow中name scope和variable scope區別
TF中有兩種範圍類型
命名域 (name scope),通過tf.name_scope 或 tf.op_scope建立;
變數域 (variable scope),通過tf.variable_scope 或 tf.variable_op_scope建立;
這兩種範圍,對於使用tf.Variable()方式建立的變數,具有相同的效果,都會在變數名稱前面,加上網域名稱稱。

對於通過tf.get_variable()方式建立的變數,只有variable scope名稱會加到變數名稱前面,而name scope不會作為首碼。例如 print(v1.name) # var1:0

例子:

with tf.name_scope("my_name_scope"):    v1 = tf.get_variable("var1", [1], dtype=tf.float32)     v2 = tf.Variable(1, name="var2", dtype=tf.float32)    a = tf.add(v1, v2)    print(v1.name)    print(v2.name)     print(a.name)

輸出:

var1:0my_name_scope/var2:0my_name_scope/Add:0

小結:name_scope不會作為tf.get_variable變數的首碼,但是會作為tf.Variable的首碼。

with tf.variable_scope("my_variable_scope"):    v1 = tf.get_variable("var1", [1], dtype=tf.float32)    v2 = tf.Variable(1, name="var2", dtype=tf.float32)    a = tf.add(v1, v2)    print(v1.name)     print(v2.name)    print(a.name) 

輸出:

my_variable_scope/var1:0my_variable_scope/var2:0

my_variable_scope/Add:0

小結:在variable_scope的範圍下,tf.get_variable()和tf.Variable()都加了scope_name首碼。因此,在tf.variable_scope的範圍下,通過get_variable()可以使用已經建立的變數,實現了變數的共用。

原文連結:80099822

tf.name_scope()和tf.variable_scope() (轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.