Skip to content

Instantly share code, notes, and snippets.

@ghdawn
Created November 8, 2012 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghdawn/4038410 to your computer and use it in GitHub Desktop.
Save ghdawn/4038410 to your computer and use it in GitHub Desktop.
a example of BP Network to solve XOR problem
w1=(rand(3,2)-0.5).*2+0.1;
w2=(rand(3,1)-0.5).*2+0.1;
ita=1;
alpha=1;
%x=[1 1;1 3;1 2;1 4];
%x=[ones(4,1).*(-1) x];
x=[0 0;1 1; 1 0; 0 1];
h=[0 0 0 0]';
tq=[1 1 -1 -1]';
flag=1;
total=1;
c=1;
while total~=0
total=0;
for i = 1:4
flag=1;
while(flag>0)
flag=0;
I1=[-1,x(i,:)];
O1=I1;
I2=[-1,O1*w1];
O2=tanh(I2*c);
I3=O2*w2;
O3=tanh(I3*c);
h(i)=O3;
abs(O3-tq(i))
if(abs(O3-tq(i))>0.03)
flag=1;
total=total+1;
d1o=(O3-tq(i))*c*sech(c*I3).^2;
d2=d1o*w2.*(sech(c*I2').^2)*c;
d2=d2( 2:3 , 1 ) ;
w2=alpha*w2-ita*O2'*d1o;
w1=alpha*w1-ita*O1'*d2'
end
end
end
total
end
hold on;
for i = 1:4
if(h(i)<0)
plot(x(i,1),x(i,2),'rx');
axis([-1 2 -1 2])
else
plot(x(i,1),x(i,2),'bo');
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment