莫烦大大keras的Mnist手写识别(5)----自编码
一、步骤:
导入包和读取数据
数据预处理
编码层和解码层的建立 + 构建模型
编译模型
训练模型
测试模型【只用编码层来画图】
二、代码:
1、导入包和读取数据
#导入相关的包
import numpy as np
np.random.seed(1337) # for reproducibility from keras.datasets import mnist
from keras.models import Model #采用通用模型
from keras.layers import Dense, Input #只用到全连接层
import matplotlib.pyplot as plt #读取数据
(X_train, _), (X_test, y_test) = mnist.load_data()
2、数据预处理:将28*28维度的数据拉成一个向量784,原数据X_train的shape为(60000,28,28),转成x_train(60000,784)。
x_train = X_train.astype('float32') / 255. - 0.5 # minmax_normalized
x_test = X_test.astype('float32') / 255. - 0.5 # minmax_normalized
x_train = X_train.reshape((x_train.shape[0], -1))
x_test = X_test.reshape((x_test.shape[0], -1))
print(x_train.shape) #(60000, 784)
print(x_test.shape) #(10000, 784)
print(X_train.shape) # (60000, 28, 28)
3、编码层和解码层的建立+构建模型
# in order to plot in a 2D figure
encoding_dim = 2 # this is our input placeholder
input_img = Input(shape=(784,)) # encoder layers编码层
encoded = Dense(128, activation='relu')(input_img)
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(10, activation='relu')(encoded)
encoder_output = Dense(encoding_dim)(encoded) # decoder layers解码层
decoded = Dense(10, activation='relu')(encoder_output)
decoded = Dense(64, activation='relu')(decoded)
decoded = Dense(128, activation='relu')(decoded)
decoded = Dense(784, activation='tanh')(decoded) #构建模型
#包括编码层也包括解码层
autoencoder = Model(input = input_img,output = decoded)
#只包括编码层
encoder = Model(input = input_img,output = encoder_output)
4、编译模型
#编译模型
autoencoder.compile(optimizer='adam', loss='mse')
5、训练模型【编码和解码一起训练】
autoencoder.fit(x_train, x_train,
epochs=20,
batch_size=256,
shuffle=True)
6、测试模型并画图显示【仅用编码来预测2维的特征空间】
encoded_imgs = encoder.predict(x_test)
plt.scatter(encoded_imgs[:, 0], encoded_imgs[:, 1], c=y_test) #c表示颜色维度
plt.colorbar()
plt.show()

莫烦大大keras的Mnist手写识别(5)----自编码的更多相关文章
- tensorflow笔记(四)之MNIST手写识别系列一
tensorflow笔记(四)之MNIST手写识别系列一 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7436310.html ...
- tensorflow笔记(五)之MNIST手写识别系列二
tensorflow笔记(五)之MNIST手写识别系列二 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7455233.html ...
- win10下通过Anaconda安装TensorFlow-GPU1.3版本,并配置pycharm运行Mnist手写识别程序
折腾了一天半终于装好了win10下的TensorFlow-GPU版,在这里做个记录. 准备安装包: visual studio 2015: Anaconda3-4.2.0-Windows-x86_64 ...
- Tensorflow之基于MNIST手写识别的入门介绍
Tensorflow是当下AI热潮下,最为受欢迎的开源框架.无论是从Github上的fork数量还是star数量,还是从支持的语音,开发资料,社区活跃度等多方面,他当之为superstar. 在前面介 ...
- 使用tensorflow实现mnist手写识别(单层神经网络实现)
import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data import n ...
- Tensorflow编程基础之Mnist手写识别实验+关于cross_entropy的理解
好久没有静下心来写点东西了,最近好像又回到了高中时候的状态,休息不好,无法全心学习,恶性循环,现在终于调整的好一点了,听着纯音乐突然非常伤感,那些曾经快乐的大学时光啊,突然又慢慢的一下子出现在了眼前, ...
- Haskell手撸Softmax回归实现MNIST手写识别
Haskell手撸Softmax回归实现MNIST手写识别 前言 初学Haskell,看的书是Learn You a Haskell for Great Good, 才刚看到Making Our Ow ...
- [机器学习] keras:MNIST手写数字体识别(DeepLearning 的 HelloWord程序)
深度学习界的Hello Word程序:MNIST手写数字体识别 learn from(仍然是李宏毅老师<机器学习>课程):http://speech.ee.ntu.edu.tw/~tlka ...
- 基于tensorflow的MNIST手写识别
这个例子,是学习tensorflow的人员通常会用到的,也是基本的学习曲线中的一环.我也是! 这个例子很简单,这里,就是简单的说下,不同的tensorflow版本,相关的接口函数,可能会有不一样哟.在 ...
随机推荐
- 相克军_Oracle体系_随堂笔记 PPT
http://www.cnblogs.com/jyzhao/category/581259.html http://download.csdn.net/detail/yzj149286454/8960 ...
- Tween公式
Tween公式 4个参数 t:current time(当前时间) b:beginning value(初始值) c: change in value(变化量) d:duration(持续时间) re ...
- [Angular] Using ngTemplateOutlet to create dynamic template
I can use <tamplete> syntax and a entry component as a container to create a dynamic component ...
- Gevent的协程实现原理
之前之所以看greenlet的代码实现,主要就是想要看看gevent库的实现代码. .. 然后知道了gevent的协程是基于greenlet来实现的...所以就又先去看了看greenlet的实现... ...
- hibernate之关于一对一单向,双向关联映射
[hibernate]之关于一对一单向,双向关联映射 首先我们来看,Hibernate官方对于一对一单向关联的解释: 基于外键关联的单向一对一关联和单向多对一关联差点儿是一样的. 唯一的不同就是单向一 ...
- 抽象类(Abstract)和接口的不同点、共同点(Interface)。
同样点: (1) 都能够被继承 (2) 都不能被实例化 (3) 都能够包括方法声明 (4) 派生类必须实现未实现的方法 区 别: (1) 抽象基类能够定义字段.属性.方法实现.接口仅仅能定义属性.索引 ...
- oc25--私有变量私有方法
// // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { @public int _a ...
- 3-4 第三天 Generator生成器
Generator是ES6里面的新增规范,ES6其实就是ES2015.ES5.ES6.ES7这些术语大家上网一查就都明白了.JavaScript是一个范程,就是我们说的JS.ES就是ECMA Scri ...
- Shuffle'm Up(串)
http://poj.org/problem?id=3087 题意:每组3个串,前两个串长度为n,第三个串长度为2*n,依次从第二个串(s2)中取一个字符,从第一个串(s1)中取一个字符,...... ...
- Centos6系列安装nginx
设置ssh服务为开机启动 输入命令:chkconfig sshd on 即可.注:若是chkconfig sshd off则禁止SSH开机启动 设定账号为bizuser ,密码为123456 第一步: ...