Skip to content

Instantly share code, notes, and snippets.

@zhezheng
Created October 11, 2013 05:22
Show Gist options
  • Save zhezheng/6929954 to your computer and use it in GitHub Desktop.
Save zhezheng/6929954 to your computer and use it in GitHub Desktop.
Improving the convergence of the Back-Propagation Algorithm
clc;
clear;
nure = 2;
w1 = 2*rand(nure,1)-1;
w2 = 2*rand(1,nure)-1;
b1 = 2*rand(nure,1)-1;
b2 = 2*rand(1,1)-1;
w11 = w1;
w22 = w2;
b11 = b1;
b22 = b2;
lr = 0.01;
lr_1 = 0.1;
s = [];
s_1 = [];
time = 1000;
for k=1:time
sum_e = 0;
sum_s2 = 0;
sum_s1 = 0;
for p = -2:0.2:2
t = sin(pi*p/2);
a1 = logsig(w1*p+b1);
a2 = w2*a1+b2;
e = t-a2;
s2 = -2*e;
s1 = diag((1-a1).*a1)*w2'*s2;
b1 = b1-lr*s1;
b2 = b2-lr*s2;
sum_s2 = sum_s2+s2*a1';
sum_s1 = sum_s1+s1*p;
sum_e = sum_e + (t-a2)^2;
end
s = [s sum_e];
w2 = w2-lr*sum_s2;
w1 = w1-lr*sum_s1;
end
for k=1:time
sum_e = 0;
sum_s2 = 0;
sum_s1 = 0;
for p = -2:0.2:2
t = sin(pi*p/2);
a1 = logsig(w11*p+b11);
a2 = w22*a1+b22;
e = a2-t;
s2 = e;
s1 = diag((1-a1).*a1)*w22'*s2;
b11 = b11-lr_1*s1;
b22 = b22-lr_1*s2;
sum_s2 = sum_s2+s2*a1';
sum_s1 = sum_s1+s1*p;
sum_e = sum_e + (t-a2)^2;
end
s_1 = [s_1 sum_e];
w22 = w22-lr_1*sum_s2;
w11 = w11-lr_1*sum_s1;
end
figure(1);
for q3 = -2:0.01:2;
t = sin(pi*q3/2);
plot(q3,t,'k');
hold on;
a1 = logsig(w1*q3+b1);
a2 = w2*a1+b2;
plot(q3,a2,'b');
hold on;
a3 = logsig(w11*q3+b11);
a4 = w22*a3+b22;
plot(q3,a4,'r');
end
figure(2);
plot(1:length(s),s,'b');
hold on;
plot(1:length(s_1),s_1,'r');
hold on;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment