-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradDesc_practice.m
More file actions
248 lines (200 loc) · 8.3 KB
/
Copy pathGradDesc_practice.m
File metadata and controls
248 lines (200 loc) · 8.3 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
%Practice for Gradient Descent on a least-squares cost function in 1D
clear all
close all
clc
%Create random 'training data' by selecting true underlying parameters
%(slope and y-intercept) and then generating points with deviations from
%that line
%Select true parameters
P1_true = rand - 0.5;
P2_true = rand - 0.5;
%Set the 'difficulty' of the dataset by setting how large the variations
%will be and how many points the algorithm gets to work with
Spread = .3;
Size = 100;
%Set the learning rate
Aleph = 2;
%Choose points in the space to create data for
X_vals = 1*rand(Size,1);
%Create the training set
Train_set = zeros(size(X_vals));
for index = 1:Size
Train_set(index) = (rand-0.5)*Spread + P1_true*X_vals(index) + P2_true;
end
%Generate an initial hypothesis
P1_guess = rand - 0.5;
P2_guess = rand - 0.5;
%Iteratively improve the hypothesis
J_old = 10;
J_new = 1;
iter = 0;
while (abs(J_old - J_new) > J_old * 1e-10)
%Calculate the old cost function
J_old = 1/length(X_vals) * sum((P1_guess*X_vals + P2_guess - Train_set).^2);
%Plot the current state
plot(X_vals,Train_set,'o'), hold on
plot([min(X_vals),max(X_vals)],[P1_guess*min(X_vals) + P2_guess,P1_guess*max(X_vals) + P2_guess])
legend('Data',sprintf('J = %d',J_old))
drawnow
%pause(.05)
%Update the parameters
ddP1 = 1/length(X_vals) * sum((P1_guess*X_vals + P2_guess - Train_set).*X_vals);
ddP2 = 1/length(X_vals) * sum((P1_guess*X_vals + P2_guess - Train_set));
P1_guess = P1_guess - Aleph*ddP1;
P2_guess = P2_guess - Aleph*ddP2;
J_new = 1/length(X_vals) * sum((P1_guess*X_vals + P2_guess - Train_set).^2);
iter = iter + 1;
if J_new>J_old
Aleph = Aleph/2;
end
end
plot(X_vals,Train_set,'o')
plot([min(X_vals),max(X_vals)],[P1_guess*min(X_vals) + P2_guess,P1_guess*max(X_vals) + P2_guess],'r','Linewidth',3)
plot([min(X_vals),max(X_vals)],[P1_true*min(X_vals) + P2_true,P1_true*max(X_vals) + P2_true],'g','Linewidth',3)
legend('Data',sprintf('J = %d',J_new),'Actual')
pause(1)
%%
%The same thing, without derivatives this time!
clear all
close all
disp('Section 2')
%Create random 'training data' by selecting true underlying parameters
%(slope and y-intercept) and then generating points with deviations from
%that line
%Select true parameters
P1_true = rand - 0.5;
P2_true = rand - 0.5;
%Set the 'difficulty' of the dataset by setting how large the variations
%will be and how many points the algorithm gets to work with
Spread = .5;
Size = 200;
%Set the learning rate
Aleph = 10;
Bet = 0.3;
%Choose points in the space to create data for
X_vals = 1*rand(Size,1);
%Create the training set
Train_set = zeros(size(X_vals));
for index = 1:Size
Train_set(index) = (rand-0.5)*Spread + P1_true*X_vals(index) + P2_true;
end
%Generate an initial hypothesis
P1_guess = rand - 0.5;
P2_guess = rand - 0.5;
%Iteratively improve the hypothesis
J_old = 10;
J_new = 1;
iter = 0;
while ((abs(J_old - J_new) > J_old * 1e-7) && iter < 100000)
%Calculate the old cost function
J_old = 1/length(X_vals) * sum((P1_guess*X_vals + P2_guess - Train_set).^2);
%Plot the current state
plot(X_vals,Train_set,'o'), hold on
plot([min(X_vals),max(X_vals)],[P1_guess*min(X_vals) + P2_guess,P1_guess*max(X_vals) + P2_guess])
legend('Data',sprintf('J = %d',J_old))
drawnow
%pause(.05)
%Update the parameters
P1_up = P1_guess + Bet;
P1_down = P1_guess - Bet;
P2_up = P2_guess + Bet;
P2_down = P2_guess - Bet;
P1_temp = P1_guess;
P2_temp = P2_guess;
if sum((P1_up*X_vals + P2_guess - Train_set).^2) < sum((P1_down*X_vals + P2_guess - Train_set).^2)
P1_temp = P1_up;
elseif sum((P1_up*X_vals + P2_guess - Train_set).^2) > sum((P1_down*X_vals + P2_guess - Train_set).^2)
P1_temp = P1_down;
end
if sum((P1_guess*X_vals + P2_up - Train_set).^2) < sum((P1_guess*X_vals + P2_down - Train_set).^2)
P2_temp = P2_up;
elseif sum((P1_down*X_vals + P2_up - Train_set).^2) > sum((P1_guess*X_vals + P2_down - Train_set).^2)
P2_temp = P2_down;
end
P1_guess = P1_temp;
P2_guess = P2_temp;
J_new = 1/length(X_vals) * sum((P1_guess*X_vals + P2_guess - Train_set).^2);
if J_new>J_old
Aleph = Aleph/2;
Bet = Bet/2;
end
iter = iter + 1;
if mod(iter,200)==0
Aleph = Aleph*2;
Bet = Bet*2;
end
if Bet>=1
Bet = 0.99;
end
end
hold off
plot(X_vals,Train_set,'o'), hold on
plot([min(X_vals),max(X_vals)],[P1_guess*min(X_vals) + P2_guess,P1_guess*max(X_vals) + P2_guess],'r','Linewidth',3)
plot([min(X_vals),max(X_vals)],[P1_true*min(X_vals) + P2_true,P1_true*max(X_vals) + P2_true],'g','Linewidth',3)
J_actual = 1/length(X_vals) * sum((P1_true*X_vals + P2_true - Train_set).^2);
legend('Data',sprintf('J = %d',J_new),sprintf('Actual J = %d',J_actual))
%%
%Fun! Now do it in 3D!
clear all
close all
%Create random 'training data' by selecting true underlying parameters
%(slope in X and Y, and Z-intercept) and then generating points with deviations from
%that line EDIT: THIS ISN'T HOW 3D LINES WORK, IGNORE
%Select true parameters
P1_true = rand - 0.5;
P2_true = rand - 0.5;
P3_true = rand - 0.5;
%Set the 'difficulty' of the dataset by setting how large the variations
%will be and how many points the algorithm gets to work with
Spread = .3;
Size = 100;
%Set the learning rate
Aleph = .07;
%Choose points in the space to create data for
X_vals = 1*rand(Size,1);
Y_vals = 1*rand(Size,1);
%Create the training set
Train_set = zeros(Size,1);
for index = 1:Size
Train_set(index) = rand*Spread + P1_true*X_vals(index) + P2_true*Y_vals(index) + P3_true;
end
%Generate an initial hypothesis
P1_guess = rand - 0.5;
P1_orig = P1_guess;
P2_guess = rand - 0.5;
P2_orig = P2_guess;
P3_guess = rand - 0.5;
P3_orig = P3_guess;
%Iteratively improve the hypothesis
J_old = 10;
J_new = 1;
iter = 0;
while (abs(J_old - J_new) > J_old * 0.0001)
%Calculate the old cost function
J_old = 1/Size * sum((P1_guess*X_vals + P2_guess*Y_vals + P3_guess - Train_set).^2);
%Plot the current state
plot3(X_vals,Y_vals,Train_set,'o'), hold on
plot3([min(X_vals),max(X_vals)],[min(Y_vals),max(Y_vals)],[P1_guess*min(X_vals) + P2_guess*min(Y_vals) + P3_guess, P1_guess*max(X_vals) + P2_guess*max(Y_vals) + P3_guess])
legend('Data',sprintf('J = %d',J_old))
drawnow
%pause(.05)
%Update the parameters (I think this is how it works in 3D?)
ddP1 = 1/Size * sum((P1_guess*X_vals + P2_guess*Y_vals + P3_guess - Train_set).*X_vals);
ddP2 = 1/Size * sum((P1_guess*X_vals + P2_guess*Y_vals + P3_guess - Train_set).*Y_vals);
ddP3 = 1/Size * sum((P1_guess*X_vals + P2_guess*Y_vals + P3_guess - Train_set));
%Trying to derive in 3D - stalls in the same way as above, which is maybe a good sign?
% ddP1 = 1/(2*Size) * sum(P1_guess*X_vals*2 + (P2_guess*Y_vals).^2 + P3_guess^2 + Train_set.^2 + 2*(P2_guess*X_vals.*Y_vals + P3_guess*X_vals + P2_guess*P3_guess*Y_vals - X_vals.*Train_set - P2_guess*Y_vals.*Train_set - P3_guess*Train_set));
% ddP2 = 1/(2*Size) * sum((P1_guess*X_vals).^2 + P2_guess*Y_vals*2 + P3_guess^2 + Train_set.^2 + 2*(P1_guess*X_vals.*Y_vals + P1_guess*P3_guess*X_vals + P3_guess*Y_vals - P1_guess*X_vals.*Train_set - Y_vals.*Train_set - P3_guess*Train_set));
% ddP3 = 1/(2*Size) * sum((P1_guess*X_vals).^2 + (P2_guess*Y_vals).^2 + P3_guess + Train_set.^2 + 2*(P1_guess*P2_guess*X_vals.*Y_vals + P1_guess*X_vals + P2_guess*Y_vals - P1_guess*X_vals.*Train_set - P2_guess*Y_vals.*Train_set - Train_set));
P1_guess = P1_guess - Aleph*ddP1;
P2_guess = P2_guess - Aleph*ddP2;
P3_guess = P3_guess - Aleph*ddP3;
J_new = 1/length(X_vals) * sum((P1_guess*X_vals + P2_guess - Train_set).^2);
iter = iter + 1;
%if J_new>J_old
% Aleph = Aleph/2;
%end
end
plot3(X_vals,Y_vals,Train_set,'o')
plot3([min(X_vals),max(X_vals)],[min(Y_vals),max(Y_vals)],[P1_guess*min(X_vals) + P2_guess*min(Y_vals) + P3_guess, P1_guess*max(X_vals) + P2_guess*max(Y_vals) + P3_guess],'r','Linewidth',3)
legend('Data',sprintf('J = %d',J_new))