目前我已把我自己手工敲写加注释的代码放到自己的github账户上面,项目地址在这里:https://github.com/RootYuan/tensorflow_examples_practice/。
最近邻算法,即找到训练样本中与之最近的数据,采用那个数据点的标注作为预测输出。
实际上这里没有训练,选取样本本身就是训练。计算量主要在遍历所有点挨个计算距离。
训练样本数量越多,越准确,但是计算量越大。
训练样本不同数目对应的准确率,但是后面提升越来越慢。(由于每次都是随机选取一定数量样本,因此多次运行会有一定差异,但是能定量说明问题)
样本数 | 准确率 |
---|---|
1000 | 0.845 |
2500 | 0.92 |
5000 | 0.955 |
20000 | 0.960 |
25000 | 0.965 |
源码如下:
import numpy as np
import tensorflow as tf
# 导入MNIST数据
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('./data/', one_hot=True)
# 在本代码中,我们限制使用的mnist数据的数量
Xtr, Ytr = mnist.train.next_batch(5000) # 5000个作为训练使用(nn candidates)
Xte, Yte = mnist.test.next_batch(200) #200个用于测试
# 计算图的输入
xtr = tf.placeholder('float', [None, 784])
xte = tf.placeholder('float', [784])
# 最近邻计算标准选择L1范数(曼哈顿距离)
# 计算曼哈顿距离
distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))), reduction_indices=1)
# 预测:获取最近距离的下标(找到最近邻)
pred = tf.arg_min(distance, 0)
accuracy = 0
# 初始化变量
init = tf.global_variables_initializer()
# 开始测试
with tf.Session() as sess:
# 初始化图中的变量
sess.run(init)
# 循环测试数据
for i in range(len(Xte)):
# 获取最近邻
nn_index = sess.run(pred, feed_dict={xtr:Xtr, xte:Xte[i, :]})
# 获取获取最近邻类的标签,和真实标注比较
print( "Test", i, "Prediction:", np.argmax( Ytr[nn_index] ), \
"True Class:", np.argmax( Yte[i] ) )
# 计算准确率
if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):
accuracy += 1./len(Xte)
print("Done!")
print("Accuracy:", accuracy)
输出:
...此处省略一千字....
Test 192 Prediction: 9 True Class: 9
Test 193 Prediction: 5 True Class: 5
Test 194 Prediction: 8 True Class: 8
Test 195 Prediction: 4 True Class: 4
Test 196 Prediction: 6 True Class: 6
Test 197 Prediction: 0 True Class: 0
Test 198 Prediction: 9 True Class: 9
Test 199 Prediction: 1 True Class: 2
Done!
Accuracy: 0.9550000000000007