Deep Learning by Andrew Ng --- Sparse Autoencoder
来源:互联网 发布:剑三纯阳脸型数据 编辑:程序博客网 时间:2024/04/30 11:23
这是UFLDL的编程练习,因为只看到第一章节,还没有看到向量化,所以本篇博客只注意对算法的理解,没有注意向量化。因为进入机器学习领域也只有一个多月,许多错误之处望指出。
传统的神经网络一般用于监督学习(supervised learning),一般而言,SVM比传统的backpropagation分类算法更有效。但是最近神经网络在无监督学习(unsupervised learning)领域开始大放异彩。
自编码神经网络是一种无监督学习算法,它使用了反向传播算法,并让目标值等于输入值值,比如 y(i) = x(i) 。
自编码神经网络尝试学习一个
我们刚才的论述是基于隐藏神经元数量较小的假设。但是即使隐藏神经元的数量较大(可能比输入像素的个数还要多),我们仍然通过给自编码神经网络施加一些其他的限制条件来发现输入数据中的结构。具体来说,如果我们给隐藏神经元加入稀疏性限制,那么自编码神经网络即使在隐藏神经元数量较多的情况下仍然可以发现输入数据中一些有趣的结构。
稀疏性可以被简单地解释如下。如果当神经元的输出接近于1的时候我们认为它被激活,而输出接近于0的时候认为它被抑制,那么使得神经元大部分的时间都是被抑制的限制则被称作稀疏性限制。这里我们假设的神经元的激活函数是sigmoid函数。如果你使用tanh作为激活函数的话,当神经元输出为-1的时候,我们认为神经元是被抑制的。
注意到
表示隐藏神经元 \textstyle j 的平均活跃度(在训练集上取平均)。我们可以近似的加入一条限制
其中,
为了实现这一限制,我们将会在我们的优化目标函数中加入一个额外的惩罚因子,而这一惩罚因子将惩罚那些
BackPropagation实现步骤如下(这一步用于理解backpropagation算法比较好,先理解以下的每一步再去实现向量化):
- Perform a feedforward(参照UFLDL第一章第一节) pass, computing the activations for layers L2, L3, and so on up to the output layer L_{n_l}.
- For each output unit i in layer nl (the output layer), set
δ(nl)i=∂∂z(nl)i12∥∥y−hW,b(x)∥∥2=−(yi−a(nl)i)⋅f′(z(nl)i) For l = n(l)-1, n(l)-2, n(l)-3, …, 2
For each node i in layer l, setδ(l)i=⎛⎝∑j=1sl+1W(l)jiδ(l+1)j⎞⎠f′(z(l)i) Compute the desired partial derivatives, which are given as:
∂∂W(l)ijJ(W,b;x,y)∂∂b(l)iJ(W,b;x,y)=a(l)jδ(l+1)i=δ(l+1)i.
下面给出完整的Sparese Autoencoder的实现流程(这些步骤你应该在练习的sparseAntoencoderCost.m中实现):
- Set (matrix/vector of zeros) for all \textstyle l.
ΔW(l):=0,Δb(l):=0 - For i = 1 to m:
a.Use backpropagation(上面给出的步骤) to computeand∇W(l)J(W,b;x,y) ∇b(l)J(W,b;x,y).
b.SetΔW(l):=ΔW(l)+∇W(l)J(W,b;x,y).
c.SetΔb(l):=Δb(l)+∇b(l)J(W,b;x,y) - Update the parameters:
W(l)b(l)=W(l)−α[(1mΔW(l))+λW(l)]=b(l)−α[1mΔb(l)]
根据:
以及:
- squared-error term
- regularization term (also called a weight decay term)
- penalty term(BP神经网络没有这一项),which is for measuring how different two different distributions are
需要注意的是,在将BP神经网络转化为Sparse Autoencoder时,除了让y=x,在BackPropagation中计算第二层( \textstyle l=2 )更新的时候我们已经计算了
现在我们将其换成
就可以了。
本人实现的代码如下(可能存在诸多错误):
- sampleIMAGES.m
for index = 1:numpatches rand_im=randperm(10); rand_image=rand_im(3); rand_vec=randperm(505); rand_num=rand_vec(3); patch=IMAGES(rand_num:rand_num+7,rand_num:rand_num+7,rand_image); patches(:,index)=patch(:);end
- computeNumericalGradient.m
EPSILON=0.0001;for i=1:size(theta) theta_plus=theta; theta_minu=theta; theta_plus(i)=theta_plus(i)+EPSILON; theta_minu(i)=theta_minu(i)-EPSILON; numgrad(i)=(J(theta_plus)-J(theta_minu))/(2*EPSILON);end
- sparseAutoencoderCost.m
Jcost = 0;%直接误差Jweight = 0;%权值惩罚Jsparse = 0;%稀疏性惩罚[n m] = size(data);%m为样本的个数,n为样本的特征数%前向算法计算各神经网络节点的线性组合值和active值z2 = W1*data+repmat(b1,1,m);%注意这里一定要将b1向量复制扩展成m列的矩阵a2 = sigmoid(z2);z3 = W2*a2+repmat(b2,1,m);a3 = sigmoid(z3);% 计算预测产生的误差Jcost = (0.5/m)*sum(sum((a3-data).^2));%计算权值惩罚项Jweight = (1/2)*(sum(sum(W1.^2))+sum(sum(W2.^2)));%计算稀释性规则项rho = (1/m).*sum(a2,2);%求出第一个隐含层的平均值向量Jsparse = sum(sparsityParam.*log(sparsityParam./rho)+ ... (1-sparsityParam).*log((1-sparsityParam)./(1-rho)));%损失函数的总表达式cost = Jcost+lambda*Jweight+beta*Jsparse;%反向算法求出每个节点的误差值d3 = -(data-a3).*sigmoidInv(z3);sterm = beta*(-sparsityParam./rho+(1-sparsityParam)./(1-rho));%因为加入了稀疏规则项,所以 %计算偏导时需要引入该项d2 = (W2'*d3+repmat(sterm,1,m)).*sigmoidInv(z2); %计算W1grad W1grad = W1grad+d2*data';W1grad = (1/m)*W1grad+lambda*W1;%计算W2grad W2grad = W2grad+d3*a2';W2grad = (1/m).*W2grad+lambda*W2;%计算b1grad b1grad = b1grad+sum(d2,2);b1grad = (1/m)*b1grad;%注意b的偏导是一个向量,所以这里应该把每一行的值累加起来%计算b2grad b2grad = b2grad+sum(d3,2);b2grad = (1/m)*b2grad;
其实Autoencoder算法类似PCA,都是找输入参数结构的联系,然后将之压缩。如果网络的输入数据是完全随机的,比如每一个输入 x(i) 都是一个跟其它特征完全无关的独立同分布高斯随机变量,那么这一压缩表示将会非常难学习。但是如果输入数据中隐含着一些特定的结构,比如某些输入特征是彼此相关的,那么这一算法就可以发现输入数据中的这些相关性。事实上,这一简单的自编码神经网络通常可以学习出一个跟主元分析(PCA)结果非常相似的输入数据的低维表示。
前向和反向算法其实是分步的,对于给定的minfun算法,只需将cost和grad传入计算即可。
如果我们给隐藏神经元加入稀疏性限制,那么自编码神经网络即使在隐藏神经元数量较多的情况下仍然可以发现输入数据中一些有趣的结构。
0 0
- Deep Learning by Andrew Ng --- Sparse Autoencoder
- Deep Learning by Andrew Ng --- stacked autoencoder
- Deep Learning by Andrew Ng --- Sparse coding
- Deep learning:(Sparse Autoencoder)
- Deep Learning by Andrew Ng --- Softmax regression
- Deep Learning by Andrew Ng --- self-taught
- Deep learning by Andrew Ng --- Linear Decoder
- Deep learning:(Sparse Autoencoder练习)
- Deep learning:八【sparse autoencoder】
- Deep learning:八(Sparse Autoencoder)
- Deep learning:八(Sparse Autoencoder)
- Deep learning:八(Sparse Autoencoder)
- Deep Learning:Sparse Autoencoder练习
- Deep learning:八(Sparse Autoencoder)
- Deep Learning by Andrew Ng --- PCA and whitening
- Deep learning:九(Sparse Autoencoder练习)
- Deep Learning 练习一:Sparse Autoencoder
- Deep Learning模型之:Sparse AutoEncoder
- CMake配合CodeBlocks编译FLTK
- MD5字符串加密Md5Parser
- 个人重构——配置文件
- goto语句的简单使用
- 010:列表:一个打了激素的数组
- Deep Learning by Andrew Ng --- Sparse Autoencoder
- 美国最大婚恋交友网站eHarmony的机器学习实践
- 【Android开发】用户界面设计-使用XML和Java代码混合控制UI界面
- Dynea phenolic film faced construction plywood,pp plastic formply,scaffolding panel
- adams 小轮前进
- eclipse搭建SSH框架详解
- 日志统一打印的工具类MyLog
- eclipse package,source folder,folder区别及相互转换
- Java break 和 continue 关键词