Permalink
Cannot retrieve contributors at this time
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?
curve_fitting/cost_logistic.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
34 lines (27 sloc)
1.04 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |