2016-06-20 41 views
7

我基本上想要將選項輸入到圖形中間並計算從那裏輸出。我的一個想法是使用默認爲零張量的tf.placeholder_with_default。然後我可以使用加法混合可選輸入,但是在大的形狀上添加這似乎是很多不必要的計算。有沒有更好的方法來完成?如何將可選輸入添加到TensorFlow中的圖形中?

input_enabled = tf.placeholder_with_default(tf.constant(1.), [1]) 

input_shape = [None, in_size] 
input = tf.placeholder_with_default(tf.zeros(input_shape), input_shape) 
// ... 
bottleneck_shape = [None, bottleneck_size] 
bottleneck = input_enabled * f(prev_layer) + tf.placeholder_with_default(tf.zeros(bottleneck_shape), bottleneck_shape) 
// ... 

// Using graph with input at first layer: 
sess.run([output], feed_dict={input: x}) 

// Using graph with input at bottleneck layer: 
sess.run([output], feed_dict={bottleneck: b, input_enabled: 0.}) 
+0

你能給出一個更具體的問題概述嗎?你想要什麼類型的可選輸入,並做什麼? –

+0

A有一個類似autoencoder的圖形,我想用一個用於訓練的圖形重建代碼作爲瓶頸輸入。 –

+0

你可以給你現在使用'tf.placeholder_with_default'的部分代碼來改變嗎? –

回答

10

由於您的代碼,我理解得更好。

基本架構是:

 input  <- you can feed here 
     |   
    (encoder) 
     | 
    bottleneck <- you can also feed here instead 
     | 
    (decoder) 
     | 
     output 

你想兩個用例:

  1. 火車:喂圖像爲input,計算輸出
  2. 測試 :將代碼饋入瓶頸,計算輸出

你並不需要創建bottleneck一個佔位符,因爲sess.run()讓你值喂到圖形非佔位符

input_shape = [None, in_size] 
input = tf.placeholder(tf.float32, input_shape) 
# ... 

bottleneck = f(prev_layer) # of shape [None, bottleneck_size] 
# ... 

# Using graph with input at first layer: 
sess.run([output], feed_dict={input: x}) 

# Using graph with input at bottleneck layer: 
sess.run([output], feed_dict={bottleneck: b}) 

從文檔sess.run()

可選的feed_dict參數允許調用方覆蓋圖中張量的值。 feed_dict中的每個鍵可以是以下類型之一:

如果鍵是一個張量,則該值可以是可以轉換爲與該張量相同的dtype的Python標量,字符串,列表或numpy ndarray。此外,如果鍵是佔位符,則將檢查值的形狀是否與佔位符兼容。

相關問題