1234567891011121314151617181920212223242526272829303132333435363738394041 |
- '''
- Nearest Neighbor classification on MNIST with TensorFlow
- '''
- import numpy as np
- import tensorflow as tf
- # Import MINST data
- import input_data
- mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
- #In this example, we limit mnist data
- Xtr, Ytr = mnist.train.next_batch(5000) #5000 for training (nn candidates)
- Xte, Yte = mnist.test.next_batch(200) #200 for testing
- Xtr = np.reshape(Xtr, newshape=(-1, 28*28))
- Xte = np.reshape(Xte, newshape=(-1, 28*28))
- xtr = tf.placeholder("float", [None, 784])
- xte = tf.placeholder("float", [784])
- nn = tf.Variable(tf.zeros([10]))
- #Calculation of L1 Distance
- distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.neg(xte))), reduction_indices=1)
- #Predict: Get min distance index (Nearest neighbor)
- pred = tf.arg_min(distance, 0)
- accuracy = 0.
- init = tf.initialize_all_variables()
- with tf.Session() as sess:
- sess.run(init)
- for i in range(len(Xte)):
- nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i,:]})
- #Get nn class label and compare it to its true label
- print "Test", i, "Prediction:", np.argmax(Ytr[nn_index]), "True Class:", np.argmax(Yte[i])
- if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):
- accuracy += 1./len(Xte)
- print "Done!"
- print "Accuracy:", accuracy
|