美文网首页人工智能/模式识别/机器学习精华专题
UFLDL新版教程与编程练习(二):Logistic Regre

UFLDL新版教程与编程练习(二):Logistic Regre

作者: 赖子啊 | 来源:发表于2019-08-06 16:54 被阅读3次

    UFLDL是吴恩达团队编写的较早的一门深度学习入门,里面理论加上练习的节奏非常好,每次都想快点看完理论去动手编写练习,因为他帮你打好了整个代码框架,也有详细的注释,所以我们只要实现一点核心的代码编写工作就行了,上手快!

    有人对这一篇也有翻译,可以看一下

    第二节是:Logistic Regression(逻辑回归)

    我们之前看过的Linear Regression(线性回归)是争对连续值预测的,但是这并不能很好地处理二分类问题,也就是y^{(i)} \in\{0,1\}的问题。我们就需要使用另外一个“hypothesis class”来预测当给了一个样本,有多少的可能性它是属于0还是属于1,其实“逻辑回归”名字上有回归二字,其实是一个分类算法。以下是要学习的函数:
    \begin{array}{l}{P(y=1 | x)=h_{\theta}(x)=\frac{1}{1+\exp \left(-\theta^{\top} x\right)} \equiv \sigma\left(\theta^{\top} x\right)} \\ {P(y=0 | x)=1-P(y=1 | x)=1-h_{\theta}(x)}\end{array}
    其中的\sigma(z) \equiv \frac{1}{1+\exp (-z)}就是大名鼎鼎的sigmoid函数,输出介于0和1之间,所以我们也可以把h_{\theta}(x)理解为y=1的概率,1-h_{\theta}(x)就是y=0的概率。

    我们要优化的目标函数是这样的,是一个二分类的交叉熵:
    J(\theta)=-\sum_{i}\left(y^{(i)} \log \left(h_{\theta}\left(x^{(i)}\right)\right)+\left(1-y^{(i)}\right) \log \left(1-h_{\theta}\left(x^{(i)}\right)\right)\right)
    可以分析得到,当y^{(i)}\in1的时候,要减少cost的值的话,那就得让h_{\theta}\left(x^{(i)}\right)增大,即让y是1的概率增大;y^{(i)}\in0的时候亦然。

    我们可以推导出梯度的表达式和线性回归里面一样,一开始我也很惊讶,后来推了一下确实是如此的,大伙可以自己推一下,实在不明白下面留言。只是这里的hypothesis function是h_{\theta}(x)=\sigma\left(\theta^{\top} x\right),不再是线性回归里面的h_{\theta}(x)=\theta^{\top} x
    \frac{\partial J(\theta)}{\partial \theta_{j}}=\sum_{i} x_{j}^{(i)}\left(h_{\theta}\left(x^{(i)}\right)-y^{(i)}\right)
    向量化的写法就是:
    \nabla_{\theta} J(\theta)=\sum_{i} x^{(i)}\left(h_{\theta}\left(x^{(i)}\right)-y^{(i)}\right)
    之后也是在logistic_regression.m文件中编写好损失函数和梯度,然后分别赋给fg就可以运行ex1/ex1b_logreg.m了。
    这是我的代码,里面的路径自己改一下,我用的是MATLAB2016a

    function [f,g] = logistic_regression(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);
      n=size(X,1);
      % initialize objective value and gradient.
      f = 0;
      g = zeros(size(theta));
    
    
      %
      % TODO:  Compute the objective function by looping over the dataset and summing
      %        up the objective values for each example.  Store the result in 'f'.
      %
      % TODO:  Compute the gradient of the objective by looping over the dataset and summing
      %        up the gradients (df/dtheta) for each example. Store the result in 'g'.
      %
    %%% MY CODE HERE %%%
    h = inline('1./(1+exp(-z))');
    % Calculate f
    temp = zeros(1,m);
    for i=1:m
        for j=1:n
            temp(i) = temp(i) + theta(j) * X(j,i);
        end
    end
    h_temp = h(temp);
    for i = 1:m
        f = f - (y(i) * log(h_temp(i)) + (1-y(i)) * log(1-h_temp(i)));
    end
    
    % Calculate g
    for j=1:n
        for i=1:m
            g(j) = g(j) + X(j,i) * (h_temp(i) - y(i));
        end
    end
    

    运行结果:


    逻辑回归(非向量化).png

    有理解不到位之处,还请指出,有更好的想法,可以在下方评论交流!

    相关文章

      网友评论

        本文标题:UFLDL新版教程与编程练习(二):Logistic Regre

        本文链接:https://www.haomeiwen.com/subject/naewdctx.html