Skip to content

Commit a4bd175

Browse files
solves week 4 programmign exercise
1 parent fc99d97 commit a4bd175

File tree

7 files changed

+115
-190
lines changed

7 files changed

+115
-190
lines changed

test.m

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
11
clc;
2+
clear;
23

3-
function result = square(number)
4-
result = number * number;
5-
end
4+
function value = sigmoid(matrix)
5+
value = 1./ (1 + exp(-matrix));
6+
endfunction
67

7-
function [sq, cube] = squareAndCube(number)
8-
sq = square(number);
9-
cube = sq * number;
10-
end
8+
X = [1 1 ; 1 2 ; 1 3];
9+
all_theta = [0 0 ; 1 1 ; 2 2 ; 3 3];
1110

12-
function cost = costFunction(x, hypothesis, result)
13-
trainingExamles = size(x)(1);
14-
predictions = x * hypothesis;
15-
squareErrors = (predictions - result) .^ 2;
16-
cost = 0.5 / trainingExamles * sum(squareErrors);
17-
end
11+
hypotheses = X * all_theta';
12+
disp(hypotheses);
13+
probabilities = sigmoid(hypotheses);
14+
disp(probabilities);
1815

19-
20-
vector = [1 ; 2; 3; 4; 5];
21-
22-
for i = 1:length(vector),
23-
vector(i) = 2^i;
24-
end
25-
26-
disp(vector);
27-
disp(square(3))
28-
[sq, c] = squareAndCube(10);
29-
disp(sq)
30-
31-
x = [1 1 ; 1 2 ; 1 3];
32-
hypothesis = [100 ; 0];
33-
y = [1 ; 2; 3];
34-
disp(costFunction(x, hypothesis, y));
35-
36-
37-
disp(computeCost(x, y, hypothesis));
38-
gradientDescent()
16+
[maxProbabilities index] = max(probabilities, [], 2);
17+
disp(maxProbabilities);
18+
disp(index);

week3/regularized-logistic-regression.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
trainingSamples = length(y);
1616
J = -(1 / trainingSamples) * sum(
1717
y .* log(estimatedResults)
18-
+ (1 - y) .* log(estimatedResults)
18+
+ (1 - y) .* log(1 - estimatedResults)
1919
);
2020
endfunction
2121

@@ -25,7 +25,7 @@
2525

2626
J = (- 1 / trainingSamples) * sum(
2727
y .* log(estimatedResults)
28-
+ (1 - y) .* log(estimatedResults)
28+
+ (1 - y) .* log(1 - estimatedResults)
2929
) + (regularizationFactor() / (2 * trainingSamples)) * (
3030
sum(theta .^ 2) - theta(1) ^ 2
3131
);
Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,35 @@
11
function [J, grad] = lrCostFunction(theta, X, y, lambda)
2-
%LRCOSTFUNCTION Compute cost and gradient for logistic regression with
3-
%regularization
4-
% J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using
5-
% theta as the parameter for regularized logistic regression and the
6-
% gradient of the cost w.r.t. to the parameters.
7-
8-
% Initialize some useful values
9-
m = length(y); % number of training examples
10-
11-
% You need to return the following variables correctly
12-
J = 0;
13-
grad = zeros(size(theta));
14-
15-
% ====================== YOUR CODE HERE ======================
16-
% Instructions: Compute the cost of a particular choice of theta.
17-
% You should set J to the cost.
18-
% Compute the partial derivatives and set grad to the partial
19-
% derivatives of the cost w.r.t. each parameter in theta
20-
%
21-
% Hint: The computation of the cost function and gradients can be
22-
% efficiently vectorized. For example, consider the computation
23-
%
24-
% sigmoid(X * theta)
25-
%
26-
% Each row of the resulting matrix will contain the value of the
27-
% prediction for that example. You can make use of this to vectorize
28-
% the cost function and gradient computations.
29-
%
30-
% Hint: When computing the gradient of the regularized cost function,
31-
% there're many possible vectorized solutions, but one solution
32-
% looks like:
33-
% grad = (unregularized gradient for logistic regression)
34-
% temp = theta;
35-
% temp(1) = 0; % because we don't add anything for j = 0
36-
% grad = grad + YOUR_CODE_HERE (using the temp variable)
37-
%
38-
39-
40-
41-
42-
43-
44-
45-
46-
47-
48-
% =============================================================
49-
50-
grad = grad(:);
51-
2+
%LRCOSTFUNCTION Compute cost and gradient for logistic regression with
3+
%regularization
4+
% J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using
5+
% theta as the parameter for regularized logistic regression and the
6+
% gradient of the cost w.r.t. to the parameters.
7+
8+
function J = logisticRegressionRegularizedCost(theta, X, y)
9+
estimatedResults = sigmoid(X * theta);
10+
trainingExamples = length(y);
11+
12+
J = (- 1 / trainingExamples) * (
13+
y' * log(estimatedResults)
14+
+ (1 - y)' * log(1 - estimatedResults)
15+
) + (lambda / (2 * trainingExamples)) * (
16+
sum(theta .^ 2) - theta(1) ^ 2
17+
);
18+
endfunction
19+
20+
function gradient = gradientVector(theta, X, y)
21+
trainingExamples = length(y);
22+
gradient = (1 / trainingExamples) * (X' * (sigmoid(X * theta) - y));
23+
endfunction
24+
25+
function gradient = regularizedGradientVector(theta, X, y)
26+
trainingExamples = length(y);
27+
gradient = gradientVector(theta, X, y);
28+
modifiedHypothesis = (lambda / trainingExamples) * theta;
29+
modifiedHypothesis(1) = 0;
30+
gradient += modifiedHypothesis;
31+
endfunction
32+
33+
J = logisticRegressionRegularizedCost(theta, X, y);
34+
grad = regularizedGradientVector(theta, X, y);
5235
end
Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,27 @@
11
function [all_theta] = oneVsAll(X, y, num_labels, lambda)
2-
%ONEVSALL trains multiple logistic regression classifiers and returns all
3-
%the classifiers in a matrix all_theta, where the i-th row of all_theta
4-
%corresponds to the classifier for label i
5-
% [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels
6-
% logistic regression classifiers and returns each of these classifiers
7-
% in a matrix all_theta, where the i-th row of all_theta corresponds
8-
% to the classifier for label i
9-
10-
% Some useful variables
11-
m = size(X, 1);
12-
n = size(X, 2);
13-
14-
% You need to return the following variables correctly
15-
all_theta = zeros(num_labels, n + 1);
16-
17-
% Add ones to the X data matrix
18-
X = [ones(m, 1) X];
19-
20-
% ====================== YOUR CODE HERE ======================
21-
% Instructions: You should complete the following code to train num_labels
22-
% logistic regression classifiers with regularization
23-
% parameter lambda.
24-
%
25-
% Hint: theta(:) will return a column vector.
26-
%
27-
% Hint: You can use y == c to obtain a vector of 1's and 0's that tell you
28-
% whether the ground truth is true/false for this class.
29-
%
30-
% Note: For this assignment, we recommend using fmincg to optimize the cost
31-
% function. It is okay to use a for-loop (for c = 1:num_labels) to
32-
% loop over the different classes.
33-
%
34-
% fmincg works similarly to fminunc, but is more efficient when we
35-
% are dealing with large number of parameters.
36-
%
37-
% Example Code for fmincg:
38-
%
39-
% % Set Initial theta
40-
% initial_theta = zeros(n + 1, 1);
41-
%
42-
% % Set options for fminunc
43-
% options = optimset('GradObj', 'on', 'MaxIter', 50);
44-
%
45-
% % Run fmincg to obtain the optimal theta
46-
% % This function will return theta and the cost
47-
% [theta] = ...
48-
% fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ...
49-
% initial_theta, options);
50-
%
51-
52-
53-
54-
55-
56-
57-
58-
59-
60-
61-
62-
63-
% =========================================================================
64-
65-
2+
%ONEVSALL trains multiple logistic regression classifiers and returns all
3+
%the classifiers in a matrix all_theta, where the i-th row of all_theta
4+
%corresponds to the classifier for label i
5+
% [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels
6+
% logistic regression classifiers and returns each of these classifiers
7+
% in a matrix all_theta, where the i-th row of all_theta corresponds
8+
% to the classifier for label i
9+
10+
11+
dataSetSize = size(X, 1);
12+
features = size(X, 2);
13+
all_theta = zeros(num_labels, features + 1);
14+
15+
% Add ones to the X data matrix
16+
X = [ones(dataSetSize, 1) X];
17+
18+
initial_theta = zeros(features + 1, 1);
19+
20+
% Set options for fminuncg
21+
options = optimset('GradObj', 'on', 'MaxIter', 50);
22+
23+
for c = 1:num_labels
24+
[theta, cost] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);
25+
all_theta(c,:) = theta(:);
26+
endfor
6627
end

week4/machine-learning-ex3/ex3/predict.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88
num_labels = size(Theta2, 1);
99

1010
% You need to return the following variables correctly
11-
p = zeros(size(X, 1), 1);
11+
% p = zeros(m, 1);
12+
13+
% add x0 in x
14+
a1 = [ones(m, 1) X];
15+
a2 = sigmoid(a1 * Theta1');
16+
a2 = [ones(m, 1) a2];
17+
a3 = sigmoid(a2 * Theta2');
18+
[maxProbability index] = max(a3, [], 2);
19+
p = index;
1220

1321
% ====================== YOUR CODE HERE ======================
1422
% Instructions: Complete the following code to make predictions using
Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
11
function p = predictOneVsAll(all_theta, X)
2-
%PREDICT Predict the label for a trained one-vs-all classifier. The labels
3-
%are in the range 1..K, where K = size(all_theta, 1).
4-
% p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions
5-
% for each example in the matrix X. Note that X contains the examples in
6-
% rows. all_theta is a matrix where the i-th row is a trained logistic
7-
% regression theta vector for the i-th class. You should set p to a vector
8-
% of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2
9-
% for 4 examples)
10-
11-
m = size(X, 1);
12-
num_labels = size(all_theta, 1);
13-
14-
% You need to return the following variables correctly
15-
p = zeros(size(X, 1), 1);
16-
17-
% Add ones to the X data matrix
18-
X = [ones(m, 1) X];
19-
20-
% ====================== YOUR CODE HERE ======================
21-
% Instructions: Complete the following code to make predictions using
22-
% your learned logistic regression parameters (one-vs-all).
23-
% You should set p to a vector of predictions (from 1 to
24-
% num_labels).
25-
%
26-
% Hint: This code can be done all vectorized using the max function.
27-
% In particular, the max function can also return the index of the
28-
% max element, for more information see 'help max'. If your examples
29-
% are in rows, then, you can use max(A, [], 2) to obtain the max
30-
% for each row.
31-
%
32-
33-
34-
35-
36-
37-
38-
39-
% =========================================================================
40-
41-
2+
%PREDICT Predict the label for a trained one-vs-all classifier. The labels
3+
%are in the range 1..K, where K = size(all_theta, 1).
4+
% p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions
5+
% for each example in the matrix X. Note that X contains the examples in
6+
% rows. all_theta is a matrix where the i-th row is a trained logistic
7+
% regression theta vector for the i-th class. You should set p to a vector
8+
% of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2
9+
% for 4 examples)
10+
11+
trainingDataSize = size(X)(1);
12+
13+
% Add ones to the X data matrix
14+
X = [ones(trainingDataSize, 1) X];
15+
16+
hypotheses = X * all_theta';
17+
probabilities = sigmoid(hypotheses);
18+
[maxProbability index] = max(probabilities, [], 2);
19+
p = index;
4220
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Created by Octave 5.2.0, Mon Jun 15 03:58:24 2020 GMT <unknown@anishLearnsToCode>
2+
# name: email
3+
# type: sq_string
4+
# elements: 1
5+
# length: 21
6+
7+
8+
9+
# name: token
10+
# type: sq_string
11+
# elements: 1
12+
# length: 16
13+
RV8uyaZ6iH1Bc0On
14+
15+

0 commit comments

Comments
 (0)