2017-08-27 91 views
0

我想通過使用get_slot_names獲得張量流中Momentum優化器的插槽名稱,因爲它在tensorflow網頁中解釋爲here。我使用下面一行在我的代碼,讓他們:獲取tensorflow中動量優化器的插槽名稱

slots=tf.train.MomentumOptimizer(learning_rate=1e-3, momentum=0.9,).get_slot_names() 

我跑我的圖,然後當我打印插槽還給我只是一個空列表。任何幫助將不勝感激。

順便說一句,我的網絡在減少損失或其他方面工作正常。 另外,我嘗試了與其他優化器,但它有同樣的問題。

我使用TF 1.3在Ubuntu 14 感謝,

回答

0

Slot_names是優化具體。如果您想獲得每個可訓練變量的插槽,您可以使用get_slot方法並使用正確的slot_name。爲momentum_optimizer創建的插槽名稱(默認情況下)爲momentum。以下是一個簡單的例子來說明這些觀點。

x_input = np.linspace(0, 30, 200) 
y_input = 4 * x_input + 6 
W = tf.Variable(0.0, name="weight") 
b = tf.Variable(0.0, name="bias") 
X = tf.placeholder(tf.float32, name='InputX') 
Y = tf.placeholder(tf.float32, name='InputY') 
Y_pred = X * W + b 

loss = tf.reduce_mean(tf.square(Y_pred - Y)) 

# define the optimizer 
optimizer = tf.train.MomentumOptimizer(learning_rate=0.001, momentum=.9) 

# training op 
train_op = optimizer.minimize(loss) 

# print the optimizer slot name 
print(optimizer.get_slot_names()) 

# Results:['momentum'] 

# print out slot created for each trainable variables using the slot_name from above result 
for v in tf.trainable_variables(): 
    print(optimizer.get_slot(v, 'momentum')) 

# Results: <tf.Variable 'weight/Momentum:0' shape=() dtype=float32_ref> 
      <tf.Variable 'bias/Momentum:0' shape=() dtype=float32_ref> 
+0

感謝您的回覆。那麼,你的意思是它只能恢復勢頭嗎?由於它在[https://www.tensorflow.org/versions/r1.3/api_docs/python/tf/train/MomentumOptimizer]中提到,我認爲它也可以在勢頭公式中返回'累積'變量。 –

+0

這只是TF中不連貫命名的一個例子。 「動量」槽參數實際上是從公式中「積累」的,而來自公式的「動量」僅僅是一個浮點數,超參數,存儲在opt._momentum中。 – lejlot

+0

@AliAsgharMortazi正如我在這個答案中提到的,你可以使用'get_slot'來獲得累積的變量和值。看我的例子。 –

0

在代碼中的唯一問題是,slot_variables期間創建最小化呼叫(actually in apply_gradients)。由於您在之前調用get_slot_variables()- 它們是空的。

所以不是

slots=tf.train.MomentumOptimizer(learning_rate=1e-3, momentum=0.9,).get_slot_names() 

你應該做的

opt = tf.train.MomentumOptimizer(learning_rate=1e-3, momentum=0.9,) 

train_op = opt.minimize(your_loss) # or anything else 

slota = opt.get_slot_names() # this will work 

其原因非常簡單 - 很多插槽特定變量的方法,例如像亞當將創建每一個插槽每個優化變量,並在調用之前.minimize - 優化器不知道它將優化哪些變量。

特別是,對於MomentumOptimizer,您保留累積梯度每個變量。因此在調用最小化之前無法計算它們。這些累積的梯度被存儲在「動量」槽中(對於名稱來說是相當不好的選擇,但是它們在TF中)。

+0

它工作,但它似乎很奇怪,爲什麼這樣的事情應該完成!無論如何,謝謝,我不知道你爲什麼得到反對票。另外,你知道是否(或如何)可以改變傳遞給最小值的「累積」值?假設我想添加一個數字(alpha)的積累,然後更新參數 –

+0

那麼這是TF人們必須做出的許多硬設計決策之一。在修改優化器方面 - 如果沒有自己的優化器,就無法實現這一點。另一方面 - 這應該不那麼難,只需複製MomentumOptimizer並調整其apply_gradients例程以使用附加信號。這同樣來自TF假定圖僅附加 - 你不能修改受約束的圖,因爲apply_gradients已經創建了整個更新的圖,所以你必須修改它的邏輯來改變它。 – lejlot

+0

我希望有一個更簡單的解決方案!不管怎樣,謝謝。 –