-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.m
101 lines (60 loc) · 2.5 KB
/
new.m
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
94
95
96
97
98
99
100
101
%% Initialization
%clear ; close all; clc
%% Setup the parameters you will use for this exercise
input_layer_size = 2; % x1 and x2
hidden_layer_size = 10; % 5 hidden units
num_labels = 2; % 2 labels, 0 and 1
lambda = 1; %for regularization
%% Load Data
% The first two columns contains X and the third column
% contains label.
data = load('datac_118.txt');
X = data(:, [1, 2]); y = data(:, 3);
m = size(X, 1);
%% ==================== Plotting ====================
fprintf(['Plotting data \n']);
plotData(X, y);
% Put some labels
hold on;
% Labels and Legend
xlabel('Feature 1')
ylabel('Feature 2')
% Specified in plot order
%legend('Label-0', 'Label-1')
hold off;
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
%% ================ Initializing Pameters ================
fprintf('\nInitializing Neural Network Parameters ...\n')
initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size);
initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels);
% Unroll parameters
initial_nn_params = [initial_Theta1(:) ; initial_Theta2(:)];
%J = new1(nn_params, input_layer_size, hidden_layer_size, ...
% num_labels, X, y, lambda);
%% =================== Training NN ===================
%
fprintf('\nTraining Neural Network... \n')
% We can change the MaxIter to a larger
% value to see how more training helps.
options = optimset('MaxIter', 60);
% You should also try different values of lambda
lambda = 0;
% Create "short hand" for the cost function to be minimized
costFunction = @(p) nnCostFunction(p, ...
input_layer_size, ...
hidden_layer_size, ...
num_labels, X, y, lambda);
% Now, costFunction is a function that takes in only one argument (the
% neural network parameters)
[nn_params, cost] = fmincg(costFunction, initial_nn_params, options);
% Obtain Theta1 and Theta2 back from nn_params
Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...
hidden_layer_size, (input_layer_size + 1));
Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...
num_labels, (hidden_layer_size + 1));
fprintf('Program paused. Press enter to continue.\n');
pause;
%% ================= Implement Predict =================
pred = predict1(Theta1, Theta2, X);
fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);