Standford《Machine Learning》 编程练习2

来源:互联网 发布:淘宝外卖会员卡怎么用 编辑:程序博客网 时间:2024/06/05 12:04

1.1 Visualizing the data

这段主要练习一下如何用plot画散点图。其实很简单,但是当时由于不知道“find”这个函数还是想了一会的。(要积累基础啊!!)
问题:给定m*2矩阵X,m*1向量y,y为label向量,其元素值为1或者0,要求以X(:,1)(i)为纵坐标,X(:,2)(j)为横坐标,根据y(i)的值画不同标记。
其实思路很简单,找出y中所有为1的点的index,然后按照这些index画出类型一的点,y=0的亦然。代码如下:
figure; hold on;
pos = find(y==1); neg = find(y==0);
plot(X(pos,1),X(pos,2),'k+');
plot(X(neg,1),X(neg,2),'ro');

关键不知道find这个函数,想怎么分离y中的元素想了好久。。。(囧)

1.2.1 

1.2.2 Cost function and gradient

跟之前的作业一样,将公式转换为MatLab表达式即可。注意矩阵的转换。
用到了sum(),sum(X,index),exp(),log() 函数。

1.2.3 Learning parameters using fminunc

这一段主要训练了一下fminunc的基本用法。fminunc是MatLab/Octave里面自带的求优化解的函数。其定义如下:
“fminunc finds a local minimum of a function of several variables.”【摘自MatLab】
fminunc本身很强大,有很多种用法。这个作业用到的是其中的一种,提供:“1.目标函数 2.函数各参数的梯度” 给fminunc用于求解的方法。(输入参数和“梯度下降法”是一样的,不过内部实现比梯度下降要复杂)
所以,首先我们需要定义一个目标函数,使得fminunc可以在这个函数上寻找其局部最优解。
在这里,很明显,目标函数为costFunction。
前面我们已经把costFunction实现好了:

function [J, grad] = costFunction(theta, X, y)

m = length(y); % number of training examples

J = 0;

grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================


hThetaX = sigmoid(X*theta);

J = (1/m)*sum((-y.*log(hThetaX)-(1-y).*log(1-hThetaX)));

tempM = repmat(hThetaX - y,1,length(grad));

grad = (1/m)*sum(tempM.*X,1)';

% =============================================================

end

可以看到,costFunction的输入有三个,分别是函数自变量theta,以及X,y两组数据。那么现在问题来了,我们如何让fminunc知道我们要求的是costFunction关于theta的局部最优解呢?
MatLab的官方指南如下:
If FUN is parameterized, you can use anonymous functions to capture the
    problem-dependent parameters. Suppose you want to minimize the 
    objective given in the function myfun, which is parameterized by its 
    second argument c. Here myfun is a MATLAB file function such as
 
      function [f,g] = myfun(x,c)
 
      f = c*x(1)^2 + 2*x(1)*x(2) + x(2)^2; % function
      g = [2*c*x(1) + 2*x(2)               % gradient
           2*x(1) + 2*x(2)];
 
    To optimize for a specific value of c, first assign the value to c. 
    Then create a one-argument anonymous function that captures that value 
    of c and calls myfun with two arguments. Finally, pass this anonymous 
    function to fminunc:
 
      c = 3;                              % define parameter first
      options = optimoptions('fminunc','GradObj','on'); % indicate gradient is provided 
      x = fminunc(@(x) myfun(x,c),[1;1],options)
 所以,在需要指定problem-dependent parameters时,我们可以通过构造带参数的匿名函数封装原目标函数,在匿名函数的参数里面指定这些problem-dependent parameters,就像Dr. Ng的PDF里面这样:@(t) ( costFunction(t, X, y) )  。
我们通过@(t)(function((t,...))来告诉fminunc,t是关于目标函数function的自变量。

剩下的没啥了,写作业的时候看到这里不懂为什么要用@(t)(fucntion)封装costFunction,就在MatLab里面查了查。



0 0
原创粉丝点击