1 Vectorization 简述

Vectorization 翻译过来就是向量化,各简单的理解就是实现矩阵计算。
为什么MATLAB叫MATLAB?大概就是Matrix Lab,最根本的差别于其它通用语言的地方就是MATLAB能够用最直观的方式实现矩阵运算。MATLAB的变量都能够是矩阵。
通过Vectorization,我们能够将代码变得极其简洁。尽管简洁带来的问题就是其它人看你代码就须要研究一番了。但不论什么让事情变得simple的事情都是值得去做的。

关于Vectorization核心在于代码的实现,以下我们直接通过Linear Regression和Logistic Regression的练习来看看怎样Vectorization。

2 Linear Regression的Vectorization

基本的不同点就是计算cost function和gradient的方法。

先看看一般的通过循环计算的方法:
function [f,g] = linear_regression(theta, X,y)
%
% Arguments:
% theta - A vector containing the parameter values to optimize.
% X - The examples stored in a matrix.
% X(i,j) is the i'th coordinate of the j'th example.
% y - The target value for each example. y(j) is the target for example j.
% m=size(X,2);
n=size(X,1); f=0;
g=zeros(size(theta)); %
% TODO: Compute the linear regression objective by looping over the examples in X.
% Store the objective function value in 'f'.
%
% TODO: Compute the gradient of the objective with respect to theta by looping over
% the examples in X and adding up the gradient for each example. Store the
% computed gradient in 'g'. %%% YOUR CODE HERE %%% % Step 1 : Compute f cost function
for i = 1:m
f = f + (theta' * X(:,i) - y(i))^2;
end f = 1/2*f; % Step 2: Compute gradient for j = 1:n
for i = 1:m
g(j) = g(j) + X(j,i)*(theta' * X(:,i) - y(i));
end end

再来看Vectorization的方法:

function [f,g] = linear_regression_vec(theta, X,y)
%
% Arguments:
% theta - A vector containing the parameter values to optimize.
% X - The examples stored in a matrix.
% X(i,j) is the i'th coordinate of the j'th example.
% y - The target value for each example. y(j) is the target for example j.
%
m=size(X,2); % initialize objective value and gradient.
f = 0;
g = zeros(size(theta)); %
% TODO: Compute the linear regression objective function and gradient
% using vectorized code. (It will be just a few lines of code!)
% Store the objective function value in 'f', and the gradient in 'g'.
%
%%% YOUR CODE HERE %%%
f = 1/2*sum((theta'*X - y).^2); g = X*(theta'*X - y)';

能够看到。这里仅仅须要一条语句就搞定了。

怎样思考Vectorization?
我认为最简单的方法就是看Vector的size。

比方f,我们最后要得到的是一个值。theta是nx1,X是nxm,y是1xm。我们须要theta和X相乘得到1xm好和y相减,那么肯定得把theta转置。theta‘xX 的size变化就1xnxnxm = 1xm,这就是我们想要的。
得到1xm之后,因为f的值,我们使用sum函数得到
对于gradient。也是一样的道理。

g为nx1,而theta’xX-y为1xm,为了和X相乘。必须转置为mx1,从而nxmxmx1 = nx1.

方法就是这样。
以下直接贴出logistic_regression_vec.m
function [f,g] = logistic_regression_vec(theta, X,y)
%
% Arguments:
% theta - A column vector containing the parameter values to optimize.
% 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); % initialize objective value and gradient.
f = 0;
g = zeros(size(theta)); %
% TODO: Compute the logistic regression objective function and gradient
% using vectorized code. (It will be just a few lines of code!)
% Store the objective function value in 'f', and the gradient in 'g'.
%
%%% YOUR CODE HERE %%%
f = -sum(y.*log(sigmoid(theta'*X)) + (1-y).*log(1 - sigmoid(theta'*X)));
g = X*(sigmoid(theta'*X) - y)';

得到的结果一样,但速度变快非常多

Optimization took 6.675841 seconds.
Training accuracy: 100.0%
Test accuracy: 100.0%
本节到此结束。

【说明:本文为原创文章,转载请注明出处 blog.csdn.net/songrotek 欢迎交流QQ:363523441】

深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 3:Vectorization的更多相关文章

  1. 深度学习 Deep Learning UFLDL 最新 Tutorial 学习笔记 1:Linear Regression

    1 前言 Andrew Ng的UFLDL在2014年9月底更新了. 对于開始研究Deep Learning的童鞋们来说这真的是极大的好消息! 新的Tutorial相比旧的Tutorial添加了Conv ...

  2. 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 5:Softmax Regression

    Softmax Regression Tutorial地址:http://ufldl.stanford.edu/tutorial/supervised/SoftmaxRegression/ 从本节開始 ...

  3. 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 4:Debugging: Gradient Checking

    1 Gradient Checking 说明 前面我们已经实现了Linear Regression和Logistic Regression.关键在于代价函数Cost Function和其梯度Gradi ...

  4. 【深度学习Deep Learning】资料大全

    最近在学深度学习相关的东西,在网上搜集到了一些不错的资料,现在汇总一下: Free Online Books  by Yoshua Bengio, Ian Goodfellow and Aaron C ...

  5. 机器学习(Machine Learning)&深度学习(Deep Learning)资料【转】

    转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...

  6. 机器学习(Machine Learning)&amp;深度学习(Deep Learning)资料

    机器学习(Machine Learning)&深度学习(Deep Learning)资料 機器學習.深度學習方面不錯的資料,轉載. 原作:https://github.com/ty4z2008 ...

  7. 机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)

    ##机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)---#####注:机器学习资料[篇目一](https://github.co ...

  8. 机器学习——深度学习(Deep Learning)

    Deep Learning是机器学习中一个非常接近AI的领域,其动机在于建立.模拟人脑进行分析学习的神经网络,近期研究了机器学习中一些深度学习的相关知识,本文给出一些非常实用的资料和心得. Key W ...

  9. (转)深度学习(Deep Learning, DL)的相关资料总结

    from:http://blog.sciencenet.cn/blog-830496-679604.html 深度学习(Deep Learning,DL)的相关资料总结 有人认为DL是人工智能的一场革 ...

随机推荐

  1. P3908 异或之和

    题目描述 求1 \bigoplus 2 \bigoplus\cdots\bigoplus N1⨁2⨁⋯⨁N 的值. A \bigoplus BA⨁B 即AA , BB 按位异或. 输入输出格式 输入格 ...

  2. Spatial Pyramid Matching

    转自:http://blog.csdn.net/jwh_bupt/article/details/9625469 SPM 全称是Spatial Pyramid Matching,出现的背景是bag o ...

  3. 织梦(dedecms)彩色标签云(tag)随机颜色和字体大小

    1.选择你所要加页面的模板,一般是在首页index.htm 加上如下代码 01 <!-- /下面开始tag标签云 --> 02 <dl class="tbox light& ...

  4. 【Linux端口大全】

    2端口:管理实用程序 3端口:压缩进程 5端口:远程作业登录 7端口:回显 9端口:丢弃 11端口:在线用户 13端口:时间 17端口:每日引用 18端口:消息发送协议 19端口:字符发生器 20端口 ...

  5. OCP-1Z0-051-题目解析-第27题

    27. Which two statements are true regarding tables? (Choose two.)  A. A table name can be of any len ...

  6. HDU4596 Yet another end of the world 扩展欧几里德性质

    这题坑了,我真该吃翔啊,竟然一開始方程设错了并且没有去想连列的问题,我真是坑货,做不出就该又一次理一下嘛.操蛋. 题意:给了N组x,y,z然后 问你是否存在两个或者两个以上的id,是的 id%x的值在 ...

  7. AutoLayout具体解释+手把手实战

    首先说一下这篇博客尽管是标记为原创,可是事实并不是本人亲自写出来的.知识点和样例本人花了一天各处查找和整理终于决定写一个汇总的具体解释,解去各位朋友到处盲目查找的必要,由于不是转载某一个人的内容.故此 ...

  8. C++ 不输入回车就不结束输入的实现方法

    方法一 char mystr[128]; mystr[0] = '\0'; char c = getchar(); int i=0; while(c!='\n') { mystr[i] = c; c ...

  9. U盘版Windows 10已经在亚马逊Amazon開始接受预订啦

    Windows 10定于下周7月29日正式公布. Windows 10家庭版119美元.专业版199美元,这个价格包含 Windows 10 授权.

  10. Excel显示当前日期

    https://zhidao.baidu.com/question/431460329693825764.html 直接选中单元格,在公示栏输入=now()