XOR gate using backpropagation.not getting correct output

2 views (last 30 days)
this is the complete code no matter how many iterations i take, the weights are not converging and the output is not showing correct answers
input=[0 0 1 1 0 1 0 1];
output=[0 1 1 0];
bias = [-1 -1 -1];
coeff = 0.2;
weights=randn(3,3);
for i = 1:8000
for j=1:4
I=input(:,j);
q=output(1,j);
H1 = (-1)*weights(1,1)+I(1,1)*weights(1,2)+I(2,1)*weights(2,2);
x1 = logsig(H1);
H2 = (-1)*weights(2,1)+I(1,1)*weights(1,3)+I(2,1)*weights(2,3);
x2 = logsig(H2);
x3_1 = (-1)*weights(3,1)+x1*weights(3,2)+x2*weights(3,3);
out= logsig(x3_1);
err=q-out;
e=.5*err^2;
delta3_1 = out*(1-out)*(q-out);
delta2_1 = x1*(1-x1)*weights(3,2)*delta3_1;
delta2_2 = x2*(1-x2)*weights(3,3)*delta3_1;
weights(1,1) = weights(1,1) + (coeff*(-1)*delta2_1);
weights(2,1) = weights(2,1) + (coeff*(-1)*delta2_2);
weights(3,1) = weights(3,1) + (coeff*(-1)*delta3_1);
weights(1,2) = weights(1,2)+ (coeff*I(1,1)*delta2_1);
weights(2,2) = weights(2,2)+ (coeff*I(2,1)*delta2_1);
weights(3,2) = weights(3,2)+ (coeff*x1*delta3_1);
weights(1,3) = weights(1,3) + (coeff*I(1,1)*delta2_2);
weights(2,3) = weights(2,3) + (coeff*I(2,1)*delta2_2);
weights(3,3) = weights(3,3) + (coeff*x2*delta3_1);
end
end
q1=logsig((0*weights(1,2))+(0*weights(2,2))+weights(1,1));
q2=logsig((0*weights(1,3))+(0*weights(2,3))+weights(2,1));
q3=logsig((q1*weights(3,2))+(q2*weights(3,3))+weights(3,1))

Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!