深度学习 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是人工智能的一场革 ...
随机推荐
- 问题-MethodAddress返回NIL?MethodAddress与published的关系?
问题现象:有位朋友(397085381)在Q群里问“为什么书上的可以访问,而自己写的代码访问时为nil” 问题原因:因为要用“Self.MethodAddress('Test')”访问,方法必须放在“ ...
- tomcat7.0 windows部署使用80端口问题
如果安装有.net的IDE,那么80端口容易被IIS内的站点占用,如果不是则手动查找. 1:在命令行中输入netstat -ano,得到端口号对应的PID pid这么来显示
- mysql行转列转换
http://blog.csdn.net/sinat_27406925/article/details/77507478 mysql 行列转换 ,在项目中应用的极其频繁,尤其是一些金融项目里的报表.其 ...
- C语言 · 周期字串
算法提高 周期字串 时间限制:1.0s 内存限制:256.0MB 问题描述 右右喜欢听故事,但是右右的妈妈总是讲一些“从前有座山,山里有座庙,庙里有个老和尚给小和尚讲故事,讲的什么呢 ...
- Linux 系统位数以及 Linux 软件位数查看
一. Linux系统位数查看: getconf LONG_BIT uname -a 二. 软件位数查看: file ....txt  的一组扩展,为 Internet 上计算机之间的编辑和文件管理提供了标准.利用这个协议用户可以通过Web进行远程的基本文件操作,如拷贝.移动.删除等.在I ...
- 常用css3技巧
H5移动前端开发常用高能css3汇总 1.禁止a标签点击高亮,这些都是非官方属性,但实用性超强 html,body{ -webkit-touch-callout: none; //禁止或显示系 ...
- TiKV 源码解析系列——如何使用 Raft
本系列文章主要面向 TiKV 社区开发者,重点介绍 TiKV 的系统架构,源码结构,流程解析.目的是使得开发者阅读之后,能对 TiKV 项目有一个初步了解,更好的参与进入 TiKV 的开发中. 需要注 ...
- Servlet、Filter、Listener总结
servlet规范提供了一组标准的servlet api.servlet容器就是servlet规范的实现. 1.In Action (1)写一个类继承HttpServlet: (2)重写其中的方法. ...