-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmat_drop.m~
210 lines (129 loc) · 3.9 KB
/
mat_drop.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
%%Dropout
%Setup the parameters you will use for this code
input_layer_size = 400; % 20x20 Input Images of Digits
hidden_layer_size = 25; % 25 hidden units
num_labels = 10; % 10 labels, from 1 to 10
% (note that we have mapped "0" to label 10)
%% =========== Loading and Visualizing Data =============
% Load Training Data
fprintf('Loading and Visualizing Data ...\n')
load('ex4data1.mat');
m = size(X, 1);
% Randomly select 100 data points to display
sel = randperm(size(X, 1));
sel = sel(1:100);
displayData(X(sel, :));
fprintf('Program 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(:)];
%%%%%i am adding %%%%%%%
for iteration = 1:1
increment = uint16(100); %used for deciding size of mini-batch
increment
for xyz = 1:increment:m %this for-loop iterates over all
fprintf('\n');
xyz
if xyz+increment>m
X_ran = X(xyz:m, :);
y_ran = y(xyz:m);
else
X_ran = X(xyz:xyz+increment, :);
y_ran = y(xyz:xyz+increment);
end
%%Randomly ommiting hidden units with probability 0.5
c = 0;
percent = 0.5;
ran_mat = rand(hidden_layer_size, 1);
for i=1:hidden_layer_size
if ran_mat(i)>=percent
c++;
end
end
ini_theta1_ran = zeros(c, input_layer_size+1);
ini_theta2_ran = zeros(num_labels, c+1);
k=0;
for i=1:hidden_layer_size
if ran_mat(i)>=percent
k++;
for j=1:input_layer_size+1
ini_theta1_ran(k,j) = initial_Theta1(i, j);
end
end
end
for i=1:num_labels
ini_theta2_ran(i, 1) = initial_Theta2(i, 1);
end
k=1;
for i=1:hidden_layer_size
if ran_mat(i)>=percent
k++;
for j=1:num_labels
ini_theta2_ran(j, k) = initial_Theta2(j, (i+1));
end
end
end
initial_nn_params_ran = [ini_theta1_ran(:) ; ini_theta2_ran(:)];
c
%%%%%%%% i have added %%%%
%% =================== Training NN ===================
fprintf('\nTraining Neural Network... \n')
options = optimset('MaxIter', 50);
lambda = 1;
% Create "short hand" for the cost function to be minimized
costFunction = @(p) nnCostFunction(p, ...
input_layer_size, ...
c, ...
num_labels, X, y, lambda);
% Now, costFunction is a function that takes in only one argument (the
% neural network parameters)
[nn_params_ran, cost] = fmincg(costFunction, initial_nn_params_ran, options);
% Obtain Theta1 and Theta2 back from nn_params
ini_theta1_ran = reshape(nn_params_ran(1:c * (input_layer_size + 1)), ...
c, (input_layer_size + 1));
ini_theta2_ran = reshape(nn_params_ran((1 + (c * (input_layer_size + 1))):end), ...
num_labels, (c + 1));
%%%%%
%putting back these in the original theta
%%%
k=0;
for i=1:hidden_layer_size
if ran_mat(i)>=percent
k++;
for j=1:input_layer_size+1
initial_Theta1(i, j) = ini_theta1_ran(k,j);
end
end
end
k=1;
for i=1:num_labels
initial_Theta2(i, 1) = ini_theta2_ran(i, 1);
end
for i=1:hidden_layer_size
if ran_mat(i)>=percent
k++;
for j=1:num_labels
initial_Theta2(j, (i+1)) = ini_theta2_ran(j, k);
end
end
end
end %ending loop of xyz
end %end of iterations loop
Theta1 = initial_Theta1;
Theta2 = initial_Theta2;
%% ================= Implement Predict =================
for i=1:size(Theta2, 1)
for j=1:size(Theta2, 2)
Theta2(i, j) = Theta2(i, j)/2; %outgoing weights halved
end
end
pred = predict(Theta1, Theta2, X);
fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);
%max iter 50 .. and increment 100 -> 94%
%max iter 20 .. and increment 100 -> 89%
%max iter 15 .. and increment 100 -> 91-93%
%max iter 10 .. and increment 100 -> 89 %