アンドリュー教授の機械学習コースをいくつか受講し、ニュートン法を使用したロジスティック回帰のトランスクリプトを見ました。ただし、勾配降下法を使用してロジスティック回帰を実装する場合、特定の問題に直面します。
生成されたグラフは凸ではありません。
私のコードは次のようになります。
方程式のベクトル化された実装を使用しています。
%1. The below code would load the data present in your desktop to the octave memory
x=load('ex4x.dat');
y=load('ex4y.dat');
%2. Now we want to add a column x0 with all the rows as value 1 into the matrix.
%First take the length
m=length(y);
x=[ones(m,1),x];
alpha=0.1;
max_iter=100;
g=inline('1.0 ./ (1.0 + exp(-z))');
theta = zeros(size(x(1,:)))'; % the theta has to be a 3*1 matrix so that it can multiply by x that is m*3 matrix
j=zeros(max_iter,1); % j is a zero matrix that is used to store the theta cost function j(theta)
for num_iter=1:max_iter
% Now we calculate the hx or hypothetis, It is calculated here inside no. of iteration because the hupothesis has to be calculated for new theta for every iteration
z=x*theta;
h=g(z); % Here the effect of inline function we used earlier will reflect
j(num_iter)=(1/m)*(-y'* log(h) - (1 - y)'*log(1-h)) ; % This formula is the vectorized form of the cost function J(theta) This calculates the cost function
j
grad=(1/m) * x' * (h-y); % This formula is the gradient descent formula that calculates the theta value.
theta=theta - alpha .* grad; % Actual Calculation for theta
theta
end
コードごとにエラーは発生しませんが、適切な凸グラフは生成されません。
誰かが間違いを指摘したり、問題の原因についての洞察を共有したりできれば幸いです.
ありがとう