自编码器初探——深度自编码器

因为最近在研究对抗自编码器,所以想对编码器有个大致的了解,于是就从最基础的自编码器开始看起,正好熟悉一下keras模型的搭建以及其各种各样的函数和层的调用,最后还会介绍一下如何用tensorboard将训练过程中的一些误差和精度可视化。
简单介绍一下自编码器,按照我的理解(理解可能会不是很到位,大家有问题可以留言),我们所说的自编码器通常包含了编码(encoder)和解码(decoder)两部分。编码部分呢就是相当于数据压缩,将输入的高维数据(这里所谓的高维指的是数据的属性变量较多,不是高维张量的意思)通过一定的函数使得其转化为低维的潜在特征数据(我觉着这些潜在特征没有具体的物理含义),只不过这些潜在特征能够包含原始数据中的大部分主要特征,对于一些微小的特征可能就丢失了。所以说经过编码器压缩后的特征用处很大,可以将它用于数据传输(会比传送大量原始数据快得多),也可以用这些潜在的特征构建原始数据的特征空间,对一些工业场景做过程监测或是故障诊断。当然自编码器的发明初衷就是为了加速数据传输,但是在接收端接受到了潜在的特征,如何将其还原呢?答案就是解码器,通过解码器将数据尽可能少失真的还原,所以这一方面来看解码器和编码器是个对偶的过程。关于自编码器就先简单的介绍到这里,下面我们来说一说深度自编码器的结构。
其实我觉得深度自编码器本质还是多层感知机(MLP)只不过隐层数目稍微多一点。下面就是我搭建的深度自编码器的结构。
在这里插入图片描述
因为是mnist数据集,都是28*28的灰度图,但是我们想将其看作数据进行操作而不是图像,所以首先将其reshape成一维张量(784,)

x_train = x_train.reshape(-1,784)#60000*784
x_test  = x_test.reshape(-1,784)

紧接着再送入神经网络之前要进行一下数据的标准化,我们将0-255间的灰度像素除以255化为0-1之间,这样在训练时可以加速梯度的收敛。

#将灰度图进行归一化0-1,加速收敛
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.

接下来我们可以给数据手动加上高斯噪声

x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape)
x_train_noisy = np.clip(x_train_noisy, 0., 1.)#输出限幅
x_test_noisy = np.clip(x_test_noisy, 0., 1.)

至此数据就准备好了,下面开始搭建网络模型

# 使用一个全连接网络来搭建编码器
encoded1 = Dense(encoding_dim1, activation='relu',input_dim=784,name='Input')
encoded2 = Dense(encoding_dim2, activation='relu',name='encoder_feature1')
encoded3 = Dense(encoding_dim3, activation='relu',name='encoder_feature2')
encoded4 = Dense(encoding_dim4, activation='relu',name='encoder_feature3')
# 使用一个全连接网络来对编码器进行解码
decoded1 = Dense(encoding_dim3, activation='relu',name='decoder_feature1')
decoded2 = Dense(encoding_dim2, activation='relu',name='decoder_feature2')
decoded3 = Dense(encoding_dim1, activation='relu',name='decoder_feature3')
decoded4 = Dense(784, activation='sigmoid',name='Output')
# 构建keras模型
AutoEncoder = Sequential()
AutoEncoder.add(encoded1)
AutoEncoder.add(encoded2)
AutoEncoder.add(encoded3)
AutoEncoder.add(encoded4)
AutoEncoder.add(decoded1)
AutoEncoder.add(decoded2)
AutoEncoder.add(decoded3)
AutoEncoder.add(decoded4)

网络搭好了之后,我们来看一下模型的结构

AutoEncoder.summary()#画出模型的结构图

在这里插入图片描述
接下来就可以编译模型,训练了。
下图是原图和经过自编码器重构图的对比
在这里插入图片描述
可以看出深度自编码器的去噪效果还是挺好的。
接下来我们用tensorboard来查看训练日志。要在训练里加上一句话。

AutoEncoder.fit(x_train_noisy,x_train,epochs=10,batch_size=50,shuffle=True,verbose=1,callbacks=[TensorBoard(log_dir='my_log_dir')])

tensorflow作为后端就会生成一个叫my_log_dir的文件夹来保存训练日志,接着打开windows命令行先输入activate tensorflow1.12.0激活tensorflow虚拟环境,然后输入tensorboard --logdir=E:\MY_Python\AutoEncoder,注意这里的AutoEncoder文件夹是比my_log_dir文件夹大一级的,然后就会出现一个网页连接http://localhost:6006/,将其复制到Microsoft Edge浏览器或者是谷歌浏览器打开即可出现tensorboard界面(注意其他浏览器可能打不开)。
在这里插入图片描述
第一个图是训练集的loss,第二个图是验证集的loss,从第二个图可以看出来训练集的loss一直在减小,但验证集的loss反而在增加了,由此可见是过拟合了。可以加入一些正则化或者是dropout等方法处理。深度自编码器比较基础,还有一些效果出多的比如卷积自编码器、稀疏自编码器等,好了,今天就写到这了,过两天写变分自编码器。
下面是全部代码
https://github.com/JianfengZhang112358/AutoEncoder

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Code provided by Ruslan Salakhutdinov and Geoff Hinton Permission is granted for anyone to copy, use, modify, or distribute this program and accompanying programs and documents for any purpose, provided this copyright notice is retained and prominently displayed, along with a note saying that the original programs are available from our web page. The programs and documents are distributed without any warranty, express or implied. As the programs were written for research purposes only, they have not been tested to the degree that would be advisable in any important application. All use of these programs is entirely at the user's own risk. How to make it work: 1. Create a separate directory and download all these files into the same directory 2. Download from http://yann.lecun.com/exdb/mnist the following 4 files: o train-images-idx3-ubyte.gz o train-labels-idx1-ubyte.gz o t10k-images-idx3-ubyte.gz o t10k-labels-idx1-ubyte.gz 3. Unzip these 4 files by executing: o gunzip train-images-idx3-ubyte.gz o gunzip train-labels-idx1-ubyte.gz o gunzip t10k-images-idx3-ubyte.gz o gunzip t10k-labels-idx1-ubyte.gz If unzipping with WinZip, make sure the file names have not been changed by Winzip. 4. Download Conjugate Gradient code minimize.m 5. Download Autoencoder_Code.tar which contains 13 files OR download each of the following 13 files separately for training an autoencoder and a classification model: o mnistdeepauto.m Main file for training deep autoencoder o mnistclassify.m Main file for training classification model o converter.m Converts raw MNIST digits into matlab format o rbm.m Training RBM with binary hidden and binary visible units o rbmhidlinear.m Training RBM with Gaussian hidden and binary visible units o backprop.m Backpropagation for fine-tuning an autoencoder o backpropclassify.m Backpropagation for classification using "encoder" network o CG_MNIST.m Conjugate Gradient optimization for fine-tuning an autoencoder o CG_CLASSIFY_INIT.m Co

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值