# MNIST Digit Prediction with k-Nearest Neighbors
#-----------------------------------------------
#
# This script will load the MNIST data, and split
# it into test/train and perform prediction with
# nearest neighbors
#
# For each test integer, we will return the
# closest image/integer.
#
# Integer images are represented as 28x8 matrices
# of floating point numbers import random
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from PIL import Image
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python.framework import ops
ops.reset_default_graph() # Create graph
sess = tf.Session() # Load the data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # Random sample
np.random.seed(13) # set seed for reproducibility
train_size = 1000
test_size = 102
rand_train_indices = np.random.choice(len(mnist.train.images), train_size, replace=False)
rand_test_indices = np.random.choice(len(mnist.test.images), test_size, replace=False)
x_vals_train = mnist.train.images[rand_train_indices]
x_vals_test = mnist.test.images[rand_test_indices]
y_vals_train = mnist.train.labels[rand_train_indices]
y_vals_test = mnist.test.labels[rand_test_indices] # Declare k-value and batch size
k = 4
batch_size=6 # Placeholders
x_data_train = tf.placeholder(shape=[None, 784], dtype=tf.float32)
x_data_test = tf.placeholder(shape=[None, 784], dtype=tf.float32)
y_target_train = tf.placeholder(shape=[None, 10], dtype=tf.float32)
y_target_test = tf.placeholder(shape=[None, 10], dtype=tf.float32) # Declare distance metric
# L1
distance = tf.reduce_sum(tf.abs(tf.subtract(x_data_train, tf.expand_dims(x_data_test,1))), axis=2) # L2
#distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(x_data_train, tf.expand_dims(x_data_test,1))), reduction_indices=1)) # Predict: Get min distance index (Nearest neighbor)
top_k_xvals, top_k_indices = tf.nn.top_k(tf.negative(distance), k=k)
prediction_indices = tf.gather(y_target_train, top_k_indices)
# Predict the mode category
count_of_predictions = tf.reduce_sum(prediction_indices, axis=1)
prediction = tf.argmax(count_of_predictions, axis=1) # Calculate how many loops over training data
num_loops = int(np.ceil(len(x_vals_test)/batch_size)) test_output = []
actual_vals = []
for i in range(num_loops):
min_index = i*batch_size
max_index = min((i+1)*batch_size,len(x_vals_train))
x_batch = x_vals_test[min_index:max_index]
y_batch = y_vals_test[min_index:max_index]
predictions = sess.run(prediction, feed_dict={x_data_train: x_vals_train, x_data_test: x_batch,
y_target_train: y_vals_train, y_target_test: y_batch})
test_output.extend(predictions)
actual_vals.extend(np.argmax(y_batch, axis=1)) accuracy = sum([1./test_size for i in range(test_size) if test_output[i]==actual_vals[i]])
print('Accuracy on test set: ' + str(accuracy)) # Plot the last batch results:
actuals = np.argmax(y_batch, axis=1) Nrows = 2
Ncols = 3
for i in range(len(actuals)):
plt.subplot(Nrows, Ncols, i+1)
plt.imshow(np.reshape(x_batch[i], [28,28]), cmap='Greys_r')
plt.title('Actual: ' + str(actuals[i]) + ' Pred: ' + str(predictions[i]),
fontsize=10)
frame = plt.gca()
frame.axes.get_xaxis().set_visible(False)
frame.axes.get_yaxis().set_visible(False) plt.show()

效果:

最新文章

  1. Android搜索功能的案例,本地保存搜索历史记录......
  2. RadioGroup实现导航栏
  3. 神盾解密工具 之 解密 “ PHP 神盾解密工具 ”
  4. [JAVA设计模式]第四部分:行为模式
  5. Entity Framework6 访问MySQL
  6. NaN属性,isNaN函数
  7. 什么是PWM、PFM及VFM
  8. nodejs - json序列化&反序列化示例
  9. 懒人小工具:自动生成Model,Insert,Select,Delete以及导出Excel的方法
  10. Luogu P1541 乌龟棋(NOIP2010TG)
  11. Go语言中的string知识点
  12. 【转】依赖注入的威力,.NET Core的魅力:解决MVC视图中的中文被html编码的问题
  13. Chinese Mahjong UVA - 11210 (DFS)
  14. NLog基础配置
  15. windows服务中对外提供API接口
  16. 自学Linux Shell11.4-重定向输入输出
  17. 虚拟机vmnet0、vmnet1和vmnet8的区别
  18. 【区块链Go语言实现】Part 1:区块链基本原型
  19. PetaPoco轻量级ORM框架 - Database API 手册
  20. ③ 设计模式的艺术-09.组合(Composite)模式

热门文章

  1. UNP学习笔记(第十六章 非阻塞I/O)
  2. java GC(Garbage Collector) | System.gc()
  3. Easy UI form表单提交 IE浏览器不执行success ,以及 datagrid 展示过慢
  4. Android-Animations介绍
  5. 给js对象赋值,赋值key
  6. iOS 10 的杂碎资料
  7. UIScrollView奇葩不滑动
  8. rtmp直播拉流客户端EasyRTMPClient TCP窗口大小设计方法
  9. 九度OJ 1012:畅通工程 (最小生成树)
  10. exception_action