tensorflow基础模型之KNN(最邻近值)算法
KNN算法原理,本文将用tensorflow使用KNN算法训练MINST数据集。
Codes:
from __future__ import print_function, division
import numpy as np
import tensorflow as tf
# 导入MNIST数据
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./tmp/data/", one_hot=True)
# 取5000训练数据和200测试数据
Xtr, Ytr = mnist.train.next_batch(5000) # 5000 for training (nn candidates)
Xte, Yte = mnist.test.next_batch(200) # 200 for testing
# 图输入
xtr = tf.placeholder("float", [None, 784])
xte = tf.placeholder("float", [784])
# 使用L1距离获取最邻近值
# 计算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)
---------------------