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 (逻辑回归) 有之前学生的数据,建立逻辑回归模型预测,根据两次考试结果预测一个学生是否有资格被大 ...
随机推荐
- BlocksKit(2)-DynamicDelegate
BlocksKit(2)-DynamicDelegate 动态代理可以说是这个Block里面最精彩的一部分了,可以通过自己给一个类的的协议方法指定对应的block来实现让这个协议的回调都直接在bloc ...
- 关于mysql_connect CLIENT_MULTI_RESULTS
自己写了一个mysql存储过程,以为php有用于调用存储过程的内建函数,查了一下发现只能用mysql_query(call pro())这样的方式,我认为从本质上也就相当于在mysql命令行里执行语句 ...
- 【原创】实战padding oracle漏洞
首先关于padding oracle漏洞的原理请看: 步入正传~~ 搭建漏洞利用环境Perl 环境下载地址:链接:http://pan.baidu.com/s/1skFxVm1 密码:anuw 首先查 ...
- bzoj 1176: [Balkan2007]Mokia&&2683: 简单题 -- cdq分治
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要 ...
- Codeforces Round #276 div1 B. Maximum Value Hash 乱搞
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...
- HDU 4709 Herding (枚举)
Herding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- linux内核源码之基础准备篇
http://blog.csdn.net/eastmoon502136/article/details/8711104
- PyQt5 各种菜单实现
# -*- coding: utf-8 -*- # Created by PCITZDF on 2018/4/8 15:36. # FileName: menuandtools.py import s ...
- Java基础加强总结(二)——泛型
一.体验泛型 JDK1.5之前的集合类中存在的问题——可以往集合中加入任意类型的对象,例如下面代码: package cn.gacl.generic.summary; import java.util ...
- 使用jquery加载部分视图01-使用$.get()
使用Html.RenderParital或Html.RenderAction可以在主视图中加载部分视图. 两种方法是有区别的,在"RenderPartial和RenderAction区别&q ...