我收到以下錯誤:不兼容形狀與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)
你能告訴我們'打印test_input.shape的輸出; print test_input [index,:]。shape'? – mrry
我將這些打印添加到上面的原始帖子,謝謝 – redress
你能打印'index'嗎?我認爲這是一個簡單的整數,但這並不能解釋'test_input [index,:]的形狀' – mrry