nearest_neighbor
来源: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相关推荐
- 最近邻插值法(nearest_neighbor)
1.原理与应用 最近邻插值法nearest_neighbor是最简单的灰度值插值.也称作零阶插值,就是令变换后像素的灰度值等于距它最近的输入像素的灰度值.最近邻插值法可应用于图像的缩放,因为简单的变换 ...
- 数字图像处理笔记二 - 图片缩放(最近邻插值(Nearest Neighbor interpolation))
图片缩放的两种常见算法: 最近邻域内插法(Nearest Neighbor interpolation) 双向性内插法(bilinear interpolation) 本文主要讲述最近邻插值(Near ...
- TensorFlow学习笔记之五——源码分析之最近算法
[python] view plaincopy import numpy as np import tensorflow as tf # Import MINST data import input_ ...
- TensorFlow与OpenCV,读取图片,进行简单操作并显示
本文是OpenCV 2 Computer Vision Application Programming Cookbook读书笔记的第一篇.在笔记中将以Python语言改写每章的代码. PythonO ...
- TensorFlow 图像数据预处理及可视化
图像是人们喜闻乐见的一种信息形式,"百闻不如一见",有时一张图能胜千言万语.图像处理是利用计算机将数值化的图像进行一定(线性或非线性)变换获得更好效果的方法.Photoshop,美 ...
- halcon的仿射变换算子的介绍
1. 仿射变换类型 仿射变换有:平移.旋转.缩放.斜切(就是将斜体字导正). 2. 求稳定的特征点 要进行仿射变换,必须先获取变换矩阵.要获取变换矩阵,必须先获取特征点坐标.角度等信息,几何匹配和bo ...
- Halcon算子--图像、区域、轮廓、测量、拟合、垂足、夹角
Halcon算子–图像.区域.轮廓.测量.拟合.垂足.夹角 read_image (Image,'fabrik') 画矩形 draw_rectangle1 (3600, Row1, Column1, ...
- 官方资源帖!手把手教你在TensorFlow 2.0中实现CycleGAN,推特上百赞
铜灵 发自 凹非寺 量子位 出品| 公众号 QbitAI CycleGAN,一个可以将一张图像的特征迁移到另一张图像的酷算法,此前可以完成马变斑马.冬天变夏天.苹果变桔子等一颗赛艇的效果. 这行被顶会 ...
- TensorFlow实现案例汇集:代码+笔记
这是使用 TensorFlow 实现流行的机器学习算法的教程汇集.本汇集的目标是让读者可以轻松通过案例深入 TensorFlow. 这些案例适合那些想要清晰简明的 TensorFlow 实现案例的初学 ...
最新文章
- 解禁 115 天,中兴事件的“反思”中藏着什么?
- 反序列化(先序)——split : string--vectorstring
- 项目立项管理:招投标
- 华为防火墙查看日志命令_华为防火墙异常日志,请高手进来查看,跪谢
- 【OpenCV 例程200篇】57. 低通高斯滤波器
- 百度SEO进云jys系统应用开发框架
- JavaScript + CSS3 实现的海报画廊特效
- 在js中通过location.href方式跳转页面并在路径上传递参数中文乱码解决
- python 实现数据化大屏_基于Python实现交互式数据可视化的工具(用于Web)
- 单例模式几种实现方式和代码
- 联想电脑安装黑苹果全教程
- 取石子游戏--尼姆博弈
- Bypass disable_function
- scipy.ndimage.measurements.find_objects()
- Git 进行分布式管理的入门
- 如何设置默认浏览器?快速学会,简单易懂
- 苹果Airplay协议以及AirTunes流程总结
- 转:绝对论坛元老,05年注册。告诉你一个真实的魅族
- 女大学生深度揭露大学最露骨生活,值得我们深思!
- 【C语言】八大排序算法