-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
function [J, grad] = costFunction(theta, X, y) | ||
%COSTFUNCTION Compute cost and gradient for logistic regression | ||
% J = COSTFUNCTION(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 | ||
m = length(y); % number of training examples | ||
|
||
% You need to return the following variables correctly | ||
J = 0; | ||
grad = zeros(size(theta)); | ||
|
||
% ====================== YOUR CODE HERE ====================== | ||
% Instructions: Compute the cost of a particular choice of theta. | ||
% You should set J to the cost. | ||
% 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 | ||
% | ||
|
||
|
||
J = 1/m * ( -y' * log(sigmoid(X*theta)) - (1-y)' * log(1 - sigmoid(X*theta))); | ||
|
||
grad = X' * (sigmoid(X*theta)-y) ./ m; | ||
|
||
|
||
|
||
|
||
% ============================================================= | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
function [J, grad] = costFunctionReg(theta, X, y, lambda) | ||
%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization | ||
% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using | ||
% theta as the parameter for regularized logistic regression and the | ||
% gradient of the cost w.r.t. to the parameters. | ||
|
||
% Initialize some useful values | ||
m = length(y); % number of training examples | ||
|
||
% You need to return the following variables correctly | ||
J = 0; | ||
grad = zeros(size(theta)); | ||
|
||
% ====================== YOUR CODE HERE ====================== | ||
% Instructions: Compute the cost of a particular choice of theta. | ||
% You should set J to the cost. | ||
% Compute the partial derivatives and set grad to the partial | ||
% derivatives of the cost w.r.t. each parameter in theta | ||
|
||
% do not regularize on the Theta0 | ||
theta_1 = theta; | ||
theta_1(1) = 0; | ||
|
||
J = 1/m * ( -y' * log(sigmoid(X*theta)) - (1-y)' * log(1 - sigmoid(X*theta))) + lambda/(2*m) * sum (theta_1 .* theta_1); | ||
|
||
grad = (X' * (sigmoid(X*theta)-y) + lambda .* theta_1)./ m; | ||
|
||
% ============================================================= | ||
|
||
end |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function [X_norm, mu, sigma] = featureNormalize(X) | ||
%FEATURENORMALIZE Normalizes the features in X | ||
% FEATURENORMALIZE(X) returns a normalized version of X where | ||
% the mean value of each feature is 0 and the standard deviation | ||
% is 1. This is often a good preprocessing step to do when | ||
% working with learning algorithms. | ||
|
||
mu = mean(X); | ||
X_norm = bsxfun(@minus, X, mu); | ||
|
||
sigma = std(X_norm); | ||
X_norm = bsxfun(@rdivide, X_norm, sigma); | ||
|
||
|
||
% ============================================================ | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function p = predict(theta, X) | ||
%PREDICT Predict whether the label is 0 or 1 using learned logistic | ||
%regression parameters theta | ||
% p = PREDICT(theta, X) computes the predictions for X using a | ||
% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1) | ||
|
||
m = size(X, 1); % Number of training examples | ||
|
||
% You need to return the following variables correctly | ||
p = zeros(m, 1); | ||
|
||
% ====================== YOUR CODE HERE ====================== | ||
% Instructions: Complete the following code to make predictions using | ||
% your learned logistic regression parameters. | ||
% You should set p to a vector of 0's and 1's | ||
% | ||
|
||
p = sigmoid(X*theta) >= 0.5; | ||
|
||
|
||
|
||
% ========================================================================= | ||
|
||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function g = sigmoid(z) | ||
%SIGMOID Compute sigmoid functoon | ||
% J = SIGMOID(z) computes the sigmoid of z. | ||
|
||
% You need to return the following variables correctly | ||
g = zeros(size(z)); | ||
|
||
% ====================== YOUR CODE HERE ====================== | ||
% Instructions: Compute the sigmoid of each value of z (z can be a matrix, | ||
% vector or scalar). | ||
|
||
|
||
g = 1 ./ (1 + exp(-1 .* z)); | ||
|
||
|
||
% ============================================================= | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
% Cry Detector Training | ||
|
||
data = load('data.txt'); | ||
|
||
% Split data to train, test and crossvalidation sets | ||
total_count = size(data,1); | ||
data = data(randperm(total_count), :); | ||
|
||
train_count = int32(total_count*0.6); | ||
test_count = int32(total_count*0.2); | ||
cv_count = total_count - train_count - test_count; | ||
|
||
|
||
Xtrain = data(1:train_count, 1:128); | ||
ytrain = data(1:train_count, 129); | ||
|
||
Xtest = data(train_count+1:train_count+test_count, 1:128); | ||
ytest = data(train_count+1:train_count+test_count, 129); | ||
rtest = data(train_count+1:train_count+test_count, 130); | ||
|
||
Xval = data(train_count+test_count+1:total_count, 1:128); | ||
yval = data(train_count+test_count+1:total_count, 129); | ||
|
||
X = Xtrain; | ||
y = ytrain; | ||
|
||
% Xval = featureNormalize(Xval); | ||
% yval = featureNormalize(yval); | ||
|
||
#lambda_vec = [0, 0.1, 0.5, 1, 10, 50, 100 1000 5000 10000 30000 50000 60000 100000]'; | ||
lambda_vec = [1000 5000 10000 30000]'; | ||
#error_train = zeros(length(lambda_vec), 1); | ||
#error_val = zeros(length(lambda_vec), 1); | ||
|
||
[m, n] = size(X); | ||
X = [ones(m, 1) X]; % Add intercept term to X | ||
Xval = [ones(cv_count, 1) Xval]; % Add intercept term to Xval | ||
Xtest = [ones(test_count, 1) Xtest]; | ||
|
||
% Compute and display initial cost and gradient | ||
initial_theta = zeros(n + 1, 1); % Initialize fitting parameters | ||
[cost, grad] = costFunctionReg(initial_theta, X, y, 0); | ||
fprintf('Cost at initial theta (zeros): %f\n', cost); | ||
|
||
options = optimset('GradObj', 'on', 'MaxIter', 400); | ||
[theta, cost] = fminunc(@(t)(costFunctionReg(t, X, y, 0)), initial_theta, options); | ||
|
||
% error_train = zeros(size(1:50:m,2), 1); | ||
% error_val = zeros(size(1:50:m,2), 1); | ||
% for k=1:50:m | ||
% fprintf('%d\n', k); | ||
% Xtrain = X(1:k,:); | ||
% ytrain = y(1:k); | ||
% [theta, cost] = fminunc(@(t)(costFunctionReg(t, Xtrain, ytrain, 0)), initial_theta, options); | ||
% error_train(int32(k/50)+1) = 100 * (1 - mean(predict(theta, Xtrain) == ytrain)); | ||
% error_val(int32(k/50)+1) = 100 * (1 - mean(predict(theta, Xval) == yval)); | ||
%end | ||
|
||
% plot(1:50:m, error_train, 1:50:m, error_val); | ||
% title('Learning curve for linear regression') | ||
% legend('Train', 'Cross Validation') | ||
% xlabel('Number of training examples') | ||
% ylabel('Error') | ||
|
||
% Selecting right lambda | ||
% options = optimset('GradObj', 'on', 'MaxIter', 10000); | ||
% for k = 1:length(lambda_vec) | ||
% lambda = lambda_vec(k); | ||
% [theta, cost] = fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options); | ||
% error_train(k) = 100 * (1 - mean(predict(theta, X) == y)); | ||
% error_val(k) = 100 * (1 - mean(predict(theta, Xval) == yval)); | ||
% error_train(k) | ||
% error_val(k) | ||
% fprintf('%f Cost at theta found by fminunc: %f\n', lambda, cost); | ||
%end | ||
|
||
% plot(lambda_vec, error_train, lambda_vec, error_val); | ||
% title('Learning curve for linear regression') | ||
% legend('Train', 'Cross Validation') | ||
% xlabel('lambda') | ||
% ylabel('Error') | ||
|
||
|
||
% Compute accuracy on our training set | ||
p = predict(theta, Xtest); | ||
|
||
fprintf('Train Accuracy: %f \n', mean(double(p == ytest)) * 100); | ||
fprintf('Total size %d \n', length(rtest == 1)); | ||
fprintf('Total right predicted %d \n', sum(predict(theta, Xtest) == ytest)); | ||
fprintf('Right predicted cries: %f \n', sum((predict(theta, Xtest) == ytest)(ytest==1))); | ||
fprintf('Right predicted nocries: %f \n', sum((predict(theta, Xtest) == ytest)(ytest==0))); | ||
fprintf('Not predicted cries: %f \n', sum((predict(theta, Xtest) != ytest)(ytest==1))); | ||
fprintf('Wrong predicted cries: %f \n', sum((predict(theta, Xtest) != ytest)(ytest==0))); | ||
fprintf('Total Cry %d\n', sum(ytest)); | ||
fprintf('Total NoCry %d\n', sum(ytest==0)); | ||
pause | ||
|
||
theta |