来自:http://deeplearning.net/tutorial/SdA.html#sda

Stacked Denoising Autoencoders (SdA)

note:这部分需要读者读过 (Theano3.3-练习之逻辑回归)和(Theano3.4-练习之多层感知机)。另外会使用到的theano函数和概念: T.tanh, shared variables, basic arithmetic ops, T.grad, Random numbers,floatX.如果你想将代码运行在GPU上,记得看看 GPU.

note:这部分的代码下载地址 here.

堆叠消噪自动编码器(Stacked Denoising Autoencoder,SdA)是堆叠自动编码器的一个扩展 [Bengio07] 在 [Vincent08]有所介绍。

这个教程是建立在之前的一个教程 Denoising Autoencoders翻译上的。特别是如果你没有任何ae的经验的话,我们推荐你先读完之前的部分,再来这里。

一、Stacked Autoencoders

dae可以堆叠起来形成一个深层网络,只要将下一层dae的潜在表征(输出编码)前馈给当前层的输入就行了。这样一个结构的无监督预训练就是一次完成一层来实现的每一层都是作为一个独立的dae进行训练的,一旦前  层训练好了,那么就可以训练第  层了,因为我们现在可以计算下层的编码或者潜在表征了。

一旦所有的层都预训练好了,该网络就可以进行到下一个阶段了:微调。这里说的是有监督微调,即最小化在一个有监督任务上的预测误差。 所以,我们先在这个网络的顶层(也就是输出层的输出编码上)增加一个逻辑回归层。然后训练整个网络就和我们训练一个多层感知机一样 。这时候,我们只考虑每个ae的编码部分。这个阶段是有监督的,因为现在在训练的阶段,我们会用到目标类别。 (见 Multilayer Perceptron 的多层感知机部分。)

这可以在theano中很容易的实现,并重用之前对dae定义的类。我们可以看到堆叠消噪自动编码器有两面(facade):一个ae组成的列表,和一个MLP。在预训练过程中,我们使用第一面(facade),即将模型看成是一个ae组成的列表,然后独立的训练每个ae。在训练的第二阶段,我们使用第二面(facade),这两面可以连接起来是因为:

  • ae可以和MLP的sigmoid层共享参数;
  • 通过MLP的中间层计算得到的潜在的表征可以作为ae的输入。
class SdA(object):"""Stacked denoising auto-encoder class (SdA)A stacked denoising autoencoder model is obtained by stacking severaldAs. The hidden layer of the dA at layer `i` becomes the input ofthe dA at layer `i+1`. The first layer dA gets as input the input ofthe SdA, and the hidden layer of the last dA represents the output.Note that after pretraining, the SdA is dealt with as a normal MLP,the dAs are only used to initialize the weights."""def __init__(self,numpy_rng,theano_rng=None,n_ins=784,hidden_layers_sizes=[500, 500],n_outs=10,corruption_levels=[0.1, 0.1]):""" This class is made to support a variable number of layers.:type numpy_rng: numpy.random.RandomState:param numpy_rng: numpy random number generator used to draw initialweights:type theano_rng: theano.tensor.shared_randomstreams.RandomStreams:param theano_rng: Theano random generator; if None is given one isgenerated based on a seed drawn from `rng`:type n_ins: int:param n_ins: dimension of the input to the sdA:type n_layers_sizes: list of ints:param n_layers_sizes: intermediate layers size, must containat least one value:type n_outs: int:param n_outs: dimension of the output of the network:type corruption_levels: list of float:param corruption_levels: amount of corruption to use for eachlayer"""self.sigmoid_layers = []self.dA_layers = []self.params = []self.n_layers = len(hidden_layers_sizes)assert self.n_layers > 0if not theano_rng:theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))# allocate symbolic variables for the dataself.x = T.matrix('x')  # the data is presented as rasterized imagesself.y = T.ivector('y')  # the labels are presented as 1D vector of# [int] labels

self.sigmoid_layers 将会保存MLP面(facade)的sigmoid层,然而 self.dA_layers 将会保存和MLP层相连接的dae。

下一步,我们构建 n_layers sigmoid 层和n_layers dae,这里 n_layers 表示为模型的深度。我们使用 Multilayer Perceptron中介绍的 HiddenLayer 类:将 tanh 非线性替换成逻辑函数 ). 通过将sigmoid层连接起来形成一个MLP,然后构建dae,使得每个共享权重矩阵和对应于sigmoid层的编码部分偏置 。

        for i in xrange(self.n_layers):# construct the sigmoidal layer# the size of the input is either the number of hidden units of# the layer below or the input size if we are on the first layerif i == 0:input_size = n_inselse:input_size = hidden_layers_sizes[i - 1]# the input to this layer is either the activation of the hidden# layer below or the input of the SdA if you are on the first# layerif i == 0:layer_input = self.xelse:layer_input = self.sigmoid_layers[-1].outputsigmoid_layer = HiddenLayer(rng=numpy_rng,input=layer_input,n_in=input_size,n_out=hidden_layers_sizes[i],activation=T.nnet.sigmoid)# add the layer to our list of layersself.sigmoid_layers.append(sigmoid_layer)# its arguably a philosophical question...# but we are going to only declare that the parameters of the# sigmoid_layers are parameters of the StackedDAA# the visible biases in the dA are parameters of those# dA, but not the SdAself.params.extend(sigmoid_layer.params)# Construct a denoising autoencoder that shared weights with this# layerdA_layer = dA(numpy_rng=numpy_rng,theano_rng=theano_rng,input=layer_input,n_visible=input_size,n_hidden=hidden_layers_sizes[i],W=sigmoid_layer.W,bhid=sigmoid_layer.b)self.dA_layers.append(dA_layer)

现在所需要做的就是在sigmoid层的顶部增加一个逻辑层从而得到一个 MLP.这里会用到(Theano3.3-练习之逻辑回归)中介绍的 LogisticRegression类。

        # We now need to add a logistic layer on top of the MLPself.logLayer = LogisticRegression(input=self.sigmoid_layers[-1].output,n_in=hidden_layers_sizes[-1],n_out=n_outs)self.params.extend(self.logLayer.params)# construct a function that implements one step of finetunining# compute the cost for second phase of training,# defined as the negative log likelihoodself.finetune_cost = self.logLayer.negative_log_likelihood(self.y)# compute the gradients with respect to the model parameters# symbolic variable that points to the number of errors made on the# minibatch given by self.x and self.yself.errors = self.logLayer.errors(self.y)

这个 SdA 类同样提供方法来为当前层中的dae生成训练函数。并返回一个列表,其中的元素  就是实现对应的第 i 层da的一步训练的函数。

    def pretraining_functions(self, train_set_x, batch_size):''' Generates a list of functions, each of them implementing onestep in trainnig the dA corresponding to the layer with same index.The function will require as input the minibatch index, and to traina dA you just need to iterate, calling the corresponding function onall minibatch indexes.:type train_set_x: theano.tensor.TensorType:param train_set_x: Shared variable that contains all datapoints usedfor training the dA:type batch_size: int:param batch_size: size of a [mini]batch:type learning_rate: float:param learning_rate: learning rate used during training for any ofthe dA layers'''# index to a [mini]batchindex = T.lscalar('index')  # index to a minibatch

为了在训练中改变被腐蚀的层或者学习率,我们将它们与theano的变量联系起来:

        corruption_level = T.scalar('corruption')  # % of corruption to uselearning_rate = T.scalar('lr')  # learning rate to use# begining of a batch, given `index`batch_begin = index * batch_size# ending of a batch given `index`batch_end = batch_begin + batch_sizepretrain_fns = []for dA in self.dA_layers:# get the cost and the updates listcost, updates = dA.get_cost_updates(corruption_level,learning_rate)# compile the theano functionfn = theano.function(inputs=[index,theano.Param(corruption_level, default=0.2),theano.Param(learning_rate, default=0.1)],outputs=cost,updates=updates,givens={self.x: train_set_x[batch_begin: batch_end]})# append `fn` to the list of functionspretrain_fns.append(fn)return pretrain_fns

现在任何函数 pretrain_fns[i] 会被当作参数index 和可选的 corruption—腐蚀的程度,或者 lr—学习率。注意到 参数的名字在被构建的时候都是被传入theano 变量的名称,而不是python变量 (learning_rate 或者 corruption_level).。当使用theano的时候,这些是需要记住的。

我们以同样的风格来建立在微调的时候函数的方法 (train_fnvalid_score 和test_score).

    def build_finetune_functions(self, datasets, batch_size, learning_rate):'''Generates a function `train` that implements one step offinetuning, a function `validate` that computes the error ona batch from the validation set, and a function `test` thatcomputes the error on a batch from the testing set:type datasets: list of pairs of theano.tensor.TensorType:param datasets: It is a list that contain all the datasets;the has to contain three pairs, `train`,`valid`, `test` in this order, where each pairis formed of two Theano variables, one for thedatapoints, the other for the labels:type batch_size: int:param batch_size: size of a minibatch:type learning_rate: float:param learning_rate: learning rate used during finetune stage'''(train_set_x, train_set_y) = datasets[0](valid_set_x, valid_set_y) = datasets[1](test_set_x, test_set_y) = datasets[2]# compute number of minibatches for training, validation and testingn_valid_batches = valid_set_x.get_value(borrow=True).shape[0]n_valid_batches /= batch_sizen_test_batches = test_set_x.get_value(borrow=True).shape[0]n_test_batches /= batch_sizeindex = T.lscalar('index')  # index to a [mini]batch# compute the gradients with respect to the model parametersgparams = T.grad(self.finetune_cost, self.params)# compute list of fine-tuning updatesupdates = [(param, param - gparam * learning_rate)for param, gparam in zip(self.params, gparams)]train_fn = theano.function(inputs=[index],outputs=self.finetune_cost,updates=updates,givens={self.x: train_set_x[index * batch_size: (index + 1) * batch_size],self.y: train_set_y[index * batch_size: (index + 1) * batch_size]},name='train')test_score_i = theano.function([index],self.errors,givens={self.x: test_set_x[index * batch_size: (index + 1) * batch_size],self.y: test_set_y[index * batch_size: (index + 1) * batch_size]},name='test')valid_score_i = theano.function([index],self.errors,givens={self.x: valid_set_x[index * batch_size: (index + 1) * batch_size],self.y: valid_set_y[index * batch_size: (index + 1) * batch_size]},name='valid')# Create a function that scans the entire validation setdef valid_score():return [valid_score_i(i) for i in xrange(n_valid_batches)]# Create a function that scans the entire test setdef test_score():return [test_score_i(i) for i in xrange(n_test_batches)]return train_fn, valid_score, test_score

注意到 valid_score 和 test_score 都不是theano函数,而是python函数,分别用在整个验证集和整个测试集上的,从而在这些集合上生成损失值列表。

二、Putting it all together

下面的代码是用来构建sda的:

    numpy_rng = numpy.random.RandomState(89677)print '... building the model'# construct the stacked denoising autoencoder classsda = SdA(numpy_rng=numpy_rng,n_ins=28 * 28,hidden_layers_sizes=[1000, 1000, 1000],n_outs=10)

在训练这个网络的时候有两个阶段:逐层预训练,然后是微调。

对于预训练阶段来说,我们会在网络的所有层上进行循环。对于每一层来说,我们会使用编译后的theano函数来实现一个SGD步长,从而优化权重来减少这一层的重构误差。该函数将会被用在给定epochs pretraining_epochs的训练集上。

    ########################## PRETRAINING THE MODEL ##########################print '... getting the pretraining functions'pretraining_fns = sda.pretraining_functions(train_set_x=train_set_x,batch_size=batch_size)print '... pre-training the model'start_time = time.clock()## Pre-train layer-wisecorruption_levels = [.1, .2, .3]for i in xrange(sda.n_layers):# go through pretraining epochsfor epoch in xrange(pretraining_epochs):# go through the training setc = []for batch_index in xrange(n_train_batches):c.append(pretraining_fns[i](index=batch_index,corruption=corruption_levels[i],lr=pretrain_lr))print 'Pre-training layer %i, epoch %d, cost ' % (i, epoch),print numpy.mean(c)end_time = time.clock()print >> sys.stderr, ('The pretraining code for file ' +os.path.split(__file__)[1] +' ran for %.2fm' % ((end_time - start_time) / 60.))

微调循环类似于MLP中对应的操作。唯一的差别在于它使用build_finetune_functions给定的函数。

三、Running the Code

用户可以通过下面方式来运行代码:

python code/SdA.py

默认情况下,该代码对每一层运行15次预训练, batch size 为 1。对于第一层来说,腐蚀程度为0.1,第二层为 0.2,第三层为 0.3。预训练的学习率为 0.001 微调的学习率为0.1。预训练花费的时间为 585.01 分钟,也就是每一个epoch需要13分钟。微调是在444.2分钟内36个epochs下完成的,也就是每个epoch为12.34分钟。。最后得到的验证集错误率为 1.39% ,测试集的错误率为 1.3%。该实验在Intel Xeon E5430 @ 2.66GHz CPU上完成,并且是单线程的 GotoBLAS.

四、Tips and Tricks

一个提升你代码的运行时间的方法 (假设你有足够的内存可用), 就是计算如何用这个网络(从底层到k-1层)转换你的数据。即,通过训练第一层dA开始。一旦训练好了,你可以计算你数据集中的每一个数据点的隐藏单元的值,然后将这个存储作为一个新的数据集从而用来训练第2层对应的dA。一旦你训练好了第2层的dA,以之前相似的,将其作为第三层的数据集。你现在会发现,其实dAs都是相互独立训练的,然后只是对输入进行非线性转换。一旦所有的dAs都训练好了,你就可以对整个模型进行微调了。

转载于:https://www.cnblogs.com/shouhuxianjian/p/4590235.html

Theano3.7-练习之堆叠消噪自动编码器相关推荐

  1. bose耳机信号断续_最强真无线降噪耳机?BOSE QC Earbuds消噪耳塞上手评测

    鸽了这么久,实在抱歉~ 千呼万唤始出来,BOSE终于在2020年的下半年终于更新了其旗下的真无线降噪耳机,这也是bose的首款真无线降噪耳机.bose作为降噪耳机的"始祖",拥有了 ...

  2. matlab 小波 cdd,[Matlab] 单导联心电数据的小波(包)消噪及压缩

    % 用小波(包)对MitbihCmprTstExample_08730_01(软硬阈值)进行消噪与压缩 clear all;clc;close all; disp('用小波(包)对MitbihCmpr ...

  3. Bose 700无线消噪耳机评测:让用户不受打扰是它最大的温柔

    现在生活中充满各种噪声,人们在大多情况下是避之唯恐不及.降噪耳机的出现,拯救需要安静的用户于水火之中.Bose虽然作为降噪耳机中的鼻祖,却也一直受到其他品牌的挑战,但它坚持外观和功能上的不断创新,使它 ...

  4. 科技新品 | 索尼最新高级条形音箱;Bose消噪耳塞全新配色;新一代人工智能社交机器人Musio S...

    "科技新产品动态"栏目把新鲜的具有代表性的科学产品带到您眼前,涉及消费电子,半导体.服务器.智能家电等众多品类,提供图片和简单的文字介绍. 索尼推出最新高级条形音箱HT-A5000 ...

  5. 数字信号处理中小波消噪原理、应用及代码实现

    ​​​​​ 目 录 1. 小波消噪原理 2. 小波阈值消噪步骤 ​​​​​​3. 参数选择 (1)小波基的选择 (2)分解尺度的选择 (3) 阈值的选择 (4)阈值函数的选择 4. 语音消噪中的实例运 ...

  6. 即构SDK5月迭代:新增声道选择、网络探测、智能消噪等功能,打造更优的视听体验

    即构SDK5月份的迭代更新如期而至,本月互动视频(LiveRoom).实时语音(AudioRoom)两大SDK以及录制插件(PlayRecord)均有新功能上线.新增的声道选择.变调控制.智能消噪.枚 ...

  7. 舰船辐射噪声 matlab,基于MATLAB的舰船辐射噪声信号小波消噪处理

    基于MATLAB的舰船辐射噪声信号小波消噪处理 针对舰船辐射噪声信号的特点提出了小波消噪的方法,对小波消噪理论作了简要的阐述,并设计了一种消噪方案,最后利用MATLAB,在计算机 (本文共4页) 阅读 ...

  8. 极低噪声幻像电源如何设计?详细原理图和三种消噪方法拿走不谢

    极低噪声幻像电源如何设计?详细原理图和三种消噪方法拿走不谢 原创 ADI 亚德诺半导体 2022-04-19 11:48 极低噪声幻像电源如何设计?详细原理图和三种消噪方法拿走不谢 Q: 是否可以利用 ...

  9. matlab神经网络预测太阳黑子,基于小波消噪与BP神经网络的太阳黑子时间序列预测...

    摘要: 现实世界中的时间序列通常包含多种不确定性因素,所以很难用传统的线性模型建模.预测.且含噪声时间序列的非线性预测是自然界和社会科学中普遍存在的问题,解决这一问题不仅具有理论意义而且有广泛的应用前 ...

最新文章

  1. Win7 IIS7 ASP.NET MVC3.0 部署问题
  2. html5渲染,HTML的渲染过程
  3. dingding post POST请求
  4. 【爬虫剑谱】三卷3章 拾遗篇-有关于bs4库中的BeautifulSoup模块使用小结
  5. 北京大学生物信息学(8)
  6. Hibernate之HQL查询
  7. revit2016与2017区别_revit2017下载及新功能介绍
  8. java quartz配置_Quartz配置参考
  9. 调用python-nmap实现扫描局域网存活主机
  10. 如何提升自己(一) 谈学习
  11. Android ScrollView 长截屏
  12. Linux预科知识篇之认识计算机
  13. 解决:dependencies.dependency.version' for com.google.guava:guava:jar is missing.
  14. 西瓜视频官方缩短域名网址接口v.ixigua.com生成原理解析
  15. Run-Time Check Failure #3 - The variable 'p' is being used without being initialized.
  16. bms中soh计算方式_电动汽车BMS中SOH和SOP估算策略总结
  17. props 更新.使用规则
  18. Python 操作配置文件
  19. 多线程处理十万百万级List(大list处理)
  20. mac下用browser-sync 做一个webapp的手机端的测试

热门文章

  1. erlang 编译 安装
  2. MS SQL 2008认证考试大纲
  3. 伦敦帝国学院M+T实验室,全奖博士招生
  4. 医疗设备-呼吸机-数据解析
  5. 【OpenCV】OpenCV函数精讲之 -- copyTo()函数及Mask详解(附代码详解)
  6. 干货|基于深度学习的目标检测算法面试必备(RCNN~YOLOv5)
  7. 经验 | 计算机专业科班出身如何提高自己编程能力?
  8. 21 张让你代码能力突飞猛进的速查表(神经网络、线性代数、可视化等)
  9. CornerNet: 将目标检测问题视作关键点检测与配对
  10. 数据部门如何All In AI