tensorflow实现KNN识别MNIST
KNN算法算是最简单的机器学习算法之一了,这个算法最大的特点是没有训练过程,是一种懒惰学习,这种结构也可以在tensorflow实现。
KNN的最核心就是距离度量方式,官方例程给出的是L1范数的例子,我这里改成了L2范数,也就是我们常说的欧几里得距离度量,另外,虽然是叫KNN,意思是选取k个最接近的元素来投票产生分类,但是这里只是用了最近的那个数据的标签作为预测值了。
__author__ = 'freedom' import tensorflow as tf import numpy as np def loadMNIST(): from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data',one_hot=True) return mnist def KNN(mnist): train_x,train_y = mnist.train.next_batch(5000) test_x,test_y = mnist.train.next_batch(200) xtr = tf.placeholder(tf.float32,[None,784]) xte = tf.placeholder(tf.float32,[784]) distance = tf.sqrt(tf.reduce_sum(tf.pow(tf.add(xtr,tf.neg(xte)),2),reduction_indices=1)) pred = tf.argmin(distance,0) init = tf.initialize_all_variables() sess = tf.Session() sess.run(init) right = 0 for i in range(200): ansIndex = sess.run(pred,{xtr:train_x,xte:test_x[i,:]}) print 'prediction is ',np.argmax(train_y[ansIndex]) print 'true value is ',np.argmax(test_y[i]) if np.argmax(test_y[i]) == np.argmax(train_y[ansIndex]): right += 1.0 accracy = right/200.0 print accracy if __name__ == "__main__": mnist = loadMNIST() KNN(mnist)
相关推荐
xjtukuixing 2020-10-27
sherry颖 2020-07-18
Kindle君 2020-07-15
xiaoxiaokeke 2020-06-27
xx0cw 2020-06-08
liqing 2020-06-07
liqing 2020-05-28
EastCarFxxBlog 2020-03-31
sunnyhappy0 2020-03-01
JM 2020-02-13
lybbb 2020-02-13
wndong 2020-02-02
liqing 2020-02-01
songbinxu 2020-01-08
minushuang 2019-12-24
dataastron 2019-11-10
lovetheme 2019-07-01
五小郎的学习笔记 2019-07-01
yangzzguang 2019-06-27