0
votes

I am doing 4th week assignment in Andrew Ng's Machine Learning course on coursera. I am supposed to compute cost function and gradient of regularized logistic regression. Here is what I wrote:

function [J, grad] = lrCostFunction(theta, X, y, lambda)
m = length(y); % number of training examples
J = 0;
grad = zeros(size(theta));

n=length(theta);
thetat=theta';
t=thetat(:,[2:n]);
t=t';

J = (-1/m*(sum((log(sigmoid(X*theta)))'*y)+((log(1-sigmoid(X*theta)))'*(1-y))))+((lambda/(2*m))*(sum(t.^2)));

grad=(1/m)*(X'*(sigmoid(X*theta)-y))+((lambda/m)*theta);
grad(1)=(1/m)*(sum(sigmoid(X*theta)-y));

end

I am getting this error:

!! Submission failed: operator *: nonconformant arguments (op1 is 16x3, op2 is 4x3)


Function: lrCostFunction
FileName: c:\users\syed\desktop\machine-learning-ex3\ex3\lrCostFunction.m
LineNumber: 19

Please correct your code and resubmit.

I am unable to understand where I am going wrong because I had the same kind of question in week 3 where I had to compute cost function and gradient and the above code worked.

1

1 Answers

1
votes

you are doing an inner product of a 16x3 matrix with a 4x3 matrix in line 19 as the error is saying itself. transpose the second matrix and this error will gone.

when you want to do inner product second dimension of first matrix/vector should match the first dimension of second matrix/vector.

you have some mistakes in calculation of cost, it is element wise product , use .* instead of * , and your parenthesis formation is also wrong, compare it with this and see if there are any mistakes:

-1/m * sum( ( y .* log(y^) ) + ( (1-y) .* (log(1-y^)) ) )