Stacked Denoising Autoencoders (SDAE)

教程地址:http://www.deeplearning.net/tutorial/SdA.html

The Stacked Denoising Autoencoder (SdA) is an extension of the stacked autoencoder[Bengio07]and it was introduced in [Vincent08].

推荐先看这个UFLDL,讲得很详细:http://deeplearning.stanford.edu/wiki/index.php/%E6%A0%88%E5%BC%8F%E8%87%AA%E7%BC%96%E7%A0%81%E7%AE%97%E6%B3%95

我之前一个做的UFLDL中栈式自编码的作业(但是不是降噪自编码,是稀疏自编码):UFLDL教程答案(6):Exercise:Implement deep networks for digit classification

Stacked Autoencoders

回顾下自编码:(目标是输出层(重构层)尽量重构输入,即与输入尽量一致,那么隐藏层就相当于抓住了数据的内在特征)

Stacked SparseAE Features1.png

降噪自编码器可以一层一层堆叠成栈式自编码器,(重构层去掉),前一层的隐层输出(latent representation (output code))为后一自编码层的输入下图是UFLDL中两层自编码层堆叠的图例:

Stacked Combined.png

逐层贪婪训练:每层自编码层都单独进行非监督训练,以最小化输入(输入为前一层的隐层输出)与重构结果之间的误差为训练目标。前K层训练好了,就可以训练K+1层,因为已经前向传播求出K层的输出,再用K层的输出当作K+1的输入训练K+1层。

在所有自编码层都完成预训练之后,在对网络进行微调,即用样本进行有监督训练,在网络最后一层加上一层logistic regression layer(softmax层),像Multilayer Perceptron那样训练。

 

构建class SdA

下面这段是如何利用之前实现的LR,MLP,DA等构建SDA

We can see the stacked denoising autoencoder(sda) as having two facades: a list of autoencoders, and an MLP.

在预训练时,sda可以看作很多个自编码器相连,逐个无监督训练;

在微调时,sda可以看作一个多层感知器进行有监督训练。

[python]  view plain  copy
  1. sigmoid_layer = HiddenLayer(rng=numpy_rng,  
  2.                                         input=layer_input,  
  3.                                         n_in=input_size,  
  4.                                         n_out=hidden_layers_sizes[i],  
  5.                                         activation=T.nnet.sigmoid)  
[python]  view plain  copy
  1. dA_layer = dA(numpy_rng=numpy_rng,  
  2.                           theano_rng=theano_rng,  
  3.                           input=layer_input,  
  4.                           n_visible=input_size,  
  5.                           n_hidden=hidden_layers_sizes[i],  
  6.                           W=sigmoid_layer.W,  
  7.                           bhid=sigmoid_layer.b)  

代码中可以看出:自编码层和多层感知器的隐层其实是共用的权重,其实每层既是dA,又是HiddenLayer,这样就可以使用之前的mlp.py,dA.py来实现sda。

[python]  view plain  copy
  1. class SdA(object):  
  2.     """Stacked denoising auto-encoder class (SdA) 
  3.  
  4.     A stacked denoising autoencoder model is obtained by stacking several 
  5.     dAs. The hidden layer of the dA at layer `i` becomes the input of 
  6.     the dA at layer `i+1`. The first layer dA gets as input the input of 
  7.     the SdA, and the hidden layer of the last dA represents the output. 
  8.     Note that after pretraining, the SdA is dealt with as a normal MLP, 
  9.     the dAs are only used to initialize the weights. 
  10.     """  
  11.   
  12.     def __init__(  
  13.         self,  
  14.         numpy_rng,  
  15.         theano_rng=None,  
  16.         n_ins=784,  
  17.         hidden_layers_sizes=[500500],  
  18.         n_outs=10,  
  19.         corruption_levels=[0.10.1]  
  20.     ):  
  21.         """ This class is made to support a variable number of layers. 
  22.  
  23.         :type numpy_rng: numpy.random.RandomState 
  24.         :param numpy_rng: numpy random number generator used to draw initial 
  25.                     weights 
  26.  
  27.         :type theano_rng: theano.tensor.shared_randomstreams.RandomStreams 
  28.         :param theano_rng: Theano random generator; if None is given one is 
  29.                            generated based on a seed drawn from `rng` 
  30.  
  31.         :type n_ins: int 
  32.         :param n_ins: dimension of the input to the sdA 
  33.  
  34.         :type n_layers_sizes: list of ints 
  35.         :param n_layers_sizes: intermediate layers size, must contain 
  36.                                at least one value 
  37.  
  38.         :type n_outs: int 
  39.         :param n_outs: dimension of the output of the network 
  40.  
  41.         :type corruption_levels: list of float 
  42.         :param corruption_levels: amount of corruption to use for each 
  43.                                   layer 
  44.         """  
  45.   
  46.         self.sigmoid_layers = []  
  47.         self.dA_layers = []  
  48.         self.params = []  
  49.         self.n_layers = len(hidden_layers_sizes)  
  50.   
  51.         assert self.n_layers > 0  
  52.   
  53.         if not theano_rng:  
  54.             theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))  
  55.         # allocate symbolic variables for the data  
  56.         self.x = T.matrix('x')  # the data is presented as rasterized images  
  57.         self.y = T.ivector('y')  # the labels are presented as 1D vector of  
  58.                                  # [int] labels  

 

Next, we construct n_layers sigmoid layers and n_layers denoising autoencoders, where n_layers is the depth of our model.
注意:MLP隐层稍微有点修改激活函数把tanh函数改成了sigmoid函数

[python]  view plain  copy
  1. # start-snippet-2  
  2.         for i in xrange(self.n_layers):  
  3.             # construct the sigmoidal layer  
  4.   
  5.             # the size of the input is either the number of hidden units of  
  6.             # the layer below or the input size if we are on the first layer  
  7.             if i == 0:  
  8.                 input_size = n_ins  
  9.             else:  
  10.                 input_size = hidden_layers_sizes[i - 1]  
  11.   
  12.             # the input to this layer is either the activation of the hidden  
  13.             # layer below or the input of the SdA if you are on the first  
  14.             # layer  
  15.             if i == 0:  
  16.                 layer_input = self.x  
  17.             else:  
  18.                 layer_input = self.sigmoid_layers[-1].output  
  19.   
  20.             sigmoid_layer = HiddenLayer(rng=numpy_rng,  
  21.                                         input=layer_input,  
  22.                                         n_in=input_size,  
  23.                                         n_out=hidden_layers_sizes[i],  
  24.                                         activation=T.nnet.sigmoid)  
  25.             # add the layer to our list of layers  
  26.             self.sigmoid_layers.append(sigmoid_layer)  
  27.             # its arguably a philosophical question...  
  28.             # but we are going to only declare that the parameters of the  
  29.             # sigmoid_layers are parameters of the StackedDAA  
  30.             # the visible biases in the dA are parameters of those  
  31.             # dA, but not the SdA  
  32.             self.params.extend(sigmoid_layer.params)  
  33.   
  34.             # Construct a denoising autoencoder that shared weights with this  
  35.             # layer  
  36.             dA_layer = dA(numpy_rng=numpy_rng,  
  37.                           theano_rng=theano_rng,  
  38.                           input=layer_input,  
  39.                           n_visible=input_size,  
  40.                           n_hidden=hidden_layers_sizes[i],  
  41.                           W=sigmoid_layer.W,  
  42.                           bhid=sigmoid_layer.b)  
  43.             self.dA_layers.append(dA_layer)  
  44.         # end-snippet-2  
  45.         # We now need to add a logistic layer on top of the MLP  
  46.         self.logLayer = LogisticRegression(  
  47.             input=self.sigmoid_layers[-1].output,  
  48.             n_in=hidden_layers_sizes[-1],  
  49.             n_out=n_outs  
  50.         )  
  51.   
  52.         self.params.extend(self.logLayer.params)  
  53.         # construct a function that implements one step of finetunining  
  54.   
  55.         # compute the cost for second phase of training,  
  56.         # defined as the negative log likelihood  
  57.         self.finetune_cost = self.logLayer.negative_log_likelihood(self.y)  
  58.         # compute the gradients with respect to the model parameters  
  59.         # symbolic variable that points to the number of errors made on the  
  60.         # minibatch given by self.x and self.y  
  61.         self.errors = self.logLayer.errors(self.y)  

 

再加一个softmax层(2类问题时就是logistic layer)

[python]  view plain  copy
  1. # We now need to add a logistic layer on top of the MLP  
  2.        self.logLayer = LogisticRegression(  
  3.            input=self.sigmoid_layers[-1].output,  
  4.            n_in=hidden_layers_sizes[-1],  
  5.            n_out=n_outs  
  6.        )  
  7.   
  8.        self.params.extend(self.logLayer.params)  
  9.        # construct a function that implements one step of finetunining  
  10.   
  11.        # compute the cost for second phase of training,  
  12.        # defined as the negative log likelihood  
  13.        self.finetune_cost = self.logLayer.negative_log_likelihood(self.y)  
  14.        # compute the gradients with respect to the model parameters  
  15.        # symbolic variable that points to the number of errors made on the  
  16.        # minibatch given by self.x and self.y  
  17.        self.errors = self.logLayer.errors(self.y)  

 

构建Theano function:各层的预训练函数


下面这个函数是为每 i 层生成一个Theano function类型的预训练函数,返回值是一个list,list中就是每层的预训练函数
为了能在训练中更改 the corruption level or the learning rate ,我们定义Theano variables。

[python]  view plain  copy
  1. def pretraining_functions(self, train_set_x, batch_size):  
  2.         ''''' Generates a list of functions, each of them implementing one 
  3.         step in trainnig the dA corresponding to the layer with same index. 
  4.         The function will require as input the minibatch index, and to train 
  5.         a dA you just need to iterate, calling the corresponding function on 
  6.         all minibatch indexes. 
  7.  
  8.         :type train_set_x: theano.tensor.TensorType 
  9.         :param train_set_x: Shared variable that contains all datapoints used 
  10.                             for training the dA 
  11.  
  12.         :type batch_size: int 
  13.         :param batch_size: size of a [mini]batch 
  14.  
  15.         :type learning_rate: float 
  16.         :param learning_rate: learning rate used during training for any of 
  17.                               the dA layers 
  18.         '''  
  19.   
  20.         # index to a [mini]batch  
  21.         index = T.lscalar('index')  # index to a minibatch  
  22. corruption_level = T.scalar('corruption')  # % of corruption to use  
  23.         learning_rate = T.scalar('lr')  # learning rate to use  
  24.         # begining of a batch, given `index`  
  25.         batch_begin = index * batch_size  
  26.         # ending of a batch given `index`  
  27.         batch_end = batch_begin + batch_size  
  28.   
  29.         pretrain_fns = []  
  30.         for dA in self.dA_layers:  
  31.             # get the cost and the updates list  
  32.             cost, updates = dA.get_cost_updates(corruption_level,  
  33.                                                 learning_rate)  
  34.             # compile the theano function  
  35.             fn = theano.function(  
  36.                 inputs=[  
  37.                     index,  
  38.                     theano.Param(corruption_level, default=0.2),  
  39.                     theano.Param(learning_rate, default=0.1)  
  40.                 ],  
  41.                 outputs=cost,  
  42.                 updates=updates,  
  43.                 givens={  
  44.                     self.x: train_set_x[batch_begin: batch_end]  
  45.                 }  
  46.             )  
  47.             # append `fn` to the list of functions  
  48.             pretrain_fns.append(fn)  
  49.   
  50.         return pretrain_fns  

 

每层的训预练函数为 pretrain_fns[i] ,输入为 index (minibatch的序号,即这次训练用第index个minibatch),此外还有两个Theano 变量可以修改:corruption—the corruption level or lr—the learning rate.

构建Theano function:微调部分所需函数

3个函数:train_fn,valid_score,test_score。其中valid_score,test_score是python 函数,不是Theano function,是用来训练过程中计算损失的。

[python]  view plain  copy
  1. def build_finetune_functions(self, datasets, batch_size, learning_rate):  
  2.         '''''Generates a function `train` that implements one step of 
  3.         finetuning, a function `validate` that computes the error on 
  4.         a batch from the validation set, and a function `test` that 
  5.         computes the error on a batch from the testing set 
  6.  
  7.         :type datasets: list of pairs of theano.tensor.TensorType 
  8.         :param datasets: It is a list that contain all the datasets; 
  9.                          the has to contain three pairs, `train`, 
  10.                          `valid`, `test` in this order, where each pair 
  11.                          is formed of two Theano variables, one for the 
  12.                          datapoints, the other for the labels 
  13.  
  14.         :type batch_size: int 
  15.         :param batch_size: size of a minibatch 
  16.  
  17.         :type learning_rate: float 
  18.         :param learning_rate: learning rate used during finetune stage 
  19.         '''  
  20.   
  21.         (train_set_x, train_set_y) = datasets[0]  
  22.         (valid_set_x, valid_set_y) = datasets[1]  
  23.         (test_set_x, test_set_y) = datasets[2]  
  24.   
  25.         # compute number of minibatches for training, validation and testing  
  26.         n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]  
  27.         n_valid_batches /= batch_size  
  28.         n_test_batches = test_set_x.get_value(borrow=True).shape[0]  
  29.         n_test_batches /= batch_size  
  30.   
  31.         index = T.lscalar('index')  # index to a [mini]batch  
  32.   
  33.         # compute the gradients with respect to the model parameters  
  34.         gparams = T.grad(self.finetune_cost, self.params)  
  35.   
  36.         # compute list of fine-tuning updates  
  37.         updates = [  
  38.             (param, param - gparam * learning_rate)  
  39.             for param, gparam in zip(self.params, gparams)  
  40.         ]  
  41.   
  42.         train_fn = theano.function(  
  43.             inputs=[index],  
  44.             outputs=self.finetune_cost,  
  45.             updates=updates,  
  46.             givens={  
  47.                 self.x: train_set_x[  
  48.                     index * batch_size: (index + 1) * batch_size  
  49.                 ],  
  50.                 self.y: train_set_y[  
  51.                     index * batch_size: (index + 1) * batch_size  
  52.                 ]  
  53.             },  
  54.             name='train'  
  55.         )  
  56.   
  57.         test_score_i = theano.function(  
  58.             [index],  
  59.             self.errors,  
  60.             givens={  
  61.                 self.x: test_set_x[  
  62.                     index * batch_size: (index + 1) * batch_size  
  63.                 ],  
  64.                 self.y: test_set_y[  
  65.                     index * batch_size: (index + 1) * batch_size  
  66.                 ]  
  67.             },  
  68.             name='test'  
  69.         )  
  70.   
  71.         valid_score_i = theano.function(  
  72.             [index],  
  73.             self.errors,  
  74.             givens={  
  75.                 self.x: valid_set_x[  
  76.                     index * batch_size: (index + 1) * batch_size  
  77.                 ],  
  78.                 self.y: valid_set_y[  
  79.                     index * batch_size: (index + 1) * batch_size  
  80.                 ]  
  81.             },  
  82.             name='valid'  
  83.         )  
  84.   
  85.         # Create a function that scans the entire validation set  
  86.         def valid_score():  
  87.             return [valid_score_i(i) for i in xrange(n_valid_batches)]  
  88.   
  89.         # Create a function that scans the entire test set  
  90.         def test_score():  
  91.             return [test_score_i(i) for i in xrange(n_test_batches)]  
  92.   
  93.         return train_fn, valid_score, test_score  


Putting it all together

使用 class SdA 构造:

[python]  view plain  copy
  1. numpy_rng = numpy.random.RandomState(89677)  
  2.     print '... building the model'  
  3.     # construct the stacked denoising autoencoder class  
  4.     sda = SdA(  
  5.         numpy_rng=numpy_rng,  
  6.         n_ins=28 * 28,  
  7.         hidden_layers_sizes=[100010001000],  
  8.         n_outs=10  
  9.     )  

 

训练分为两部分:逐层预训练(自编码);微调(与MLP训练一样)。

第一部分:我们对所有层逐层训练,对每层,我们用 the compiled Theano function (pretrain_fns[ i ])利用随机梯度下降(SGD),以最小化重构误差为优化目标,
训练优化当前层权重。根据 pretraining_epochs 决定训练集迭代多少次。

[python]  view plain  copy
  1. #########################  
  2.     # PRETRAINING THE MODEL #  
  3.     #########################  
  4.     print '... getting the pretraining functions'  
  5.     pretraining_fns = sda.pretraining_functions(train_set_x=train_set_x,  
  6.                                                 batch_size=batch_size)  
  7.   
  8.     print '... pre-training the model'  
  9.     start_time = timeit.default_timer()  
  10.     ## Pre-train layer-wise  
  11.     corruption_levels = [.1, .2, .3]  
  12.     for i in xrange(sda.n_layers):  
  13.         # go through pretraining epochs  
  14.         for epoch in xrange(pretraining_epochs):  
  15.             # go through the training set  
  16.             c = []  
  17.             for batch_index in xrange(n_train_batches):  
  18.                 c.append(pretraining_fns[i](index=batch_index,  
  19.                          corruption=corruption_levels[i],  
  20.                          lr=pretrain_lr))  
  21.             print 'Pre-training layer %i, epoch %d, cost ' % (i, epoch),  
  22.             print numpy.mean(c)  
  23.   
  24.     end_time = timeit.default_timer()  
  25.   
  26.     print >> sys.stderr, ('The pretraining code for file ' +  
  27.                           os.path.split(__file__)[1] +  
  28.                           ' ran for %.2fm' % ((end_time - start_time) / 60.))  


第二部分:微调和 Multilayer Perceptron 中的训练基本一样,唯一不同是这里使用了 build_finetune_functions,更加简洁方便。

Running the Code

By default the code runs 15 pre-training epochs for each layer, with a batch size of 1. The corruption levels are 0.1 for the first layer, 0.2 for the second, and 0.3 for the third. The pretraining learning rate is 0.001 and the finetuning learning rate is 0.1. Pre-training takes 585.01 minutes, with an average of 13 minutes per epoch. Fine-tuning is completed after 36 epochs in 444.2 minutes, with an average of 12.34 minutes per epoch. The final validation score is 1.39% with a testing score of 1.3%.

UFLDL教程答案(6):Exercise:Implement deep networks for digit classification 结果对比:UFLDL中为2.26%,这里为1.3%,这里比UFLDL效果好一些。

结果不同的可能原因:

1.这里是降噪自编码,UFLDL中是稀疏自编码。

2.这里是3个隐藏层(自编码层),UFLDL中只有2个。

3.但是需要注意的是,UFLDL中把mnist数据中的train set 和 validation set 都合成了一个train set ,样本量有60000个,比这里多了一个validation set的数量。

 

Tips and Tricks

这个 trick 主要应用于预训练,预训练一层完成后,把数据通过这层,前向传播算得这层的输出,然后把输出记录下来(你得有足够内存)。再预训练后面一层,训练好后,又用刚才记录的数据输入这层,得到前向传播输出,再记录下来。。。。

这样把所有层都预训练一遍,如果为n层,只需要计算n此前向传播,不用每层都得把数据从第一层前向传播过来

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值