python降维可视化 自编码_如何使用自动编码器可视化降维? (Python | TensorFlow)...
我正在尝试调整Aymeric Damien's code来可视化由 TensorFlow 中实现的自动编码器执行的降维 . 我看到的所有示例都在 mnist digits数据集上工作,但我想使用此方法将2维中的虹膜数据集可视化为玩具示例,以便我可以弄清楚如何为我的真实数据集调整它 .
我的问题是: How can one get the sample-specific 2 dimensional embeddings to visualize?
例如,iris数据集的 150 samples 与 4 attributes . 我添加了 4 noise attributes 以获得 8 attributes . 编码/解码如下: [8, 4, 2, 4, 8] 但我不知道如何提取形状 (150, 2) 数组来可视化嵌入 . 我还没有找到任何关于如何使用 TensorFlow 可视化降维的教程 .
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
# Set random seeds
np.random.seed(0)
tf.set_random_seed(0)
# Load data
iris = load_iris()
# Original Iris : (150,4)
X_iris = iris.data
# Iris with noise : (150,8)
X_iris_with_noise = np.concatenate([X_iris, np.random.random(size=X_iris.shape)], axis=1).astype(np.float32)
y_iris = iris.target
# PCA
pca_xy = PCA(n_components=2).fit_transform(X_iris_with_noise)
with plt.style.context("seaborn-white"):
fig, ax = plt.subplots()
ax.scatter(pca_xy[:,0], pca_xy[:,1], c=y_iris, cmap=plt.cm.Set2)
ax.set_title("PCA | Iris with noise")
# Training Parameters
learning_rate = 0.01
num_steps = 1000
batch_size = 10
display_step = 250
examples_to_show = 10
# Network Parameters
num_hidden_1 = 4 # 1st layer num features
num_hidden_2 = 2 # 2nd layer num features (the latent dim)
num_input = 8 # Iris data input
# tf Graph input
X = tf.placeholder(tf.float32, [None, num_input], name="input")
weights = {
'encoder_h1': tf.Variable(tf.random_normal([num_input, num_hidden_1]), dtype=tf.float32, name="encoder_h1"),
'encoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_hidden_2]), dtype=tf.float32, name="encoder_h2"),
'decoder_h1': tf.Variable(tf.random_normal([num_hidden_2, num_hidden_1]), dtype=tf.float32, name="decoder_h1"),
'decoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_input]), dtype=tf.float32, name="decoder_h2"),
}
biases = {
'encoder_b1': tf.Variable(tf.random_normal([num_hidden_1]), dtype=tf.float32, name="encoder_b1"),
'encoder_b2': tf.Variable(tf.random_normal([num_hidden_2]), dtype=tf.float32, name="encoder_b2"),
'decoder_b1': tf.Variable(tf.random_normal([num_hidden_1]), dtype=tf.float32, name="decoder_b1"),
'decoder_b2': tf.Variable(tf.random_normal([num_input]), dtype=tf.float32, name="decoder_b2"),
}
# Building the encoder
def encoder(x):
# Encoder Hidden layer with sigmoid activation #1
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']),
biases['encoder_b1']))
# Encoder Hidden layer with sigmoid activation #2
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']),
biases['encoder_b2']))
return layer_2
# Building the decoder
def decoder(x):
# Decoder Hidden layer with sigmoid activation #1
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']),
biases['decoder_b1']))
# Decoder Hidden layer with sigmoid activation #2
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['decoder_h2']),
biases['decoder_b2']))
return layer_2
# Construct model
encoder_op = encoder(X)
decoder_op = decoder(encoder_op)
# Prediction
y_pred = decoder_op
# Targets (Labels) are the input data.
y_true = X
# Define loss and optimizer, minimize the squared error
loss = tf.reduce_mean(tf.pow(y_true - y_pred, 2))
optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(loss)
# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
# Start Training
# Start a new TF session
with tf.Session() as sess:
# Run the initializer
sess.run(init)
# Training
for i in range(1, num_steps+1):
# Prepare Data
# Get the next batch of Iris data
idx_train = np.random.RandomState(i).choice(np.arange(X_iris_with_noise.shape[0]), size=batch_size)
batch_x = X_iris_with_noise[idx_train,:]
# Run optimization op (backprop) and cost op (to get loss value)
_, l = sess.run([optimizer, loss], feed_dict={X: batch_x})
# Display logs per step
if i % display_step == 0 or i == 1:
print('Step %i: Minibatch Loss: %f' % (i, l))
python降维可视化 自编码_如何使用自动编码器可视化降维? (Python | TensorFlow)...相关推荐
- python无法打印unicode编码_【整理】Python中实际上已经得到了正确的Unicode或某种编码的字符,但是看起来或打印出来却是乱码...
[背景] Python中的字符编码,其实的确有点复杂. 再加上,不同的开发环境和工具中,显示的逻辑和效果又不太相同,尤其是,中文的,初级用户,最常遇到的: (1)在Python自带的IDE:IDLE中 ...
- python创建类统计属性_轻松创建统计数据的Python包
python创建类统计属性 介绍 (Introduction) Sometimes you may need a distribution figure for your slide or class ...
- python制作ai小说网_【案例分享】使用Python创建AI比你想象的轻松
您可能对AI领域,主要开发阶段,成就,结果和产品使用感兴趣.有数百个免费源和教程描述使用Python的AI.但是,没有必要浪费你的时间看他们.这里是一个详细的指南,你需要知道在使用Python构建人工 ...
- python转go感觉难_读《我为什么从python转向go》的一些感受
一开始我以为是一篇2013年的老帖子,没想到竟然是2015年.不懂Python不要乱喷啊.你直接说"我不懂Python,我也不愿意维护前任写的糟糕代码,我Go牛B,所以我要重构一遍!&quo ...
- python新手入门项目推荐_推荐:一个适合于Python新手的入门练手项目
随着人工智能的兴起,国内掀起了一股Python学习热潮,入门级编程语言,大多选择Python,有经验的程序员,也开始学习Python,正所谓是人生苦短,我用Python 有个Python入门练手项目, ...
- python哪个方向工资高_【看完这五大Python就业方向,你选择哪个?】- 环球网校
[摘要]当今世界充满了各种数据,而python是其中一种的重要组成部分.然而,若想其有所应用,我们需要对这些python理论进行实践.其中包含很多有趣的的过程,然后将其用于某些方面.其中,在就业上有五 ...
- python运用在哪些地方_必看 | 2020年,Python十大应用领域介绍!
原标题:必看 | 2020年,Python十大应用领域介绍! python作为一门当下极其火爆的编程语言,得到世界范围内无数编程爱好者和开发者喜欢并不是偶然的,除了要比其他编程语言更容易入门,pyth ...
- python就业需要的技能_教你如何快速掌握Python就业技能
-人生苦短,为什么要学Python? 简单易学 应用广泛 大厂青睐 (油管大神评选的2020最值得学就业语言-Python) 因为以上的种种理由,无论是国外还是国内, Python都荣登2020最值得 ...
- python shell的无法保存_关于shell:如何保存Python交互式会话?
我发现自己经常使用Python的解释器来处理数据库,文件等-基本上是半结构化数据的许多手动格式化.我没有按我想的那样适当地保存和清理有用的位.有没有一种方法可以将我的输入保存到外壳中(数据库连接,变量 ...
最新文章
- 然后Denton et al, 2015 搞出了非常NB的改进版本
- Python爬虫入门教程 32-100 B站博人传评论数据抓取 scrapy
- Box-Cox(python实现)
- php oauth api,PHP实现人人OAuth登录和API调用
- 如何利用tcp或udp实现应用层协议_HTTP协议开发应用总结?
- 技术贴:asp.net实现唯一账户在线 禁止同一帐号同时在线 asp.net实现您的帐号在别处登录,您已被迫下线!...
- sublime自定义主题-修改行号的颜色
- 在windows下,编译可访问https的libcurl静态库过程
- 新浪微博澄清“花钱撤热搜”、“花钱压热搜”等不实传言
- 【Spring】Spring的生态项目
- newton法分形图
- python调用视频流_RTSP协议进行视频取流的方法、注意点及python实现
- linux虚拟usbgs0,USB模拟串口
- [2018.07.24 T1] 真板题
- html5通讯录模板,[应用模板]HTML5+Phonegap通讯录
- 《Android编程入门很简单》PDF版电子书下载
- 教你在线免费PDF转Word,建议收藏
- util-caleAge 计算年龄
- 食物也疯狂!KOOCAN盘点因为食物毁掉的中国电视剧
- VS2019配置WinRT
热门文章
- mysql升序nuul在最后,javaweb连接数据库并完成增删改查
- MDC功能软件-归控算法介绍
- 畅通工程再续 最小生成树
- I - Crossword Answers
- ##CSP 201812-2 小明放学(C语言)(100分)
- 强化学习——From drew追寻Mitsuha的学习笔记
- 图像处理——双线性插值(Bilinear Interpolation)
- 【干货】如何删除“自豪地采用WordPress“
- markdown如何修改为 微软雅黑 字体
- linux interfaces配置文件详解