PDA

View Full Version : Help with perceptron(MATLAB)

juslai
08-16-2012, 02:26 PM
%Perceptron Training Single Layer

%Initialize the variables

x =[0 0; 0 1; 1 0; 1 1] ; %Input
b = -1; %Bias
c = 0.1; %Learning rate
w = randi([-1 1],3,1); %Weights
t = [0; 0; 0; 1]; %Target Output
tt = 10000000; %Training Time
numIn = 4; %Numbers used in perceptron training

%Code for perceptron training

for i = 1:tt %Loop from 1 - initialized tt value
out = zeros(4,1); %Create zero based array(4,1)
for j = 1:numIn %Loop from 1 - Numbers used in training
y = b*w(1,1)+...
x(j,1)*w(2,1)+x(j,2)*w(3,1); %(X1 x W1) + (X2 x W2) + B
out(j) = 1/(1+exp(-y)); %Sigmoid Function
delta = t(j)-out(j); %Delta Rule learning rule for updating the weights of the artificial neurons in a single-layer perceptron
w(1,1) = w(1,1)+c*b*delta;
w(2,1) = w(2,1)+c*x(j,1)*delta;
w(3,1) = w(3,1)+c*x(j,2)*delta;
end
end

%Print output
fprintf('Weights:\n');
disp(w);

fprintf('Output:\n');
disp(out);

when i calculate the weights manually i dont get the right answer.
for example AND table

X1 X2 Ans
0 0 0
1 0 0
0 1 0
1 1 1

the weight that are given to me are so big it doesnt give me the correct weight