深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 5:Softmax Regression
Softmax Regression Tutorial地址:http://ufldl.stanford.edu/tutorial/supervised/SoftmaxRegression/
从本节開始,难度開始加大了。我将更具体地解释一下这个Tutorial。
1 Softmax Regression 介绍
其实。我们仅仅要把Logistic Regression练习中的样本换成手的样本。那么就能用训练出来的结果来识别手了,因此Logistic Regression是非常实用且强大的算法。
2 Cost Function
然后假设k=2就是仅仅有0或1,能够推出Logistic Regression的Cost Function是上面公式的特殊形式。
在Softmax Regression 中,有
P(y(i)=k|x(i);θ)=exp(θ(k)⊤x(i))∑Kj=1exp(θ(j)⊤x(i))
然后给出theta偏导的公式:
这里 ∇θ(k)J(θ) 本身是向量, 因此它的第j个元素是 ∂J(θ)∂θlk , J(θ) 关于 θ(k)的第j个元素的偏导。
3 Softmax Regression參数化的属性
4 Weight Decay 权值衰减
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc29uZ3JvdGVr/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="500" alt="" />
5 Softmax Regression vs. k Binary Classifiers
。。
这些类是相关的,就不能用Softmax Regression来攻克了。
6 exercise解答
方法和前面的练习都是一样的。最困难的问题在于怎样用Vectorization来将Cost Function和Gradient表达出来。
以下是我的解答,仅仅列出softmax_regression_vec.m
function [f,g] = softmax_regression_vec(theta, X,y)
%
% Arguments:
% theta - A vector containing the parameter values to optimize.
% In minFunc, theta is reshaped to a long vector. So we need to
% resize it to an n-by-(num_classes-1) matrix.
% Recall that we assume theta(:,num_classes) = 0.
%
% X - The examples stored in a matrix.
% X(i,j) is the i'th coordinate of the j'th example.
% y - The label for each example. y(j) is the j'th example's label.
%
m=size(X,2);
n=size(X,1); % theta is a vector; need to reshape to n x num_classes.
theta=reshape(theta, n, []);
num_classes=size(theta,2)+1;
theta = [theta,zeros(n,1)]; % initialize objective value and gradient.
f = 0;
g = zeros(size(theta)); %
% TODO: Compute the softmax objective function and gradient using vectorized code.
% Store the objective function value in 'f', and the gradient in 'g'.
% Before returning g, make sure you form it back into a vector with g=g(:);
%
%%% YOUR CODE HERE %%% yCompare = full(sparse(y, 1:m, 1)); %??y == k ?? ? %yCompare = yCompare(1:num_classes-1,:); % ? ?y = 10??? M = exp(theta'*X);
p = bsxfun(@rdivide, M, sum(M));
f = - yCompare(:)' * log(p(:)); g = - X*(yCompare - p)';
g = g(:,1:num_classes - 1); g=g(:); % make gradient a vector for minFunc
怎样解释是个比較麻烦的问题,我推出的方法还是通过矩阵的size。
首先cost function有两个连加号,这意味着假设每个计算得出一个值,cost function能够得到一个kxm的矩阵,而yCompare就是kxm,因此后面的概率项也应该如此。theta‘*X是非常easy想到的,得到kxm,而对于概率项的分母,我们得这样理解:kxm每个列就是某一个样本相应于每个类的数据,我们因此对于分母项的求法非常easy。就是用sum把每一列的数据加起来。
其它的推导是一样的道理。
执行结果为:
Average error :0.000005 (Gradient Checking 结果显示梯度计算没有问题)
Training accuracy: 94.4%
Test accuracy: 92.2%
这里有一些实用的MATLAB函数须要关注一下:
full 和 sparse。举比例如以下:
>> y = [1 2 3]
y =
1 2 3
>> sparse(y,1:3,1)
ans =
(1,1) 1
(2,2) 1
(3,3) 1
>> full(sparse(y,1:3,1))
ans =
1 0 0
0 1 0
0 0 1
而bsxfun能够用来做矩阵的各种运算,非常快!
非常多函数假设不清楚一种就是直接在MATLAB help,一种那就是直接百度了。
【本文为原创文章,转载请注明出处:blog.csdn.net/songrotek 欢迎交流QQ:363523441】
深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 5:Softmax Regression的更多相关文章
- 深度学习 Deep Learning UFLDL 最新 Tutorial 学习笔记 1:Linear Regression
1 前言 Andrew Ng的UFLDL在2014年9月底更新了. 对于開始研究Deep Learning的童鞋们来说这真的是极大的好消息! 新的Tutorial相比旧的Tutorial添加了Conv ...
- 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 3:Vectorization
1 Vectorization 简述 Vectorization 翻译过来就是向量化,各简单的理解就是实现矩阵计算. 为什么MATLAB叫MATLAB?大概就是Matrix Lab,最根本的差别于其它 ...
- 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 4:Debugging: Gradient Checking
1 Gradient Checking 说明 前面我们已经实现了Linear Regression和Logistic Regression.关键在于代价函数Cost Function和其梯度Gradi ...
- 【深度学习Deep Learning】资料大全
最近在学深度学习相关的东西,在网上搜集到了一些不错的资料,现在汇总一下: Free Online Books by Yoshua Bengio, Ian Goodfellow and Aaron C ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料【转】
转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料
机器学习(Machine Learning)&深度学习(Deep Learning)资料 機器學習.深度學習方面不錯的資料,轉載. 原作:https://github.com/ty4z2008 ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)
##机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)---#####注:机器学习资料[篇目一](https://github.co ...
- 机器学习——深度学习(Deep Learning)
Deep Learning是机器学习中一个非常接近AI的领域,其动机在于建立.模拟人脑进行分析学习的神经网络,近期研究了机器学习中一些深度学习的相关知识,本文给出一些非常实用的资料和心得. Key W ...
- (转)深度学习(Deep Learning, DL)的相关资料总结
from:http://blog.sciencenet.cn/blog-830496-679604.html 深度学习(Deep Learning,DL)的相关资料总结 有人认为DL是人工智能的一场革 ...
随机推荐
- MDK 的编译过程及文件类型全解
MDK 的编译过程及文件类型全解 ------(在arm9的开发中,这些东西都是我们自己搞定的,但是在windows上,IDE帮我们做好了,了解这些对深入开发是很有帮助的,在有arm9开发的基础上,下 ...
- Cents os 7下如何安装bzip2
# Cents os 7下如何安装bzip2 ### 安装```yum search bzip2 //查询安装包 yum -y install bzip2.x86_64 ``` ### 原因---- ...
- ubuntu18安装ubuntu kylin软件中心
Install ubuntu-kylin-software-center Installing ubuntu-kylin-software-center package on Ubuntu 14.04 ...
- C#学习笔记(8)——委托应用(显示,写入时间)
说明(2017-5-30 09:08:10): 1. 定义一个委托,public delegate void MyDel();无参数,无返回值. 2. 委托作为DoSth的参数,DoSth里面调用委托 ...
- iOS微信实现第三方登录的方法
这篇文章主要介绍了iOS微信第三方登录实现的全过程,一步一步告诉大家iOS微信实现第三方登录的方法,感兴趣的小伙伴们可以参考一下 一.接入微信第三方登录准备工作.移动应用微信登录是基于OAuth2 ...
- bootstarp-table 设置隐藏列
在做隐藏列的时候会发现一个问题. var settings = { url: ctx + "rollapply/list", pageSize: 10, queryParams: ...
- WPF样式继承
场景:样式A和样式B的背景颜色一样,但是文字颜色不一样 <Style x:key="BaseStyle" TargetType="Button"> ...
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
在使用selenium处理中文网页或者网页标题是中文的时候,出现UnicodeEncodeError: 'ascii' codec can't encode characters in positio ...
- Free-form语言
在计算机编程领域,程序指令文本中的字符在『纸面』上所处的位置无关紧要 - 不像老式的穿孔卡片系统(punched card system)程序指令文本需要放置在指定列,这种编程语言就可算是自由形式语言 ...
- 如何使用sendEmail发送邮件
sendEmail是一个轻量级,命令行的SMTP邮件客户端.如果你需要使用命令行发送邮件,那么sendEmail是非常完美的选择:使用简单并且功能强大.这个被设计用在php.bashperl和web站 ...