如何使用八度中的梯度下降来求解逻辑回归?

时间:2019-03-22 15:03:32

标签: machine-learning octave logistic-regression gradient-descent

我正在从Andrews Ng的Coursera学习机器学习课程。我已经编写了八度逻辑回归的代码。但是,它不起作用。有人可以帮我吗?

我从以下链接获取了数据集: Titanic survivors

这是我的代码:

pkg load io;

[An, Tn, Ra, limits] = xlsread("~/ML/ML Practice/dataset/train_and_test2.csv", "Sheet2", "A2:H1000");

# As per CSV file we are reading columns from 1 to 7. 8-th column is Survived, which is what we are going to predict
X = [An(:, [1:7])];

Y = [An(:, 8)];

X = horzcat(ones(size(X,1), 1), X);

# Initializing theta values as zero for all
#theta = zeros(size(X,2),1);
theta = [-3;1;1;-3;1;1;1;1];
learningRate = -0.00021;
#learningRate = -0.00011;

# Step 1: Calculate Hypothesis
function g_z = estimateHypothesis(X, theta)
  z = theta' * X';
  z = z';

  e_z = -1 * power(2.72, z);
  denominator = 1.+e_z;
  g_z = 1./denominator;
endfunction

# Step 2: Calculate Cost function
function cost = estimateCostFunction(hypothesis, Y)
  log_1 = log(hypothesis);
  log_2 = log(1.-hypothesis);

  y1 = Y;
  term_1 = y1.*log_1;

  y2 = 1.-Y;
  term_2 = y2.*log_2;

  cost = term_1 + term_2;
  cost = sum(cost);

  # no.of.rows
  m = size(Y, 1);
  cost = -1 * (cost/m); 

endfunction

# Step 3: Using gradient descent I am updating theta values
function updatedTheta = updateThetaValues(_X, _Y, _theta, _hypothesis, learningRate)
  #s1 = _X * _theta;
  #s2 = s1 - _Y;
  #s3 = _X' * s2;

  # no.of.rows
  #m = size(_Y, 1);

  #s4 = (learningRate * s3)/m;

  #updatedTheta = _theta - s4;

  s1 = _hypothesis - _Y;

  s2 = s1 .* _X;
  s3 = sum(s2);

  # no.of.rows
  m = size(_Y, 1);

  s4 = (learningRate * s3)/m;
  updatedTheta = _theta .- s4';
endfunction

costVector = [];
iterationVector = [];
for i = 1:1000
  # Step 1
  hypothesis = estimateHypothesis(X, theta);
  #disp("hypothesis");
  #disp(hypothesis);

  # Step 2
  cost = estimateCostFunction(hypothesis, Y);
  costVector = vertcat(costVector, cost);
  #disp("Cost");
  #disp(cost);

  # Step 3 - Updating theta values
  theta = updateThetaValues(X, Y, theta, hypothesis, learningRate);

  iterationVector = vertcat(iterationVector, i);
endfor

function plotGraph(iterationVector, costVector)
  plot(iterationVector, costVector);
  ylabel('Cost Function');
  xlabel('Iteration');  
endfunction

plotGraph(iterationVector, costVector);

这是我根据迭代次数和成本函数进行绘制时得到的图形。

Logistic regression graph

通过调整theta值和学习速度,我感到很累。有人可以帮我解决这个问题。

谢谢。

1 个答案:

答案 0 :(得分:0)

我做数学错误。我应该使用power(2.72,-z)或exp(-z)。相反,我已将其用作-1 * power(2.72,z)。现在,我得到一条适当的曲线。

Logistic Regression Cost function curve

谢谢。