深度学习 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是人工智能的一场革 ...
随机推荐
- 03-老马jQuery教程-DOM操作
jQuery DOM操作 在没有jQuery之前,DOM的操作相对来说有点麻烦,尤其是DOM节点的搜索.目前我们已经学习了jQuery的选择器,接下带大家一块学习jQuery的DOM操作,jQuery ...
- C#学习笔记(28)——委托排序(2)自定义排序
说明(2017-11-21 15:24:50): 1. 定义一个排序方法,参数是字符串数组,和委托.MySort(nums, string.Compare),调用时只需要更换里面的委托方法就行,或者直 ...
- WebAPI 消息处理器
由上图可以看出消息处理器的使用场合和使用方法. 使用场合: HttpServer 得到请求时. public static class WebApiConfig { public static voi ...
- WCF终结点——终结点地址(EndpointAddress)
终结点的地址的Uri属性作为终结点地址的唯一标示. 包括客户端终结点和服务端终结点. 一.服务端终结点: 服务端的终结点通过宿主的添加方法暴露出来,从而成为可以调用的资源. 下面是将服务绑定到宿主的代 ...
- 微信小程序学习资料
官方文档 https://mp.weixin.qq.com/debug/wxadoc/dev/ W3CSchool的教程 https://www.w3cschool.cn/weixinapp/ 更多参 ...
- [echo]echo输出换行
echo -e "hello\nworld", -e处理转义字符
- 【Python】 linecache模块读取文件
[linecache] 过往在读取文件的时候,我们通常使用的是这种模式: with open('file.txt','r') as f: line = f.readline() while line: ...
- python 基础干货 02
list 与 tuple list 类似 数组 tuple 跟 list 一样, 只是一旦定义, 里边的内容不可以改变. 这样, 上边的内容就不可以改变了. "可变的" tuple ...
- CentosMySQL5.6安装方法
1. download rpm包先确定系统版本[root@xcldtc5m /]# cat /proc/versionLinux version 2.6.32-431.el6.x86_64 (mock ...
- Enums and Lookup Tables with EF Code First
With EntityFramework’s support for enums, there is no longer any need to include lookup tables in th ...