Reputation: 1439
I am getting the following error:
ValueError: Cannot feed value of shape (2, 2) for Tensor u'Placeholder_1:0', which has shape '(2,)'
At the following line:
nn_index = sess.run(pred, feed_dict={xtr: training_input, xte: test_input[index, :]})
The training input data is of the shape (24, 2)
and test input data is of (300, 2)
While the placeholders which feed the data are initialized as
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)
Cant figure out what to change in my code to solve this.
~~~~ EDIT ~~~~
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)
Upvotes: 0
Views: 437
Reputation: 126154
The problem seems to be that index
is a list and not a Python integer, in this line:
nn_index = sess.run(pred, feed_dict={xtr: training_input, xte: test_input[index, :]})
If you have a numpy array of size 4x4, like follows, your indexing expression will have the following behavior:
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)
Since when you printed index
you got the result [0 0]
, it looks like you are in case 3 here. Without knowing anything about what index
means, I suspect you might want to change your feed_dict
to convert index
to an int, for example:
nn_index = sess.run(pred, feed_dict={xtr: training_input, xte: test_input[index[0], :]})
Upvotes: 1