matlab - Logistic regression - Calculating cost function returns wrong results -


i started taking andrew ng's course on machine learning on coursera. topic of third week logistic regression, trying implement following cost function.

cost function

the hypothesis defined as: hypothesis

where g sigmoid function: sigmoid

this how function looks @ moment:

function [j, grad] = costfunction(theta, x, y)  m = length(y); % number of training examples s = 0; j = 0;  i=1:m     yi = y(i);     xi = x(i,:);     h = sigmoid(transpose(theta).*xi);     s = s + ((-yi)*log(h)-((1-yi)*log(1-h))); end  j = s/m;  end 

given following values

x = [magic(3) ; magic(3)]; y = [1 0 1 0 1 0]'; [j g] = costfunction([0 1 0]', x, y) 

j returns 0.6931 2.6067 0.6931 though result should j = 2.6067. assuming there problem xi, can't see error.

i thankful if point me right direction.

you supposed apply sigmoid function dot product of parameter vector (theta) , input vector (xi, in case row vector). so, should change

h = sigmoid(transpose(theta).*xi); 

to

h = sigmoid(theta' * xi'); % or sigmoid(xi * theta) 

of course, need make sure bias input 1 added inputs (a row of 1s x).

next, think how can vectorize entire operation can written without loops. way considerably faster.


Comments