Как решить логистическую регрессию с использованием градиентного спуска в октаве? - PullRequest
0 голосов
/ 22 марта 2019

Я изучаю курс машинного обучения от Coursera от Andrews Ng. Я написал код для логистической регрессии в октаве. Но это не работает. Кто-нибудь может мне помочь?

Я взял набор данных по следующей ссылке: Титаник выживших

Вот мой код:

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

Я устал от настройки значений тета и скорости обучения. Может кто-нибудь помочь мне решить эту проблему.

Спасибо.

1 Ответ

0 голосов
/ 23 марта 2019

Я сделал математическую ошибку. Я должен был использовать либо power (2.72, -z), либо exp (-z). Вместо этого я использовал -1 * мощность (2,72, z). Теперь я получаю правильную кривую.

Logistic Regression Cost function curve

Спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...