|
5 | 5 |
|
6 | 6 | 本节的所有代码可以在[这里](http://deeplearning.net/tutorial/code/SdA.py)下载。 |
7 | 7 |
|
8 | | -层叠降噪自动编码机(Stacked Denoising Autoencoder,SdA)是层叠自动编码机([Bengio](http://deeplearning.net/tutorial/references.html#bengio07))的一个扩展,在[Vincent08](http://deeplearning.net/tutorial/references.html#vincent08)中被介绍。 |
| 8 | +层叠降噪自动编码机(Stacked Denoising Autoencoder,SdA)是层叠自动编码机([Bengio07](http://deeplearning.net/tutorial/references.html#bengio07))的一个扩展,在[Vincent08](http://deeplearning.net/tutorial/references.html#vincent08)中被介绍。 |
9 | 9 |
|
10 | | -这个教程建立在前一个[降噪自动编码机](https://github.com/Syndrome777/DeepLearningTutorial/blob/master/5_Denoising_Autoencoders_降噪自动编码.md)。我们建议,对于没有自动编码机经验的人应该阅读上述章节。 |
| 10 | +这个教程建立在前一个[降噪自动编码机](https://github.com/Syndrome777/DeepLearningTutorial/blob/master/5_Denoising_Autoencoders_降噪自动编码.md)之上。我们建议,对于没有自动编码机经验的人应该阅读上述章节。 |
11 | 11 |
|
12 | 12 | ###层叠自动编码机 |
13 | | -降噪自动编码机可以被叠加起来形成一个深度网络,通过反馈前一层的降噪自动编码机的潜在表达(输出编码)作为当前层的输入。这个非监督的预学习结构一次只能学习一个层。每一层都被作为一个降噪自动编码机以最小化重构误差来进行训练。当前k个层被训练完了,我们可以进行k+1层的训练,因此此时我们才可以计算前一层的编码和潜在表达。当所有的层都被训练了,整个网络进行第二阶段训练,称为微调(fine-tuning)。这里,我们考虑监督微调,当我们需要最小化一个监督任务的预测误差吧。为此我们现在网络的顶端添加一个逻辑回归层(是输出层的编码更加精确)。然后我们像训练多层感知器一样训练整个网络。这里,我们考虑每个自动编码的机的编码模块。这个阶段是有监督的,因为我们在训练的时候使用了目标类别(更多细节请看[多层感知机](https://github.com/Syndrome777/DeepLearningTutorial/blob/master/3_Multilayer_Perceptron_多层感知机.md)) |
| 13 | +降噪自动编码机可以被叠加起来形成一个深度网络,通过反馈前一层的降噪自动编码机的潜在表达(输出编码)作为当前层的输入。这个非监督的预学习结构一次只能学习一个层。每一层都被作为一个降噪自动编码机以最小化重构误差来进行训练。当前k个层被训练完了,我们可以进行k+1层的训练,因此此时我们才可以计算前一层的编码和潜在表达。当所有的层都被训练了,整个网络进行第二阶段训练,称为微调(fine-tuning)。这里,我们考虑监督微调,当我们需要最小化一个监督任务的预测误差吧。为此我们现在网络的顶端添加一个逻辑回归层(使输出层的编码更加精确)。然后我们像训练多层感知器一样训练整个网络。这里,我们考虑每个自动编码的机的编码模块。这个阶段是有监督的,因为我们在训练的时候使用了目标类别(更多细节请看[多层感知机](https://github.com/Syndrome777/DeepLearningTutorial/blob/master/3_Multilayer_Perceptron_多层感知机.md)) |
14 | 14 |
|
15 | 15 | 这在Theano里面,使用之前定义的降噪自动编码机,可以轻易的被实现。我们可以将层叠降噪自动编码机看作两部分,一个是自动编码机链表,另一个是一个多层感知机。在预训练阶段,我们使用了第一部分,例如我们将模型看作一系列的自动编码机,然后分别训练每一个自动编码机。在第二阶段,我们使用第二部分。这个两个部分通过分享参数来实现连接。 |
16 | 16 |
|
@@ -76,9 +76,74 @@ class SdA(object): |
76 | 76 | ``` |
77 | 77 | `self.sigmoid_layers`将会储存多层感知机的sigmoid层,`self.dA_layers`将会储存连接多层感知机层的降噪自动编码机。 |
78 | 78 |
|
| 79 | +下一步,我们构建`n_layers`个sigmoid层(我们使用在[多层感知机](https://github.com/Syndrome777/DeepLearningTutorial/blob/master/3_Multilayer_Perceptron_多层感知机.md)中介绍的`HiddenLayer`类,唯一的更改是将原本的非线性函数`tanh`换成了logistic函数s=1/(1+exp(-x)))和`n_layers`个降噪自动编码机,当然`n_layers`就是我们模型的深度。我们连接sigmoid函数,使得他们形成一个MLP,构建每一个自动编码机和他们对应的sigmoid层,去共享编码部分的权值矩阵和偏执 |
79 | 80 |
|
| 81 | +```Python |
| 82 | + for i in xrange(self.n_layers): |
| 83 | + # construct the sigmoidal layer |
| 84 | + |
| 85 | + # the size of the input is either the number of hidden units of |
| 86 | + # the layer below or the input size if we are on the first layer |
| 87 | + if i == 0: |
| 88 | + input_size = n_ins |
| 89 | + else: |
| 90 | + input_size = hidden_layers_sizes[i - 1] |
| 91 | + |
| 92 | + # the input to this layer is either the activation of the hidden |
| 93 | + # layer below or the input of the SdA if you are on the first |
| 94 | + # layer |
| 95 | + if i == 0: |
| 96 | + layer_input = self.x |
| 97 | + else: |
| 98 | + layer_input = self.sigmoid_layers[-1].output |
| 99 | + |
| 100 | + sigmoid_layer = HiddenLayer(rng=numpy_rng, |
| 101 | + input=layer_input, |
| 102 | + n_in=input_size, |
| 103 | + n_out=hidden_layers_sizes[i], |
| 104 | + activation=T.nnet.sigmoid) |
| 105 | + # add the layer to our list of layers |
| 106 | + self.sigmoid_layers.append(sigmoid_layer) |
| 107 | + # its arguably a philosophical question... |
| 108 | + # but we are going to only declare that the parameters of the |
| 109 | + # sigmoid_layers are parameters of the StackedDAA |
| 110 | + # the visible biases in the dA are parameters of those |
| 111 | + # dA, but not the SdA |
| 112 | + self.params.extend(sigmoid_layer.params) |
| 113 | + |
| 114 | + # Construct a denoising autoencoder that shared weights with this |
| 115 | + # layer |
| 116 | + dA_layer = dA(numpy_rng=numpy_rng, |
| 117 | + theano_rng=theano_rng, |
| 118 | + input=layer_input, |
| 119 | + n_visible=input_size, |
| 120 | + n_hidden=hidden_layers_sizes[i], |
| 121 | + W=sigmoid_layer.W, |
| 122 | + bhid=sigmoid_layer.b) |
| 123 | + self.dA_layers.append(dA_layer) |
| 124 | +``` |
80 | 125 |
|
| 126 | +现在,我们需要在sigmoid层的上方添加逻辑层,所以我们将有一个MLP。我们将使用在[使用逻辑回归进MNIST分类](https://github.com/Syndrome777/DeepLearningTutorial/blob/master/2_Classifying_MNIST_using_LR_逻辑回归进行MNIST分类.md)的`LogisticRegression`类。 |
81 | 127 |
|
| 128 | +```Python |
| 129 | + # We now need to add a logistic layer on top of the MLP |
| 130 | + self.logLayer = LogisticRegression( |
| 131 | + input=self.sigmoid_layers[-1].output, |
| 132 | + n_in=hidden_layers_sizes[-1], |
| 133 | + n_out=n_outs |
| 134 | + ) |
| 135 | + |
| 136 | + self.params.extend(self.logLayer.params) |
| 137 | + # construct a function that implements one step of finetunining |
| 138 | + |
| 139 | + # compute the cost for second phase of training, |
| 140 | + # defined as the negative log likelihood |
| 141 | + self.finetune_cost = self.logLayer.negative_log_likelihood(self.y) |
| 142 | + # compute the gradients with respect to the model parameters |
| 143 | + # symbolic variable that points to the number of errors made on the |
| 144 | + # minibatch given by self.x and self.y |
| 145 | + self.errors = self.logLayer.errors(self.y) |
| 146 | +``` |
82 | 147 |
|
83 | 148 |
|
84 | 149 |
|
|
0 commit comments