Pacific-Design.com

    
Home Index

1. Machine Learning

2. 2 Perceptron

Machine Learning / 2 Perceptron /

Matlab - Perceptron Learning

%-------------------------------------------------------------------------%
%  Perceptron Learning
%  Sample [val1, val2, val3, class]
%-------------------------------------------------------------------------%

Sample1 = [1,0,0,-1];
Sample2 = [1,0,1, 1];
weight = [0.1, 0.55, 0.91];
threshold = 0.4;

fprintf('------------------------ Sample 1 ------------------------------------\n')

InputOutput1 = (weight(1)*Sample1(1) + weight(2)*Sample1(2) + weight(3)*Sample1(3))
if InputOutput1 <= threshold
    fprintf('Sample(1) OutputNode1:%4.2f <= threshold:%4.2f: \n', InputOutput1, threshold);
    OutputNode1=-1
else
    fprintf('Sample(1) Fire: OutputNode1:%4.2f > threshold:%4.2f: \n', InputOutput1, threshold);
    OutputNode1=1
end

if OutputNode1 == Sample1(4)
    fprintf('Sample(1) No Need to change weights, expected=%4.2f\n', Sample1(4))
else
    fprintf('Sample(1) Need to change weights.\n')    
end

fprintf('------------------------ Sample 2 ------------------------------------\n')

InputOutput2 = (weight(1)*Sample2(1) + weight(2)*Sample2(2) + weight(3)*Sample2(3))
if InputOutput2 <= threshold
    fprintf('Sample(2) OutputNode:%4.2f <= threshold:%4.2f:\n', InputOutput2, threshold);
    OutputNode2=-1
else
    fprintf('Sample(2) Fire: OutputNode:%4.2f > threshold:%4.2f:\n', InputOutput2, threshold);
    OutputNode2=1
end

if OutputNode2 == Sample2(4)
    fprintf('Sample(2) No Need to change weights, expected=%4.2f\n', Sample2(4))
else
    fprintf('Sample(2) Need to change weights.\n')
end

%--------------------------------------------------------------------------------%

Results:

------------------------ Sample 1 ------------------------------------

InputOutput1 =

    0.1000

Sample(1) OutputNode1:0.10 <= threshold:0.40: 

OutputNode1 =

    -1

Sample(1) No Need to change weights, expected=-1.00
------------------------ Sample 2 ------------------------------------

InputOutput2 =

    1.0100

Sample(2) Fire: OutputNode:1.01 > threshold:0.40:

OutputNode2 =

     1

Sample(2) No Need to change weights, expected=1.00