1. 程式人生 > >TensorFlow的最近鄰演算法

TensorFlow的最近鄰演算法

'''
A nearest neighbor learning algorithm example using TensorFlow library.
This example is using the MNIST database of handwritten digits
(http://yann.lecun.com/exdb/mnist/)

Author: Aymeric Damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/
'''

from __future__ import print_function

import numpy as np
import tensorflow as tf

# Import MNIST data
from tensorflow.examples.tutorials.mnist 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

# 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)
# Prediction: Get min distance index (Nearest neighbor)
pred = tf.arg_min(distance, 0)

accuracy = 0.

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()

# Start training
with tf.Session() as sess:

    # Run the initializer
    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: 0 True Class: 0 Test 1 Prediction: 6 True Class: 6 Test 2 Prediction: 4 True Class: 4 Test 3 Prediction: 1 True Class: 2 Test 4 Prediction: 0 True Class: 0 Test 5 Prediction: 2 True Class: 2 Test 6 Prediction: 7 True Class: 7 Test 7 Prediction: 7 True Class: 7 Test 8 Prediction: 7 True Class: 7 Test 9 Prediction: 1 True Class: 1 Test 10 Prediction: 4 True Class: 4 Test 11 Prediction: 1 True Class: 1 Test 12 Prediction: 2 True Class: 2 Test 13 Prediction: 6 True Class: 6 Test 14 Prediction: 8 True Class: 8 Test 15 Prediction: 5 True Class: 5 Test 16 Prediction: 3 True Class: 3 Test 17 Prediction: 6 True Class: 6 Test 18 Prediction: 1 True Class: 1 Test 19 Prediction: 5 True Class: 5 Test 20 Prediction: 3 True Class: 3 Test 21 Prediction: 6 True Class: 6 Test 22 Prediction: 3 True Class: 3 Test 23 Prediction: 8 True Class: 3 Test 24 Prediction: 0 True Class: 0 Test 25 Prediction: 5 True Class: 5 Test 26 Prediction: 0 True Class: 8 Test 27 Prediction: 8 True Class: 8 Test 28 Prediction: 2 True Class: 2 Test 29 Prediction: 4 True Class: 4 Test 30 Prediction: 1 True Class: 1 Test 31 Prediction: 3 True Class: 3 Test 32 Prediction: 5 True Class: 5 Test 33 Prediction: 0 True Class: 0 Test 34 Prediction: 4 True Class: 4 Test 35 Prediction: 6 True Class: 6 Test 36 Prediction: 7 True Class: 7 Test 37 Prediction: 1 True Class: 1 Test 38 Prediction: 5 True Class: 5 Test 39 Prediction: 0 True Class: 0 Test 40 Prediction: 2 True Class: 3 Test 41 Prediction: 7 True Class: 7 Test 42 Prediction: 2 True Class: 2 Test 43 Prediction: 1 True Class: 1 Test 44 Prediction: 4 True Class: 4 Test 45 Prediction: 3 True Class: 3 Test 46 Prediction: 8 True Class: 8 Test 47 Prediction: 3 True Class: 3 Test 48 Prediction: 8 True Class: 8 Test 49 Prediction: 8 True Class: 3 Test 50 Prediction: 7 True Class: 7 Test 51 Prediction: 1 True Class: 1 Test 52 Prediction: 0 True Class: 0 Test 53 Prediction: 6 True Class: 6 Test 54 Prediction: 9 True Class: 9 Test 55 Prediction: 8 True Class: 8 Test 56 Prediction: 1 True Class: 7 Test 57 Prediction: 2 True Class: 2 Test 58 Prediction: 4 True Class: 4 Test 59 Prediction: 8 True Class: 8 Test 60 Prediction: 4 True Class: 4 Test 61 Prediction: 2 True Class: 0 Test 62 Prediction: 4 True Class: 4 Test 63 Prediction: 5 True Class: 5 Test 64 Prediction: 3 True Class: 3 Test 65 Prediction: 1 True Class: 1 Test 66 Prediction: 3 True Class: 3 Test 67 Prediction: 3 True Class: 3 Test 68 Prediction: 0 True Class: 0 Test 69 Prediction: 3 True Class: 3 Test 70 Prediction: 6 True Class: 6 Test 71 Prediction: 7 True Class: 7 Test 72 Prediction: 1 True Class: 1 Test 73 Prediction: 9 True Class: 9 Test 74 Prediction: 4 True Class: 4 Test 75 Prediction: 1 True Class: 9 Test 76 Prediction: 1 True Class: 1 Test 77 Prediction: 3 True Class: 3 Test 78 Prediction: 4 True Class: 4 Test 79 Prediction: 5 True Class: 5 Test 80 Prediction: 7 True Class: 7 Test 81 Prediction: 0 True Class: 0 Test 82 Prediction: 2 True Class: 2 Test 83 Prediction: 7 True Class: 7 Test 84 Prediction: 5 True Class: 5 Test 85 Prediction: 0 True Class: 0 Test 86 Prediction: 7 True Class: 7 Test 87 Prediction: 1 True Class: 1 Test 88 Prediction: 5 True Class: 5 Test 89 Prediction: 8 True Class: 8 Test 90 Prediction: 9 True Class: 9 Test 91 Prediction: 4 True Class: 4 Test 92 Prediction: 5 True Class: 5 Test 93 Prediction: 7 True Class: 7 Test 94 Prediction: 3 True Class: 3 Test 95 Prediction: 8 True Class: 8 Test 96 Prediction: 1 True Class: 4 Test 97 Prediction: 5 True Class: 5 Test 98 Prediction: 3 True Class: 3 Test 99 Prediction: 2 True Class: 2 Test 100 Prediction: 8 True Class: 2 Test 101 Prediction: 1 True Class: 1 Test 102 Prediction: 3 True Class: 3 Test 103 Prediction: 2 True Class: 2 Test 104 Prediction: 5 True Class: 5 Test 105 Prediction: 1 True Class: 1 Test 106 Prediction: 0 True Class: 0 Test 107 Prediction: 7 True Class: 7 Test 108 Prediction: 2 True Class: 2 Test 109 Prediction: 0 True Class: 0 Test 110 Prediction: 6 True Class: 6 Test 111 Prediction: 4 True Class: 4 Test 112 Prediction: 2 True Class: 2 Test 113 Prediction: 3 True Class: 3 Test 114 Prediction: 6 True Class: 6 Test 115 Prediction: 8 True Class: 8 Test 116 Prediction: 3 True Class: 3 Test 117 Prediction: 4 True Class: 4 Test 118 Prediction: 8 True Class: 8 Test 119 Prediction: 9 True Class: 9 Test 120 Prediction: 5 True Class: 5 Test 121 Prediction: 5 True Class: 5 Test 122 Prediction: 7 True Class: 7 Test 123 Prediction: 3 True Class: 3 Test 124 Prediction: 7 True Class: 7 Test 125 Prediction: 9 True Class: 9 Test 126 Prediction: 1 True Class: 7 Test 127 Prediction: 4 True Class: 4 Test 128 Prediction: 7 True Class: 7 Test 129 Prediction: 1 True Class: 1 Test 130 Prediction: 8 True Class: 8 Test 131 Prediction: 3 True Class: 3 Test 132 Prediction: 6 True Class: 6 Test 133 Prediction: 1 True Class: 1 Test 134 Prediction: 9 True Class: 9 Test 135 Prediction: 4 True Class: 4 Test 136 Prediction: 8 True Class: 8 Test 137 Prediction: 3 True Class: 3 Test 138 Prediction: 7 True Class: 7 Test 139 Prediction: 6 True Class: 1 Test 140 Prediction: 7 True Class: 7 Test 141 Prediction: 6 True Class: 6 Test 142 Prediction: 7 True Class: 7 Test 143 Prediction: 3 True Class: 3 Test 144 Prediction: 0 True Class: 0 Test 145 Prediction: 9 True Class: 9 Test 146 Prediction: 1 True Class: 1 Test 147 Prediction: 6 True Class: 6 Test 148 Prediction: 0 True Class: 0 Test 149 Prediction: 1 True Class: 1 Test 150 Prediction: 5 True Class: 8 Test 151 Prediction: 1 True Class: 1 Test 152 Prediction: 2 True Class: 2 Test 153 Prediction: 5 True Class: 5 Test 154 Prediction: 6 True Class: 6 Test 155 Prediction: 0 True Class: 0 Test 156 Prediction: 8 True Class: 8 Test 157 Prediction: 0 True Class: 0 Test 158 Prediction: 1 True Class: 1 Test 159 Prediction: 5 True Class: 5 Test 160 Prediction: 0 True Class: 0 Test 161 Prediction: 1 True Class: 1 Test 162 Prediction: 5 True Class: 5 Test 163 Prediction: 6 True Class: 6 Test 164 Prediction: 6 True Class: 6 Test 165 Prediction: 7 True Class: 9 Test 166 Prediction: 3 True Class: 3 Test 167 Prediction: 2 True Class: 2 Test 168 Prediction: 7 True Class: 7 Test 169 Prediction: 3 True Class: 8 Test 170 Prediction: 4 True Class: 4 Test 171 Prediction: 9 True Class: 9 Test 172 Prediction: 0 True Class: 0 Test 173 Prediction: 7 True Class: 7 Test 174 Prediction: 5 True Class: 5 Test 175 Prediction: 4 True Class: 4 Test 176 Prediction: 7 True Class: 7 Test 177 Prediction: 2 True Class: 2 Test 178 Prediction: 5 True Class: 5 Test 179 Prediction: 2 True Class: 2 Test 180 Prediction: 1 True Class: 1 Test 181 Prediction: 8 True Class: 8 Test 182 Prediction: 4 True Class: 4 Test 183 Prediction: 2 True Class: 2 Test 184 Prediction: 8 True Class: 8 Test 185 Prediction: 7 True Class: 7 Test 186 Prediction: 2 True Class: 2 Test 187 Prediction: 2 True Class: 2 Test 188 Prediction: 8 True Class: 8 Test 189 Prediction: 0 True Class: 0 Test 190 Prediction: 2 True Class: 2 Test 191 Prediction: 3 True Class: 3 Test 192 Prediction: 4 True Class: 4 Test 193 Prediction: 3 True Class: 3 Test 194 Prediction: 5 True Class: 5 Test 195 Prediction: 1 True Class: 1 Test 196 Prediction: 5 True Class: 5 Test 197 Prediction: 2 True Class: 2 Test 198 Prediction: 5 True Class: 5 Test 199 Prediction: 0 True Class: 0 Done! Accuracy: 0.9250000000000007