一、步骤:

  1. 导入包和读取数据

  2. 数据预处理

  3. 编码层和解码层的建立 + 构建模型

  4. 编译模型

  5. 训练模型

  6. 测试模型【只用编码层来画图】

二、代码:

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)----自编码的更多相关文章

  1. tensorflow笔记(四)之MNIST手写识别系列一

    tensorflow笔记(四)之MNIST手写识别系列一 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7436310.html ...

  2. tensorflow笔记(五)之MNIST手写识别系列二

    tensorflow笔记(五)之MNIST手写识别系列二 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7455233.html ...

  3. win10下通过Anaconda安装TensorFlow-GPU1.3版本,并配置pycharm运行Mnist手写识别程序

    折腾了一天半终于装好了win10下的TensorFlow-GPU版,在这里做个记录. 准备安装包: visual studio 2015: Anaconda3-4.2.0-Windows-x86_64 ...

  4. Tensorflow之基于MNIST手写识别的入门介绍

    Tensorflow是当下AI热潮下,最为受欢迎的开源框架.无论是从Github上的fork数量还是star数量,还是从支持的语音,开发资料,社区活跃度等多方面,他当之为superstar. 在前面介 ...

  5. 使用tensorflow实现mnist手写识别(单层神经网络实现)

    import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data import n ...

  6. Tensorflow编程基础之Mnist手写识别实验+关于cross_entropy的理解

    好久没有静下心来写点东西了,最近好像又回到了高中时候的状态,休息不好,无法全心学习,恶性循环,现在终于调整的好一点了,听着纯音乐突然非常伤感,那些曾经快乐的大学时光啊,突然又慢慢的一下子出现在了眼前, ...

  7. Haskell手撸Softmax回归实现MNIST手写识别

    Haskell手撸Softmax回归实现MNIST手写识别 前言 初学Haskell,看的书是Learn You a Haskell for Great Good, 才刚看到Making Our Ow ...

  8. [机器学习] keras:MNIST手写数字体识别(DeepLearning 的 HelloWord程序)

    深度学习界的Hello Word程序:MNIST手写数字体识别 learn from(仍然是李宏毅老师<机器学习>课程):http://speech.ee.ntu.edu.tw/~tlka ...

  9. 基于tensorflow的MNIST手写识别

    这个例子,是学习tensorflow的人员通常会用到的,也是基本的学习曲线中的一环.我也是! 这个例子很简单,这里,就是简单的说下,不同的tensorflow版本,相关的接口函数,可能会有不一样哟.在 ...

随机推荐

  1. dataguard switchover to physical stnadby

    首先做一系列的check check 当前primary 的 standby redo log是否存在 SQL> select * from v$logfile; GROUP# STATUS T ...

  2. firedac数据集和字符串相互转换

    uses Data.FireDACJSONReflect 1)FIREDAC数据库序列为字符串,进行了加压和BASE64编码 function DataSetToString(const ADataS ...

  3. saprk里面的action - aggregate

    上一篇讲到了spark里面的action函数: Action列表: reduce collect count first take takeSample takeOrdered saveAsTextF ...

  4. Java三行代码搞定MD5加密

    Java三行代码搞定MD5加密 https://www.dexcoder.com/selfly/article/4026 public class MD5Test { public static vo ...

  5. (OK) Installing Quagga—zebra—configure—make—CentOS7

    -------------------Installing Quagga # tar xzf quagga-0.99.21mr2.2.tar.gz # cd quagga-0.99.21mr2.2 / ...

  6. BAPC2014 B&amp;&amp;HUNNU11582:Button Bashing(BFS)

    题意: 给出n,m,代表微波炉有n个button,要求达到总时间为m 然后给出n个数.代表n个button能添加的时间,问最少几步,可以使得按出的总时间大于等于要求的时间,而且相差最小 输出最小的步数 ...

  7. WPF学习笔记——DataContext 与 ItemSource

    作为一个WPF新手,在ListBox控件里,我分不清 DataContext 与 ItemSource的区别. 在实践中,似乎: <ListBox x:Name="Lst" ...

  8. Codeforces Round #277 (Div. 2) B.OR in Matrix 模拟

    B. OR in Matrix   Let's define logical OR as an operation on two logical values (i. e. values that b ...

  9. 洛谷 P3112 后卫马克 —— 状压DP

    题目:https://www.luogu.org/problemnew/show/P3112 状压DP...转移不错. 代码如下: #include<iostream> #include& ...

  10. aspectC++常用命令

    常用命令:1.ag++ main.cc //在工程目录下产生编译后的exe2.ag++ main.cc --weave_only //产生.acc 纯c++文件3.ag++ main.cc --gen ...