TensorFlow-K近邻算法

看了GitHub上的example代码,自己理解了,然后改了下,之前是最近邻,我改了下,K=9,写了注释,调整了 下参数

import numpy as np
import tensorflow as tf

# Import MINST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)#导入数据.one hot 表示成【0,1,0,0,0,0,】
# In this example, we limit mnist data
Xtr, Ytr = mnist.train.next_batch(5000) #5000 for training (nn candidates)
Xte, Yte = mnist.test.next_batch(200) #200 for testing
#5000数据用来比较,200来测试
# tf Graph Input
xtr = tf.placeholder("float", [None, 784])
xte = tf.placeholder("float", [784])#占位

# Nearest Neighbor calculation using L1 Distance
# Calculate L1 Distance
distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))), reduction_indices=1)
#这一句很关键,reduce_sum是一个降维操作,add是加法,negative表示取反,最后的‘1’表示降维的维度为1
#也就是按行压缩,把同一行的相加
# Prediction: Get min distance index (Nearest neighbor)
# pred = tf.argmin(distance, 0)#每列最小值
pred,index=tf.nn.top_k(-distance,9)#取负,负数大的反而小
# index=np.argsort(distance)[:9]
pred=-pred#恢复原来的数字
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(index, feed_dict={xtr: Xtr, xte: Xte[i, :]})
        klabels=np.argmax(Ytr[nn_index],axis=1)
        predictLabel=np.argmax(np.bincount(klabels))
#         喂数据,把5000个数据每个都跟验证的数据计算距离
#         Get nearest neighbor class label and compare it to its true label
        print( "Test", i, "Prediction:",predictLabel , \
            "True Class:", np.argmax(Yte[i]))#很简单,不解释
        # Calculate accuracy
        if (predictLabel == np.argmax(Yte[i])):
#             print('yes')
            accuracy += 1./len(Xte)
    print ("Done!")
    print( "Accuracy:", accuracy)

接下来是结果:

Test 194 Prediction: 7 True Class: 7
Test 195 Prediction: 8 True Class: 8
Test 196 Prediction: 9 True Class: 9
Test 197 Prediction: 9 True Class: 9
Test 198 Prediction: 4 True Class: 4
Test 199 Prediction: 7 True Class: 2
Done!
Accuracy: 0.9150000000000007

最后,有这么几个函数学习到了:

nn.top_k()#排序
np.bincount()#统计每个元素次数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值