Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 4/Programming Assignment - 3/machine-learning-ex3/ex3/ex3_nn.m
865 views
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
91
92