ufldl学习笔记与编程作业:Softmax Regression(vectorization加速)
ufldl学习笔记与编程作业:Softmax Regression(vectorization加速)
ufldl出了新教程,感觉比之前的好。从基础讲起。系统清晰,又有编程实践。
在deep learning高质量群里面听一些前辈说,不必深究其它机器学习的算法,能够直接来学dl。
于是近期就開始搞这个了。教程加上matlab编程,就是完美啊。
新教程的地址是:http://ufldl.stanford.edu/tutorial/
本节是对ufldl学习笔记与编程作业:Softmax Regression(softmax回归)版本号的改进。
哈哈,把向量化的写法给写出来了,尼玛好快啊。
仅仅须要2分钟。200迭代就跑完了。
昨晚的for循环写法跑了我1个半小时。
事实上实现向量化写法,要把各种矩阵给在纸上写出来。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGluZ2VybGFubGFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">
1 感谢tornadomeet,尽管他做的是旧教程的实验,可是从他那里学了几个matlab函数。http://www.cnblogs.com/tornadomeet/archive/2013/03/23/2977621.html
比方sparse和full。‘
2 还有从旧教程http://deeplearning.stanford.edu/wiki/index.php/Exercise:Softmax_Regression
学了
% M is the matrix as described in the text
M = bsxfun(@rdivide, M, sum(M))
3 新教程学到了
I=sub2ind(size(A), 1:size(A,1), y);
values = A(I);
下面是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本来是矩阵,传參的时候,theta(:)这样进来的。是一个vector。仅仅有一列,如今我们得把她变为矩阵
% theta is a vector; need to reshape to n x num_classes.
theta=reshape(theta, n, []);
num_classes=size(theta,2)+1; % initialize objective value and gradient.
f = 0;
g = zeros(size(theta)); h = theta'*X;%h(k,i)第k个theta。第i个样本
a = exp(h);
a = [a;ones(1,size(a,2))];%加1行
p = bsxfun(@rdivide,a,sum(a));
c = log2(p);
i = sub2ind(size(c), y,[1:size(c,2)]);
values = c(i);
f = -sum(values); d = full(sparse(1:m,y,1));
d = d(:,1:(size(d,2)-1));
p = p(1:(size(p,1)-1),:);%减1行
g = X*(p'.-d); %
% 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 %%% g=g(:); % make gradient a vector for minFunc
本文作者:linger
本文链接:http://blog.csdn.net/lingerlanlan/article/details/38425929
ufldl学习笔记与编程作业:Softmax Regression(vectorization加速)的更多相关文章
- ufldl学习笔记和编程作业:Softmax Regression(softmax回报)
ufldl学习笔记与编程作业:Softmax Regression(softmax回归) ufldl出了新教程.感觉比之前的好,从基础讲起.系统清晰,又有编程实践. 在deep learning高质量 ...
- ufldl学习笔记与编程作业:Logistic Regression(逻辑回归)
ufldl学习笔记与编程作业:Logistic Regression(逻辑回归) ufldl出了新教程,感觉比之前的好,从基础讲起.系统清晰,又有编程实践. 在deep learning高质量群里面听 ...
- ufldl学习笔记与编程作业:Linear Regression(线性回归)
ufldl学习笔记与编程作业:Linear Regression(线性回归) ufldl出了新教程,感觉比之前的好.从基础讲起.系统清晰,又有编程实践. 在deep learning高质量群里面听一些 ...
- ufldl学习笔记与编程作业:Multi-Layer Neural Network(多层神经网络+识别手写体编程)
ufldl学习笔记与编程作业:Multi-Layer Neural Network(多层神经网络+识别手写体编程) ufldl出了新教程,感觉比之前的好,从基础讲起,系统清晰,又有编程实践. 在dee ...
- ufldl学习笔记和编程作业:Feature Extraction Using Convolution,Pooling(卷积和汇集特征提取)
ufldl学习笔记与编程作业:Feature Extraction Using Convolution,Pooling(卷积和池化抽取特征) ufldl出了新教程,感觉比之前的好,从基础讲起.系统清晰 ...
- 学习笔记TF024:TensorFlow实现Softmax Regression(回归)识别手写数字
TensorFlow实现Softmax Regression(回归)识别手写数字.MNIST(Mixed National Institute of Standards and Technology ...
- UFLDL深度学习笔记 (二)SoftMax 回归(矩阵化推导)
UFLDL深度学习笔记 (二)Softmax 回归 本文为学习"UFLDL Softmax回归"的笔记与代码实现,文中略过了对代价函数求偏导的过程,本篇笔记主要补充求偏导步骤的详细 ...
- Andrew Ng机器学习编程作业: Linear Regression
编程作业有两个文件 1.machine-learning-live-scripts(此为脚本文件方便作业) 2.machine-learning-ex1(此为作业文件) 将这两个文件解压拖入matla ...
- Andrew Ng机器学习编程作业:Logistic Regression
编程作业文件: machine-learning-ex2 1. Logistic Regression (逻辑回归) 有之前学生的数据,建立逻辑回归模型预测,根据两次考试结果预测一个学生是否有资格被大 ...
随机推荐
- PHP中CGI,FastCGI,PHP-CGI与PHP-FPM对比
CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...
- 2017-2018-1 JAVA实验站 第八周作业
2017-2018-1 JAVA实验站 第八周作业 详情请见团队博客
- Treap树理解
title: Treap树理解 comments: true date: 2016-10-06 07:57:37 categories: 算法 tags: Treap树 树 Treap树理解 简介 随 ...
- python—第三库的安装方法
Windows系统下安装第三方Python库的三种方法: 1.使用easy_install命令安装 一般在安装完Python后再C:\Python27\Scripts 目录下有 easy_instal ...
- HAproxy和TIME WAIT的一次问题排查
近日平稳运行了将近4年的发号器突然出现问题,在元旦0分的时候出现短暂的性能下降,导致发号失败率飙高到一个不可接收的值,哎,意外总是发生在你想不到的地方. 这几天赶紧和小伙伴们赶紧追查原因,制定改造方案 ...
- Tasker to answer incoming call by pressing power button
nowadays, the smartphone is getting bigger in size, eg. samsung galaxy note and note 2, sorta big in ...
- STM32 3.3V参考电压 TL431/MC1403/LM385
TL431作为一个高性价比的常用分流式电压基准,有很广泛的用途. 图(1)是TL431的典型接法,输出一个固定电压值,计算公式是: Vout = ( (R1+R2) / R2 ) * 2.5 V 同时 ...
- oracle 取整的几种方法
--1.取整(大) select ceil(-1.001) value from dual ; --2.取整(小) select floor(-1.001) value from dual ...
- java中的几种对象(PO,VO,DAO,BO,POJO)
一.PO :(persistant object ),持久对象 可以看成是与数据库中的表相映射的java对象.使用Hibernate来生成PO是不错的选择. 二.VO :(value object) ...
- Bootstrap 3之美02-Grid简介和应用
本篇主要包括: ■ Grid简介■ 应用Grid■ Multiple Grids Grid简介 Bootstrap中,把页面分成12等份,这就是所谓的Grid. 在Bootstrap中,用类名控 ...