前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一看就懂的Tensorflow实战(最近邻)

一看就懂的Tensorflow实战(最近邻)

作者头像
AI异构
发布2020-07-29 11:20:29
3240
发布2020-07-29 11:20:29
举报
文章被收录于专栏:AI异构AI异构

最近邻算法简介

k近邻模型的核心就是使用一种距离度量,获得距离目标点最近的k个点,根据分类决策规则,决定目标点的分类。[2]

距离度量(L1范数):

K值选择:这里k为10。

分类决策规则:k近邻的分类决策规则是最为常见的简单多数规则,也就是在最近的K个点中,哪个标签数目最多,就把目标点的标签归于哪一类。

Tensorflow 最近邻

代码语言:javascript
复制
import numpy as np
import tensorflow as tf
导入 mnist数据集
代码语言:javascript
复制
# Import MINST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./data/", one_hot=True)

代码语言:javascript
复制
Extracting ./data/train-images-idx3-ubyte.gz
Extracting ./data/train-labels-idx1-ubyte.gz
Extracting ./data/t10k-images-idx3-ubyte.gz
Extracting ./data/t10k-labels-idx1-ubyte.gz
构建模型
代码语言:javascript
复制
# 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(10) #10 for testing

# 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)
# Prediction: Get min distance index (Nearest neighbor)
pred = tf.argmin(distance, 0)

补充:Tenosrflow中基本算术运算函数:[1]

  • tf.add(x,y,name=None) #求和运算
  • tf.subtract(x,y,name=None) #减法运算
  • tf.multiply(x,y,name=None) #乘法运算
  • tf.div(x,y,name=None) #除法运算
  • tf.mod(x,y,name=None) #取模运算
  • tf.abs(x,name=None)#求绝对值
  • tf.negative(x,name=None) #取负运算(y=-x)
  • tf.sign(x,name=None) #返回符合 x大于0,则返回1,小于0,则返回-1
  • tf.reciprocal(x,name=None) #取反运算
  • tf.square(x,name=None) #计算平方
  • tf.round(x,name=None) #舍入最接近的整数
  • tf.pow(x,y,name=None) #幂次方
训练
代码语言:javascript
复制
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
        # 5000个样本点分别和10个测试点计算距离
        nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i, :]})
        print(nn_index)
        # Get nearest neighbor class label and compare it to its true label
        print ("Test", i, "Prediction:", np.argmax(Ytr[nn_index]), \
            "True Class:", np.argmax(Yte[i]))
        # Calculate accuracy
        if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):
            accuracy += 1./len(Xte)
    print ("Done!")
    print ("Accuracy:", accuracy)

代码语言:javascript
复制
190
Test 0 Prediction: 9 True Class: 9
475
Test 1 Prediction: 5 True Class: 5
3152
Test 2 Prediction: 7 True Class: 7
2413
Test 3 Prediction: 2 True Class: 2
1088
Test 4 Prediction: 2 True Class: 2
1427
Test 5 Prediction: 2 True Class: 2
4743
Test 6 Prediction: 7 True Class: 7
4826
Test 7 Prediction: 6 True Class: 6
4099
Test 8 Prediction: 5 True Class: 5
2421
Test 9 Prediction: 5 True Class: 5
Done!
Accuracy: 0.9999999999999999

参考

[1] Tenosrflow中基本算术运算函数:https://blog.csdn.net/zSean/article/details/75097937

[2] 统计学习方法——K近邻模型:https://blog.csdn.net/qq_35082030/article/details/60965320

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-04-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AI异构 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 最近邻算法简介
  • Tensorflow 最近邻
    • 导入 mnist数据集
      • 构建模型
        • 训练
        • 参考
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档