堆叠降噪自动编码机(SdA)是堆叠自动编码机的延伸。如果对降噪自动编码机不太熟悉的话建议先阅读此前的相关文章。
堆叠自动编码机
降噪自动编码机可以堆叠起来构建深层网络,降噪自动编码机在下层发现的隐藏表征(输出码)可以作为当前层的输入。这种结构的非监督预训练以每次一层的方式进行。每层都使用降噪自动编码机最小化重构输入(前一层的输出)的误差。当前k层完成训练后就可以训练第k+1层因为此时我们可以计算下层的代码或隐藏表征。
当所有层都预训练完后则进入第二阶段称作细调。这里我们考虑有监督细调,最小化有监督任务的预测误差。首先,我们在网络上(输出层的输出码)增加一个逻辑回归层。然后我们像训练多层感知机一样训练整个网络。此时我们只考虑自动编码机的编码部分。我们在训练中使用目标类因此这一过程是有监督的。
在Theano中实现这一过程很容易,使用此前定义的降噪自动编码机的类。我们可以看到堆叠自动编码机有两部分内容,一个自动编码机列表和一个MLP。在预训练阶段,我们将模型视为一个自动编码机列表,并逐个训练它们,然后我们使用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 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_layer将储存与MLP层相关的降噪自动编码机。
接下来我们构建n_layers sigmoid层和 n_layers降噪自动编码机,n_layers是我们模型的深度。我们使用Multilayer Perceptron中介绍的HiddenLayer,将非线性的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)
我们现在只需在sigmoid层之上加逻辑层以构建MLP。我们使用此前介绍的逻辑回归类。
# 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)
SdA类也提供了在层中生成降噪自动编码机训练模块的方法。他们以列表的形式返回,其中元素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
为能在训练中更改损坏程度或学习速率,我们使用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—损坏程度或者lr—学习速率。注意参数名称是构建时给予Theano变量的名称,不是Python变量名称(learning_rate或者corruption_level),这点要注意。
以同样方式我们构建细调时所需的构造函数(train_fn, valid_score, test_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_score和test_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
)
训练分两个阶段,以层为单位的预训练和细调。
预训练阶段我们遍历网络所有层。每一层我们使用编译的Theano函数实现SGD以优化该层降低重建成本的权重。函数执行次数由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 = 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)
细调与多层感知机中的类似,唯一不同在于函数由build_finetune_functions给出。
执行代码
python code/SdA.py
代码默认每层执行15次预训练,批次大小为1。损坏度地一层0.1, 第二层0.2,第三层0.3。速率0.001,细调速率0.1。预训练耗时581.01分钟,平均每次13分钟。细调在444.2分钟36次后完成,平均12.34分钟。最终验证1.39%,测试1.3%。使用了 IntelXeon E5430 @ 2.66GHz CPU, 单一线程 GotoBLAS。
技巧
一个提高运行速度的方法(假设有足够的内存)是计算网络如何转化到k-1层的数据。即训练第1层dA,计算数据集中每个点的隐藏单元值并存储以训练对应第2层的dA,依次重复。可以看到,dA是分别训练的,只是为输入提供了非线性转化。当所有dA训练完成后,可以开始微调模型。