Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % XOR calculator using BackPropagation
- clear all;clc;
- %specify input samples (creating trainings set)
- pattern = [1 1;
- 0 0;
- 1 0;
- 0 1];
- [patternRows,patternCols] = size(pattern);
- %creating targets or desired answers for each set
- target = [0,0,1,1];
- %initializing weights with random values
- w1 = [-1 1 ; %neuron 1
- -1 2]; %neuron two
- w2 = [-2,0.5]; %outputneuron
- %bias
- b12 = [1 -1]; % for neuron one and two respectively
- b3 = -1.5; % for neuron three (output neuron)
- lr = 0.01; %learning rate
- a12 = [2 1]; %layer 1 outputs (neuron one and twos output respectively)
- a3 = 1; %layer 2 output (neuron 3 output)
- delta = 0;
- i=1;
- while (i<1000 || error ~=0)
- for row_number = 1: patternRows
- for neuron = 1:2
- %calculate the output of each neuron
- a12(neuron) = logsig( pattern(row_number,:) * w1(neuron,:)' + b12(neuron) );
- end
- %calculate the output
- a3 = hardlim(w2(:)'*a12(:)+ b3 );
- %calculate the error
- error = target(row_number) - a3;
- %calculate the local gradient (sensivity)
- %which is f'(n) * e(n) for output layer
- SM = a3*error;
- %calculating delta which is learningrate * localgradient*inputs
- delta = lr * SM * a12;
- %updating the weights of last layer
- w2_new = w2 + delta;
- %updating the last layers bias
- b3_new = b3 + lr * SM;
- %calculating local gradients(sensivities) for hiddent layers
- %which is f(n)*SumOfNextLayers(localgradient*weights)
- Sm = [a12(1)*(1-a12(1)) 0;
- 0 a12(2)*(1-a12(2)) ] * w2' * SM ;
- %calculating delta which is the same as before
- delta = (lr .* Sm)* pattern(row_number,:);
- %updating the weights and biases of hidden layer
- w2 = w2_new;
- b3 = b3_new;
- w1 = w1 + delta;
- b12 = b12 + (lr * Sm)';
- sss = sprintf('%d ) a3= %d # e= %d # w1=%d,%d # w2 =%d,%d \n',i,a3 ,error,w1(1),w1(2),w2(1),w2(2));
- fprintf(' %s ',sss);
- end
- i= i+1;
- end
Advertisement
Add Comment
Please, Sign In to add comment