堆叠去噪自动编码器(SdA)

Note

本节假设你已阅读过使用逻辑回归分析MNIST数字多层感知器此外,它使用以下Theano函数和概念:T.tanh共享变量基本算术操作T.grad随机数floatX如果你打算在GPU上运行代码还要阅读GPU

Note

The code for this section is available for download here.

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

本教程基于上一个教程去噪自动编码器特别是如果你没有自动编码器的经验,我们建议再进一步之前先阅读它。

堆叠自动编码器

去噪自动编码器可以堆叠形成深层网络,通过将去噪自动编码器在下层上找到的潜在表示(输出编码)作为当前层的输入。The unsupervised pre-training of such an architecture is done one layer at a time. 通过最小化重构输入(前一层的输出编码)中的误差,将每一层作为去噪自动编码器训练。一旦第一个k层被训练,我们可以训练第k+1层,因为我们现在可以从下面的层计算编码或潜在表示。

在所有层预训练完成之后,网络将进行第二阶段训练,称为微调这里,我们考虑监督微调,其中我们希望在一个有监督的任务上最小化预测误差。为此,我们首先在网络的最上层(更确切地说,在输出层的输出编码上)添加一个逻辑回归层。然后我们训练整个网络,和我们将训练多层感知器一样。At this point, we only consider the encoding parts of each auto-encoder. 这个阶段是监督的,因为现在我们在训练期间使用目标的类别。(有关多层感知器的详细信息,请参阅多层感知器)。

This can be easily implemented in Theano, using the class defined previously for a denoising autoencoder. 我们可以看到堆叠去噪自动编码器具有两个主体:一系列的自动编码器和MLP。在预训练期间,我们使用第一个主体,即我们将我们的模型视为一系列的自动编码器,并且单独训练每个自动编码器。在第二阶段的训练中,我们使用第二个主体。这两个主体是链接的,因为:

  • the autoencoders and the sigmoid layers of the MLP share parameters, and
  • 由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 hidden_layers_sizes: list of ints
        :param hidden_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的层相关联的去噪自动编码器。

Next, we construct n_layers sigmoid layers and n_layers denoising autoencoders, where n_layers is the depth of our model. 我们使用多层感知器中引入的HiddenLayer类,并进行一个修改:我们用逻辑函数s(x) = \frac{1}{1+e^{-x}}替换tanh我们链接sigmoid层以形成MLP,并构造的噪自动编码器,每个编码器的编码部分与对应的sigmoid层共享权重矩阵和偏置。

        for i in range(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)

All we need now is to add a logistic layer on top of the sigmoid layers such that we have an MLP. 我们将使用在使用逻辑回归分类MNIST数字中介绍的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)

The SdA class also provides a method that generates training functions for the denoising autoencoders in its layers. They are returned as a list, where element i is a function that implements one step of training the dA corresponding to layer i.


    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水平或学习率,我们将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.In(corruption_level, value=0.2),
                    theano.In(learning_rate, value=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]函数都接收参数index和可选的corruption — corruption等级或lr — 学习率。Note that the names of the parameters are the names given to the Theano variables when they are constructed, not the names of the Python variables (learning_rate or corruption_level). Keep this in mind when working with Theano.

我们以相同的方式编写一个方法用于构建微调(train_fnvalid_scoretest_score)过程中所需的函数。

    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 range(n_valid_batches)]

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

        return train_fn, valid_score, test_score

注意,valid_scoretest_score不是Theano函数,而是Python函数,分别循环遍历整个验证集和整个测试集,产生这些集合上的一个损失列表。

将它们放在一起

下面的几行代码构造堆叠去噪自动编码器:

    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
    )

There are two stages of training for this network: layer-wise pre-training followed by fine-tuning.

For the pre-training stage, we will loop over all the layers of the network. For each layer we will use the compiled Theano function that implements a SGD step towards optimizing the weights for reducing the reconstruction cost of that layer. 该函数将应用于训练集时,由pretraining_epochs给出固定数目的epoch。

    #########################
    # 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 = timeit.default_timer()
    ## Pre-train layer-wise
    corruption_levels = [.1, .2, .3]
    for i in range(sda.n_layers):
        # go through pretraining epochs
        for epoch in range(pretraining_epochs):
            # go through the training set
            c = []
            for batch_index in range(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 %f' % (i, epoch, numpy.mean(c, dtype='float64')))

    end_time = timeit.default_timer()

    print(('The pretraining code for file ' +
           os.path.split(__file__)[1] +
           ' ran for %.2fm' % ((end_time - start_time) / 60.)), file=sys.stderr)

微调循环与多层感知器非常类似。The only difference is that it uses the functions given by build_finetune_functions.

运行代码

The user can run the code by calling:

python code/SdA.py

默认情况下,代码为每个层运行15个epoch的预训练,batch大小为1。第一层的corruption级别为0.1,第二层的corruption级别为0.2,第三层的corruption级别为0.3。The pretraining learning rate is 0.001 and the finetuning learning rate is 0.1. 预训练需要585.01分钟,平均每个epoch 13分钟。微调需要36个epoch、444.2分钟完成,每个epoch平均为12.34分钟。The final validation score is 1.39% with a testing score of 1.3%. 这些结果是在Intel Xeon E5430 @ 2.66GHz CPU、单线程GotoBLAS的机器上获得的。

提示和技巧

One way to improve the running time of your code (assuming you have sufficient memory available), is to compute how the network, up to layer k-1, transforms your data. Namely, you start by training your first layer dA. 在它训练好之后,你可以计算数据集中每个数据点的隐藏单元值,并将其存储为一个新的数据集,你将使用它来训练对应于第2层的dA。在你已经训练好第二层的dA之后,以类似的方式计算第三层的数据集等等。You can see now, that at this point, the dAs are trained individually, and they just provide (one to the other) a non-linear transformation of the input. 在所有的DA都训练好之后,你可以开始微调模型。