다음은 로지스틱 회귀에 대한 일반 그래디언트 디센트에 대한 주석이 달린 코드입니다. 정규화를 도입하려면 비용 및 그라디언트 방정식을 업데이트해야합니다. 이 코드에서, 세타가 매개 변수는, X 클래스의 예측은, 내가이 도움이 :) 희망 y는 클래스 라벨은 알파 학습 속도
입니다
function [theta,J_store] = logistic_gradientDescent(theta, X, y,alpha,numIterations)
% Initialize some useful values
m = length(y); % number of training examples
n = size(X,2); %number of features
J_store = 0;
%J_store = zeros(numIterations,1);
for iter=1:numIterations
%predicts the class labels using the current weights (theta)
Z = X*theta;
h = sigmoid(Z);
%This is the normal cost function equation
J = (1/m).*sum(-y.*log(h) - (1-y).*log(1-h));
%J_store(iter) = J;
%This is the equation to obtain the given the current weights, without regularisation
grad = [(1/m) .* sum(repmat((h - y),1,n).*X)]';
theta = theta - alpha.*grad;
end
end
당신은 개시 아마 더 좋을 것 같아 자신의 구현보다 일부 사전 컴파일 최적화 도구. LBFGS와 conjugate gradient는 바닐라 그래디언트 디센트가 아닌 LR 모델을 정확하게 최적화하는 데 가장 널리 사용되는 알고리즘입니다. 예 : [이 도구 상자] (http://www.di.ens.fr/~mschmidt/Software/minFunc.html). –
질문에 올바르게 태그를 지정하면 (즉, matlab 태그 사용) 다른 사람들이이 질문을 쉽게 찾고 답을 얻을 수있는 기회를 얻게됩니다. – tr9sh
이 질문은 실제로 통계 스택 교환에 대한 더 나은 해답을 얻을 수 있습니다. –