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

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秒。

下面是稀疏自编码的结果


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/566145.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python小白的数学建模课-A2.2021年数维杯C题(运动会优化比赛模式探索)探讨

关注收藏,国赛再会。 运动会优化比赛模式问题,是公平分配问题。 『Python小白的数学建模课 Youcans』带你从数模小白成为国赛达人。 2021第六届数维杯大学生数学建模 赛题已于5月27日公布,C题是"运动会优化比赛模式探索"。本文对…

Python小白的数学建模课-03.线性规划

线性规划是很多数模培训讲的第一个算法,算法很简单,思想很深刻。 要通过线性规划问题,理解如何学习数学建模、如何选择编程算法。 『Python小白的数学建模课 Youcans』带你从数模小白成为国赛达人。 1. 求解方法、算法和编程方案 线性规…

Python小白的数学建模课-A1.国赛赛题类型分析

分析赛题类型,才能有的放矢。 评论区留下邮箱地址,送你国奖论文分析 『Python小白的数学建模课 Youcans』 带你从数模小白成为国赛达人。 1. 数模竞赛国赛 A题类型分析 年份题目要求方法2020A炉温曲线建立温度模型,计算炉温曲线&#xff…

白话(whitening)

白化 Contents [hide]1 介绍2 2D 的例子3 ZCA白化4 正则化5 中英文对照6 中文译者 介绍 我们已经了解了如何使用PCA降低数据维度。在一些算法中还需要一个与之相关的预处理步骤,这个预处理过程称为白化(一些文献中也叫sphering)。举例来说&…

Python小白的数学建模课-04.整数规划

整数规划与线性规划的差别只是变量的整数约束。 问题区别一点点,难度相差千万里。 选择简单通用的编程方案,让求解器去处理吧。 『Python小白的数学建模课 Youcans』带你从数模小白成为国赛达人。 1. 从线性规划到整数规划 1.1 为什么会有整数规划&…

实现主成分分析和白化

实现主成分分析和白化 在这一节里,我们将总结PCA, PCA白化和ZCA白化算法,并描述如何使用高效的线性代数库来实现它们。 首先,我们需要确保数据的均值(近似)为零。对于自然图像,我们通过减去每个图像块(patc…

Python小白的数学建模课-05.0-1规划

0-1 规划不仅是数模竞赛中的常见题型,也具有重要的现实意义。 双十一促销中网购平台要求二选一,就是互斥的决策问题,可以用 0-1规划建模。 小白学习 0-1 规划,首先要学会识别 0-1规划,学习将问题转化为数学模型。 『…

mac下一些终端命令的使用

mac基础终端命令入门作为一名编程人员,(叫程序猿显得屌丝,叫攻城狮感觉还达不到),我经常看到许多大神在终端里面进行一些神操作。鉴于此,我今天就百度了一下,别问我为什么不Google,穷…

Python小白的数学建模课-06.固定费用问题

Python 实例介绍固定费用问题的建模与求解。 学习 PuLP工具包中处理复杂问题的快捷使用方式。 『Python小白的数学建模课 Youcans』带你从数模小白成为国赛达人。 前文讲到几种典型的 0-1 规划问题,给出了 PuLP 求解的案例。由于 0-1 规划问题种类很多&#xff0…

Python小白的数学建模课-07.选址问题

选址问题是要选择设施位置使目标达到最优,是数模竞赛中的常见题型。 小白不一定要掌握所有的选址问题,但要能判断是哪一类问题,用哪个模型。 进一步学习 PuLP工具包中处理复杂问题的字典格式快捷建模方法。 欢迎关注『Python小白的数学建模…

Python小白的数学建模课-09.微分方程模型

小白往往听到微分方程就觉得害怕,其实数学建模中的微分方程模型不仅没那么复杂,而且很容易写出高水平的数模论文。 本文介绍微分方程模型的建模与求解,通过常微分方程、常微分方程组、高阶常微分方程 3个案例手把手教你搞定微分方程。 通过…

Python小白的数学建模课-B2. 新冠疫情 SI模型

传染病的数学模型是数学建模中的典型问题,常见的传染病模型有 SI、SIR、SIRS、SEIR 模型。 SI 模型是最简单的传染病模型,适用于只有易感者和患病者两类人群。 我们就从 SI 模型开始吧,从模型、例程、运行结果到模型分析,全都在…

Python小白的数学建模课-B3. 新冠疫情 SIS模型

传染病的数学模型是数学建模中的典型问题,常见的传染病模型有 SI、SIR、SIRS、SEIR 模型。 SIS 模型型将人群分为 S 类和 I 类,考虑患病者可以治愈而变成易感者,但不考虑免疫期。 本文详细给出了 SIS 模型的建模、例程、运行结果和模型分析…

html里面Meta标签的使用

HTML meta标签使用 先上思维导图,接下来在是文章内容。一、meta标签的组成 meta标签共有两个属性,它们分别是http-equiv属性和name属性,不同的属性又有不同的参数值,这些不同的参数值就实现了不同的网页功能。 1、name属性 name属…

Python小白的数学建模课-B4. 新冠疫情 SIR模型

传染病的数学模型是数学建模中的典型问题,常见的传染病模型有 SI、SIR、SIRS、SEIR 模型。 SIR 模型将人群分为易感者(S类)、患病者(I类)和康复者(R 类),考虑了患病者治愈后的免疫能…

Python小白的数学建模课-B5. 新冠疫情 SEIR模型

传染病的数学模型是数学建模中的典型问题,常见的传染病模型有 SI、SIR、SIRS、SEIR 模型。 考虑存在易感者、暴露者、患病者和康复者四类人群,适用于具有潜伏期、治愈后获得终身免疫的传染病。 本文详细给出了 SEIR 模型微分方程的建模、例程、结果和分…

Python小白的数学建模课-B6. 新冠疫情 SEIR 改进模型

传染病的数学模型是数学建模中的典型问题,常见的传染病模型有 SI、SIR、SIRS、SEIR 模型。 SEIR 模型考虑存在易感者、暴露者、患病者和康复者四类人群,适用于具有潜伏期、治愈后获得终身免疫的传染病。 本文详细给出了几种改进 SEIR 模型微分方程的思…

iOS里面MVC模式详解

iOS里面MVC模式详解MVC是IOS里面也是很多程序设计里面的一种设计模式,M是model,V是view,C是controller。MVC模式在ios开发里面可谓是用得淋漓尽致。 以下是对斯坦福大学ios开发里面MVC模式的一段话的翻译 主要的宗旨是把所有的对象分为3个阵营…

Python小白的数学建模课-10.微分方程边值问题

小白往往听到微分方程就觉得害怕,其实数学建模中的微分方程模型不仅没那么复杂,而且很容易写出高水平的数模论文。 本文介绍微分方程模型边值问题的建模与求解,不涉及算法推导和编程,只探讨如何使用 Python 的工具包,…