堆栈式自编码器_DeepLearning 0.1 documentation中文翻译

DeepLearning 0.1 documentation中文翻译: Stacked Denoising Autoencoders (SdA)_堆栈式自编码器

原文网址: http://deeplearning.net/tutorial/SdA.html


Note
这部分假设读者已经阅读过Classifying MNIST digits using Logistic RegressionMultilayer Perceptron. 此外, 本节会使用到新的Theano函数和概念: T.tanh, shared variables, basic arithmetic ops, T.grad, Random numbers, floatX. 如果你打算在GPU上运行,也要阅读GPU.
Note
代码可以在此下载:code

堆栈式( 堆叠式 )去噪自编码器( SdA )是堆栈式自编码器[ Bengio07 ]的扩展, 关于堆栈式去噪自编码器的介绍见: 文献 [ Vincent08 ].

这篇教程基于上一篇去噪自编码器的内容, 建议, 在继续之前, 先读一下上一篇教程(特别是不熟悉自编码器的).

堆栈式去噪自编码器(Stacked Autoencoders)

堆叠多个去噪自编码器, 并把上一层的隐藏层表示(编码)作为当前层的输入, 可以形成深层网络. 这样一个体系结构的无监督预训练(unsupervised pre-training), 一次只做一层. 每一层都作为一个降噪自编码器, 并通过最小化( 即前一层的输出编码 )重构输入的误差来训练. 一旦训练好前 k 层, 我们就可以训练第k+1层, 这是因为现在可以计算下一层的编码或隐层表示. 一旦所有层都经过预训练, 网络开始进入训练的第二阶段, 即微调( fine-tuning). 这里, 我们采用有监督的微调(supervised fine-tuning), 其中我们需要最小化有监督任务的预测误差. 为此, 我们首先在网络的顶层( 更准确地说是加在输出层的输出编码 )增加一个Logistic回归层. 然后我们再像训练一个多层感知器那样, 训练整个网络. 在这一点上, 我们只考虑每个自动编码器的编码部分. 因为从现在起, 我们在训练过程中使用了目标类别信息, 所以这一阶段是有监督的( 多层感知器的更多详细内容, 参见: Multilayer Perceptron 多层感知器 ).

在Theano中, 这很容易实现, 依然使用先前定义的降噪自编码器类. 我们可以看到堆叠式降噪自编码器具有两个要点, 一个是一系列的自动编码器, 另一个是多层感知器(MLP). 预训练过程使用一系列的自动编码器, 即把我们的模型当作一系列的自编码器, 并且各自独立地训练. 在训练的第二阶段, 我们使用多层感知器. 这两点通过自编码器和MLP的sigmoid层共享参数的事实, 以及自编码器以MLP的中间层的隐层表达作为它的输入这一事实联系起来.

class SdA(object):
    """Stacked denoising auto-encoder class (SdA)

    A stacked denoising autoencoder model is obtained by stacking several
    dAs. The hidden layer of the dA at layer `i` becomes the input of
    the dA at layer `i+1`. The first layer dA gets as input the input of
    the 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 initial
                    weights

        :type theano_rng: theano.tensor.shared_randomstreams.RandomStreams
        :param theano_rng: Theano random generator; if None is given one is
                           generated 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 contain
                               at 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 each
                                  layer
        """

        self.sigmoid_layers = []
        self.dA_layers = []
        self.params = []
        self.n_layers = len(hidden_layers_sizes)

        assert self.n_layers > 0

        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
        # allocate symbolic variables for the data
        self.x = T.matrix('x')  # the data is presented as rasterized images
        self.y = T.ivector('y')  # the labels are presented as 1D vector of
                                 # [int] labels

self.sigmoid_layers 存储多层感知器(MLP)的sigmoid层信息, 而self.dA_layers 存储与多层感知器(MLP)层相关的降噪自编码器的信息.

下一步, 我们构造 n_layers 层sigmoid层( 使用在Multilayer Perceptron中引入的 HiddenLayer类, 其中, 仅仅把非线性函数 tanh 换成了 Logistic 函数 s(x)=11+ex) ) 和 n_layers 层去噪自编码器, 其中, n_layers 是模型的深度. 我们联接各 sigmoid 层, 从而形成一个MLP, 并构造每个去噪自编码器, 这样, 它们就能共享对应编码部分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 layer
            if i == 0:
                input_size = n_ins
            else:
                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
            # layer
            if i == 0:
                layer_input = self.x
            else:
                layer_input = self.sigmoid_layers[-1].output

            sigmoid_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 layers
            self.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 SdA
            self.params.extend(sigmoid_layer.params)

            # Construct a denoising autoencoder that shared weights with this
            # layer
            dA_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 层的前端(top)添加 Logistic层, 这样, 就得到了一个多层感知器(MLP). 我们将使用在Classifying MNIST digits using Logistic Regression 中引入的LogisticRegression类实现.

        # We now need to add a logistic layer on top of the MLP
        self.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 likelihood
        self.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.y
        self.errors = self.logLayer.errors(self.y)

这个类还提供了一个为每个与不同层关联的降噪自编码器生成训练函数的方法. 返回参数是一个列表, 其中, 元素 i 是一个实现一步对应于第i层的dA的训练的函数.

    def pretraining_functions(self, train_set_x, batch_size):
        ''' Generates a list of functions, each of them implementing one
        step in trainnig the dA corresponding to the layer with same index.
        The function will require as input the minibatch index, and to train
        a dA you just need to iterate, calling the corresponding function on
        all minibatch indexes.

        :type train_set_x: theano.tensor.TensorType
        :param train_set_x: Shared variable that contains all datapoints used
                            for 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 of
                              the dA layers
        '''

        # index to a [mini]batch
        index = T.lscalar('index')  # index to a minibatch

为了能够改变训练期间的损坏等级(corruption level), 或学习率(learning rate), 我们给它们关联一个 Theano 变量.

        corruption_level = T.scalar('corruption')  # % of corruption to use
        learning_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_size

        pretrain_fns = []
        for dA in self.dA_layers:
            # get the cost and the updates list
            cost, updates = dA.get_cost_updates(corruption_level,
                                                learning_rate)
            # compile the theano function
            fn = 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 functions
            pretrain_fns.append(fn)

        return pretrain_fns

函数 pretrain_fns[i] 以索引为参数, 并且损坏等级(corruption level), 或学习率(learning rate)是可选的. 注意, 参数的名字, 是当Theano变量创建时的名字, 而不是Python变量的名字(learning_rate or corruption_level). 在使用Theano的过程中, 要始终记住这一点.

我们以同样的方式(In the same fashion), 为在调整过程中需要的构造函数( 一个train_model, 一个 validate_model 和 一个 test_model 函数 ), 构建了一个方法.

    def build_finetune_functions(self, datasets, batch_size, learning_rate):
        '''Generates a function `train` that implements one step of
        finetuning, a function `validate` that computes the error on
        a batch from the validation set, and a function `test` that
        computes 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 pair
                         is formed of two Theano variables, one for the
                         datapoints, 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 testing
        n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
        n_valid_batches /= batch_size
        n_test_batches = test_set_x.get_value(borrow=True).shape[0]
        n_test_batches /= batch_size

        index = T.lscalar('index')  # index to a [mini]batch

        # compute the gradients with respect to the model parameters
        gparams = T.grad(self.finetune_cost, self.params)

        # compute list of fine-tuning updates
        updates = [
            (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 set
        def valid_score():
            return [valid_score_i(i) for i in xrange(n_valid_batches)]

        # Create a function that scans the entire test set
        def test_score():
            return [test_score_i(i) for i in xrange(n_test_batches)]

        return train_fn, valid_score, test_score

注意, 返回的 valid_scoretest_score 不是 Theano 函数, 而是 Python 函数, 这些函数也在整个验证集(entire validation set)和整个测试集(entire test set)上循环, 并产生在这些集合上的损失列表.

整合程序(Putting it all together)

下面的几行代码构成降噪自编码器 :

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

训练这个网络, 包含两个步骤: 逐层的预训练(layer-wise pre-training)和之后的微调(fine-tuning afterwards).

在预训练阶段(pre-training stage), 将循环网络的所有层. 在每一层, 为了减小当前层的重构误差, 使用编译的 Theano 函数, 实现一次前向的SGD(随机梯度下降, Stochastic gradient descent)操作来优化权重. 这个函数, 以固定代数(迭代次数, number of 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-wise
    corruption_levels = [.1, .2, .3]
    for i in xrange(sda.n_layers):
        # go through pretraining epochs
        for epoch in xrange(pretraining_epochs):
            # go through the training set
            c = []
            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.))

微调循环与在多层感知器中的操作类似, 唯一的不同是, 现在使用 build_finetune_functions 函数.

运行代码(Running the Code)

调用以下函数运行程序:

python code/SdA.py

默认情况下, 每层的预训练执行15代, 批(batch)大小是 1. 第一层的损坏等级(corruption level)是 0.1, 第二层是 0.2, 第三层是 0.3. 预训练学习率(pretraining learning rate)是 0.001, 且微调学习率(finetuning learning rate)是 0.1. 预训练耗时 585.01 分钟, 平均每代 13 分钟. 微调在 36 代后完成, 耗时 444.2 分钟, 平均每代 12.34 分钟. 最终的验证得分(validation score)是 1.39%, 测试得分是1.3%. 这些结果是在一个 Intel Xeon E5430 @ 2.66GHz CPU, 机器上, 以单线程(single-threaded) GotoBLAS 运行得到的.

译者注: BLAS, 即 Basic Linear Algebra Subprograms, 是执行向量和矩阵运算的子程序集合, 公认性能最好的就是 GotoBLAS, 点此转到下载页GotoBLAS.

提示和技巧(Tips and Tricks)

提高你的代码的运行效率的一种方式( 假如你有足够的内存 ), 是计算直到第 k1 层, 网络是如何转换数据的. 即, 从训练第一层dA开始, 一旦它被训练, 你就可以计算数据集中的每个数据点在隐层单元的值, 并把它存储为一个新的数据集, 然后使用这个新数据集训练dA的第二层. 第二层训练好后, 进行类似的计算, 得到用于第三层训练的新数据集, 依次类推. 你现在可以发现, 就此, dAs单独进行训练, 并且他们只是对输入进行一个非线性变换( 一个对另一个, one to the other ). 一旦所有dAs训练好, 就可以开始微调模型了.



DeepLearning 0.1 documentation中文翻译

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值