-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc99d97
commit a4bd175
Showing
7 changed files
with
115 additions
and
190 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,18 @@ | ||
clc; | ||
clear; | ||
|
||
function result = square(number) | ||
result = number * number; | ||
end | ||
function value = sigmoid(matrix) | ||
value = 1./ (1 + exp(-matrix)); | ||
endfunction | ||
|
||
function [sq, cube] = squareAndCube(number) | ||
sq = square(number); | ||
cube = sq * number; | ||
end | ||
X = [1 1 ; 1 2 ; 1 3]; | ||
all_theta = [0 0 ; 1 1 ; 2 2 ; 3 3]; | ||
|
||
function cost = costFunction(x, hypothesis, result) | ||
trainingExamles = size(x)(1); | ||
predictions = x * hypothesis; | ||
squareErrors = (predictions - result) .^ 2; | ||
cost = 0.5 / trainingExamles * sum(squareErrors); | ||
end | ||
hypotheses = X * all_theta'; | ||
disp(hypotheses); | ||
probabilities = sigmoid(hypotheses); | ||
disp(probabilities); | ||
|
||
|
||
vector = [1 ; 2; 3; 4; 5]; | ||
|
||
for i = 1:length(vector), | ||
vector(i) = 2^i; | ||
end | ||
|
||
disp(vector); | ||
disp(square(3)) | ||
[sq, c] = squareAndCube(10); | ||
disp(sq) | ||
|
||
x = [1 1 ; 1 2 ; 1 3]; | ||
hypothesis = [100 ; 0]; | ||
y = [1 ; 2; 3]; | ||
disp(costFunction(x, hypothesis, y)); | ||
|
||
|
||
disp(computeCost(x, y, hypothesis)); | ||
gradientDescent() | ||
[maxProbabilities index] = max(probabilities, [], 2); | ||
disp(maxProbabilities); | ||
disp(index); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,35 @@ | ||
function [J, grad] = lrCostFunction(theta, X, y, lambda) | ||
%LRCOSTFUNCTION Compute cost and gradient for logistic regression with | ||
%regularization | ||
% J = LRCOSTFUNCTION(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 | ||
% | ||
% Hint: The computation of the cost function and gradients can be | ||
% efficiently vectorized. For example, consider the computation | ||
% | ||
% sigmoid(X * theta) | ||
% | ||
% Each row of the resulting matrix will contain the value of the | ||
% prediction for that example. You can make use of this to vectorize | ||
% the cost function and gradient computations. | ||
% | ||
% Hint: When computing the gradient of the regularized cost function, | ||
% there're many possible vectorized solutions, but one solution | ||
% looks like: | ||
% grad = (unregularized gradient for logistic regression) | ||
% temp = theta; | ||
% temp(1) = 0; % because we don't add anything for j = 0 | ||
% grad = grad + YOUR_CODE_HERE (using the temp variable) | ||
% | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
% ============================================================= | ||
|
||
grad = grad(:); | ||
|
||
%LRCOSTFUNCTION Compute cost and gradient for logistic regression with | ||
%regularization | ||
% J = LRCOSTFUNCTION(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. | ||
|
||
function J = logisticRegressionRegularizedCost(theta, X, y) | ||
estimatedResults = sigmoid(X * theta); | ||
trainingExamples = length(y); | ||
|
||
J = (- 1 / trainingExamples) * ( | ||
y' * log(estimatedResults) | ||
+ (1 - y)' * log(1 - estimatedResults) | ||
) + (lambda / (2 * trainingExamples)) * ( | ||
sum(theta .^ 2) - theta(1) ^ 2 | ||
); | ||
endfunction | ||
|
||
function gradient = gradientVector(theta, X, y) | ||
trainingExamples = length(y); | ||
gradient = (1 / trainingExamples) * (X' * (sigmoid(X * theta) - y)); | ||
endfunction | ||
|
||
function gradient = regularizedGradientVector(theta, X, y) | ||
trainingExamples = length(y); | ||
gradient = gradientVector(theta, X, y); | ||
modifiedHypothesis = (lambda / trainingExamples) * theta; | ||
modifiedHypothesis(1) = 0; | ||
gradient += modifiedHypothesis; | ||
endfunction | ||
|
||
J = logisticRegressionRegularizedCost(theta, X, y); | ||
grad = regularizedGradientVector(theta, X, y); | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,27 @@ | ||
function [all_theta] = oneVsAll(X, y, num_labels, lambda) | ||
%ONEVSALL trains multiple logistic regression classifiers and returns all | ||
%the classifiers in a matrix all_theta, where the i-th row of all_theta | ||
%corresponds to the classifier for label i | ||
% [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels | ||
% logistic regression classifiers and returns each of these classifiers | ||
% in a matrix all_theta, where the i-th row of all_theta corresponds | ||
% to the classifier for label i | ||
|
||
% Some useful variables | ||
m = size(X, 1); | ||
n = size(X, 2); | ||
|
||
% You need to return the following variables correctly | ||
all_theta = zeros(num_labels, n + 1); | ||
|
||
% Add ones to the X data matrix | ||
X = [ones(m, 1) X]; | ||
|
||
% ====================== YOUR CODE HERE ====================== | ||
% Instructions: You should complete the following code to train num_labels | ||
% logistic regression classifiers with regularization | ||
% parameter lambda. | ||
% | ||
% Hint: theta(:) will return a column vector. | ||
% | ||
% Hint: You can use y == c to obtain a vector of 1's and 0's that tell you | ||
% whether the ground truth is true/false for this class. | ||
% | ||
% Note: For this assignment, we recommend using fmincg to optimize the cost | ||
% function. It is okay to use a for-loop (for c = 1:num_labels) to | ||
% loop over the different classes. | ||
% | ||
% fmincg works similarly to fminunc, but is more efficient when we | ||
% are dealing with large number of parameters. | ||
% | ||
% Example Code for fmincg: | ||
% | ||
% % Set Initial theta | ||
% initial_theta = zeros(n + 1, 1); | ||
% | ||
% % Set options for fminunc | ||
% options = optimset('GradObj', 'on', 'MaxIter', 50); | ||
% | ||
% % Run fmincg to obtain the optimal theta | ||
% % This function will return theta and the cost | ||
% [theta] = ... | ||
% fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ... | ||
% initial_theta, options); | ||
% | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
% ========================================================================= | ||
|
||
|
||
%ONEVSALL trains multiple logistic regression classifiers and returns all | ||
%the classifiers in a matrix all_theta, where the i-th row of all_theta | ||
%corresponds to the classifier for label i | ||
% [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels | ||
% logistic regression classifiers and returns each of these classifiers | ||
% in a matrix all_theta, where the i-th row of all_theta corresponds | ||
% to the classifier for label i | ||
|
||
|
||
dataSetSize = size(X, 1); | ||
features = size(X, 2); | ||
all_theta = zeros(num_labels, features + 1); | ||
|
||
% Add ones to the X data matrix | ||
X = [ones(dataSetSize, 1) X]; | ||
|
||
initial_theta = zeros(features + 1, 1); | ||
|
||
% Set options for fminuncg | ||
options = optimset('GradObj', 'on', 'MaxIter', 50); | ||
|
||
for c = 1:num_labels | ||
[theta, cost] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options); | ||
all_theta(c,:) = theta(:); | ||
endfor | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,20 @@ | ||
function p = predictOneVsAll(all_theta, X) | ||
%PREDICT Predict the label for a trained one-vs-all classifier. The labels | ||
%are in the range 1..K, where K = size(all_theta, 1). | ||
% p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions | ||
% for each example in the matrix X. Note that X contains the examples in | ||
% rows. all_theta is a matrix where the i-th row is a trained logistic | ||
% regression theta vector for the i-th class. You should set p to a vector | ||
% of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2 | ||
% for 4 examples) | ||
|
||
m = size(X, 1); | ||
num_labels = size(all_theta, 1); | ||
|
||
% You need to return the following variables correctly | ||
p = zeros(size(X, 1), 1); | ||
|
||
% Add ones to the X data matrix | ||
X = [ones(m, 1) X]; | ||
|
||
% ====================== YOUR CODE HERE ====================== | ||
% Instructions: Complete the following code to make predictions using | ||
% your learned logistic regression parameters (one-vs-all). | ||
% You should set p to a vector of predictions (from 1 to | ||
% num_labels). | ||
% | ||
% Hint: This code can be done all vectorized using the max function. | ||
% In particular, the max function can also return the index of the | ||
% max element, for more information see 'help max'. If your examples | ||
% are in rows, then, you can use max(A, [], 2) to obtain the max | ||
% for each row. | ||
% | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
% ========================================================================= | ||
|
||
|
||
%PREDICT Predict the label for a trained one-vs-all classifier. The labels | ||
%are in the range 1..K, where K = size(all_theta, 1). | ||
% p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions | ||
% for each example in the matrix X. Note that X contains the examples in | ||
% rows. all_theta is a matrix where the i-th row is a trained logistic | ||
% regression theta vector for the i-th class. You should set p to a vector | ||
% of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2 | ||
% for 4 examples) | ||
|
||
trainingDataSize = size(X)(1); | ||
|
||
% Add ones to the X data matrix | ||
X = [ones(trainingDataSize, 1) X]; | ||
|
||
hypotheses = X * all_theta'; | ||
probabilities = sigmoid(hypotheses); | ||
[maxProbability index] = max(probabilities, [], 2); | ||
p = index; | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Created by Octave 5.2.0, Mon Jun 15 03:58:24 2020 GMT <unknown@anishLearnsToCode> | ||
# name: email | ||
# type: sq_string | ||
# elements: 1 | ||
# length: 21 | ||
[email protected] | ||
|
||
|
||
# name: token | ||
# type: sq_string | ||
# elements: 1 | ||
# length: 16 | ||
RV8uyaZ6iH1Bc0On | ||
|
||
|