Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
function [J, grad] = cost_logistic(a, x, y)
% cost_logistic Compute cost and gradient for logistic regression
% J = cost_logistic(theta, X, y) computes the cost of using theta as the
% parameter for logistic regression and the gradient of the cost
% w.r.t. to the parameters.
% Initialize some useful values
N = length(x); % number of training examples
% You need to return the following variables correctly
J = 0;
grad = zeros(size(a));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of a.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
%
e = exp(1);
sigma = @(t) 1./(1 + e.^(-t));
a0 = a(1);
a1 = a(2);
t = a0+a1*x;
J = 1/N*sum(-y.*log(sigma(t))-(1-y).*log(1-sigma(t)));
grad(1) = 1/N*sum((sigma(t)-y).*x.^0);
grad(2) = 1/N*sum((sigma(t)-y).*x.^1);
% =============================================================
end