Monday, 11 January 2016

MLP trained with Backpropagation for XOR Function.... MATLAB Code

function y=binsig(x)
y=1/(1+exp(-x));
end

function y=binsig1(x)
y=binsig(x)*(1-binsig(x));
end

%Back Propagation Network for XOR function with Binary Input and Output
clc;
clear;
%Initialize weights and bias
v=[0.197 0.3191 -0.1448 0.3394;0.3099 0.1904 -0.0347 -0.4861];
v1=zeros(2,4);
b1=[-0.3378 0.2771 0.2859 -0.3329];
b2=-0.1401;
w=[0.4919;-0.2913;-0.3979;0.3581];
w1=zeros(4,1);
x=[1 1 0 0;1 0 1 0];
t=[0 1 1 0];
alpha=0.02;
mf=0.9;
con=1;
epoch=0;
while con
    e=0;
    for I=1:4
        %Feed forward
        for j=1:4
            zin(j)=b1(j);
            for i=1:2
                zin(j)=zin(j)+x(i,I)*v(i,j);
            end
            z(j)=binsig(zin(j));
        end
        yin=b2+z*w;
        y(I)=binsig(yin);
        %Backpropagation of Error
        delk=(t(I)-y(I))*binsig1(yin);
        delw=alpha*delk*z'+mf*(w-w1);
        delb2=alpha*delk;
        delinj=delk*w;
        for j=1:4
            delj(j,1)=delinj(j,1)*binsig1(zin(j));
        end
        for j=1:4
            for i=1:2
                delv(i,j)=alpha*delj(j,1)*x(i,I)+mf*(v(i,j)-v1(i,j));
            end
        end
        delb1=alpha*delj;
        w1=w;
        v1=v;
        %Weight updation
        w=w+delw;
        b2=b2+delb2;
        v=v+delv;
        b1=b1+delb1';
        e=e+(t(I)-y(I))^2;
    end
    if e<0.005
        con=0;
    end
    epoch=epoch+1;
end
disp('BPN for XOR funtion with Binary input and Output');
disp('Total Epoch Performed');
disp(epoch);
disp('Error');
disp(e);
disp('Final Weight matrix and bias');
v
b1
w
b2

1 comment:

  1. Hie.
    I'm getting thefollowing error.
    please help me out with this.

    ??? Input argument "x" is undefined.

    Error in ==> binsig at 2
    y=1/(1+exp(-x));

    ReplyDelete