Friday, 9 September 2016

Activation Function in NN

% Illustration of various activation functions used in NN's
x = -10:0.1:10;
tmp = exp(-x);
y1 = 1./(1+tmp);
y2 = (1-tmp)./(1+tmp);
y3 = x;
subplot(231); plot(x, y1); grid on;
axis([min(x) max(x) -2 2]);
title('Logistic Function');
xlabel('(a)');
axis('square');
subplot(232); plot(x, y2); grid on;
axis([min(x) max(x) -2 2]);
title('Hyperbolic Tangent Function');
xlabel('(b)');
axis('square');
subplot(233); plot(x, y3); grid on;
axis([min(x) max(x) min(x) max(x)]);
title('Identity Function');
xlabel('(c)');
axis('square');

Tuesday, 5 April 2016

Implement defuzzyfication (Max-membership principle, Centroid method, Weighted average method)

clc;
clear all;
close all;

ip=11;
a1=1;
a2=5;
b1=4;
b2=7;
c1=7;
c2=9;
d2=11;
s1=0;
s2=0;

%Triangular function

for u=1:ip
    if(u<=a1)
        t(u)=0;
    elseif((u>a1)&&(u<=b1))
        t(u)=((u-a1)/(b1-a1));
    elseif((u>b1)&&(u<=c1))
        t(u)=((c1-u)/(c1-b1));
    else
        t(u)=0;
    end
end

subplot(221)
plot(t);
title('Triangular Function');

%Pie function

for u=1:ip
    if(u<=a2)
        p(u)=0;
    elseif((u>a2)&&(u<=b2))
        p(u)=((u-a2)/(b2-a2));
    elseif((u>b2)&&(u<=c2))
        p(u)=1;
    elseif((u>c2)&&(u<=d2))
        p(u)=((d2-u)/(d2-c2));
    else
        p(u)=0;
    end
end

subplot(222)
plot(p);
title('Pie Function');

z=t+p;
subplot(223);
plot(z);

%Using Centroid Method
for i=1:11
    s1=s1+(z(i)*i);
    s2=s2+z(i);
end

op1=s1/s2;
disp('Using Centroid Method:');
disp(op1);


%Using Weighted average method
a=mean(ip);
s1=s1+t(a)*(a)+p(a)*(a);
s2=s2+z(a)+p(a);
op1=s1/s2;
disp('Using Weighted Average MEthod');
disp(op1);

%Using Max-Membership Principle
k=z(1);
m=1;
for i=2:ip
    if(k<z(i))
    k=z(i);
    m=i;
    end
end
disp('Using Max Membership principle');
disp(m);

Monday, 15 February 2016

Kohonen Self organizing feature map MATLAB code

clc;
clear all;
close all;
 
alpha=0.5;
x1=rand(1,100)-0.5;
x2=rand(1,100)-0.5;
x=[x1;x2];
 
w1=rand(1,50)-rand(1,50);
w2=rand(1,50)-rand(1,50);
w=[w1;w2];
 
figure(1);
plot([-0.5 0.5 0.5 -0.5 -0.5],[0.5 0.5 -0.5 -0.5 0.5]);
hold on;
plot(x1,x2,'b.');
axis([-1 1 -1 1]);
 
figure(2);
plot([-0.5 0.5 0.5 -0.5 -0.5],[0.5 0.5 -0.5 -0.5 0.5]);
hold on;
plot(w(1,:),w(2,:),'b.',w(1,:),w(2,:));
axis([-1 1 -1 1]);
hold off;
con=1;
ep=0;
while(con)
    for i=1:100
        for j=1:50
            d(j)=0;
            for k=1:2
                d(j)=d(j)+(w(k,j)-x(k,i))^2;
            end
        end
%         for j=1:50
%             if d(j)==min(d);
%                 J=j;
%             end
%         end
        [val J]=min(d);   
        I=J-1;
        K=J+1;
        
        if(I<1)
            I=50;
        end
        
        if(K>50)
            K=1;
        end
        
        w(:,J)=w(:,J)+alpha*(x(:,i)-w(:,J));
        w(:,I)=w(:,I)+alpha*(x(:,i)-w(:,I));
        w(:,K)=w(:,K)+alpha*(x(:,i)-w(:,K));
    end
    
    alpha=alpha-0.0049;
    ep=ep+1;
    plot([-0.5 0.5 0.5 -0.5 -0.5],[0.5 0.5 -0.5 -0.5 0.5]);
    hold on;
    plot(w(1,:),w(2,:),'b*',w(1,:),w(2,:));
    axis([-1 1 -1 1]);
    pause(0.1);
    hold off;
    if(ep==100)
        con=0;
    end
end
 
figure(3);
plot([-0.5 0.5 0.5 -0.5 -0.5],[0.5 0.5 -0.5 -0.5 0.5]);
hold on;
plot(w(1,:),w(2,:),'b.',w(1,:),w(2,:));
axis([-1 1 -1 1]);

Tuesday, 2 February 2016

Implement perceptron for AND function using bipolar inputs

%Code

clc
clear all;
close all;

x1=[-1 -1 1 1];
x2=[-1 1 -1 1];
t=[-1 -1 -1 1];
alpha=input('Enter the value of alpha=');
th=input('enter the threshold=');
yin=zeros(1,4);
y=zeros(1,4);
w1=0;
w2=0;
b=0;
c=1;
cnt=0;
while(c)
    c=0
    for i=1:4
        %yin(i)=b+(x(i,1)*w1)+(x(i,2)*w2);
       yin(i)=b+(x1(i)*w1)+(x2(i)*w2);
        if yin(i)>th
            y(i)=1;
        else if yin(i)<-th
                y(i)=-1;
            else
                y(i)=0;
            end
            if t(i)~=y(i)
                w1=w1+(alpha*x1(i)*t(i));
                w2=w2+(alpha*x2(i)*t(i));
                b=b+(alpha*t(i));
                c=1;
            else
                w1=w1;
                w2=w2;
                b=b;
            end
        end
        cnt=cnt+1;
    end
    disp('OUTPUT MATRIX:');
    disp(y);
    disp('w1=');
    disp(w1);
    disp('w2=');
    disp(w2);
    disp('bias=');
    disp(b);
    a1=x1;
    a2=x2;
    a2=(-w1/w2)*a1-(b/w2);
    plot(x1,x2,'*r',a1,a2);
    axis([-2 2 -2 2]);
end

Wednesday, 13 January 2016

Implement fuzzy membership functions (triangular, trapezoidal, gbell, PI, Gamma, Gaussian) ......... MATLAB Code

clc;
clear all;
close all;

ip=input('Enter input range : ');
a=input('Enter value of alpha : ');
b=input('Enter value of beta : ');
c=input('Enter value of gamma : ');
d=input('Enter value of delta : ');
m=input('Enter value of m : ');
del=input('Enter value of del : ');

%Gamma function

for u=1:ip
    if(u<=a)
        g(u)=0;
    elseif((u>a)&&(u<=b))
        g(u)=((u-a)/(b-a));
    else
        g(u)=1;
    end
end

subplot(2,3,1)
plot(g)
title('Gamma Function')
axis([0 ip 0 1]);

%S function

for u=1:ip
    if(u<=a)
        s(u)=0;
    elseif((u>a)&&(u<=b))
        s(u)=2*(((u-a)/(c-a))^2);
    elseif((u>b)&&(u<=c))
        s(u)=1-(2*(((u-c)/(c-a))^2));
    else
        s(u)=1;
    end
end


subplot(2,3,2)
plot(s);
title('S Function');
axis([0 ip 0 1]);

%Triangular function

for u=1:ip
    if(u<=a)
        t(u)=0;
    elseif((u>a)&&(u<=b))
        t(u)=((u-a)/(b-a));
    elseif((u>b)&&(u<=c))
        t(u)=((c-u)/(c-b));
    else
        t(u)=0;
    end
end

subplot(2,3,3)
plot(t);
title('Triangular Function');
axis([0 ip 0 1]);

%Pie function

for u=1:ip
    if(u<=a)
        p(u)=0;
    elseif((u>a)&&(u<=b))
        p(u)=((u-a)/(b-a));
    elseif((u>b)&&(u<=c))
        p(u)=1;
    elseif((u>c)&&(u<=d))
        p(u)=((d-u)/(d-c));
    else
        p(u)=0;
    end
end

subplot(2,3,4)
plot(p);
title('Pie Function');
axis([0 ip 0 1]);

%Gaussian function

for u=1:ip
   gs(u)=exp((-((u-m)^2))/(2*(del^2)));
end

subplot(2,3,5)
plot(gs);
title('Gaussian Function');
axis([0 ip 0 1]);

Monday, 11 January 2016

Implement a simple linear regressor with a single neuron model .... MATLAB Code

// data.txt file


166, 54.00
195, 82.00
200, 72.00
260, 72.00
265, 90.00
335, 124.00
370, 94.00
450, 118.00


//    .m file for linear regression

%Load the data from our text file
data = load('/home/svcet/Desktop/data.txt');
% Define x and y
x = data(:,2);
y = data(:,1);
% Create a function to plot the data
function plotData(x,y)
plot(x,y,'rx','MarkerSize',8); % Plot the data
end
% Plot the data
plotData(x,y);
xlabel('Cost of Book'); % Set the x-axis label
ylabel('Number of Pages'); % Set the y-axis label
fprintf('Program paused. Press enter to continue.\n');
pause;
% Count how many data points we have
m = length(x);
% Add a column of all ones (intercept term) to x
X = [ones(m, 1) x];
% Calculate theta
theta = (pinv(X'*X))*X'*y
% Plot the fitted equation we got from the regression
hold on; % this keeps our previous plot of the training data visible
plot(X(:,2), X*theta, '-')
legend('Training data', 'Linear regression')
hold off % Don't put any more plots on this figure


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