博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 3:Vectorization
阅读量:4123 次
发布时间:2019-05-25

本文共 3388 字,大约阅读时间需要 11 分钟。

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 functionfor i = 1:m    f = f + (theta' * X(:,i) - y(i))^2;endf = 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】
你可能感兴趣的文章
Valid Parentheses --括号匹配
查看>>
Remove Element--原地移除重复元素
查看>>
Remove Duplicates from Sorted Array--从有序数组中移除重复元素
查看>>
Count and Say
查看>>
Gas Station
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
Valid Palindrome 简单的回文判断
查看>>
Pascal's Triangle -- 生成杨辉三角
查看>>
Pascal's Triangle II 生成杨辉三角中的某行
查看>>
Minimum Depth of Binary Tree -- 二叉树的最小深度 DFS 加剪枝
查看>>
Climbing Stairs 爬楼梯方法 动态规划
查看>>
Merge Two Sorted Lists 合并两个有序链表
查看>>
pow(x,n) 为什么错这么多次
查看>>
Jump Game 动态规划
查看>>
Binary Tree Maximum Path Sum 自底向上求解(重重重重)
查看>>
Subsets 深搜
查看>>
Subsets II
查看>>
Edit Distance 字符串距离(重重)
查看>>
Gray Code 格雷码
查看>>
对话周鸿袆:从程序员创业谈起
查看>>