开始学习深度学习了,既然确定目标就要努力前行!为自己加油!——2015.6.11

Sparse Encoder

1.神经网络
概念:假设我们有训练样本集 (x(^ i),y(^ i)) ,那么神经网络算法能够提供一种复杂且非线性的假设模型 h_{W,b}(x) ,它具有参数 W, b ,可以以此参数来拟合我们的数据。
激活函数:
f(z)=sigmoid(z)=1/(1+exp(-z))
导数:f’(z)=f(z)(1-f(z)) 很重要,求代价函数极值的时候要用到
模型:
一个简单的神经网络,只有输入层,一个隐藏层和输出层组成。每加一层就相当于对输入多进行一次非线性处理,进而形成复杂的目标函数hw,b(x).
(https://img-blog.csdn.net/20150611084555088)
目标值从前往后计算:
Z2=W1*data+b1
a2=f(Z2)
Z3=W2*a2+b2
a3=f(Z3)
目标函数的代价函数:
第一部分是:直接误差——m个输入的平均误差
第二部分是:权值惩罚——所有W元素的平方和,目的是为了减少权重的幅度,防止过度拟合
(https://img-blog.csdn.net/20150611085816673)
为使代价函数最小,可以使用批梯度下降法,从而确定参数W1,W2,b1,b2。
步骤:(1)给W1,W2,b1,b2初始值:初始值设计很关键,否则将会得到不好的结果,练习里面是这样设计的:

r  = sqrt(6) / sqrt(hiddenSize+visibleSize+1);   % we'll choose weights uniformly from the interval [-r, r]
W1 = rand(hiddenSize, visibleSize) * 2 * r - r;
W2 = rand(visibleSize, hiddenSize) * 2 * r - r;b1 = zeros(hiddenSize, 1);
b2 = zeros(visibleSize, 1);

(2)需要给出代价函数:即上述代码公式
(3)需要给出代价函数对W和b的偏导

说明:这里需要的是后面有的1/m括号里的部分。括号里的第一部分其实就是每个输入对W,b求导值的平均。
2、反向传导
这部分很关键,用于计算每个输入对的求导。为了对W求导,先对Z求导,对Z求导的结果就是残差。从后向前计算,有点类似于计算图的关键路径的计算方法。

3、稀疏自编码器
代价函数和W偏导与普通神经网络有一点区别,代价函数需要加入稀疏代价。

心得:感觉稀疏自编码器就是对输入进行压缩表示,前面编码,最后一层解码。做个实验试了一下两个隐藏层的情况,想第一次发现边,第二次发现拐角,然而效果好差!翻了一下教程,发现后面有专门的栈式自编码器,汗~~~不过至少说明自己思考的方向是对滴~

代码完成中出现的问题:
错误总结:

Jcost=(0.5/m)*sum(sum((a3-data).^2));
%%正确sum((a3-data).^2),写成sum(a3-data).^2导致错误,找了好久的原因啊,原来是因为一对括号!
Jweight=0.5*(sum(sum(W1.^2))+sum(sum(W2.^2)));
%%写成了Jweight=0.5*sum(sum(W1.^2))+sum(sum(W2.^2));还是少了括号!!

经验:
minFun的用法

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 = 400;    % Maximum number of iterations of L-BFGS to run
options.display = 'on';[opttheta, cost] = minFunc( @(p) sparseAutoencoderCost(p, ...visibleSize, hiddenSize, ...lambda, sparsityParam, ...beta, patches), ...theta, options);%需要提供一个返回代价函数和偏导的函数sparseAutoencoderCost

sparseAutoencoderCost.h

function [cost,grad] = sparseAutoencoderCost(theta, visibleSize, hiddenSize, ...lambda, sparsityParam, beta, data)% visibleSize: the number of input units (probably 64)
% hiddenSize: the number of hidden units (probably 25)
% 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(1:hiddenSize*visibleSize), hiddenSize, visibleSize);
W2 = reshape(theta(hiddenSize*visibleSize+1:2*hiddenSize*visibleSize), visibleSize, hiddenSize);
b1 = theta(2*hiddenSize*visibleSize+1:2*hiddenSize*visibleSize+hiddenSize);
b2 = theta(2*hiddenSize*visibleSize+hiddenSize+1:end);% Cost and gradient variables (your code needs to compute these values).
% Here, we initialize them to zeros.
cost = 0;
W1grad = zeros(size(W1));
W2grad = zeros(size(W2));
b1grad = zeros(size(b1));
b2grad = zeros(size(b2));%% ---------- 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.
%
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%ych代码
Jcost = 0;%直接误差
Jweight = 0;%权值惩罚
Jsparse = 0;%稀疏性惩罚
[n m] = size(data);%m为样本的个数,n为样本的特征数
%
% %前向算法计算各神经网络节点的线性组合值和active值
z2=W1*data+repmat(b1,1,m);
a2=sigmoid(z2);
z3=W2*a2+repmat(b2,1,m);
a3=sigmoid(z3);Jcost=(0.5/m)*sum(sum((a3-data).^2));   %%正确sum((a3-data).^2),写成sum(a3-data).^2导致错误,找了好久的原因啊,原来是因为一对括号!Jweight=0.5*(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).*(sigmoid(z3).*(1-sigmoid(z3)));
sterm = beta*(-sparsityParam./rho+(1-sparsityParam)./(1-rho));
d2=(W2'*d3+repmat(sterm,1,m)).*(sigmoid(z2).*(1-sigmoid(z2)));W1grad=(1/m).*(d2*data')+lambda.*W1;
W2grad=(1/m).*(d3*a2')+lambda.*W2;
b1grad=(1/m).*sum(d2,2);
b2grad=(1/m).*sum(d3,2);
%-------------------------------------------------------------------
% 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(:)];endfunction sigm = sigmoid(x)sigm = 1 ./ (1 + exp(-x));
end

本文参考:http://ufldl.stanford.edu/wiki/index.php/UFLDL%E6%95%99%E7%A8%8B
http://www.cnblogs.com/tornadomeet/tag/Deep%20Learning/

第一次写博客,内容有些凌乱,格式也不规范,当做自己的学习笔记,不当之处敬请指正

深度学习笔记一:稀疏自编码器相关推荐

  1. 深度学习笔记之稀疏自编码器

    深度学习笔记之稀疏自编码器 引言 引子:题目描述 正确答案: A B C D \mathcal A \mathcal B \mathcal C \mathcal D ABCD 题目解析 介绍:自编码器 ...

  2. 2020-4-22 深度学习笔记20 - 深度生成模型 5 (有向生成网络--sigmoid信念网络/可微生成器网络/变分自编码器VAE/生产对抗网络GAN/生成矩匹配网络)

    第二十章 深度生成模型 Deep Generative Models 中文 英文 2020-4-17 深度学习笔记20 - 深度生成模型 1 (玻尔兹曼机,受限玻尔兹曼机RBM) 2020-4-18 ...

  3. 《繁凡的深度学习笔记》前言、目录大纲 一文让你完全弄懂深度学习所有基础(DL笔记整理系列)

    <繁凡的深度学习笔记>前言.目录大纲 (DL笔记整理系列) 一文弄懂深度学习所有基础 ! 3043331995@qq.com https://fanfansann.blog.csdn.ne ...

  4. UFLDL深度学习笔记 (三)无监督特征学习

    UFLDL深度学习笔记 (三)无监督特征学习 1. 主题思路 "UFLDL 无监督特征学习"本节全称为自我学习与无监督特征学习,和前一节softmax回归很类似,所以本篇笔记会比较 ...

  5. 深度学习笔记4:深度神经网络的正则化

    出处:数据科学家养成记 深度学习笔记4:深度神经网络的正则化 恍恍惚惚,又20天没写了.今天笔者要写的是关于机器学习和深度学习中的一项关键技术:正则化.相信在机器学习领域摸爬滚打多年的你一定知道正则化 ...

  6. 深度学习笔记(26) 卷积神经网络

    深度学习笔记(26) 卷积神经网络 1. CONV 2. POOL 3. Layer 4. FC 5. 卷积的优势 1. CONV 假设,有一张大小为32×32×3的输入图片,这是一张RGB模式的图片 ...

  7. 深度学习笔记(4) 浅层神经网络

    深度学习笔记(4) 浅层神经网络 1. 神经网络概述 2. 激活函数 3. 激活函数的导数 4. 神经网络的梯度下降 5. 随机初始化 1. 神经网络概述 神经网络看起来是如下: 有输入特征x1.x2 ...

  8. HALCON 21.11:深度学习笔记---语义分割/边缘提取(12)

    HALCON 21.11:深度学习笔记---语义分割/边缘提取(12) HALCON 21.11.0.0中,实现了深度学习方法. 本章介绍了如何使用基于深度学习的语义分割,包括训练和推理阶段. 通过语 ...

  9. HALCON 20.11:深度学习笔记(12)---语义分割

    HALCON 20.11:深度学习笔记(12)--- 语义分割 HALCON 20.11.0.0中,实现了深度学习方法. 本章解释了如何使用基于深度学习的语义分割,包括训练和推理阶段. 通过语义分割, ...

  10. 深度学习笔记其五:卷积神经网络和PYTORCH

    深度学习笔记其五:卷积神经网络和PYTORCH 1. 从全连接层到卷积 1.1 不变性 1.2 多层感知机的限制 1.2.1 平移不变性 1.2.2 局部性 1.3 卷积 1.4 "沃尔多在 ...

最新文章

  1. 使用CNN做文本分类——将图像2维卷积换成1维
  2. Adobe Captivate 2019中文版
  3. NEO从源码分析看NEOVM
  4. 如何在Linux中安装和使用Silver Searcher(程序员的代码搜索工具)
  5. 账户配置 三: Gmail
  6. 用EnumMap代替序数索引
  7. 轻量化网络:SqueezeNext
  8. 164 Maximum Gap 最大间距
  9. java poi设置单元格格式为数值_java中导出excel设置单元格的样式为数字格式怎样设置?...
  10. 云画质助手iApp源码
  11. 【硬件】PIC32单片机烧写器以及线序说明
  12. 程炳皓很明智陈一舟不吃亏
  13. 正心,修身,方能齐家,治国,平天下
  14. What is event bubbling and capturing?
  15. Android 推流--分辨率、帧率和码率三者之间的关系
  16. 数据管理执行指南 | 你需要知道什么?
  17. 【FlashDB】第二步 FlashDB 移植 STM32L475 使用QSPI驱动外部 flash W25Q64之 SFUD 移植
  18. 2021 百度网盘网页版 倍速播放技巧(亲测有效)
  19. python抓主力资金_【邢不行|量化小讲堂系列09-Python量化入门】通过逐笔数据计算主力资金流数据...
  20. IntelliJ IDEA更换代码字体为Consolas

热门文章

  1. mysql 表结构关系_mysql 表关系 与 修改表结构
  2. 深入理解JVM虚拟机(六):虚拟机类加载机制
  3. 生命银行怎么样_银行双职工的家庭现状...
  4. 通过模拟器看Windows Phone 7
  5. Markdown基础语法小结
  6. Web运行控制台输出乱码解决总结
  7. 常用前端框架Angular和React的一些认识
  8. openssl 使用命令
  9. HTML 表格tablecaptionthtrtdtheadtbodytfootcolcolgroup
  10. linux-security-limits