Tensorflow学习笔记3—MNIST Nearest Neighbor Example最近算法
Nearest neighbor算法介绍:
1.为了判断未知实列(test)的类别,选取已知类别的实例(train)作为参考(如图所示:Xu点为未知类别的点);
2.对每个未知类别的点,计算其与所有已知类别的点的distance(距离)
3.最小距离所对应的已知类别点的类型,即为该未知点的距离。
如点unknow离type1 type2 type3的距离分别为25、10、15,距离type2最近,故将 unknow归类为type2。
#-*- coding: utf-8 -*-
import input_data
import numpy as np
import tensorflow as tf
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
#需自己下载input_data,具体看上一篇文章
# 选取5000train、200test
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])
# 计算距离
distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))), reduction_indices=1)
# 预测:计算出最短距离
pred = tf.argmin(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:
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)