来源:https://github.com/aymericdamien/TensorFlow-Examples

'''
A nearest neighbor learning algorithm example using TensorFlow library.
This example is using the MNIST database of handwritten digits
(http://yann.lecun.com/exdb/mnist/)Author: Aymeric Damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/
'''from __future__ import print_functionimport numpy as np
import tensorflow as tf# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)# 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# 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.arg_min(distance, 0)accuracy = 0.# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()# Start training
with tf.Session() as sess:# Run the initializersess.run(init)# loop over test datafor i in range(len(Xte)):# Get nearest neighbornn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i, :]})# Get nearest neighbor class label and compare it to its true labelprint("Test", i, "Prediction:", np.argmax(Ytr[nn_index]), \"True Class:", np.argmax(Yte[i]))# Calculate accuracyif np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):accuracy += 1./len(Xte)print("Done!")print("Accuracy:", accuracy)

nearest_neighbor相关推荐

  1. 最近邻插值法(nearest_neighbor)

    1.原理与应用 最近邻插值法nearest_neighbor是最简单的灰度值插值.也称作零阶插值,就是令变换后像素的灰度值等于距它最近的输入像素的灰度值.最近邻插值法可应用于图像的缩放,因为简单的变换 ...

  2. 数字图像处理笔记二 - 图片缩放(最近邻插值(Nearest Neighbor interpolation))

    图片缩放的两种常见算法: 最近邻域内插法(Nearest Neighbor interpolation) 双向性内插法(bilinear interpolation) 本文主要讲述最近邻插值(Near ...

  3. TensorFlow学习笔记之五——源码分析之最近算法

    [python] view plaincopy import numpy as np import tensorflow as tf # Import MINST data import input_ ...

  4. TensorFlow与OpenCV,读取图片,进行简单操作并显示

    本文是OpenCV  2 Computer Vision Application Programming Cookbook读书笔记的第一篇.在笔记中将以Python语言改写每章的代码. PythonO ...

  5. TensorFlow 图像数据预处理及可视化

    图像是人们喜闻乐见的一种信息形式,"百闻不如一见",有时一张图能胜千言万语.图像处理是利用计算机将数值化的图像进行一定(线性或非线性)变换获得更好效果的方法.Photoshop,美 ...

  6. halcon的仿射变换算子的介绍

    1. 仿射变换类型 仿射变换有:平移.旋转.缩放.斜切(就是将斜体字导正). 2. 求稳定的特征点 要进行仿射变换,必须先获取变换矩阵.要获取变换矩阵,必须先获取特征点坐标.角度等信息,几何匹配和bo ...

  7. Halcon算子--图像、区域、轮廓、测量、拟合、垂足、夹角

    Halcon算子–图像.区域.轮廓.测量.拟合.垂足.夹角 read_image (Image,'fabrik') 画矩形 draw_rectangle1 (3600, Row1, Column1, ...

  8. 官方资源帖!手把手教你在TensorFlow 2.0中实现CycleGAN,推特上百赞

    铜灵 发自 凹非寺 量子位 出品| 公众号 QbitAI CycleGAN,一个可以将一张图像的特征迁移到另一张图像的酷算法,此前可以完成马变斑马.冬天变夏天.苹果变桔子等一颗赛艇的效果. 这行被顶会 ...

  9. TensorFlow实现案例汇集:代码+笔记

    这是使用 TensorFlow 实现流行的机器学习算法的教程汇集.本汇集的目标是让读者可以轻松通过案例深入 TensorFlow. 这些案例适合那些想要清晰简明的 TensorFlow 实现案例的初学 ...

最新文章

  1. 解禁 115 天,中兴事件的“反思”中藏着什么?
  2. 反序列化(先序)——split : string--vectorstring
  3. 项目立项管理:招投标
  4. 华为防火墙查看日志命令_华为防火墙异常日志,请高手进来查看,跪谢
  5. 【OpenCV 例程200篇】57. 低通高斯滤波器
  6. 百度SEO进云jys系统应用开发框架
  7. JavaScript + CSS3 实现的海报画廊特效
  8. 在js中通过location.href方式跳转页面并在路径上传递参数中文乱码解决
  9. python 实现数据化大屏_基于Python实现交互式数据可视化的工具(用于Web)
  10. 单例模式几种实现方式和代码
  11. 联想电脑安装黑苹果全教程
  12. 取石子游戏--尼姆博弈
  13. Bypass disable_function
  14. scipy.ndimage.measurements.find_objects()
  15. Git 进行分布式管理的入门
  16. 如何设置默认浏览器?快速学会,简单易懂
  17. 苹果Airplay协议以及AirTunes流程总结
  18. 转:绝对论坛元老,05年注册。告诉你一个真实的魅族
  19. 女大学生深度揭露大学最露骨生活,值得我们深思!
  20. 【C语言】八大排序算法

热门文章

  1. halcon直线延长
  2. 尚医通平台地址以及服务启动
  3. CTF--RsaCtfTool安装
  4. C语言的宏macro的使用
  5. 教你一招,解决头疼的DCOM配置问题
  6. picsart旧版本_PicsArt美易照片编辑
  7. DCE和DTE的区别
  8. 视频如何实现倍速播放(比如1.5倍、2倍播放)
  9. unity3D之我用过的小玩意(一)
  10. Microsoft Visual Studio 2015 Installer Projects 打包 安装 部署