Credits: Forked from TensorFlow-Examples by Aymeric Damien
Refer to the setup instructions
Note: We dont need the seperate input_data.py file as tensorflow.examples has all that built in, which is neat :)
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
Extracting /tmp/data/train-images-idx3-ubyte.gz Extracting /tmp/data/train-labels-idx1-ubyte.gz Extracting /tmp/data/t10k-images-idx3-ubyte.gz Extracting /tmp/data/t10k-labels-idx1-ubyte.gz
# 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
# Reshape images to 1D
Xtr = np.reshape(Xtr, newshape=(-1, 28*28))
Xte = np.reshape(Xte, newshape=(-1, 28*28))
# tf Graph Input
xtr = tf.placeholder("float", [None, 784])
xte = tf.placeholder("float", [784])
# Nearest Neighbor calculation using L1 Distance
# Calculate L1 Distance
distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))), reduction_indices=1)
# Predict: Get min distance index (Nearest neighbor)
pred = tf.argmin(distance, 0)
accuracy = 0.
# Initializing the variables
init = tf.initialize_all_variables()
WARNING:tensorflow:From /Users/tarrysingh/anaconda/lib/python3.6/site-packages/tensorflow/python/util/tf_should_use.py:175: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02. Instructions for updating: Use `tf.global_variables_initializer` instead.
# Launch the graph
with tf.Session() as sess:
sess.run(init)
# loop over test data
for i in range(len(Xte)):
# Get nearest neighbor
nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i,:]})
# Get nearest neighbor class label and compare it to its true label
print ("Test", i, "Prediction:", np.argmax(Ytr[nn_index]), \
"True Class:", np.argmax(Yte[i]))
# Calculate accuracy
if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):
accuracy += 1./len(Xte)
print ("Done!")
print ("Accuracy:", accuracy)
Test 0 Prediction: 5 True Class: 3 Test 1 Prediction: 5 True Class: 5 Test 2 Prediction: 2 True Class: 2 Test 3 Prediction: 2 True Class: 2 Test 4 Prediction: 3 True Class: 5 Test 5 Prediction: 4 True Class: 4 Test 6 Prediction: 3 True Class: 3 Test 7 Prediction: 7 True Class: 7 Test 8 Prediction: 3 True Class: 3 Test 9 Prediction: 8 True Class: 8 Test 10 Prediction: 1 True Class: 1 Test 11 Prediction: 4 True Class: 4 Test 12 Prediction: 3 True Class: 3 Test 13 Prediction: 8 True Class: 8 Test 14 Prediction: 9 True Class: 9 Test 15 Prediction: 2 True Class: 2 Test 16 Prediction: 9 True Class: 9 Test 17 Prediction: 3 True Class: 3 Test 18 Prediction: 7 True Class: 7 Test 19 Prediction: 6 True Class: 6 Test 20 Prediction: 5 True Class: 8 Test 21 Prediction: 2 True Class: 2 Test 22 Prediction: 6 True Class: 6 Test 23 Prediction: 0 True Class: 0 Test 24 Prediction: 0 True Class: 0 Test 25 Prediction: 1 True Class: 1 Test 26 Prediction: 3 True Class: 3 Test 27 Prediction: 1 True Class: 1 Test 28 Prediction: 2 True Class: 2 Test 29 Prediction: 1 True Class: 1 Test 30 Prediction: 7 True Class: 9 Test 31 Prediction: 6 True Class: 8 Test 32 Prediction: 2 True Class: 2 Test 33 Prediction: 5 True Class: 5 Test 34 Prediction: 9 True Class: 9 Test 35 Prediction: 9 True Class: 9 Test 36 Prediction: 7 True Class: 7 Test 37 Prediction: 1 True Class: 2 Test 38 Prediction: 7 True Class: 7 Test 39 Prediction: 6 True Class: 6 Test 40 Prediction: 3 True Class: 3 Test 41 Prediction: 3 True Class: 3 Test 42 Prediction: 8 True Class: 8 Test 43 Prediction: 9 True Class: 9 Test 44 Prediction: 1 True Class: 1 Test 45 Prediction: 5 True Class: 5 Test 46 Prediction: 9 True Class: 9 Test 47 Prediction: 0 True Class: 0 Test 48 Prediction: 8 True Class: 8 Test 49 Prediction: 0 True Class: 0 Test 50 Prediction: 0 True Class: 0 Test 51 Prediction: 4 True Class: 4 Test 52 Prediction: 3 True Class: 3 Test 53 Prediction: 7 True Class: 7 Test 54 Prediction: 6 True Class: 6 Test 55 Prediction: 0 True Class: 0 Test 56 Prediction: 7 True Class: 7 Test 57 Prediction: 1 True Class: 1 Test 58 Prediction: 5 True Class: 5 Test 59 Prediction: 0 True Class: 0 Test 60 Prediction: 8 True Class: 8 Test 61 Prediction: 8 True Class: 8 Test 62 Prediction: 1 True Class: 1 Test 63 Prediction: 0 True Class: 0 Test 64 Prediction: 6 True Class: 6 Test 65 Prediction: 7 True Class: 7 Test 66 Prediction: 4 True Class: 4 Test 67 Prediction: 2 True Class: 2 Test 68 Prediction: 3 True Class: 3 Test 69 Prediction: 8 True Class: 8 Test 70 Prediction: 3 True Class: 3 Test 71 Prediction: 6 True Class: 6 Test 72 Prediction: 2 True Class: 2 Test 73 Prediction: 5 True Class: 5 Test 74 Prediction: 0 True Class: 0 Test 75 Prediction: 7 True Class: 7 Test 76 Prediction: 9 True Class: 9 Test 77 Prediction: 9 True Class: 9 Test 78 Prediction: 1 True Class: 1 Test 79 Prediction: 2 True Class: 2 Test 80 Prediction: 5 True Class: 5 Test 81 Prediction: 3 True Class: 3 Test 82 Prediction: 8 True Class: 8 Test 83 Prediction: 2 True Class: 2 Test 84 Prediction: 4 True Class: 4 Test 85 Prediction: 7 True Class: 7 Test 86 Prediction: 9 True Class: 9 Test 87 Prediction: 2 True Class: 2 Test 88 Prediction: 7 True Class: 9 Test 89 Prediction: 2 True Class: 2 Test 90 Prediction: 1 True Class: 1 Test 91 Prediction: 4 True Class: 4 Test 92 Prediction: 7 True Class: 9 Test 93 Prediction: 0 True Class: 0 Test 94 Prediction: 9 True Class: 9 Test 95 Prediction: 8 True Class: 8 Test 96 Prediction: 0 True Class: 0 Test 97 Prediction: 7 True Class: 7 Test 98 Prediction: 2 True Class: 2 Test 99 Prediction: 4 True Class: 4 Test 100 Prediction: 2 True Class: 2 Test 101 Prediction: 1 True Class: 1 Test 102 Prediction: 4 True Class: 9 Test 103 Prediction: 9 True Class: 9 Test 104 Prediction: 5 True Class: 5 Test 105 Prediction: 6 True Class: 6 Test 106 Prediction: 7 True Class: 7 Test 107 Prediction: 4 True Class: 4 Test 108 Prediction: 4 True Class: 4 Test 109 Prediction: 7 True Class: 7 Test 110 Prediction: 7 True Class: 7 Test 111 Prediction: 9 True Class: 4 Test 112 Prediction: 9 True Class: 9 Test 113 Prediction: 5 True Class: 5 Test 114 Prediction: 6 True Class: 6 Test 115 Prediction: 0 True Class: 0 Test 116 Prediction: 7 True Class: 7 Test 117 Prediction: 5 True Class: 8 Test 118 Prediction: 1 True Class: 1 Test 119 Prediction: 6 True Class: 6 Test 120 Prediction: 2 True Class: 2 Test 121 Prediction: 5 True Class: 5 Test 122 Prediction: 2 True Class: 2 Test 123 Prediction: 7 True Class: 7 Test 124 Prediction: 0 True Class: 0 Test 125 Prediction: 3 True Class: 3 Test 126 Prediction: 8 True Class: 8 Test 127 Prediction: 0 True Class: 0 Test 128 Prediction: 7 True Class: 4 Test 129 Prediction: 6 True Class: 6 Test 130 Prediction: 4 True Class: 4 Test 131 Prediction: 4 True Class: 7 Test 132 Prediction: 5 True Class: 5 Test 133 Prediction: 9 True Class: 9 Test 134 Prediction: 4 True Class: 4 Test 135 Prediction: 5 True Class: 5 Test 136 Prediction: 5 True Class: 5 Test 137 Prediction: 3 True Class: 3 Test 138 Prediction: 7 True Class: 7 Test 139 Prediction: 7 True Class: 7 Test 140 Prediction: 8 True Class: 8 Test 141 Prediction: 8 True Class: 8 Test 142 Prediction: 0 True Class: 0 Test 143 Prediction: 3 True Class: 3 Test 144 Prediction: 4 True Class: 4 Test 145 Prediction: 0 True Class: 0 Test 146 Prediction: 8 True Class: 8 Test 147 Prediction: 5 True Class: 5 Test 148 Prediction: 2 True Class: 2 Test 149 Prediction: 2 True Class: 2 Test 150 Prediction: 7 True Class: 7 Test 151 Prediction: 1 True Class: 1 Test 152 Prediction: 2 True Class: 2 Test 153 Prediction: 3 True Class: 3 Test 154 Prediction: 5 True Class: 8 Test 155 Prediction: 8 True Class: 8 Test 156 Prediction: 4 True Class: 4 Test 157 Prediction: 3 True Class: 3 Test 158 Prediction: 6 True Class: 6 Test 159 Prediction: 6 True Class: 6 Test 160 Prediction: 8 True Class: 8 Test 161 Prediction: 0 True Class: 0 Test 162 Prediction: 2 True Class: 2 Test 163 Prediction: 6 True Class: 6 Test 164 Prediction: 5 True Class: 5 Test 165 Prediction: 5 True Class: 5 Test 166 Prediction: 8 True Class: 8 Test 167 Prediction: 7 True Class: 7 Test 168 Prediction: 5 True Class: 5 Test 169 Prediction: 1 True Class: 1 Test 170 Prediction: 2 True Class: 2 Test 171 Prediction: 1 True Class: 1 Test 172 Prediction: 5 True Class: 5 Test 173 Prediction: 7 True Class: 7 Test 174 Prediction: 4 True Class: 4 Test 175 Prediction: 8 True Class: 8 Test 176 Prediction: 7 True Class: 7 Test 177 Prediction: 4 True Class: 4 Test 178 Prediction: 2 True Class: 2 Test 179 Prediction: 3 True Class: 3 Test 180 Prediction: 6 True Class: 6 Test 181 Prediction: 1 True Class: 1 Test 182 Prediction: 9 True Class: 9 Test 183 Prediction: 6 True Class: 6 Test 184 Prediction: 6 True Class: 6 Test 185 Prediction: 6 True Class: 6 Test 186 Prediction: 1 True Class: 1 Test 187 Prediction: 7 True Class: 2 Test 188 Prediction: 7 True Class: 7 Test 189 Prediction: 9 True Class: 9 Test 190 Prediction: 2 True Class: 2 Test 191 Prediction: 8 True Class: 8 Test 192 Prediction: 9 True Class: 9 Test 193 Prediction: 1 True Class: 1 Test 194 Prediction: 9 True Class: 9 Test 195 Prediction: 7 True Class: 2 Test 196 Prediction: 3 True Class: 8 Test 197 Prediction: 6 True Class: 6 Test 198 Prediction: 0 True Class: 0 Test 199 Prediction: 7 True Class: 7 Done! Accuracy: 0.9150000000000007
accuracy*100
91.50000000000007