簡體   English   中英

Matlab正則化Logistic回歸 - 如何計算梯度

[英]Matlab Regularized Logistic Regression - how to compute gradient

我目前正在Coursera平台上進行機器學習,我正在嘗試實現Logistic回歸。 為了實現Logistic回歸,我使用梯度下降來最小化成本函數,我將編寫一個名為costFunctionReg.m的函數,它返回在當前參數集中評估的每個參數的成本和梯度。

問題更好地描述如下:

問題描述

問題描述續

我的成本函數正在運行,但梯度函數卻沒有。 請注意,我更願意使用循環而不是逐個元素的操作來實現它。

我正在分別計算theta[0] (在MATLAB, theta(1) ),因為它沒有被正則化,即我們不使用第一項(使用lambda )。

function [J, grad] = costFunctionReg(theta, X, y, lambda)
    %COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization
    %   J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using
    %   theta as the parameter for regularized logistic regression and the
    %   gradient of the cost w.r.t. to the parameters. 

    % Initialize some useful values
    m = length(y); % number of training examples
    n = length(theta); %number of parameters (features)

    % You need to return the following variables correctly 
    J = 0;
    grad = zeros(size(theta));

    % ====================== YOUR CODE HERE ======================
    % Instructions: Compute the cost of a particular choice of theta.
    %               You should set J to the cost.
    %               Compute the partial derivatives and set grad to the partial
    %               derivatives of the cost w.r.t. each parameter in theta

    % ----------------------1. Compute the cost-------------------
    %hypothesis
    h = sigmoid(X * theta);

    for i = 1 : m
        % The cost for the ith term before regularization
        J = J - ( y(i) * log(h(i)) )   -  ( (1 - y(i)) * log(1 - h(i)) );

        % Adding regularization term
        for j = 2 : n
            J = J + (lambda / (2*m) ) * ( theta(j) )^2;
        end            
    end
    J = J/m; 

    % ----------------------2. Compute the gradients-------------------

    %not regularizing theta[0] i.e. theta(1) in matlab

    j = 1;

    for i = 1 : m
        grad(j) = grad(j) + ( h(i) - y(i) ) * X(i,j);
    end

    for j = 2 : n    
        for i = 1 : m
            grad(j) = grad(j) + ( h(i) - y(i) ) * X(i,j) + lambda * theta(j);
        end    
    end

    grad = (1/m) * grad;

    % =============================================================
end

我究竟做錯了什么?

您應用正則化的方式不正確。 對所有訓練示例求和之后添加正則化而是在每個示例之后添加正則化。 如果您將代碼保留為校正前的代碼,則無意中使梯度步長變大,最終會超出解決方案的范圍。 這過沖將累積並將不可避免地給你的梯度矢量Inf-Inf所有組件(除了偏差項)。

簡單地說,在第二個for循環終止后放置lambda*theta(j)語句:

for j = 2 : n    
    for i = 1 : m
        grad(j) = grad(j) + ( h(i) - y(i) ) * X(i,j); % Change
    end
    grad(j) = grad(j) + lambda * theta(j); % Change
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM