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.