-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.m
More file actions
executable file
·93 lines (77 loc) · 3.64 KB
/
Copy pathscript.m
File metadata and controls
executable file
·93 lines (77 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
clearvars;
fprintf('\nStarting preprocess:\n');
[train_data, train_label, validation_data, ...
validation_label, test_data, test_label] = preprocess();
<<<<<<< HEAD
fprintf('\nEnd of preprocess\n');
=======
fprintf('\nEnding preprocess\n');
>>>>>>> 73ff4ee0e920609cff253fc81320fb2987e6ee39
save('dataset.mat', 'train_data', 'train_label', 'validation_data', ...
'validation_label', 'test_data', 'test_label');
load('dataset.mat');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% **************Neural Network********************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Train Neural Network
% set the number of nodes in input unit (not including bias unit)
n_input = size(train_data, 2);
% set the number of nodes in hidden unit (not including bias unit)
n_hidden = 50;
% set the number of nodes in output unit
n_class = 10;
% initialize the weights into some random matrices
initial_w1 = initializeWeights(n_input, n_hidden);
initial_w2 = initializeWeights(n_hidden, n_class);
% unroll 2 weight matrices into single column vector
initialWeights = [initial_w1(:); initial_w2(:)];
% set the maximum number of iteration in conjugate gradient descent
options = optimset('MaxIter', 50);
% set the regularization hyper-parameter
lambda = 0;
% define the objective function
objFunction = @(params) nnObjFunction(params, n_input, n_hidden, ...
n_class, train_data, train_label, lambda);
% run neural network training with fmincg
<<<<<<< HEAD
fprintf('\nStarting training\n');
[nn_params, cost] = fmincg(objFunction, initialWeights, options);
fprintf('End of training:\n');
=======
fprintf('\nStarting trINING:\n');
[nn_params, cost] = fmincg(objFunction, initialWeights, options);
fprintf('\Ending training:\n');
>>>>>>> 73ff4ee0e920609cff253fc81320fb2987e6ee39
% reshape the nn_params from a column vector into 2 matrices w1 and w2
w1 = reshape(nn_params(1:n_hidden * (n_input + 1)), ...
n_hidden, (n_input + 1));
w2 = reshape(nn_params((1 + (n_hidden * (n_input + 1))):end), ...
n_class, (n_hidden + 1));
% Test the computed parameters
predicted_label = nnPredict(w1, w2, train_data);
fprintf('\nTraining Set Accuracy: %f\n', ...
mean(double(predicted_label == train_label)) * 100);
% Test Neural Network with validation data
predicted_label = nnPredict(w1, w2, validation_data);
fprintf('\nValidation Set Accuracy: %f\n', ...
mean(double(predicted_label == validation_label)) * 100);
% Test Neural Network with test data
predicted_label = nnPredict(w1, w2, test_data);
fprintf('\nTesting Set Accuracy: %f\n', ...
mean(double(predicted_label == test_label)) * 100);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% **************K-Nearest Neighbors***************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
k = 1;
% Test KNN with validation data
predicted_label = knnPredict(k, train_data, train_label, validation_data);
fprintf('\nValidation Set Accuracy: %f\n', ...
mean(double(predicted_label == validation_label)) * 100);
% Test KNN with test data
predicted_label = knnPredict(k, train_data, train_label, test_data);
fprintf('\nTesting Set Accuracy: %f\n', ...
mean(double(predicted_label == test_label)) * 100);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% *******Save the learned parameters *************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
save('params.mat', 'n_input', 'n_hidden', 'w1', 'w2', 'lambda', 'k');