t-SNE可视化(MNIST例子)
如下所示:
import pickle as pkl
import numpy as np
from matplotlib import pyplot as plt
from tsne import bh_sne
import sys with open("data", 'rb') as f:
if sys.version_info > (3, 0):
data = pkl.load(f, encoding='latin1')
else:
data = pkl.load(f) data =data.astype('float64') with open("label", 'rb') as f:
if sys.version_info > (3, 0):
y_data = pkl.load(f, encoding='latin1')
else:
y_data = pkl.load(f)
classNum = 6
y_data = np.where(y_data==1)[1]*(9.0/classNum) vis_data = bh_sne(data) # plot the result
vis_x = vis_data[:, 0]
vis_y = vis_data[:, 1] fig = plt.figure()
plt.scatter(vis_x, vis_y, c=y_data, s=1, cmap=plt.cm.get_cmap("jet", 10))
plt.colorbar(ticks=range(10))
plt.clim(-0.5, 9.5)
plt.show()
fig.savefig('test.png')
结果:

以MNIST为例,先做PCA降到50维,再做t-sne:
from time import time
from tsne import bh_sne
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
from matplotlib import offsetbox
from sklearn import (manifold, datasets, decomposition, ensemble,
discriminant_analysis, random_projection)
from sklearn import decomposition mnist = input_data.read_data_sets('./input_data', one_hot=False)
sub_sample = 5000
y = mnist.train.labels[0:sub_sample]
X = mnist.train.images[0:sub_sample] n_samples, n_features = X.shape
n_neighbors = 30 #----------------------------------------------------------------------
# Scale and visualize the embedding vectors
def plot_embedding(X_emb, title=None):
x_min, x_max = np.min(X_emb, 0), np.max(X_emb, 0)
X_emb = (X_emb - x_min) / (x_max - x_min) plt.figure()
ax = plt.subplot(111)
for i in range(X_emb.shape[0]):
plt.text(X_emb[i, 0], X_emb[i, 1], str(y[i]),
color=plt.cm.Set1(y[i] / 10.),
fontdict={'weight': 'bold', 'size': 9}) if hasattr(offsetbox, 'AnnotationBbox'):
# only print thumbnails with matplotlib > 1.0
shown_images = np.array([[1., 1.]]) # just something big
for i in range(sub_sample):
dist = np.sum((X_emb[i] - shown_images) ** 2, 1)
if np.min(dist) < 8e-3:
# don't show points that are too close
continue
shown_images = np.r_[shown_images, [X_emb[i]]]
imagebox = offsetbox.AnnotationBbox(
offsetbox.OffsetImage(X[i].reshape(28,28)[::2,::2], cmap=plt.cm.gray_r),
X_emb[i])
ax.add_artist(imagebox)
plt.xticks([]), plt.yticks([])
if title is not None:
plt.title(title) #----------------------------------------------------------------------
# Plot images of the digits
n_img_per_row = 20
img = np.zeros((30 * n_img_per_row, 30 * n_img_per_row))
for i in range(n_img_per_row):
ix = 30 * i + 1
for j in range(n_img_per_row):
iy = 30 * j + 1
img[ix:ix + 28, iy:iy + 28] = X[i * n_img_per_row + j].reshape((28, 28)) plt.imshow(img, cmap=plt.cm.binary)
plt.xticks([])
plt.yticks([])
plt.title('A selection from the 64-dimensional digits dataset') # t-SNE embedding of the digits dataset
print("Computing t-SNE embedding")
t0 = time()
X_pca = decomposition.TruncatedSVD(n_components=50).fit_transform(X)
# data =X.astype('float64')
X_tsne = bh_sne(X_pca) plot_embedding(X_tsne,
"t-SNE embedding of the digits (time %.2fs)" %
(time() - t0)) plt.show()
结果如下:


t-SNE可视化(MNIST例子)的更多相关文章
- 一个简单的TensorFlow可视化MNIST数据集识别程序
下面是TensorFlow可视化MNIST数据集识别程序,可视化内容是,TensorFlow计算图,表(loss, 直方图, 标准差(stddev)) # -*- coding: utf-8 -*- ...
- Tensorflow可视化MNIST手写数字训练
简述] 我们在学习编程语言时,往往第一个程序就是打印“Hello World”,那么对于人工智能学习系统平台来说,他的“Hello World”小程序就是MNIST手写数字训练了.MNIST是一个手写 ...
- 可视化MNIST之降维探索Visualizing MNIST: An Exploration of Dimensionality Reduction
At some fundamental level, no one understands machine learning. It isn’t a matter of things being to ...
- 莫烦TensorFlow_09 MNIST例子
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...
- (4运行例子)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署
1.联通ColaB 2.运行最基础mnist例子,并且打印图表结果 # https://pypi.python.org/pypi/pydot#!apt-get -qq install -y gra ...
- 使用t-SNE做降维可视化
最近在做一个深度学习分类项目,想看看训练集数据的分布情况,但由于数据本身维度接近100,不能直观的可视化展示,所以就对降维可视化做了一些粗略的了解以便能在低维空间中近似展示高维数据的分布情况,以下内容 ...
- 用vs2013(cpu-only)调试caffe的mnist
在调试Mnist例子之前,首先需要用vs2013编译好caffe.详情请参见: [caffe-Windows]caffe+VS2013+Windows无GPU快速配置教程 按照上述教程编译好caffe ...
- [转] kaldi中FST的可视化-以yesno为例
http://blog.csdn.net/u013677156/article/details/77893661 1.kaldi解码过程 kaldi识别解码一段语音的过程是:首先提取特征,然后过声学模 ...
- Caffe 使用记录(一)mnist手写数字识别
1. 运行它 1. 安装caffe请参考 http://www.cnblogs.com/xuanyuyt/p/5726926.html 此例子在官网 http://caffe.berkeleyvis ...
随机推荐
- Unix环境高级编程(六)进程控制
本章介绍Unix的进程控制,包括进程创建,执行程序和进程终止,进程的属性,exec函数系列,system函数,进程会计机制. 1.进程标识符 每一个进程都有一个非负整数标识的唯一进程ID.ID为0表示 ...
- GraphX实现N度关系
背景 本文给出了一个简单的计算图中每一个点的N度关系点集合的算法,也就是N跳关系. 之前通过官方文档学习和理解了一下GraphX的计算接口. N度关系 目标: 在N轮里.找到某一个点的N度关系的点集合 ...
- Python atan2() 函数
描述 atan2() 返回给定的 X 及 Y 坐标值的反正切值. 语法 以下是 atan2() 方法的语法: import math math.atan2(y, x) 注意:atan2()是不能直接访 ...
- centos 6.5 文件目录管理
Linux不存在像Windows那样分盘符的概念,Linux在安装之后就以文件目录的形式来进行管理,存储.即当我们安装完系统之后,我们就会看到有一堆的目录出现在根目录下.同时Linux使用正斜杠“/” ...
- 【转】sql server存储过程中SELECT 与 SET 对变量赋值的区别
转自:http://www.cnblogs.com/micheng11/archive/2008/07/08/1237905.html SQL Server 中对已经定义的变量赋值的方式用两种,分别是 ...
- Javascript玩转继承(二)
对象伪装来实现继承.在这里先来说一下这种方式的优缺点 这种方式是比较容易理解的,在子类中调用父类的构造函数.另外,这种方法最大的一个优点就是说构造继承可以实现多继承,复习下这个代码: function ...
- CSS控制当鼠标滑过时更换图片的效果
鼠标滑过时更换图片的效果有很多方法可以实现,在本文将为大家介绍喜爱如何通过css来实现.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tra ...
- nginx中配置404错误页面的教程
什么是404页面如果网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是NGI ...
- cocos2d-x开发记录:二,基本概念(动作,动画,坐标系统)
既然我们选择用cocos2d,那么他里面的一些基本概念我们肯定是要熟悉下的,以下资料来源于官网,英语好的可以直接去官网看. 一.Actions(动作) 动作都由于CCNode对象发出.这些动作通常修改 ...
- ISE联合modelsim功能仿真和综合后仿真
1.代码输入 (1).新建一个ISE工程,名字为count4. (2).新建一个verilog文件 (3).选择verilog module 输入file name为count4,单击next默认知道 ...