您的位置:首页 > 编程语言

[DL] 基于theano.tensor.dot的逻辑回归代码中的SGD部分的疑问探幽

2014-12-01 09:16 288 查看
在Hinton的教程中, 使用Python的theano库搭建的CNN是其中重要一环, 而其中的所谓的SGD - stochastic gradient descend算法又是如何实现的呢? 看下面源码

(篇幅考虑只取测试模型函数, 训练函数只是多了一个updates参数, 并且部分参数有改动):

classifier = LogisticRegression(input=x, n_in=24 * 48, n_out=32)

cost = classifier.negative_log_likelihood(y)

test_model = theano.function(inputs=[index],
outputs=classifier.errors(y),
givens={
x: test_set_x[index * batch_size: (index + 1) * batch_size],
y: test_set_y[index * batch_size: (index + 1) * batch_size]})


行3声明了一个对象classifer, 它的输入是符号x, 大小为24*48, 输出长度为32.

行11定义了一个theano的函数对象, 接收的是下标index, 使用输入数据的第index*batch_size~第(index+1)*batch_size个数据作为函数的输入, 输出为误差.

我们再来看看行12中的errors函数的定义:

def errors(self, y):
# check if y has same dimension of y_pred
if y.ndim != self.y_pred.ndim:
raise TypeError('y should have the same shape as self.y_pred',
('y', target.type, 'y_pred', self.y_pred.type))
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1
# represents a mistake in prediction
return T.mean(T.neq(self.y_pred, y))
else:
raise NotImplementedError()


self.y_pred 是一个大小为batch_size的向量, 每个元素代表batch_size中对应输入的网络判断结果, errors函数接受1个同等大小的期望输出y, 将两者进行比较求差后作均值返回, 这正是误差的定义.

那么问题来了, 这个 self.y_pred 是如何计算的? 这里我们看LogisticRegression的构造函数:

def __init__(self, input, n_in, n_out):

# initialize with 0 the weights W as a matrix of shape (n_in, n_out)
self.W = theano.shared(value=numpy.zeros((n_in, n_out),
dtype=theano.config.floatX),
name='W', borrow=True)
# initialize the baises b as a vector of n_out 0s
self.b = theano.shared(value=numpy.zeros((n_out,),
dtype=theano.config.floatX),
name='b', borrow=True)

# compute vector of class-membership probabilities in symbolic form
self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b)

# compute prediction as class whose probability is maximal in
# symbolic form
self.y_pred = T.argmax(self.p_y_given_x, axis=1)

# parameters of the model
self.params = [self.W, self.b]


在行13可以看到, 使用input和self.W, self.b进行了一个softmax的映射. softmax本身是一个一一映射所以此处不再细说, 详情可以搜索引擎. 这里我们来说一说T.dot(input, self.W).

这里的dot是theano的tensor变量的点乘操作, T.dot接受两个矩阵(向量)输入, 计算它们的点积并返回一个保存了点乘信息的节点对象, 使用返回的对象调用eval()方法即可获得实际数值结果, 如下:

>>> a = numpy.asarray([1,2,3])
>>> b = numpy.asarray([[3],[2],[1]])
>>> T.dot(a,b).eval()
array([10])


上述代码是在ab大小匹配的情况, 即a的第2维等于b的第1维. 所以本例中正常情况(非SGD)下, input是1行1152列的, W是1152行32列的, b的长度为1行32列.

这里的T.dot计算的结果是(input*W), 见如下代码:

>>> a = numpy.ones((1, 1152))
>>> W.shape
(1152, 32)
>>> dotaW = T.dot(a, W)
>>> dotaW = dotaW.eval()


>>> dotaW.shape
(1, 32)


注意, 如果颠倒了a和W的位置, 会报尺寸不匹配的错误.

那么在SGD算法中, 行13代码中的input的大小实际上是batch_size行1152(24*48)列的, 所以我不禁疑惑, 如果a和b大小不匹配呢? 一个合理的推断就是, T.dot会自动截断input. 看如下代码:

>>> a = numpy.ones((10, 1152))
>>> dotaW = T.dot(a, W)
>>> dotaW = dotaW.eval()
>>> dotaW.shape
(10, 32)


果然, T.dot会自动截断input, 将其按照匹配W的尺寸(1行1152列)依次和W进行点乘后返回, 所以大小为(10, 32), 又偏置b的大小为(1, 32), 所以b会按列进行相加.

解决了T.dot的问题, 一开篇的疑问就比较容易解答了, SGD基于T.dot的这种特性, 一次性输入batch_size行1152列样本, T.dot配合softmax生成batch_size行32列输出, 再基于 T.argmax(self.p_y_given_x, axis=1) , 计算每行(样本)的最大值作为期望输出y_pred(尺寸为batch_size行1列).

本人预测, 卷积神经网络中的conv2d也是同样的机理, 对于一定数量的输入, 如20个48行24列大小的图像, 它会针对每一个输入样本产生对应的N张特征图->降采样->第二层卷积层->MLP, 到了MLP中, 配合上述的T.dot的特性, 就完成了SGD中的批训练的目的.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐