下面,将UFLDL教程中的sparseae_exercise练习中的各函数及注释列举如下

首先,给出各函数的调用关系

主函数:train.m

(1)调用sampleIMAGES函数从已知图像中扣取多个图像块儿

(2)调用display_network函数,以网格的形式,随机显示多个扣取的图像块儿

(3)梯度校验,该部分的目的是测试函数是否正确,可以由单独的函数checkSparseAutoencoderCost实现

①利用sparseAutoencoderCost函数计算网路的代价函数和梯度值

②利用computeNumericalGradient函数计算梯度值(这里,要利用checkNumericalGradient函数验证该梯度计算函数是否正确)

③比较①和②的梯度计算结果,判断编写的sparseAutoencoderCost函数是否正确

如果sparseAutoencoderCost函数是正确的,那么,在实际训练中,不需要运行checkSparseAutoencoderCost

(4)利用L-BFGS方法对网络进行训练,从而得到最优化的网络的权值和偏执项

(5)对训练结果进行可视化

然后,对个函数给出注释

train.m

%% CS294A/CS294W Programming Assignment Starter Code

addpath ..common\

%%======================================================================
%% STEP : Here we provide the relevant parameters values that will
% allow your sparse autoencoder to get good filters; you do not need to change the parameters below.
visibleSize = *; % number of input units
hiddenSize = ; % number of hidden units
sparsityParam = 0.01; % desired average activation of the hidden units.
% (This was denoted by the Greek alphabet rho, which looks like a lower-case "p", in the lecture notes).
lambda = 0.0001; % weight decay parameter
beta = ; % weight of sparsity penalty term %%======================================================================
%% STEP : Implement sampleIMAGES
% After implementing sampleIMAGES, the display_network command should display a random sample of patches from the dataset
%从图像中提取图像块儿,每一个提取到的图像块儿存放在patches的每一列中
patches = sampleIMAGES;
%随机提取patches中的200列,然后显示这200列所对应的图像
IMG=patches(:,randi(size(patches,),,));
display_network(IMG,); %%======================================================================
%% STEP and STEP :Implement sparseAutoencoderCost and Gradient Checking
checkSparseAutoencoderCost() %%======================================================================
%% STEP : After verifying that your implementation of % Randomly initialize the parameters
theta = initializeParameters(hiddenSize, visibleSize); % Use minFunc to minimize the function
addpath minFunc/
options.Method = 'lbfgs'; % Here, we use L-BFGS to optimize our cost function
% Generally, for minFunc to work, you need a function pointer with two outputs: the function value and the gradient.
% In our problem, sparseAutoencoderCost.m satisfies this.
options.maxIter = ; % Maximum number of iterations of L-BFGS to run
options.display = 'on'; % opttheta是整个神经网络的权值和偏执项构成的向量
[opttheta, cost] = minFunc( @(p) sparseAutoencoderCost(p, ...
visibleSize, hiddenSize, ...
lambda, sparsityParam, ...
beta, patches), ...
theta, options); %%======================================================================
%% STEP : Visualization
W1 = reshape(opttheta(:hiddenSize*visibleSize), hiddenSize, visibleSize);%第一层的权值矩阵
display_network(W1', 12); print -djpeg weights.jpg % save the visualization to a file

checkSparseAutoencoderCost.m

%% 该函数主要目的是检验SparseAutoencoderCost函数是否正确
function checkSparseAutoencoderCost() %% 产生一个稀疏自编码网络(可以与主程序相同,也可以重新产生)
visibleSize = *; % number of input units
hiddenSize = ; % number of hidden units
sparsityParam = 0.01; % desired average activation of the hidden units.
% (This was denoted by the Greek alphabet rho, which looks like a lower-case "p", in the lecture notes).
lambda = 0.0001; % weight decay parameter
beta = ; % weight of sparsity penalty term patches = sampleIMAGES; % Obtain random parameters theta
theta = initializeParameters(hiddenSize, visibleSize); %% 计算代价函数和梯度
[cost, grad] = sparseAutoencoderCost(theta, visibleSize, hiddenSize, lambda, ...
sparsityParam, beta, patches(:,:)); %% 利用近似方法计算梯度(要调用自编码器的代价函数计算程序)
numgrad = computeNumericalGradient( @(x) sparseAutoencoderCost(x, visibleSize, ...
hiddenSize, lambda, ...
sparsityParam, beta, ...
patches(:,:)), theta); %% 比较cost函数计算得到的梯度和由近似得到的梯度之
% Use this to visually compare the gradients side by side
disp([numgrad grad]); % Compare numerically computed gradients with the ones obtained from backpropagation
diff = norm(numgrad-grad)/norm(numgrad+grad);
disp(diff); % Should be small. In our implementation, these values are usually less than 1e-. end

sparseAutoencoderCost.m

%% 计算网络的代价函数和梯度
function [cost,grad] = sparseAutoencoderCost(theta, visibleSize, hiddenSize, ...
lambda, sparsityParam, beta, data) % visibleSize: the number of input units (probably )
% hiddenSize: the number of hidden units (probably )
% lambda: weight decay parameter
% sparsityParam: The desired average activation for the hidden units (denoted in the lecture
% notes by the greek alphabet rho, which looks like a lower-case "p").
% beta: weight of sparsity penalty term
% data: Our 64x10000 matrix containing the training data. So, data(:,i) is the i-th training example. % The input theta is a vector (because minFunc expects the parameters to be a vector).
% We first convert theta to the (W1, W2, b1, b2) matrix/vector format, so that this
% follows the notation convention of the lecture notes. W1 = reshape(theta(:hiddenSize*visibleSize), hiddenSize, visibleSize);
W2 = reshape(theta(hiddenSize*visibleSize+:*hiddenSize*visibleSize), visibleSize, hiddenSize);
b1 = theta(*hiddenSize*visibleSize+:*hiddenSize*visibleSize+hiddenSize);
b2 = theta(*hiddenSize*visibleSize+hiddenSize+:end); % Cost and gradient variables (your code needs to compute these values).
% Here, we initialize them to zeros.
cost = ; m=size(data,); %% ---------- 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
% [(/m) \Delta W^{()} + \lambda W^{()}] 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.
% %% 前向传播算法
a1=data;
z2=bsxfun(@plus,W1*a1,b1);
a2=sigmoid(z2);
z3=bsxfun(@plus,W2*a2,b2);
a3=sigmoid(z3); %% 计算网络误差
% 误差项J1=所有样本代价函数均值
y=data; % 网络的理想输出值
Ei=sum((a3-y).^)/; %每一个样本的代价函数
J1=sum(Ei)/m;
% 正则化项J2=所有权值项平方和
J2=sum(W1(:).^)+sum(W2(:).^);
% 稀疏项J3=所有隐藏层的神经元相对熵之和
rho_hat=sum(a2,)/m;
KL=sum(sparsityParam*log(sparsityParam./rho_hat)+...
(-sparsityParam)*log((-sparsityParam)./(-rho_hat)));
J3=KL;
% 网络的代价函数
cost=J1+lambda*J2/+beta*J3; %% 反向传播算法计算各层敏感度delta
delta3=-(data-a3).*dsigmoid(z3);
spare_delta=beta*(-sparsityParam./rho_hat+(-sparsityParam)./(-rho_hat));
delta2=bsxfun(@plus,W2'*delta3,spare_delta).*dsigmoid(z2); % 这里加入了稀疏项 %% 计算代价函数对各层权值和偏执项的梯度
W1grad=delta2*a1'/m+lambda*W1;
W2grad=delta3*a2'/m+lambda*W2;
b1grad=sum(delta2,)/m;
b2grad=sum(delta3,)/m; %-------------------------------------------------------------------
% 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 = ./ ( + exp(-x));
end %% 求解sigmoid函数的导数(这里的计算公式一定要注意啊,出过错)
function dsigm = dsigmoid(x)
sigx = sigmoid(x);
dsigm=sigx.*(-sigx);
end

梯度检验函数见另一篇博文

  

  

  

UFLDL教程之(一)sparseae_exercise的更多相关文章

  1. Deep Learning 12_深度学习UFLDL教程:Sparse Coding_exercise(斯坦福大学深度学习教程)

    前言 理论知识:UFLDL教程.Deep learning:二十六(Sparse coding简单理解).Deep learning:二十七(Sparse coding中关于矩阵的范数求导).Deep ...

  2. Deep Learning 19_深度学习UFLDL教程:Convolutional Neural Network_Exercise(斯坦福大学深度学习教程)

    理论知识:Optimization: Stochastic Gradient Descent和Convolutional Neural Network CNN卷积神经网络推导和实现.Deep lear ...

  3. Deep Learning 13_深度学习UFLDL教程:Independent Component Analysis_Exercise(斯坦福大学深度学习教程)

    前言 理论知识:UFLDL教程.Deep learning:三十三(ICA模型).Deep learning:三十九(ICA模型练习) 实验环境:win7, matlab2015b,16G内存,2T机 ...

  4. Deep Learning 11_深度学习UFLDL教程:数据预处理(斯坦福大学深度学习教程)

    理论知识:UFLDL数据预处理和http://www.cnblogs.com/tornadomeet/archive/2013/04/20/3033149.html 数据预处理是深度学习中非常重要的一 ...

  5. Deep Learning 10_深度学习UFLDL教程:Convolution and Pooling_exercise(斯坦福大学深度学习教程)

    前言 理论知识:UFLDL教程和http://www.cnblogs.com/tornadomeet/archive/2013/04/09/3009830.html 实验环境:win7, matlab ...

  6. Deep Learning 9_深度学习UFLDL教程:linear decoder_exercise(斯坦福大学深度学习教程)

    前言 实验内容:Exercise:Learning color features with Sparse Autoencoders.即:利用线性解码器,从100000张8*8的RGB图像块中提取颜色特 ...

  7. Deep Learning 8_深度学习UFLDL教程:Stacked Autocoders and Implement deep networks for digit classification_Exercise(斯坦福大学深度学习教程)

    前言 1.理论知识:UFLDL教程.Deep learning:十六(deep networks) 2.实验环境:win7, matlab2015b,16G内存,2T硬盘 3.实验内容:Exercis ...

  8. Deep Learning 7_深度学习UFLDL教程:Self-Taught Learning_Exercise(斯坦福大学深度学习教程)

    前言 理论知识:自我学习 练习环境:win7, matlab2015b,16G内存,2T硬盘 练习内容及步骤:Exercise:Self-Taught Learning.具体如下: 一是用29404个 ...

  9. Deep Learning 5_深度学习UFLDL教程:PCA and Whitening_Exercise(斯坦福大学深度学习教程)

    前言 本文是基于Exercise:PCA and Whitening的练习. 理论知识见:UFLDL教程. 实验内容:从10张512*512自然图像中随机选取10000个12*12的图像块(patch ...

随机推荐

  1. c#类的初始化顺序

    本文转载:http://www.cnblogs.com/ybhcolin/archive/2010/09/24/1834219.html c#类的初始化顺序 类在初始化时的执行顺序,依次如下: 1: ...

  2. 【转】Android 混淆代码总结

    http://blog.csdn.net/lovexjyong/article/details/24652085 为了防止自己的劳动成果被别人窃取,混淆代码能有效防止被反编译,下面来总结以下混淆代码的 ...

  3. Oracle约束操作

    约束的概念: 约束是在表中定义的用于维护数据库完整性的一些规则.通过为表中的字段定义约 束,可以防止将错误的数据插入到表中. 注意: 1.如果某个约束只作用于单独的字段,既可以在字段级定义约束,也可以 ...

  4. java注释 命名 数据类型 基本类型转换 位运算符 逻辑运算符 三目运算符

    一.java注释 1.单行注释  //注释内容 2.多行注释 /*注释内容*/ 3.文档注释(可用javadoc工具生成api文档,不过我还没试过)/**文档注释*/,文档注释可以在使用的时候看见注释 ...

  5. angularjs filter cut string

    angular.module('App.controllers.MyCtrl', []) .controller('MyCtrl', function (my) {}) .filter('cut', ...

  6. PHP的无限栏目分类

    自己在PHP的无线栏目分类上面就是搞了很久都没有明白,所以现在是趁着记忆力还没有完全的消退的时候速度的记录下来 这里讲解的是最简单的树形栏目,适合的是小中型的栏目分类需求 1.这里讲解的是针对是只要通 ...

  7. 浅谈Mamcached集成web项目

    1.资源文件配置 config.properties 添加 #memcached服务器地址 memchchedIP=192.168.1.8 2.编写工具类 MemUtils package cn.co ...

  8. DataGrid列的合并

    /// <summary> /// DataGrid列的合并 /// 注意:1.DataGrid在绑定的时候进行分组和排序,才能让相同的行放在一起 /// 2.方法应用的时机,应该在Dat ...

  9. [上传下载] C#FileUp文件上传类 (转载)

    点击下载 FileUp.zip 主要功能如下 .把上传的文件转换为字节数组 .流转化为字节数组 .上传文件根据FileUpload控件上传 .把Byte流上传到指定目录并保存为文件 看下面代码吧 // ...

  10. Quartz-2D绘图之路径(Paths)详解

    在上篇文章中,我们简单的理解了绘图上下文,今天我们来认识一下Quartz-2D中另一个重要的概念,路径(Paths). 一.理解路径 路径定义了一个或多个形状,或是子路径.一个子路径可由直线,曲线,或 ...