UFIDL稀疏自编码代码实现及解释

1.今天我们来讲一下UFIDL的第一个练习。

1.我们来看看最难的一个.m文件

%% ---------- YOUR CODE HERE --------------------------------------
%  Instructions: Compute the cost/optimization objective J_sparse(W,b) for the Sparse Autoencoder,
%                and the corresponding gradients W1grad, W2grad, b1grad, b2grad.
%
% W1grad, W2grad, b1grad and b2grad should be computed using backpropagation.
% Note that W1grad has the same dimensions as W1, b1grad has the same dimensions
% as b1, etc.  Your code should set W1grad to be the partial derivative of J_sparse(W,b) with
% respect to W1.  I.e., W1grad(i,j) should be the partial derivative of J_sparse(W,b)
% with respect to the input parameter W1(i,j).  Thus, W1grad should be equal to the term
% [(1/m) \Delta W^{(1)} + \lambda W^{(1)}] in the last block of pseudo-code in Section 2.2
% of the lecture notes (and similarly for W2grad, b1grad, b2grad).
%
% Stated differently, if we were using batch gradient descent to optimize the parameters,
% the gradient descent update to W1 would be W1 := W1 - alpha * W1grad, and similarly for W2, b1, b2.
%
% size(data, 1) % 64
% size(W1)   % 25 64
% size(W2)   % 64 25
% size(b1)   % 25  1
% size(b2)   % 64  1%样本的个数
m = size(data, 2);%前向传播
%第二层的输入值
z_2 = W1 * data + repmat(b1, 1, m);
%第二层的激活值
a_2 = sigmoid(z_2); % 25 10000%计算rho_hat的值,其值为第二层的每个维度(64)上的平均激活度,sum(a_2,2)表示对第二维求和,即对每一行求和
rho_hat = sum(a_2, 2) / m; % This doesn't contain an x because the data% above "has" the x
%第三层的输入
z_3 = W2 * a_2 + repmat(b2, 1, m);
%第三层的激活
a_3 = sigmoid(z_3); % 64 10000%激活值与实际值的差
diff = a_3 - data;%稀疏自编码的惩罚,KLdivergence(相对熵)
sparse_penalty = kl(sparsityParam, rho_hat);%简化的代价函数
%这个写的比较简洁
J_simple = sum(sum(diff.^2)) / (2*m);%正则项,即所有W元素的平方和
%这个写得也不错,W1(:)使得矩阵的元素按列重新排布
reg = sum(W1(:).^2) + sum(W2(:).^2);%总的代价
cost = J_simple + beta * sparse_penalty + lambda * reg / 2;% Backpropogationdelta_3 = diff .*            (a_3 .* (1-a_3));   % 64 10000%计算残差2的基本部分
d2_simple = W2' * delta_3;   % 25 10000
%计算残差2的惩罚
d2_pen = kl_delta(sparsityParam, rho_hat);%计算残差2
delta_2 = (d2_simple + beta * repmat(d2_pen,1, m)) .* a_2 .* (1-a_2);%计算b的梯度
b2grad = sum(delta_3, 2)/m;
b1grad = sum(delta_2, 2)/m;%计算W的梯度
W2grad = delta_3 * a_2'/m  + lambda * W2; % 25 64
W1grad = delta_2 * data'/m + lambda * W1; % 25 64%-------------------------------------------------------------------
% After computing the cost and gradient, we will convert the gradients back
% to a vector format (suitable for minFunc).  Specifically, we will unroll
% your gradient matrices into a vector.grad = [W1grad(:) ; W2grad(:) ; b1grad(:) ; b2grad(:)];end%-------------------------------------------------------------------
% Here's an implementation of the sigmoid function, which you may find useful
% in your computation of the costs and the gradients.  This inputs a (row or
% column) vector (say (z1, z2, z3)) and returns (f(z1), f(z2), f(z3)). function sigm = sigmoid(x)sigm = 1 ./ (1 + exp(-x));
end%相对熵
function ans = kl(r, rh)ans = sum(r .* log(r ./ rh) + (1-r) .* log( (1-r) ./ (1-rh)));
end%相对熵的残差计算公式
function ans = kl_delta(r, rh)ans = -(r./rh) + (1-r) ./ (1-rh);
end%sigmoid函数的导数
function pr = prime(x)pr = sigmoid(x) .* (1 - sigmoid(x));
end

我对上述进行了解释。

接下去是SampleIMAGES文件,这个比较简单我就不解释了

function patches = sampleIMAGES()
% sampleIMAGES
% Returns 10000 patches for trainingload IMAGES;    % load images from disk patchsize = 8;  % we'll use 8x8 patches
numpatches = 10000;% Initialize patches with zeros.  Your code will fill in this matrix--one
% column per patch, 10000 columns.
patches = zeros(patchsize*patchsize, numpatches);%% ---------- YOUR CODE HERE --------------------------------------
%  Instructions: Fill in the variable called "patches" using data
%  from IMAGES.
%
%  IMAGES is a 3D array containing 10 images
%  For instance, IMAGES(:,:,6) is a 512x512 array containing the 6th image,
%  and you can type "imagesc(IMAGES(:,:,6)), colormap gray;" to visualize
%  it. (The contrast on these images look a bit off because they have
%  been preprocessed using using "whitening."  See the lecture notes for
%  more details.) As a second example, IMAGES(21:30,21:30,1) is an image
%  patch corresponding to the pixels in the block (21,21) to (30,30) of
%  Image 1% imagesc(IMAGES(:,:,10)) % 1-10
% size(patches)      % 64 x 10000
% size(patches(:,1)) % 64 x 1for i = 1:numpatchesx = randi(512-8+1);y = randi(512-8+1);sample = IMAGES(x:x+7,y:y+7,randi(10));patches(:,i) = sample(:);
end%% ---------------------------------------------------------------
% For the autoencoder to work well we need to normalize the data
% Specifically, since the output of the network is bounded between [0,1]
% (due to the sigmoid activation function), we have to make sure
% the range of pixel values is also bounded between [0,1]
patches = normalizeData(patches);end%% ---------------------------------------------------------------
function patches = normalizeData(patches)% Squash data to [0.1, 0.9] since we use sigmoid as the activation
% function in the output layer% Remove DC (mean of images).
patches = bsxfun(@minus, patches, mean(patches));% Truncate to +/-3 standard deviations and scale to -1 to 1
pstd = 3 * std(patches(:));
patches = max(min(patches, pstd), -pstd) / pstd;% Rescale from [-1,1] to [0.1,0.9]
patches = (patches + 1) * 0.4 + 0.1;end

从10幅图像里面随机抽取10000个图像块,并进行了一些归一化处理。

接下去是计算梯度函数

function numgrad = computeNumericalGradient(J, theta)
% numgrad = computeNumericalGradient(J, theta)
% theta: a vector of parameters
% J: a function that outputs a real-number. Calling y = J(theta) will return the
% function value at theta. % Initialize numgrad with zeros
numgrad = zeros(size(theta));%% ---------- YOUR CODE HERE --------------------------------------
% Instructions:
% Implement numerical gradient checking, and return the result in numgrad.
% (See Section 2.3 of the lecture notes.)
% You should write code so that numgrad(i) is (the numerical approximation to) the
% partial derivative of J with respect to the i-th input argument, evaluated at theta.
% I.e., numgrad(i) should be the (approximately) the partial derivative of J with
% respect to theta(i).
%
% Hint: You will probably want to compute the elements of numgrad one at a time. % size(theta)   2 1 | 3289 1
% size(numgrad) 2 1 | 3289 1
eps = 1e-4;
n = size(numgrad);
I = eye(n, n);
for i = 1:size(numgrad)%通过单位矩阵来构造向量,比较有趣eps_vec = I(:,i) * eps;numgrad(i) = (J(theta + eps_vec) - J(theta - eps_vec)) / (2 * eps);
end% numgrad = (J(theta + eps) - J(theta - eps)) / (2 * eps)%% ---------------------------------------------------------------
end

最后我们运行官方的train文件,并等待结果(大约1分钟左右)

我们可以看到matlab的输出


可以看到我们我们通过BFGS(共轭梯度下降法),总共迭代了400次,花了51秒。

下面是稀疏自编码的结果

UFIDL稀疏自编码代码实现及解释相关推荐

  1. 深度学习(十二)稀疏自编码

    稀疏自编码 原文地址:http://blog.csdn.net/hjimce/article/details/49106869 作者:hjimce 一.相关理论 以前刚开始学CNN的时候,就是通过阅读 ...

  2. 稀疏自编码器_基于tf实现稀疏自编码和在推荐中的应用

    稀疏自编码 自编码器(Auto-Encoder)顾名思义,即可以利用自身的高阶特征编码自己.自编码器也是一种神经网络,他的输入和输出是一致的,他借助稀疏编码的思想,目标是使用稀疏的一些高阶特征重新组合 ...

  3. 稀疏自编码器_基于tensorflow实现稀疏自编码和在推荐中的应用

    稀疏自编码 自编码器(Auto-Encoder)顾名思义,即可以利用自身的高阶特征编码自己.自编码器也是一种神经网络,他的输入和输出是一致的,他借助稀疏编码的思想,目标是使用稀疏的一些高阶特征重新组合 ...

  4. 不写一行代码,也能解释XML,因为是JAVA

    不写一行代码,也能解释XML,因为是JAVA. 整个过程就三个步骤 1.XML转换成XSD 2.XSD生成JAVA Class 3.调用unmarshal实现转换结果 具体实例: 1.假设有一个XML ...

  5. 稀疏学习、稀疏表示、稀疏自编码神经网络、字典学习、主成分分析PCA、奇异值分解SVD 等概念的梳理,以及常用的特征降维方法

    稀疏学习.稀疏表示.稀疏自编码神经网络.字典学习.主成分分析PCA.奇异值分解SVD 等概念的梳理,以及常用的特征降维方法 关于稀疏 稀疏编码 Sparse Coding 与字典学习 神经网络的正则化 ...

  6. 如何检测文本文件的编码/代码页

    本文翻译自:How can I detect the encoding/codepage of a text file In our application, we receive text file ...

  7. flash 版的mp3编码代码

    flash 版的mp3编码代码 Shine MP3 Encoder on Alchemy http://code.google.com/p/flash-kikko/ 简介 Shine简单轻量级的mp3 ...

  8. 基于MPI的H.264并行编码代码移植与优化

    2010 03 25 基于MPI的H.264并行编码代码移植与优化 范 文 洛阳理工学院计算机信息工程系 洛阳 471023 摘 要 H.264获得出色压缩效果和质量的代价是压缩编码算法复杂度的增加. ...

  9. html 生成唯一码,生成唯一32位ID编码代码,以满足对ID编号的唯一性加资源性解决问题...

    生成唯一32位ID编码代码,以满足对ID编号的唯一性加资源性解决问题 package com.huayu.common; /* * RandomGUID from http://www.javaexc ...

最新文章

  1. java 配置及Eclipse安装
  2. 2020 年,人工智能和深度学习未来的五大趋势
  3. 【原】ASP.Net WebForm的发布(图解)
  4. Manage Common Field Service Jobs
  5. Java后端返回通用接口设计
  6. SAP Netweaver的负载均衡消息服务器 vs CloudFoundry的App Router
  7. Github | 深度神经网络(DNN)与生成式对抗网络(GAN)模型总览
  8. 提示cannot instantiate abstract class due to following members?
  9. Docker使用笔记-2-[之] oracle-xe安装
  10. CentOS 修改主机名(host)
  11. 牛客网暑期ACM多校训练营(第二场): H. travel(树形线头DP)
  12. 数据分析从入门到进阶,35本包邮送到家
  13. Unity官方中文网站
  14. CheckBoxPreference--数据存储
  15. JSON在线对比差异工具
  16. 显著性检验--学习笔记
  17. java 多线程数组越界_越界java数组
  18. 云计算时代迎接挑战方能脱颖而出
  19. 车载无线自组织网络的介质访问控制协议研究
  20. 必备装机软件,软件推荐

热门文章

  1. 火狐怎么在线升级 火狐浏览器在线升级方法分享
  2. 正则基本知识和常用正则
  3. 单行文字、多行文字溢出时省略号表示的多种解决方式;调整字符间距;段落首字母大写缩进效果;
  4. html%2b怎么转换成加号,Apache mod_rewrite%2B和加号(+)符号
  5. c# python 相互调用_【GhPython】Python如何使用“委托”和lambda表达式
  6. 属性的表示方法和对象的枚举
  7. mysql主键设置after_mysql如何改变主键属性
  8. 在java中jvm目录_JVM具体在哪个文件夹下的
  9. 动态规划算法之数塔问题
  10. oracle中$的用法,关于expdp 中query用法小结