2016-09-15 73 views
0

我收到以下錯誤:不兼容形狀與Tensorflow佔位符

ValueError: Cannot feed value of shape (2, 2) for Tensor u'Placeholder_1:0', which has shape '(2,)'

在下面一行:

nn_index = sess.run(pred, feed_dict={xtr: training_input, xte: test_input[index, :]})

訓練輸入數據是形狀(24, 2)和測試輸入的數據是(300, 2)

雖然placeholde喂入數據的rs被初始化爲

xtr = tf.placeholder("float", [None, 2]) 
xte = tf.placeholder("float", [2]) 

# Nearest Neighbor calculation using L1 Distance 
def metric_single(training, test): 
    distance = tf.sqrt(tf.reduce_sum(tf.square(tf.sub(training, test)), 
    reduction_indices=1, keep_dims=True)) 

    return distance 

# Prediction: Get min distance index (Nearest neighbor) 
pred = tf.arg_min(metric_single(xtr, xte), 0) 

無法弄清楚在我的代碼中改變什麼來解決這個問題。

~~~~編輯~~~~

test_input.shape 
>>>(300, 2) 

*Updated* 
test_input[index, :].shape 
>>>(2,) 

training_input.shape 
>>>(24, 2) 

*Updated* 
index 
>>>index: 0 

~~~~~ FULL ML SOURCE ~~~~~

# Nearest Neighbor calculation using L1 Distance 
def metric_single(training, test): 
    distance = tf.sqrt(tf.reduce_sum(tf.square(tf.sub(training, test)), 
    reduction_indices=1, keep_dims=True)) 

    return distance 


xtr = tf.placeholder("float", [None, 2]) 
xte = tf.placeholder("float", [None, 2]) 

# Prediction: Get min distance index (Nearest neighbor) 
pred = tf.arg_min(metric_single(xtr, xte), 0) 

accuracy = 0 

# Initializing the variables 
init = tf.initialize_all_variables() 

def calculate_knn(training_input, training_output, test_input, test_output, k, index): 
    print 'training_input' 
    print training_input 
    print 'test_input' 
    print test_input 
    for j in range(k): 
    print 'training_input.shape' 
    print training_input.shape 
    print 'test_input[index, :].shape' 
    print test_input[index, :].shape 
    print 'index: ' + str(index) 

    nn_index = sess.run(pred, feed_dict={xtr: training_input, xte: test_input[index, :]}) 

    print 'knn #: ' + str(j+1) 
    print 'nn_index: ' + str(nn_index) 
    # Get nearest neighbor class label and compare it to its true label 
    print("Test", \ 
     "Sample:", test_input[i], \ 
     "Nearest Neightbor:", training_input[nn_index], \ 
    i, "Prediction:", np.argmax(training_output[nn_index]), \ 
     "True Class:", np.argmax(test_output[i])) 

    ## Remove nearest neighbor from test data to 
    ## find (k-1)nn 
    # training_input = tf.slice(training_input, [nn_index, 0], [-1, -1]) 
    training_input = np.delete(training_input, nn_index, 0) 

# Launch the graph 
with tf.Session() as sess: 
    sess.run(init) 
    Tr = TrainingData() 
    Te = TestData() 

    ## TODO: process test data in batches 

    # loop over test data 
    test_examples = Te.get_Xte() 
    for i in test_examples: 
     print 'in test data loop' 
     # Get nearest neighbor={xtr: Xtr, xte: Xte[i, :]}) 
     print 'Tr.get_Xtr()' 
     print Tr.get_Xtr() 

     print 'Te.get_Xte()' 
     print Te.get_Xte() 

     calculate_knn(Tr.get_Xtr(), Tr.get_Ytr(), Te.get_Xte(), Te.get_Yte(), 2, i) 


     #Calculate accuracy 
     if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]): 
      accuracy += 1./len(Xte) 

    print("Done!") 
    print("Accuracy:", accuracy) 
+0

你能告訴我們'打印test_input.shape的輸出; print test_input [index,:]。shape'? – mrry

+0

我將這些打印添加到上面的原始帖子,謝謝 – redress

+0

你能打印'index'嗎?我認爲這是一個簡單的整數,但這並不能解釋'test_input [index,:]的形狀' – mrry

回答

1

這個問題似乎是index是一個列表而不是一個Python整數,在這一行:

nn_index = sess.run(pred, feed_dict={xtr: training_input, xte: test_input[index, :]}) 

如果你有4×4大小的numpy的陣列,像下面,你的索引表達式將有以下行爲:

data = np.arange(0, 16).reshape(4, 4) # ==> [[ 0, 1, 2, 3], 
               [ 4, 5, 6, 7], 
               [ 8, 9, 10, 11], 
               [12, 13, 14, 15]] 

# 1. index is an int 
print data[0, :]  # ==> [0, 1, 2, 3] (4-element vector) 

# 2. index is a list of one int 
print data[[0], :]  # ==> [[0, 1, 2, 3]] (1x4 matrix) 

# 3. index is a list of two ints 
print data[[0, 0], :] # ==> [[0, 1, 2, 3], [0, 1, 2, 3]] (2x4 matrix) 

因爲當你打印index你得到的結果[0 0],它看起來像你的情況下,3這裏。不知道什麼index意味着什麼,我懷疑你可能想改變你的feed_dictindex轉換爲int,例如:

nn_index = sess.run(pred, feed_dict={xtr: training_input, xte: test_input[index[0], :]})