【tensorflow 學習】 name_scope和variable_scope的區別
在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) arr2 = tf.Variable(1, name="arr2", dtype=tf.float32) print(name_scope) #hello/ print(arr1.name) #arr1:0 print(arr2.name) #hello/arr2:0 print("scope_name:%s " % tf.get_variable_scope().original_name_scope) #空 #輸出 hello/ arr1:0 hello/arr2:0 scope_name:
可以看出:
- tf.name_scope() 返回的是 一個string,”hello/”
- 在name_scope使用
get_variable()
中定義的 variable 的 name 並沒有字首 - 在name_scope使用
variable()
定義的variable的名字是有 “hello/”字首的 - tf.get_variable_scope().original_name_scope 是空
第二個程式:
import tensorflow as tf with tf.variable_scope("hello") as variable_scope: arr1 = tf.get_variable("arr1", shape=[2, 10], dtype=tf.float32) arr2 = tf.Variable(1, name="arr2", dtype=tf.float32) print(variable_scope)#<tensorflow.python.ops.variable_scope.VariableScope object at 0x7fbc09959210> print(variable_scope.name) #打印出變數空間名字 #hello print(arr1.name) #hello/arr1:0 print(arr2.name) #hello/arr2:0 print(tf.get_variable_scope().original_name_scope) #hello/ #tf.get_variable_scope() 獲取的就是variable_scope with tf.variable_scope("xixi") as v_scope2: print(tf.get_variable_scope().original_name_scope) #hello/xixi/ #輸出 <tensorflow.python.ops.variable_scope.VariableScope object at 0x0000020B2AAF9390> hello hello/arr1:0 hello/arr2:0 hello/ hello/xixi/
可以看出:
- tf.variable_scope() 返回的是一個
VariableScope
物件 - variable_scope使用
get_variable
定義的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_scope
和name_scope
都會給op
的name
加上字首- 這實際上是因為建立
variable_scope
時內部會建立一個同名的name_scope
對比三個個程式可以看出:
name_scope
返回的是 string, 而variable_scope
返回的是物件. 這也可以感覺到,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
其他:
with tf.name_scope("scope1") as scope1:
with tf.name_scope("scope2") as scope2:
print(scope2)
#輸出:
scope1/scope2/ 最後有斜槓
with tf.variable_scope("scope1") as scope1:
with tf.variable_scope("scope2") as scope2:
print(scope2)
print(scope2.name)
#輸出:
<tensorflow.python.ops.variable_scope.VariableScope object at 0x00000194F95C81D0>
scope1/scope2
tf.name_scope()可以用來幹什麼
對
tf.get_variable
不起作用,只對tf.Variable
起作用
典型的 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) #hidden/alpha
print(W.name) #hidden/weights
print(b.name) #hidden/biases
#輸出
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
tf.variable_scope()可以用來幹什麼
對
tf.get_variable
和tf.Variable
都起作用
variable_scope 用來管理 variable 詳見variable_scope
with tf.variable_scope("my_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) # my_scope/var1:0
print(v2.name) # my_scope/var2:0
print(a.name) # my_scope/Add:0
這種機制允許在不用的name_scope中使用tf.get_variable
來share變數,但是需要注意的是,一定要宣告reuse:
with tf.name_scope("foo"):
with tf.variable_scope("var_scope"):
v = tf.get_variable("var", [1])
with tf.name_scope("bar"):
with tf.variable_scope("var_scope", reuse=True):
v1 = tf.get_variable("var", [1])
assert v1 == v
print(v.name) # var_scope/var:0
print(v1.name) # var_scope/var:0
輸出:
var_scope/var:0
var_scope/var: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
的影響.
相關推薦
【tensorflow 學習】 name_scope和variable_scope的區別
在tensorflow中,有兩個scope, 一個是name_scope一個是variable_scope,這兩個scope到底有什麼區別呢? 三個例子 先看第一個程式: with tf.name_scope("hello") as name_scope: a
【tensorflow 學習】tf.get_variable()和tf.Variable()的區別
1. tf.Variable() W = tf.Variable(<initial-value>, name=<optional-name>) 用於生成一個初始值為initial-value的變數。必須指定初始化值 2.tf.get_variab
【tensorflow 學習】tf.split()和tf.squeeze()
split( value, num_or_size_splits, axis=0, num=None, name='split' ) 輸入: value: 輸入的tensor num_or_size_splits:
【Tensorflow學習】 RNN
cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units) init_state = cell.zero_state(batch_size, dtype=tf.float32) outputs, final_state =
TensorFlow中的name_scope和variable_scope
1、tf.Variable() 和 tf.get_variable() (1)tf.Variable()會自動檢測命名衝突並自行處理。 import tensorflow as tf sess = tf.Session() var1 = tf.Variable(
【tensorflow 學習】 gpu使用
由於tensorflow預設搶佔伺服器所有GPU視訊記憶體,只允許一個小記憶體的程式也會佔用所有GPU資源。下面提出使用GPU執行tensorflow的幾點建議: 1.在執行之前先檢視GPU的使用情況: $ nvidia-smi # 檢視GPU此時的使用情況 或者 $
【unix學習】程序和檔案備份壓縮打包
程序 nice value -20 – 19之間 表示程序的優先順序,nice值越小,代表優先順序越高,執行越早。 當設定優先順序的nice值時,最低只能是0,當小於0時報錯了。 [s14516@gdufs]$nice -n -20 fi
【C++學習】變數和儲存區
儲存區一般分為以下五種: 棧區: 由編譯器在需要的時候進行分配,不需要的時候會自動清空,棧區一般比較小,對於較大的陣列不應該放入棧區。 堆區: 由new分配的空間,他們的釋放由程式中的語句進行操
【tensorflow 學習】seq2seq模型程式碼解讀
1. sequence-to-sequence模型 官方教程使用seq2seq模型實現了英語-法語的翻譯系統。經典的sequence-to-sequence模型由兩個RNN網路構成,一個被稱為“encoder”,另一個則稱為“decoder”,前者負責
【C#學習】繼承和多型
建構函式和解構函式的呼叫順序 建構函式和解構函式的呼叫順序相反,建構函式從基類到派生類依次呼叫,解構函式反過來。 抽象類和抽象方法 抽象方法必須包含在抽象類中,也就是說,一旦一個類包含抽象方法,就應該被宣告為抽象類。 抽象類用 abstract 修飾,無法用new 來例項化。但可以用抽象類的引用
【強化學習】入門和資料
去年的alpha go到 alpha go zero 在到Alpha Zero, deeepmaid真的是不斷在刷分,追趕不及。核心還是深度學習+強化學習。感覺深度學習的發展已經逐漸進入冷卻期。N
【tensorflow 學習】給LSTM加上L2正則化
首先,用tf.trainable_variables()得到所有weights和bias, 然後,用tf.nn.l2_loss()計算L2 norm, 求和之後作為正則項加給原來的cost function tv = tf.trainable_vari
【TensorFlow學習筆記】5:variable_scope和name_scope,圖的基本操作
學習《深度學習之TensorFlow》時的一些實踐。 variable_scope 一般的巢狀 上節有學到在巢狀scope中的變數,一般是: import tensorflow as tf # with tf.variable_scope("scopeA") as
【機器學習】簡單理解精確度(precision)和準確率(accuracy)的區別
不少人對分類指標中的Precision和Accuracy區分不開,在其他部落格中也有很多相關介紹,但總體不夠簡明易懂。 筆者在查閱了若干資料後,總結如下: Precis
【TensorFlow學習筆記】3:認識TensorBoard視覺化計算圖和計算結點
學習《深度學習之TensorFlow》時的一些實踐。 TensorBoard是一個日誌展示系統,在Session中使用tf.summary中的API將日誌儲存在日誌檔案中,然後通過TensorBoard服務在瀏覽器中就可以讀取這些日誌,檢視圖形化後的資訊。 對線性迴歸做視覺
【TensorFlow學習筆記】2:基本使用流程和使用檢查點,按照時間自動管理檢查點
學習《深度學習之TensorFlow》時的一些實踐。 TF的基本使用 對於分類問題的特徵X和標籤Y,分別定義tf.placeholder,這是計算圖輸入資料的入口。 對於模型中的引數(注意不是超引數),如往往是權向量w和偏置b,定義tf.Variable,並傳入初始的值
【機器學習】MAP最大後驗估計和ML最大似然估計區別
A maximum a posteriori probability (MAP) estimate is an estimate of an unknown quantity, that equals the mode of the posterior distribution. The MAP can
【機器學習】Tensorflow:理解和實現快速風格化影象fast neural style
Neural Style開闢了計算機與藝術的道路,可以將照片風格化為名家大師的畫風。然而這種方法即使使用GPU也要花上幾十分鐘。Fast Neural Style則啟用另外一種思路來快速構建風格化影象,在筆記本CPU上十幾秒就可以風格化一張圖片。我們來看看這是什
【Tensorflow keras】Keras:基於Theano和TensorFlow的深度學習庫
catalogue 1. 引言 2. 一些基本概念 3. Sequential模型 4. 泛型模型 5. 常用層 6. 卷積層 7. 池化層 8. 遞迴層Recurrent 9. 嵌入層 Embedding 1. 引言 Keras是一
【Java架構學習】MVC和三層架構的區別
其實這篇部落格的重點不是介紹三層架構,是重點介紹MVC並幫助理解MVC。學了這麼久MVC發現對它的理解還存在很多誤區,今天就來好好整理一下MVC。 MVC即Model-View-Co