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】