Skip to content

Commit 1d13151

Browse files
solves week 4 quiz neural networks
1 parent acfb1ea commit 1d13151

27 files changed

+3592
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Machine Learning By Stanford on Coursera
22
__Instructor__: Andrew Ng.
33

4+
[Week 1](#week-4)
5+
46
## Week 1
57
### Quizzes
68
- [Introduction](week1/introduction.pdf)

week4/machine-learning-ex3/ex3.pdf

287 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function [h, display_array] = displayData(X, example_width)
2+
%DISPLAYDATA Display 2D data in a nice grid
3+
% [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data
4+
% stored in X in a nice grid. It returns the figure handle h and the
5+
% displayed array if requested.
6+
7+
% Set example_width automatically if not passed in
8+
if ~exist('example_width', 'var') || isempty(example_width)
9+
example_width = round(sqrt(size(X, 2)));
10+
end
11+
12+
% Gray Image
13+
colormap(gray);
14+
15+
% Compute rows, cols
16+
[m n] = size(X);
17+
example_height = (n / example_width);
18+
19+
% Compute number of items to display
20+
display_rows = floor(sqrt(m));
21+
display_cols = ceil(m / display_rows);
22+
23+
% Between images padding
24+
pad = 1;
25+
26+
% Setup blank display
27+
display_array = - ones(pad + display_rows * (example_height + pad), ...
28+
pad + display_cols * (example_width + pad));
29+
30+
% Copy each example into a patch on the display array
31+
curr_ex = 1;
32+
for j = 1:display_rows
33+
for i = 1:display_cols
34+
if curr_ex > m,
35+
break;
36+
end
37+
% Copy the patch
38+
39+
% Get the max value of the patch
40+
max_val = max(abs(X(curr_ex, :)));
41+
display_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ...
42+
pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...
43+
reshape(X(curr_ex, :), example_height, example_width) / max_val;
44+
curr_ex = curr_ex + 1;
45+
end
46+
if curr_ex > m,
47+
break;
48+
end
49+
end
50+
51+
% Display Image
52+
h = imagesc(display_array, [-1 1]);
53+
54+
% Do not show axis
55+
axis image off
56+
57+
drawnow;
58+
59+
end

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

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
%% Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all
2+
3+
% Instructions
4+
% ------------
5+
%
6+
% This file contains code that helps you get started on the
7+
% linear exercise. You will need to complete the following functions
8+
% in this exericse:
9+
%
10+
% lrCostFunction.m (logistic regression cost function)
11+
% oneVsAll.m
12+
% predictOneVsAll.m
13+
% predict.m
14+
%
15+
% For this exercise, you will not need to change any code in this file,
16+
% or any other files other than those mentioned above.
17+
%
18+
19+
%% Initialization
20+
clear ; close all; clc
21+
22+
%% Setup the parameters you will use for this part of the exercise
23+
input_layer_size = 400; % 20x20 Input Images of Digits
24+
num_labels = 10; % 10 labels, from 1 to 10
25+
% (note that we have mapped "0" to label 10)
26+
27+
%% =========== Part 1: Loading and Visualizing Data =============
28+
% We start the exercise by first loading and visualizing the dataset.
29+
% You will be working with a dataset that contains handwritten digits.
30+
%
31+
32+
% Load Training Data
33+
fprintf('Loading and Visualizing Data ...\n')
34+
35+
load('ex3data1.mat'); % training data stored in arrays X, y
36+
m = size(X, 1);
37+
38+
% Randomly select 100 data points to display
39+
rand_indices = randperm(m);
40+
sel = X(rand_indices(1:100), :);
41+
42+
displayData(sel);
43+
44+
fprintf('Program paused. Press enter to continue.\n');
45+
pause;
46+
47+
%% ============ Part 2a: Vectorize Logistic Regression ============
48+
% In this part of the exercise, you will reuse your logistic regression
49+
% code from the last exercise. You task here is to make sure that your
50+
% regularized logistic regression implementation is vectorized. After
51+
% that, you will implement one-vs-all classification for the handwritten
52+
% digit dataset.
53+
%
54+
55+
% Test case for lrCostFunction
56+
fprintf('\nTesting lrCostFunction() with regularization');
57+
58+
theta_t = [-2; -1; 1; 2];
59+
X_t = [ones(5,1) reshape(1:15,5,3)/10];
60+
y_t = ([1;0;1;0;1] >= 0.5);
61+
lambda_t = 3;
62+
[J grad] = lrCostFunction(theta_t, X_t, y_t, lambda_t);
63+
64+
fprintf('\nCost: %f\n', J);
65+
fprintf('Expected cost: 2.534819\n');
66+
fprintf('Gradients:\n');
67+
fprintf(' %f \n', grad);
68+
fprintf('Expected gradients:\n');
69+
fprintf(' 0.146561\n -0.548558\n 0.724722\n 1.398003\n');
70+
71+
fprintf('Program paused. Press enter to continue.\n');
72+
pause;
73+
%% ============ Part 2b: One-vs-All Training ============
74+
fprintf('\nTraining One-vs-All Logistic Regression...\n')
75+
76+
lambda = 0.1;
77+
[all_theta] = oneVsAll(X, y, num_labels, lambda);
78+
79+
fprintf('Program paused. Press enter to continue.\n');
80+
pause;
81+
82+
83+
%% ================ Part 3: Predict for One-Vs-All ================
84+
85+
pred = predictOneVsAll(all_theta, X);
86+
87+
fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);
88+
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
%% Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks
2+
3+
% Instructions
4+
% ------------
5+
%
6+
% This file contains code that helps you get started on the
7+
% linear exercise. You will need to complete the following functions
8+
% in this exericse:
9+
%
10+
% lrCostFunction.m (logistic regression cost function)
11+
% oneVsAll.m
12+
% predictOneVsAll.m
13+
% predict.m
14+
%
15+
% For this exercise, you will not need to change any code in this file,
16+
% or any other files other than those mentioned above.
17+
%
18+
19+
%% Initialization
20+
clear ; close all; clc
21+
22+
%% Setup the parameters you will use for this exercise
23+
input_layer_size = 400; % 20x20 Input Images of Digits
24+
hidden_layer_size = 25; % 25 hidden units
25+
num_labels = 10; % 10 labels, from 1 to 10
26+
% (note that we have mapped "0" to label 10)
27+
28+
%% =========== Part 1: Loading and Visualizing Data =============
29+
% We start the exercise by first loading and visualizing the dataset.
30+
% You will be working with a dataset that contains handwritten digits.
31+
%
32+
33+
% Load Training Data
34+
fprintf('Loading and Visualizing Data ...\n')
35+
36+
load('ex3data1.mat');
37+
m = size(X, 1);
38+
39+
% Randomly select 100 data points to display
40+
sel = randperm(size(X, 1));
41+
sel = sel(1:100);
42+
43+
displayData(X(sel, :));
44+
45+
fprintf('Program paused. Press enter to continue.\n');
46+
pause;
47+
48+
%% ================ Part 2: Loading Pameters ================
49+
% In this part of the exercise, we load some pre-initialized
50+
% neural network parameters.
51+
52+
fprintf('\nLoading Saved Neural Network Parameters ...\n')
53+
54+
% Load the weights into variables Theta1 and Theta2
55+
load('ex3weights.mat');
56+
57+
%% ================= Part 3: Implement Predict =================
58+
% After training the neural network, we would like to use it to predict
59+
% the labels. You will now implement the "predict" function to use the
60+
% neural network to predict the labels of the training set. This lets
61+
% you compute the training set accuracy.
62+
63+
pred = predict(Theta1, Theta2, X);
64+
65+
fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);
66+
67+
fprintf('Program paused. Press enter to continue.\n');
68+
pause;
69+
70+
% To give you an idea of the network's output, you can also run
71+
% through the examples one at the a time to see what it is predicting.
72+
73+
% Randomly permute examples
74+
rp = randperm(m);
75+
76+
for i = 1:m
77+
% Display
78+
fprintf('\nDisplaying Example Image\n');
79+
displayData(X(rp(i), :));
80+
81+
pred = predict(Theta1, Theta2, X(rp(i),:));
82+
fprintf('\nNeural Network Prediction: %d (digit %d)\n', pred, mod(pred, 10));
83+
84+
% Pause with quit option
85+
s = input('Paused - press enter to continue, q to exit:','s');
86+
if s == 'q'
87+
break
88+
end
89+
end
90+
7.16 MB
Binary file not shown.
77.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)