% Perceptron Example 1: 2-input neuron, 4 input vectors % author: G. Dempsey, Bradley University % January 2003 % learning rate = 0.5 eta = 0.5 % threshold = 0 % % Patterns: P1 = [0.3 0.7]'; % A1 P2 = [0.7 0.3]'; % A2 P3 = [-0.6 0.3]'; % B1 P4 = [-0.2 -0.8]'; % B2 % Define Pattern matrix d1=['display input vectors']; disp(d1) P= [P1 P2 P3 P4] % % Target Outputs % T1 = 1; % class 1 T2 = 1; % class 1 T3 = -1; % class 2 T4 = -1; % class 2 % define Target matrix d1=['display target matrix']; disp(d1) T= [T1 T2 T3 T4] d2=['press any key to continue']; disp(d2) pause % % Initialize synaptic weights % d1=['display initial weight matrix']; disp(d1) W = [-0.6 0.8]' d1=['press any key to start training']; disp(d1) pause % Set number of pattern presentations to 4 n = 4; epoch = 0; % initialize epochs to zero % define as each training input % OK Train the neuron for i = 1:n for j = 1:4 v(j) = W'*P(:,j); % calculate neuron input for pattern j y(j) = sign(v(j)); % calculate neuron output for pattern j e(j) = T(j)-y(j); % calculate output error for pattern j W = W + eta*e(j)*P(:,j); % adapt weights epoch=epoch+1; end d1=['display input, output, error, weights after each epoch']; disp(d1) v % input y % output e % error W % weights epoch % epochs d3=['press any key to continue training']; disp(d3) pause end