Memaparkan catatan dengan label MATLAB. Papar semua catatan
Memaparkan catatan dengan label MATLAB. Papar semua catatan

XOR Function

20 November 2011



Generate XOR function using McCulloch-Pitts neuron by writing an M-file. The truth table for the XOR function is.

X1    X2    Y
0     0     0
0     1     1
1     0     1
1     1     0 

example38.m
%XOR function using McCulloch-Pitts neuron
clear;
clc;

%Getting weights and threshold value
disp('Enter Weights');
w11 = input('Weight w11 = ');
w12 = input('Weight w12 = ');
w21 = input('Weight w21 = ');
w22 = input('Weight w22 = ');
v1 = input('Weight v1 = ');
v2 = input('Weight v2 = ');

disp('Enter Threshold Value');
theta = input('theta = ');

x1 = [0 0 1 1];
x2 = [0 1 0 1];
z = [0 1 1 0];

con = 1;
while con
    zin1 = x1 * w11 + x2 * w21;
    zin2 = x1 * w21 + x2 * w22;
  
    for i = 1:4
        if zin1(i) >= theta
            y1(i) = 1;
        else
            y1(i) = 0;
        end
      
        if zin2(i) >= theta
            y2(i) = 1;
        else
            y2(i) = 0;
        end
    end
  
    yin = y1 * v1 + y2 * v2;

    for i = 1:4
        if yin(i) >= theta
            y(i) = 1;
        else
            y(i) = 0;
        end
    end
  
    disp('Output of Net');
    disp(y);

    if y == z
        con = 0;
    else
        disp('Net is not learning enter another set of weights and Threhold value');
        w11 = input('Weight w11 = ');
        w12 = input('Weight w12 = ');
        w21 = input('Weight w21 = ');
        w22 = input('Weight w22 = ');
        v1 = input('Weight v1 = ');
        v2 = input('Weight v2 = ');
        theta = input('theta = ');
    end
end

disp('McCulloch-Pitts Net for XOR function');

disp('Weights of Neuron Z1');
disp(w11);
disp(w21);

disp('Weights of Neuron Z2');
disp(w12);
disp(w22);

disp('Weights of Neuron Y');
disp(v1);
disp(v2);

disp('Threshold value');
disp(theta);

ANDNOT

Generate  ANDNOT function using McCulloch-Pitts neural net by a MATLAB program. The truth table for the ANDNOT function is as follows:

X1    X2    Y
0     0     0
0     1     0
1     0     1
1     1     0 

example37.m
%ANDNOT function using Mcculloch-Pitts neuron
clear;
clc;

%Getting weights and threshold value
disp('Enter weights');
w1 = input('Weight w1 = ');
w2 = input('Weight w2 = ');

disp('Enter Threshold Value');
theta = input('thete = ');

y = [0 0 0 0];
x1 = [0 0 1 1];
x2 = [0 1 0 1];
z = [0 0 1 0];

con = 1;
while con
    zin = x1 * w1 + x2 * w2;
  
    for i = 1:4
        if zin(i) >= theta
            y(i) = 1;
        else
            y(i) = 0;
        end
    end
  
    disp('Output of Net');
    disp(y);
    if y == z
        con = 0;
    else
        disp('Net is not learning enter another set of weights and threshold value');
        w1 = input('Weight w1 = ');
        w2 = input('Weight w2 = ');
        theta = input('thete = ');
    end
end

disp('Mcculloch-Pitts Net for ANDNOT function');

disp('Weights of Neuron');
disp(w1);
disp(w2);

disp('Threshold value');
disp(theta);

Activation Function

Write a MATLAB program to generate a few activation functions that are being used in neural networks. The activation functions play a major role in determining the output of the functions. One such program for generating the activation functions is as given below.

example21.m

%Illustration of various activation function 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');